Developed by Y. Daniel Liang using Java Servlets (Current time is Sat Nov 21 19:26:55 EST 2009)

The questions are to accompany Introduction to Java Programming, 4E by Y. Daniel Liang. Please report errors to y.daniel.liang@gmail.com.

Introduction to Java Programming, 4E has been superseded by Introduction to Java Programming, 5E.

Chapter 3: Control Statements

Section 3.2 Selection Statements
1   Analyze the following code:

if (x < 100) & (x > 10)
   System.out.println("x is between 10 and 100");

A. The statement has syntax errors because (x<100) & (x > 10) must be enclosed inside parentheses.
B. The statement has syntax errors because (x<100) & (x > 10) must be enclosed inside parentheses and the println(?) statement must be put inside a block.
C. The statement compiles fine.
D. The statement compiles fine, but has a runtime error.
E. & should be replaced by && to avoid having a syntax error.

2   Suppose x = 1, y = -1, and z = 1. What is the printout of the following statement? (Please indent the statement correctly first.)

if (x > 0)
    if (y > 0)
       System.out.println("x > 0 and y > 0");
else if (z > 0)
       System.out.println("x < 0 and z > 0");

A. x > 0 and y > 0;
B. x < 0 and z > 0;
C. x < 0 and z < 0;
D. None of the above.

3   Analyze the following code:

boolean even = false;
if (even = true) {
   System.out.println("It is even!");
}

A. The program has a syntax error.
B. The program has a runtime error.
C. The program runs fine, but displays nothing.
D. The program runs fine and displays It is even!.

Section 3.2.4 switch Statements
4   What is y after the following switch statement is executed?

x = 3;
switch (x + 3) {
   case 6: y = 0;
   case 7: y = 1;
   default: y += 1;
}

A. 1
B. 2
C. 3
D. 4

5   Analyze the following program fragment:

int x;
double d = 1.5;

switch (d) {
   case 1.0: x = 1;
   case 1.5: x = 2;
   case 2.0: x = 3;
}

A. The program has a syntax error becaues the required break statement is missing in the switch statement.
B. The program has a syntax error becaues the required default case is missing in the switch statement.
C. The switch control variable cannot be double.
D. None of the above.

Section 3.2.5 Conditional Expressions
6   What is y after the following statement is executed?

x = 0;
y = (x > 0) ? 10 : -10;

A. -10
B. 0
C. 10
D. 20
E. Illegal expression

7   Analyze the following code fragments Which assign a boolean value to the variable even.

Code 1:
if (number % 2 == 0)
   even = true;
else
   even = false;

Code 2:
even = (number % 2 == 0) ? true: false;

Code 3:
even = number % 2 == 0;
 
A. Code 2 has a syntax error, because you cannot have true and false literals in the conditional expression.
B. Code 3 has a syntax error, because you attempt to assign number to even.
C. All three are correct, but Code 1 is preferred.
D. All three are correct, but Code 2 is preferred.
E. All three are correct, but Code 3 is preferred.

Section 3.3 Loop Statements
8   Analyze the following statement:

double sum = 0;
for (double d = 0; d<10;) {
   d += 0.1;
   sum += sum + d;
}

A. The program has a syntax error because the adjustment is missing in the for loop.
B. The program has a syntax error because the control variable in the for loop cannot be of the double type.
C. The program runs in an infinite loop because d<10 would always be true.
D. The program compiles and runs fine.
E. None of the above.

9   Do the following two statements result in the same value in sum?

for (int i = 0; i<10; ++i) {
   sum += i;
}

for (int i = 0; i<10; i++) {
   sum += i;
}

A. Yes
B. No

10   What is y after the following for loop statement is executed?

int y = 0;
for (int i = 0; i<10; ++i) {
   y += i;
}

A. 10
B. 11
C. 12
D. None of the above.

11   What is i after the following for loop?

int y = 0;
for (int i = 0; i<10; ++i) {
   y += i;
}

A. 9
B. 10
C. 11
D. undefined

12   Is the following loop correct?

for (; ; );

A. Yes
B. No

13   Will the following program terminate?

int balance = 10;

while (true) {
   if (balance < 9) continue;
   balance = balance - 9;
}

A. Yes
B. No

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

15   Analyze the following code:

public class Test {
   public static void main (String args[]) {
     int i = 0;
     for (i = 0; i<10; i++);
       System.out.println(i + 4);
   }
}

A. The program has a syntax error because of the semicolon (;) on the for loop line.
B. The program compiles despite the semicolon (;) on the for loop line, and displays 4.
C. The program compiles despite the semicolon (;) on the for loop line, and displays 14.
D. None of the above.

Section 3.5 Using the Keywords break and continue
16   Will the following program terminate?

int balance = 10;

while (true) {
   if (balance < 9) break;
   balance = balance - 9;
}

A. Yes
B. No

17   What is sum after the following loop terminates?

int sum = 0;
int item = 0;
do {
   item++;
   sum += item;
   if (sum > 4) break;
}
while (item < 5);

A. 5
B. 6
C. 7
D. 8

18   What is sum after the following loop terminates?

int sum = 0;
int item = 0;
do {
   item++;
   sum += item;
   if (sum >= 4) continue;
}
while (item < 5);
A. 15
B. 16
C. 17
D. 18