NEW
Font size
WorksheetsJava Control and Loop -2
Total questions: 20
Worksheet time: 3600secs
Which of the following will be the output of the above program?
public class Compute {
public static void main (string args [ ]){
int result, x ;
x = 1 ;
result = 0;
while (x < = 10) {
if (x%2 == 0)
result + = x ;
+ + x ;
}
System.out.println(result) ;
}
}
55
30
25
35
class Test {
public static void main(String[] args){
int i = 0, j = 9;
do {
i++;
if (j-- < i++) {
break;
}
} while (i < 5);
System.out.println(i + "" + j);
}
}
44
55
66
77
public class Test{
public static void main(String args[]){
int i = 0, j = 5 ;
for( ; (i < 3) && (j++ < 10) ; i++ ){
System.out.print(" " + i + " " + j );
}
System.out.print(" " + i + " " + j );
}
}
0 6 1 7 2 8 3 8
0 6 1 7 2 8 3 9
0 6 1 5 2 5 3 5
Compilation Error
class Test{
public static void main(String args[]){
int x=7;
if(x==2); // Note the semicolon
System.out.println("NumberSeven");
System.out.println("NotSeven");
}
}
NumberSeven NotSeven
NumberSeven
NotSeven
Error
public class Test{
public static void main(String args[]){
int i, j;
for(i=1, j=0;i<10;i++) j += i;
System.out.println(i);
}
}
10
11
9
20
public class Test{
public static void main(String[] args){
double sum = 0;
for(double d = 0; d < 10;){
d += 0.1;
sum += sum + d;
}
}
}
The program has a compile error because the adjustment is missing in the for loop.
The program has a compile error because the control variable in the for loop cannot be of the double type.
The program runs in an infinite loop because d<10 would always be true.
The program compiles and runs fine.
Which of the following for loops will be an infinite loop?
for(; ;)
for(i=0 ; i<1; i--)
for(i=0; ; i++)
All of the above
What is the value of a[1] after the following code is executed?
int[] a = {0, 2, 4, 1, 3};
for(int i = 0; i < a.length; i++)
a[i] = a[(a[i] + 3) % a.length];
0
1
2
3
for(int i=0; i<5; i++)
x += i;
In total, how many times is the inner loop executed?
5
10
15
50
Infinite Loop
Which of the following for loop declaration is not valid?
for ( int i = 99; i >= 0; i / 9 )
for ( int i = 7; i <= 77; i += 7 )
for ( int i = 20; i >= 2; - -i )
for ( int i = 2; i <= 20; i = 2* i )
What will be the output of the following program?
public class Test
{
public static void main(String[] args)
{
int count = 1;
while (count <= 15)
{ System.out.println(count % 2 == 1 ? "***" : "+++++"); ++count;
} // end while
} // end main
}
15 times ***
15 times +++++
8 times *** and 7 times +++++
Both will print only once
