Sail E0 Webinar

MCQs

Total Questions : 42 | Page 3 of 5 pages
Question 21.


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);
  1.    i = 1, j = 0
  2.    i = 1, j = 4
  3.    i = 3, j = 4
  4.    Compilation fails.
 Discuss Question
Answer: Option D. -> Compilation fails.

If you examine the code carefully you will notice a missing curly bracket at the end of the 

code, this would cause the code to fail.


Question 22.


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 ");
}
}
}
}
  1.    0 def 1
  2.    2 1 0 def 1
  3.    2 1 0 def def
  4.    2 1 0 def 1 def 1
 Discuss Question
Answer: Option D. -> 2 1 0 def 1 def 1

When z == 0 , case x-2 is matched. When z == 1, case x-1 is matched and then the 

break occurs. When z == 2, case x, then default, then x-1 are all matched. When z == 3, 

default, then x-1 are matched. The rules for default are that it will fall through from above 

like any other case (for instance when z == 2), and that it will match when no other cases

 match (for instance when z==3).


Question 23.


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");
}
  1.    a
  2.    b
  3.    c
  4.    d
 Discuss Question
Answer: Option C. -> c

Look closely at line 2, is this an equality check (==) or an assignment (=). The condition 

at line 2 evaluates to false and also assigns false to bool. bool is now false so the condition 

at line 6 is not true. The condition at line 10 checks to see if bool is not true ( if !(bool == true) ), 

it isn't so line 12 is executed.


Question 24.


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);
}
}
  1.    j = 0
  2.    j = 2
  3.    j = 4
  4.    j = 6
 Discuss Question
Answer: Option D. -> j = 6

Because there are no break statements, the program gets to the default case and adds 

2 to j, then goes to case 0and adds 4 to the new j. The result is j = 6.


Question 25.


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")
  1.    done
  2.    one two done
  3.    one two three done
  4.    one two three two three done
 Discuss Question
Answer: Option D. -> one two three two three done

The variable i will have the values 0, 1 and 2.

When i is 0, nothing will be printed because of the break in case 0.

When i is 1, "one two three" will be output because case 1, case 2 and case 3 will be 

executed (they don't have break statements).

When i is 2, "two three" will be output because case 2 and case 3 will be executed (again 

no break statements).

Finally, when the for loop finishes "done" will be output.


Question 26.


What will be the output of the program?


int i = 0;
while(1)
{
if(i == 4)
{
break;
}
++i;
}
System.out.println("i = " + i);
  1.    i = 0
  2.    i = 3
  3.    i = 4
  4.    Compilation fails.
 Discuss Question
Answer: Option D. -> Compilation fails.

Compilation fails because the argument of the while loop, the condition, must be of 

primitive type boolean. In Java, 1 does not represent the true state of a boolean, 

rather it is seen as an integer.


Question 27.


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');
}
}
}
  1.    ABDCBDCB
  2.    ABCDABCD
  3.    Compilation fails.
  4.    An exception is thrown at runtime.
 Discuss Question
Answer: Option A. -> ABDCBDCB

'A' is only printed once at the very start as it is in the initialisation SECTION of the for loop. 

The loop will only initialise that once.

'B' is printed as it is part of the test carried out in order to run the loop.

'D' is printed as it is in the loop.

'C' is printed as it is in the increment section of the loop and will 'increment' only at the end 

of each loop. Here ends the first loop. Again 'B' is printed as part of the loop test.

'D' is printed as it is in the loop.

'C' is printed as it 'increments' at the end of each loop.

Again 'B' is printed as part of the loop test. At this point the test fails because the other part of the 

test (i < 2) is no longer true. i has been increased in value by 1 for each loop with the line: i++;

This results in a printout of ABDCBDCB


Question 28.


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");
}
  1.    Zero
  2.    Twelve
  3.    Default
  4.    Compilation fails
 Discuss Question
Answer: Option D. -> Compilation fails

The switch statement can only be supported by integers or variables more "narrow" 

than an integer i.e. byte, char, short. Here a Float wrapper object is used and so the 

compilation fails.


Question 29.


What will be the output of the program?


int x = 3;
int y = 1;
if (x = y) /* Line 3 */
{
System.out.println("x =" + x);
}
  1.    x = 1
  2.    x = 3
  3.    Compilation fails.
  4.    The code runs with no output.
 Discuss Question
Answer: Option C. -> Compilation fails.

Line 3 uses an assignment as opposed to comparison. Because of this, the if statement 

receives an integer value instead of a boolean. And so the compilation fails.


Question 30.


What will be the output of the program?


for (int i = 0; i < 4; i += 2)
{
System.out.print(i + " ");
}
System.out.println(i); /* Line 5 */
  1.    0 2 4
  2.    0 2 4 5
  3.    0 1 2 3 4
  4.    Compilation fails.
 Discuss Question
Answer: Option D. -> Compilation fails.

Compilation fails on the line 5 - System.out.println(i); as the variable i has only been 

declared within the for loop. It is not a recognised variable outside the code block of loop.


Latest Videos

Latest Test Papers