Name:____Practice Test ______
Covers chs 1-6 / Sample Exam

Part I: Multiple Choice Questions: (1 pts each)

5 quizzes for Chapter 2

1 To assign a double variable d to an int variable x, you write

A. x = d;

B. x = (int)d;

C. x = (long)d

D. x = (float)d;

2 5 % 2 is _____

A. 0

B. 3

C. 4

D. 1

E. 2

3 You can cast a double value to ______.

A. int

B. float

C. byte

D. long

E. short

4 -15 % 4 is _____

A. -3

B. -1

C. 0

D. -2

E. -4

5 You can assign the value in ______to an int variable.

A. 93

B. 98.3

C. true

D. 'x'


5 quizzes for Chapter 3

6 What is x after evaluating
x = (2 > 3) ? 2 : 3;

A. 2

B. 3

C. 4

D. 5

7 A break statement is required for a switch-case statement in Java.

A. false

B. true

8 Suppose x=0 and y=0 what is x after evaluating the expression (y > 0) & (1 > x++).

A. 0

B. 1

C. -1

9 You can cast a Boolean value to an int, or an int to Boolean.

A. false

B. true

10 Analyze the following code:
// Enter an integer
String numString = JOptionPane.showInputDialog(null,
"Enter a number:",
"Exam Input", JOptionPane.QUESTION_MESSAGE);
int number = Integer.parseInt(numString);
if (number <= 0)
System.out.println(number);

A. If number is positive, number is displayed.

B. number entered from the input cannot be negative.

C. If number is zero, number is displayed;

D. System.out.println(number); must be placed inside braces;

E. The if statement is wrong, because it does not have the else clause;


5 quizzes for Chapter 4

11 What is the output of the following fragment?
for (int i = 0; i < 15; i++) {
if (i % 4 == 1)
System.out.print(i + " ");
}

A. 1 5 9 13

B. 1 3 5 7 9 11 13 15

C. 1 4 8 12

D. 1 5 9 13 16

E. 1 3 5 7 9 11 13

12 Which of the following loops produces the output
1 2 3 4 1 2 3 1 2 1
(I)
for (int i = 5; i > 0; i--) {
for (int j = 1; j < i; j++)
System.out.print(j + " ");
System.out.println();
}
(II)
for (int i = 1; i < 5; i++) {
for (int j = 1; j < i; j++)
System.out.print(j + " ");
System.out.println();
}
(III)
int i = 0;
while (i < 5) {
for (int j = 1; j < i; j++)
System.out.print(j + " ");
System.out.println();
i++;
}
(IV)
int i = 5;
while (i > 0) {
for (int j = 1; j < i; j++)
System.out.print(j + " ");
System.out.println();
i--;
}

A. (II)

B. (III)

C. (IV)

D. (I)

13 What the output of the following code:
for ( ; false ; )
System.out.println("Welcome to Java");

A. prints out Welcome to Java one time.

B. does not print anything.

C. prints out Welcome to Java forever.

D. prints out Welcome to Java two times.

14 You can always write a program without using break or continue in a loop.

A. true

B. false

15 You can always convert a while loop to a for loop.

A. false

B. true


5 quizzes for Chapter 5

16 What is (int)Math.random()?

A. 1.1

B. 1

C. 0

D. 0.5

17 You can define two methods in the same class with the same name and parameter list.

A. true

B. false

18 A call for the method with a void return type is always a statement itself, but a call for the method with a non-void return type can be treated as either a statement or an expression.

A. true

B. false

19 The actual parameters of a method must match the formal parameters in type, order, and number.

A. false

B. true

20 Math.pow(3, 2) returns ______.

A. 8

B. 8.0

C. 9.0

D. 9


5 quizzes for Chapter 6

21. Which of the following are valid array declarations?

a. char[] charArray = new char[26];

b. int[] words = new words[10];

c. char[] charArray = "Computer Science";

d. double[3] nums = {3.5, 35.1, 32.0};

22. Consider the following code fragment:

int[] list = new int[10];

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

list[i] = (int)(Math.random() * 10);

}

Which of the following statements is true?

a. list.length must be replaced by 10

b. The loop body will execute 10 times, filling up the array with random numbers.

c. The loop body will execute 10 times, filling up the array with zeros.

d. The code has a runtime error indicating that the array is out of bound.

23. Assume the signature of the method xMethod is as follows.

public static void xMethod(double[] a)

Which of the following could be used to invoke xMethod?

a. xMethod(5);

b. xMethod({3, 4});

c. xMethod(new int[2]);

d. xMethod(new double[2]);

24. Given the following statement

int[ ] list = new int[10];

list.length has the value

a. 10

b. 9

c. The value depends on how many integers are stored in list.

d. 11

25. Given the following statement

int[ ] list = new int[10];

a. The array variable list contains a memory address that refers to an array of 10 int values.

b. The array variable list contains a memory address that refers to an array of 9 int values.

c. The array variable list contains ten values of type int.

d. The array variable list contains nine values of type int.

Part II. (3 pts each) Write code fragments.

a. How many times is the following loop body repeated? What is the printout of the loop?

int i = 0;

while (i 20) {

if ((i + 3) % 2 != 0)

System.out.println(i);

i++;

}

b.  Convert the following for loop into a while loop

int sum = 0;

for (int i = 0; i < 100; i++) {

sum += i;

}

c.  Convert the following while loop into a do-while loop

int sum = 0;

int number = input.nextInt();

while (number != 0) {

sum += number;

number = input.nextInt();

}

Part III: Show the printout of the following code:

a. (2 pts)

public class Test {

public static void main(String[] args) {

int i = 1;

while (i < 10) {

i = i + 3;

System.out.print(i + " ");

}

}

}

b. (3 pts)

public class Test {

public static void main(String[] args) {

for (int i = 1; i <= 5; i++) {

for (int j = 1; j <= i; j++)

System.out.print(j + " ");

System.out.print('\n');

}

}

}

c. (3 pts)

Suppose the input is 2 3 4 5 0. What is the output of the following code?

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int number, sum = 0, count;

for (count = 0; count < 5; count++) {

number = input.nextInt();

sum += number;

}

System.out.println("sum is " + sum);

System.out.println("count is " + count);

}

}

d. (3 pts)

public class Test {

public static void main(String[] args) {

int[] list = {1, 2, 3, 4, 5};

reverse(list);

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

System.out.print(list[i] + " ");

}

public static void reverse(int[] list) {

int[] newList = new int[list.length];

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

newList[i] = list[list.length - 1 - i];

list = newList;

}

}

Part IV: (Write programs) (8 pts each)

a. Write a complete program that displays the following table (note that 1 mile is 1.609 kilometers):

Miles Kilometers

1 1.609

2 3.218

...

9 14.481

10 16.090

b. Write a method that computes the sum of the digits in an integer. Use the following method header:

public static int sumDigits(long n)

For example, sumDigits(234) returns 9 (2 + 3 + 4). Hint: Use the % operator to extract digits, and the / operator to remove the extracted digit. For instance, to extract 4 from 234, use 234 % 10 (= 4). To remove 4 from 234, use 234 / 10 (= 23). Use a loop to repeatedly extract and remove the digit until all the digits are extracted.

Write a test program that prompts the user to enter an integer and displays the sum of all its digits. The outline of the program is as follows:

public class Test {

public static void main(String[] args) {

// Fill in the code here

}

public static int sumDigits(long n) {

// Fill in the code here

}

}

c. Write a program that reads an unspecified number of scores and determines how many scores are above or equal to the average and how many scores are below the average. Enter a negative number to signify the end of the input. Assume that the maximum number of scores is 10.

Part I: Multiple Choice Questions: (1 pts each)

Keys:

1.  B
2. D
3. ABCDE
4. A
5. AD
6. B
7. A
8. A
9. A
10. C
11. A
12. CD
13. B
14. A
15. B
16. C
17. B
18. A
19. B
20. C
21. A

22. D

23. D

24. A

25. A

Part II:

a:

The loop body is executed 20 times.

The printout of the code is

0

2

4

6

8

10

12

14

16

18

b:

int sum = 0;

int i = 0;

while (i < 100) {

sum += i;

i++;

}

c.

int sum = 0;

int number;

do (number != 0) {

number = input.nextInt();

sum += number;

} while (number != 0);

Part III:

a:

4 7 10

b:

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

c:

sum is 14
count is 4
d:

5 4 3 2 1

Part IV: (Write programs)

a.

public class Test {

public static void main(String[] args) {

System.out.println("Miles\t\tKilometers");

System.out.println("------");

int miles = 1;

for (int i = 1; i <= 10; miles++, i++) {

System.out.println(miles + "\t\t" + miles * 1.609);

}

}

}

b.

public class Test {

public static void main(String[] args) {

java.util.Scanner input = new java.util.Scanner(System.in);

System.out.print("Enter a number: ");

int value = input.nextInt();

System.out.println("The sum of digits for " + value +

" is " + sumDigits(value));

}

public static int sumDigits(long n) {

int temp = (int)Math.abs(n);

int sum = 0;

while (temp != 0) {

int remainder = temp % 10;

sum += remainder;

temp = temp / 10;

}

return sum;

}

}

c.

public class Test {

// Main method

public static void main(String[] args) {

double[] scores = new double[10];

double sum = 0;

int count = 0;

java.util.Scanner input = new java.util.Scanner(System.in);

do {

System.out.print("Enter a new score: ");

scores[count] = input.nextDouble();

if (scores[count] >= 0)

sum += scores[count];

} while (scores[count++] >= 0);

System.out.println("count is " + count);

double average = (sum) / (count - 1);

int numOfAbove = 0;

int numOfBelow = 0;

for (int i = 0; i < count - 1; i++)

if (scores[i] >= average)

numOfAbove++;

else

numOfBelow++;

System.out.println("Average is " + average);

System.out.println("Number of scores above or equal to the average "

+ numOfAbove);

System.out.println("Number of scores below the average "

+ numOfBelow);

}

}

9