for (int i = 0; i < 4; i += 2)
{
System.out.print(i + " ");
}
System.out.println(i); /* Line 5 */
A -
0 2 4
B -
0 2 4 5
C -
0 1 2 3 4
D -
Compilation fails.
2-
What will be the output of the program?
int x = 3;
int y = 1;
if (x = y) /* Line 3 */
{
System.out.println("x =" + x);
}
A -
x = 1
B -
x = 3
C -
Compilation fails.
D -
The code runs with no output.
3-
What will be the output of the program?
Float f = new Float("12");
switch (f)
{
case 12: System.out.println("Twelve");
case 0: System.out.println("Zero");
default: System.out.println("Default");
}
A -
Zero
B -
Twelve
C -
Default
D -
Compilation fails
4-
What will be the output of the program?
int i = O;
while(1)
{
if(i == 4)
{
break;
}
++i;
}
System.out.println("i = " + i);
A -
i = 0
B -
i = 3
C -
i = 4
D -
Compilation fails.
5-
What will be the output of the program?
public class Delta
{
static boolean foo(char c)
{
System.out.print(c);
return true;
}
public static void main( String[] argv )
{
int i = 0;
for (foo('A'); foo('B') && (i < 2); foo('C'))
{
i++;
foo('D');
}
}
}
A -
ABDCBDCB
B -
ABCDABCD
C -
Compilation fails.
D -
An exception is thrown at runtime.
6-
What will be the output of the program?
for(int i = 0; i < 3; i++)
{
switch(i)
{
case 0: break;
case 1: System.out.print("one ");
case 2: System.out.print("two ");
case 3: System.out.print("three ");
}
}
System.out.println("done");
A -
done
B -
one two three done
C -
one two three two three done
D -
one two done
7-
What will be the output of the program?
public class Test
{
public static void main(String args[])
{
int i = 1, j = 0;
switch(i)
{
case 2: j += 6;
case 4: j += 1;
default: j += 2;
case 0: j += 4;
}
System.out.println("j = " + j);
}
}
A -
6
B -
8
C -
4
D -
2
8-
What will be the output of the program?
boolean bool = true;
if(bool = false) /* Line 2 */
{
System.out.println("a");
}
else if(bool) /* Line 6 */
{
System.out.println("b");
}
else if(!bool) /* Line 10 */
{
System.out.println("c"); /* Line 12 */
}
else
{
System.out.println("d");
}
A -
a
B -
b
C -
c
D -
d
9-
What will be the output of the program?
public class Switch2
{
final static short x = 2;
public static int y = 0;
public static void main(String [] args)
{
for (int z=0; z < 4; z++)
{
switch (z)
{
case x: System.out.print("0 ");
default: System.out.print("def ");
case x-1: System.out.print("1 ");
break;
case x-2: System.out.print("2 ");
}
}
}
}
A -
2 1 0 def def
B -
2 1 0 def 1 def 1
C -
0 def 1
D -
2 1 0 def 1
10-
What will be the output of the program?
int i = 0, j = 5;
tp: for (;;)
{
i++;
for (;;)
{
if(i > --j)
{
break tp;
}
}
System.out.println("i =" + i + ", j = " + j);