Looping Constructs

Quiz Guidelines

 

Multiple Choice Question

QID: 
169

What will the following code print?

public class BreakTest{
public static void main(String[] args){
int i = 0, j = 5;
lab1 : for( ; ; i++){
for( ; ; --j) if( i >j ) break lab1;
}
System.out.println(" i = "+i+", j = "+j);
}
}

Select 1 option

A. i = 1, j = -1
B. i = 1, j = 4
C. i = 0, j = 4
D. i = 0, j = -1
E. It will not compile.

Multiple Choice Question

QID: 
168

Given the following code, which of these statements are true?

class TestClass{
public static void main(String args[]){
int k = 0;
int m = 0;
for ( int i = 0; i <= 3; i++){
k++;
if ( i == 2){
// line 1
}
m++;
}
System.out.println( k + ", " + m );
}
}

Select 3 options

A. It will print 3, 2 when line 1 is replaced by break;
B. It will print 3, 2 when line 1 is replaced by continue.
C. It will print 4, 3 when line 1 is replaced by continue.
D. It will print 4, 4 when line 1 is replaced by i = m++;
E. It will print 3, 3 when line 1 is replaced by i = 4;

Multiple Choice Question

QID: 
167

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

class TestClass{
public static void main(String args[]){
boolean b = false;
int i = 1;
do{
i++ ;
} while (b = !b);
System.out.println( i );
}
}

Select 1 option

A. The code will fail to compile, 'while' has an invalid condition expression.
B. It will compile but will throw an exception at runtime.
C. It will print 3.
D. It will go in an infinite loop.
E. It will print 1.

Multiple Choice Question

QID: 
166

What will the following code snippet print?

int count = 0, sum = 0;
do{
if(count % 3 == 0) continue;
sum+=count;
}while(count++ < 11);
System.out.println(sum);

Select 1 option

A. 49
B. 48
C. 37
D. 36
E. 38

Multiple Choice Question

QID: 
165

Which of the following code snippets will compile without any errors?
(Assume that the statement int x = 0; exists prior to the statements below.)

Select 3 options

A. while (false) { x=3; }
B. if (false) { x=3; }
C. do{ x = 3; } while(false);
D. for( int i = 0; i< 0; i++) x = 3;

Pages