Control Flow  «Prev  Next»


Java Control Flow - Quiz

Each question is worth one point. Select the best answer or answers for each question.
 

1. What is wrong with the following if statement?
if(x++) {
 y *= x;
 x--;
else {
 y *= y;
 ++x;
}

Please select all the correct answers.
  A. Since x is used in the if condition it cannot be used in the if body.
  B. The if condition must be a boolean expression, not a numeric expression.
  C. The value of y is unknown.
  D. There is a missing } before the else.

2. What is wrong with the following switch statement?
switch(i > 5) {
case '1'
 ++i;
 break;
case "2":
 --i;
case 3:
 i *= 5;
 break;
default:
 i %= 3;
}
Please select all the correct answers.
  A. A colon should follow the first case.
  B. The switch expression must evaluate to an int value.
  C. The first case specifies a char value.
  D. There is a break statement missing in the second case.
  E. The second case specifies a String value.


3. What is wrong with the following for statement?
for(i=0; j=0, i<n,++ j; i*="k)"{
 k += i + j,
} 
Please select all the correct answers.
  A. The commas and semicolons should be switched.
  B. The variable n may have negative value.
  C. The variable k may not be used to update i since k is updated by i.
  D. There is more than one loop index.

4. What is wrong with the following while statement?
while (x << 5) do {
 x = x + y;
}

Please select all the correct answers.
  A. The "do" should not appear after the while condition.
  B. The value of x may become 0.
  C. More than one statement should appear between the braces.
  D. The expression x << 5 does not evaluate to a boolean value.


5. What is the difference between a continue statement and a break statement?
Please select all the correct answers.
  A. A break statement may be used with a switch statement, but a continue statement may not be used with a switch statement.
  B. Only a continue statement may have a label.
  C. A break statement terminates a loop and a continue statement terminates the current loop iteration.
  D. Only a break statement may have a label.

6. Which of the following are true about Java exception handling?
Please select all the correct answers.
  A. If an object is thrown by a throw statement then it must be of a subclass of the Throwable class.
  B. A finally clause of a try-catch statement is only executed if at least one catch clause has been triggered.
  C. A catch clause may only catch objects of the class Exception.
  D. The finally clause of a try-catch statement is optional and may be omitted.


7. At which line in the following code is the Vector object, created in line 4, first subject to garbage collection?
import java.util.*;
public class Question {
  public static void main(String[] args) {
    Vector v1 = new Vector();
    Vector v2 = new Vector();
    v1.addElement("123");
    v1.addElement(v2);
    String s = (String)v1.elementAt(0);
    v1 = v2;
    v2 = v1;
    v1.addElement(s);
  }
}

Please select the best answer.
  A. Line 4
  B. Line 6
  C. Line 8
  D. Line 9

8. Which of the following are true about Java garbage collection?
Please select all the correct answers.
  A. You can force an object to be garbage collected by invoking its finalize() method.
  B. Unreachable objects can become reachable again.
  C. If an object with a finalize() method has been garbage collected, then its finalize() method has been invoked.
  D. Unreachable objects are always garbage collected.

9. What output is displayed by the following program?
class Question {
 public static void main(String[] args) {
  int i = 0;
  int j = 1;
  outer: while (i < j) {
   ++i;
   inner: do {
    ++j;
    if(j = = 3) break inner;
    else if(i + j > 5) break outer;
    System.out.print(j);
   } while (j < i);
   System.out.print(i);
  }
 }
}
Please select the best answer.
  A. 12145
  B. 212
  C. 1214
  D. 2325

10. What output is displayed by the following program?
class Question {
 public static void main(String[] args) {
  int i = 0;
  int j = 1;
  outer: while (i < j) {
   inner: while (j < i + 4) {
    j *= 2;
    if(j = = 2) continue outer; 
    else if(i + j < 7) continue inner;
    System.out.print(j);
    break outer;
   }
   System.out.print(i);
   ++i;
  }
 }
}

Please select the best answer.
  A. 27
  B. 17
  C. 18
  D. 08