Review Questions for Exam 1
This review sheet is intended to give you some practice questions to
use in preparing for our first midterm. It is not necessarily complete.
The first exam covers the reading assignments, programming projects and
class/discussion material through Friday, October 2.
1. What is a CPU?
2. Give one example of an input device and one example of an output
3. What is a compiler?
4. Write Java code that displays the sum of all numbers which are
multiples of 3 between 1 and 1000.
5. Write a line of code that creates an object that may be used to read
a line of text from the keyboard.
6. Using your object in #5, write a line of Java code that reads a
floating point number from the keyboard.
7. Assume that val is an initialized variable of type double. Write a
Java statement that prints val to the console window.
8. Write a complete Java program (including any necessary import
statements) that reads 100 floating-point numbers from the user, and
prints the max, min and average value.
9. Write a Java program that reads 20 lines of text from the user and
prints the number of lines that contain the phrase "happy day"
10. Write a Java program that reads a line of text from the keyboard
and prints its reverse to the screen.
11. Write Java code that prints the string referenced by variable
myString with the first letter capitalized.
12. What is the value and type of the result?
d. Math.pow(3, 5)
g. 15 + 2 + " hellos"
h. 15 * 2 / 4
i. (5 < 4) || Character.isDigit(2)
13. Declare and initialize a class constant that represents the number
of days in March.
14. Give an example of an explicit cast, and explain when the use of an
explicit cast is, and is not, necessary.
15. Write Java code that creates an object of type Random.
16. What is the difference in comparing Strings with the equals()
method vs. the = = operator?
17. Give examples to show how the methods indexOf(), replace(),
length(), substring(), equals(), equalsIgnoreCase(), toUpperCase() and
charAt() work. (These are all methods in the String
18. What is a constructor?
19. Write a if-else if statement that prints a message indicating
the value stored in int variable n is 0, 1, 2, 3 or none of these.
20. Write code that prints the squares of the numbers
between 1 and n, where n is a value entered by the user.
If the user enters an integer less than 1, print an error message.
21. Do exercise 15, chapter 2, in the Stepp and Reges book.
22. Use loops to print the following:
The first line in the triangle contains 1 *, the second line contains 3
*, ... the last line contains 11 *'s.
23. Write Java code that simulates the throw of two dice and prints the
24. Write a program that uses a Graphics object to draw 10 concentric