Looping Constructs

Quiz Guidelines

 

Multiple Choice Question

QID: 
354

What will the following program snippet print?

int i=0, j=11;
do{
if(i > j) { break; }
j--;
}while( ++i < 5);
System.out.println(i+" "+j);

Select 1 option

A. 5 5
B. 5 6
C. 6 6
D. 6 5
E. 4 5

Multiple Choice Question

QID: 
353

What will the following code print?

void crazyLoop(){
int c = 0;
JACK: while (c < 8){
JILL: System.out.println(c);
if (c > 3) break JACK; else c++;
}
}

Select 1 option

A. It will not compile.
B. It will throw an exception at runtime.
C. It will print numbers from 0 to 8
D. It will print numbers from 0 to 3
E. It will print numbers from 0 to 4

Multiple Choice Question

QID: 
352

What will be the result of attempting to compile and run the following program?

class TestClass{
public static void main(String args[]){
int i = 0;
for (i=1 ; i<5 ; i++) continue; //(1)
for (i=0 ; ; i++) break; //(2)
for ( ; i<5?false:true ; ); //(3)
}
}

Select 1 option

A. The code will compile without error and will terminate without problems when run.
B. The code will fail to compile, since the continue can't be used this way.
C. The code will fail to compile, since the break can't be used this way
D. The code will fail to compile, since the for statement in the line 2 is invalid.
E. The code will compile without error but will never terminate.

Multiple Choice Question

QID: 
351

Consider the following code:

public static void main(String[] args) {
int[] values = { 10, 30, 50 };
for( int val : values ){
int x = 0;
while(x<values.length){
System.out.println(x+" "+val);
x++;
}
}
}

How many times is 2 printed out?

Select 1 option

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

Multiple Choice Question

QID: 
350

What will be the output when the following program is run?

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

Select 1 option

A. 0 0 will be printed twice.
B. 0 0 will be printed once.
C. It will keep on printing 0 0
D. It will not compile.
E. It will print 0 0 and then 0 1.

Pages