Looping Constructs

Quiz Guidelines

 

Multiple Choice Question

QID: 
344

What will the following code print when run?

public class TestClass {
public static void main(String[] args) throws Exception {
String[] sa = {"a", "b", "c"};
for(String s : sa){
if("b".equals(s)) continue;
System.out.println(s);
if("b".equals(s)) break;
System.out.println(s+" again");
}  }  }

Select 1 option

A. a
a again
c
c  again
B. a
a again
b
C. a
a again
b
b   again
D. c
c again

Multiple Choice Question

QID: 
343

In the following code what will be the output if 0 (integer value zero) is passed to loopTest()?

public class TestClass{
public void loopTest(int x){
loop: for (int i = 1; i < 5; i++){
for (int j = 1; j < 5; j++){
System.out.println(i);
if (x == 0) { continue loop; }
System.out.println(j);
}
}
}
}

Select 1 option

A. The program will not compile.
B. It will print 1 2 3 4
C. It will print 1 1 2 3 4
D. It will print 1 1 2 2 3 3 4 4
E. Produces no output

Multiple Choice Question

QID: 
172

What will the following program print?

class Test{
public static void main(String args[]){
int var = 20, i=0;
do{
while(true){
if( i++ > var) break;
}
}while(i<var--);
System.out.println(var);
}
}

Select 1 option

A. 19
B. 20
C. 21
D. 22
E. It will enter an infinite loop.

Multiple Choice Question

QID: 
171

What will the following code print?

public class TestClass{
int x = 5;
int getX(){ return x; }
public static void main(String args[]) throws Exception{
TestClass tc = new TestClass();
tc.looper();
System.out.println(tc.x);
}
public void looper(){
int x = 0;
while( (x = getX()) != 0 ){
for(int m = 10; m>=0; m--){
x = m;
}
}
}
}

Select 1 option

A. It will not compile.
B. It will throw an exception at runtime.
C. It will print 0.
D. It will print 5.
E. None of these.

Multiple Choice Question

QID: 
170

Consider the following method which is called with an argument of 7:

public void method1(int i){
int j = (i*30 - 2)/100;
POINT1 : for(;j<10; j++){
boolean flag = false;
while(!flag){
if(Math.random()>0.5) break POINT1;
}
}
while(j>0){
System.out.println(j--);
if(j == 4) break POINT1;
}
}

What will it print?
(Assume that Math.random() return a double between 0.0 and 1.0, not including 1.0)

Select 1 option

A. It will print 1 and 2
B. It will print 1 to N where N is a random number.
C. It will not compile.
D. It will throw an exception at runtime.

Pages