Java OCA OCP Practice Question 294

Question

Which of the following are legal loop constructions? (Choose all that apply.)

A. while (int i<7) { 
         i++; //from  ww w. j  a va 2 s.c  o  m
         System.out.println("i is " + i); 
    } 

B. int i = 3; 
   while (i) { 
         System.out.println("i is " + i); 
   } 

C. int j = 0; 
   for (int k=0, j+k != 10; j++,k++) { 
         System.out.println("j=" + j + ", k=" + k); 
   } 

D. int j=0; 
   do { 
         System.out.println("j=" + j++); 
         if (j==3) 
               continue loop; 
   } while (j<10); 


C.

Note

In A, the variable declaration for i is illegal.

This type of declaration is permitted only in the first part of a for() loop.

In B, the loop control expression-the variable i in this case-is of type int.

A boolean expression is required.

C is valid.

D is wrong, the label has been omitted from the 2nd line, which should read

loop: do {. 



PreviousNext

Related