Name:_______________________



Name:_______________________

Covers chs 1-6 |Sample Exam | |

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

1. A runtime error implies the following:

a. The program cannot be compiled.

b. The program runs, but is terminated unexpectedly.

c. The program runs, but produces incorrect results.

d. None of the above

2. When you run your program Test, you receive NoClassDefFoundError: Test. What is the reason?

a. JVM cannot find Test.class.

b. JVM cannot find Test.java.

c. Test is not a public class.

d. Test does not have a main method.

e. Test does not have a constructor.

3. When you run your program Test, you receive NoSuchMethodError: main. What is the reason?

a. JVM cannot find Test.class.

b. JVM cannot find Test.java.

c. Test is not a public class.

d. Test does not have a main method, or the signature of the main method is incorrect.

e. Test does not have a constructor.

4. What is the range of an int variable?

a. 0 to 216.

b. -216 to 216.

c. -215 to 215-1.

d. -216 to 216-1.

e. None of the above.

5. Which of the following statement is incorrect?

a. byte b = 40;

b. short s = 200;

c. int i = 200;

d. int j = 200L;

6. According to Java naming convention, which of the following is a constant?

a. MAXINTEGER

b. MAX-INTEGER

c. max_integer

d. MAX_INTEGER

e. max-integer

7. Assume x=1 and y=2, what is the result of evaluating "x + y = " + x + y?

a. x + y = 3

b. x + y = 12

c. Illegal operations

d. None of the above

8. Assume x=1 and y=2, what is the result of evaluating ++x + y?

a. 1

b. 2

c. 3

d. 4

9. Assume x=1 and y=2, what is the result of evaluating x++ + y?

a. 1

b. 2

c. 3

d. 4

e. None of the above

10. Assume x=1 and y=2, what is x, after evaluating (x > 0) || (x-- > 0)?

a. 1

b. 2

c. 3

d. 4

e. None of the above

11. Assume x=1 and y=2, what is x, after evaluating (x > 0) | (x-- > 0)?

a. 0

b. 1

c. 2

d. 3

e. None of the above

12. Suppose x = 1, y = -1, and z = 1. What is the printout of the following statement?

if (x > 0)

if (y > 0)

System.out.println("A");

else if (z > 0)

System.out.println("B");

else

System.out.println("C");

a. A

b. B

c. C

d. None of the above.

13. Analyze the following program fragment:

int x;

double d = 1.5;

switch (d) {

case 1.0: x = 1;

case 1.5: x = 2;

}

a. The required break statement is missing in the switch statement.

b. The required default case is missing in the switch statement.

c. The switch control variable cannot be double.

d. None of the above.

14. Which of the following statement is true?

a. You can define a method inside a method.

b. A local variable in a method always has a default value if you don’t explicitly assign a value to it.

c. A method can be declared with no parameters.

d. None of the above is true.

15. What is item after the following loop terminates?

int sum = 0;

int item = 0;

do {

item += 1;

sum += item;

if (sum > 4) break;

}

while (item < 5);

a. 2

b. 3

c. 4

d. 5

e. None of the above

16. What is item after the following loop terminates?

int sum = 0;

int item = 0;

while (item < 5) {

item++;

sum += item;

if (sum >= 4) continue;

}

a. 5

b. 6

c. 7

d. 8

e. None of the above

17. Analyze the following fragment:

double sum = 0;

double d = 0;

while (d != 10.0) {

d += 0.1;

sum += sum + d;

}

a. The program does not compile because sum and d are declared double, but assigned with integer value 0.

b. The program never stops because d is always 0.1 inside the loop.

c. The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.

d. None of the above.

18. A call for a 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. Which of the following is true.

a. x.length does not exist if x is null.

b. The array index is not limited to int type.

c. An array variable can be declared and redeclared in the same block.

d. Binary search can be applied on an unsorted list.

20. Which of the following is false.

a. You can use the operator == to check whether two variables refer to the same array.

b. The copyarray method does not allocate memory space for the target array. The target array must already be created with memory space allocated.

c. The array index of the first element in an array is 0.

d. The element in the array must be of primitive data type.

21. Analyze the following code:

public class Test {

public static void main(String[] args) {

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

x = new int[2];

int[] y = x;

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

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

}

}

a. The program displays 1 2 3 4.

b. The program displays 0 0.

c. The program displays 0 0 3 4.

d. The program displays 0 0 0 0.

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

a. Convert the following for loop into a while loop

int sum = 0;

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

sum += i;

}

b. Convert the following for loop into a do-while loop

int sum = 0;

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

sum += i;

}

c. Convert the following while loop into a for loop

public static void main(String[] args) {

int i = 0;

while (i < args.length) {

System.out.println(args[i++]);

}

}

d. Convert the following if statement using a switch statement

// Find interest rate based on year

if (numOfYears == 7)

annualInterestRate = 7.25;

else if (numOfYears == 15)

annualInterestRate = 8.50;

else if (numOfYears == 30)

annualInterestRate = 9.0;

else {

System.out.println("Wrong number of years");

System.exit(0);

}

Part III: Show the printout of the following code:

a. (3 pts)

public class Test {

public static void main(String[] args) {

for (int i = 1; i ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download