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 1.

1. What is a CPU? See lecture 1 notes.

2. Give one example of an input device and one example of an output device. See Lecture 1 notes.

3. What is a compiler? See class notes.

4. Write Java code that displays the sum of all numbers which are multiples of 3 between 1 and 1000.

int sum = 0;
for(int i = 3; i <= 1000; i+=3)
    sum += i;
System.out.println("Sum: " + sum);

5. Write a line of code that creates an object that may be used to read a line of text from the keyboard.

Scanner reader = new Scanner(;

6. Using your object in #5, write a line of Java code that reads a floating point number from the keyboard.

double x = reader.nextDouble();

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.

import java.util.*;
public class Number8 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(;

        // Read the first value, and initialize the min and max to that first value
        System.out.print("Enter the first number: ");
        double value = scan.nextDouble();
        double min = value;
        double max = value;
        double sum = value;

        // read remaining 99 floating point numbers
        for(int i = 0; i < 99; i++) {
            System.out.print("Enter the next number: ");
            value = scan.nextDouble();
            min = Math.min(min, value);
            max = Math.max(max, value);
            sum += value;

        // Display average, minimum, and maximum
        System.out.println("Average: " + (sum/100));
        System.out.println("Minimum: " + min);
        System.out.println("Maximum: " + max);

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" (disregard capitalization).

import java.util.*;
public class Question9 {
    public static void main(String[] args) {

        Scanner scan = new Scanner(;
        int numLines = 0;  // number of lines that contain "happy day" - initially zero

        System.out.print("Enter 20 lines of text: ");

        for(int i = 0; i < 20; i++) {
            String line = scan.nextLine().toLowerCase();
            if(line.indexOf("happy day") >= 0)

        System.out.println("Number of lines containing \"happy day\": " + numLines);

10. Write a Java program that reads a line of text from the keyboard and prints its reverse to the screen.

import java.util.*;
public class Question10 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(;
        System.out.print("Enter a line of text: ");
        String s = scan.nextLine();

        for(int i = 0; i < s.length(); i++) {

11. Write Java code that prints the string referenced by variable myString with the first letter capitalized.

String up = myString.toUpperCase();
System.out.print(up.charAt(0) + myString.substring(1));

12. What is the value and type of the result?
    a. 17/8    2 
    b. 24%5    4
    c. 14.0/4    3.5
    d. Math.pow(3, 5)    243
    e. Character.isLetter('+')    false
    f. Math.round(12.56999)   
    g. 15 + 2 + " hellos"    "17 hellos"
    h. 15 * 2 / 4        7
    i. (5 < 4) || Character.isDigit('2')  true

13. Declare and initialize a class constant that represents the number of days in March.

public static final int NUM_DAYS_MARCH = 31;

14. Give an example of an explicit cast, and explain when the use of an explicit cast is, and is not, necessary.

We haven't covered this yet.

15. Write Java code that creates an object of type Random.

Random generator = new Random();

16. What is the difference in comparing Strings with the equals() method vs. the = = operator?

Using == to compare two String reference variables compares the addresses in the reference variables. The equals() method actually compares the Strings.

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 class).

See class notes, Java documentation, and textbook.

18. What is a constructor?

It's a method that is called when a new object is created, and it is used to perform any necessary initializations for the new object. The constructor has the same name as the class.

19. Write a if-else if statement that prints a message indicating whether the value stored in int variable n is 0, 1, 2, 3 or none of these.

if( (n >= 0) && (n <=3))
    System.out.println("Between 0 and 3");
    System.out.println("Not between 0 and 3");

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.

if(n >=1) {
    for(int i = 1; i <= n; i++)
        System.out.println(Math.pow(i, 2));

21. Do exercise 21, 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.

for(int i = 1; i <= 6; i+=2) {
    // print a line with 6-i spaces followed by 2i-1 *'s
    for(int j = 1; j <= 6-i; j++)
        System.out.print(" ");
    for(int k = 1; k <= 2*i-1; k++)
    System.out.println(); //go to beginning of next line

23. Write Java code that simulates the throw of two dice and prints the value rolled.

Random gen = new Random();
int throw1 = gen.nextInt(6)+1;
int throw2 = gen.nextInt(6)+1;
System.out.println("Throw total: " + (throw1 + throw2));

24. Write a program that uses a Graphics object to draw 10 concentric circles.

We did this in class.