Looping Constructs

Quiz Guidelines

 

Multiple Choice Question

QID: 
349

What will the following code print when compiled and run?

class Test{
public static void main(String args[]){
int c = 0;
A: for(int i = 0; i < 2; i++){
B: for(int j = 0; j < 2; j++){
C: for(int k = 0; k < 3; k++){
c++;
if(k>j) break;
}
}
}
System.out.println(c);
}
}

Select 1 option

A. 7
B. 8
C. 9
D. 10
E. 11

Multiple Choice Question

QID: 
348

What will the following program print?

class Test{
public static void main(String args[]){
int i=0, j=0;
X1: for(i = 0; i < 3; i++){
X2: for(j = 3; j > 0; j--){
if(i < j) continue X1;
else break X2;
}
}System.out.println(i+" "+j);
}  }

Select 1 option

A. 0 3
B. 0 2
C. 3 0
D. 3 3
E. 2 2

Multiple Choice Question

QID: 
347

What will the following program print?

public class TestClass{
public static void main(String[] args){
for : for(int i = 0; i< 10; i++){
for (int j = 0; j< 10; j++){
if ( i+ j > 10 ) break for;
}
System.out.println( "hello");
}
}
}

Select 1 option

A. It will print hello 6 times.
B. It will not compile.
C. It will print hello 2 times.
D. It will print hello 5 times.
E. It will print hello 4 times.

Multiple Choice Question

QID: 
346

Consider the following class :

class Test{
public static void main(String[] args){
for (int i = 0; i < 10; i++) System.out.print(i + " "); //1
for (int i = 10; i > 0; i--) System.out.print(i + " "); //2
int i = 20; //3
System.out.print(i + " "); //4
}
}

Which of the following statements are true?

Select 4 options

A. As such the class will compile and print 20 at the end of its output.
B. It will not compile if line 3 is removed.
C. It will not compile if line 3 is removed and placed before line 1.
D. It will not compile if line 4 is removed and placed before line 3.
E. Only Option 2, 3, and 4 are correct.

Multiple Choice Question

QID: 
345

Which of the following statements regarding 'break' and 'continue' are true?

Select 1 option

A. break without a label, can occur only in a switch, while, do, or for statement.
B. continue without a label, can occur only in a switch, while, do, or for statement.
C. break can never occur without a label.
D. continue can never occur WITH a label.
E. None of the above.

Pages