Sail E0 Webinar

MCQs

Total Questions : 42 | Page 4 of 5 pages
Question 31.


What will be the output of the program?


int x = l, y = 6;
while (y--)
{
x++;
}
System.out.println("x = " + x +" y = " + y);
  1.    x = 6 y = 0
  2.    x = 7 y = 0
  3.    x = 6 y = -1
  4.    Compilation fails.
 Discuss Question
Answer: Option D. -> Compilation fails.

Compilation fails because the while loop demands a boolean argument for it's looping

 condition, but in the code, it's given an int argument.

while(true) { //insert code here }


Question 32.


What will be the output of the program?


int I = 0;
outer:
while (true)
{
I++;
inner:
for (int j = 0; j < 10; j++)
{
I += j;
if (j == 3)
continue inner;
break outer;
}
continue outer;
}
System.out.println(I);
  1.    1
  2.    2
  3.    3
  4.    4
 Discuss Question
Answer: Option A. -> 1

The program flows as follows: I will be incremented after the while loop is entered,

 then I will be incremented (by zero) when the for loop is entered. The if statement

 evaluates to false, and the continue statement is never reached. The break statement

 tells the JVM to break out of the outer loop, at which point I is printed and the fragment

 is done.


Question 33.


What will be the output of the program?


public class If1
{
static boolean b;
public static void main(String [] args)
{
short hand = 42;
if ( hand < 50 && !b ) /* Line 7 */
hand++;
if ( hand > 50 ); /* Line 9 */
else if ( hand > 40 )
{
hand += 7;
hand++;
}
else
--hand;
System.out.println(hand);
}
}
  1.    41
  2.    42
  3.    50
  4.    51
 Discuss Question
Answer: Option D. -> 51

In Java, boolean instance variables are initialized to false, so the if test on line 7 is

 true and hand is incremented. Line 9 is legal syntax, a do nothing statement. The

 else-if is true so hand has 7 added to it and is then incremented.


Question 34.


What will be the output of the program?


public class Test
{
public static void main(String [] args)
{
int I = 1;
do while ( I < 1 )
System.out.print("I is " + I);
while ( I > 1 ) ;
}
}
  1.    I is 1
  2.    I is 1 I is 1
  3.    No output is produced.
  4.    Compilation error
 Discuss Question
Answer: Option C. -> No output is produced.

There are two different looping constructs in this problem. The first is a do-while loop

 and the second is a while loop, nested inside the do-while. The body of the do-while

 is only a single statement-brackets are not needed. You are assured that the while

 expression will be evaluated at least once, followed by an evaluation of the do-while

 expression. Both expressions are false and no output is produced.


Question 35.


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 < 3; z++)
{
switch (z)
{
case y: System.out.print("0 "); /* Line 11 */
case x-1: System.out.print("1 "); /* Line 12 */
case x: System.out.print("2 "); /* Line 13 */
}
}
}
}
  1.    0 1 2
  2.    0 1 2 1 2 2
  3.    Compilation fails at line 11.
  4.    Compilation fails at line 12.
 Discuss Question
Answer: Option C. -> Compilation fails at line 11.

Case expressions must be constant expressions. Since x is marked final, lines

 12 and 13 are legal; however y is not a final so the compiler will fail at line 11.


Question 36.


What will be the output of the program?


int i = 1, j = 10;
do
{
if(i > j)
{
break;
}
j--;
} while (++i < 5);
System.out.println("i = " + i + " and j = " + j);
  1.    i = 6 and j = 5
  2.    i = 5 and j = 5
  3.    i = 6 and j = 4
  4.    i = 5 and j = 6
 Discuss Question
Answer: Option D. -> i = 5 and j = 6

This loop is a do-while loop, which always executes the code block within the block at 

least once, due to the testing condition being at the end of the loop, rather than at the 

beginning. This particular loop is exited prematurely if ibecomes greater than j.

The order is, test i against j, if bigger, it breaks from the loop, decrements j by one, and 

then tests the loop condition, where a pre-incremented by one i is tested for being lower 

than 5. The test is at the end of the loop, so ican reach the value of 5 before it fails. So it 

goes, start:

1, 10

2, 9

3, 8

4, 7

5, 6 loop condition fails.


Question 37.


What will be the output of the program?


public class SwitchTest
{
public static void main(String[] args)
{
System.out.println("value =" + switchIt(4));
}
public static int switchIt(int x)
{
int j = 1;
switch (x)
{
case l: j++;
case 2: j++;
case 3: j++;
case 4: j++;
case 5: j++;
default: j++;
}
return j + x;
}
}
  1.    value = 2
  2.    value = 4
  3.    value = 6
  4.    value = 8
 Discuss Question
Answer: Option D. -> value = 8

Because there are no break statements, once the desired result is found, the program

 continues though each of the remaining options.


Question 38.


What will be the output of the program?


public class If2
{
static boolean b1, b2;
public static void main(String [] args)
{
int x = 0;
if ( !b1 ) /* Line 7 */
{
if ( !b2 ) /* Line 9 */
{
b1 = true;
x++;
if ( 5 > 6 )
{
x++;
}
if ( !b1 )
x = x + 10;
else if ( b2 = true ) /* Line 19 */
x = x + 100;
else if ( b1 | b2 ) /* Line 21 */
x = x + 1000;
}
}
System.out.println(x);
}
}
  1.    0
  2.    1
  3.    101
  4.    111
 Discuss Question
Answer: Option C. -> 101

As instance variables, b1 and b2 are initialized to false. The if tests on lines 7

 and 9 are successful so b1 is set to true and x is incremented. The next if test

 to succeed is on line 19 (note that the code is not testing to see if b2 is true, it is

 setting b2 to be true). Since line 19 was successful, subsequent else-if's (line 21)

 will be skipped.


Question 39.


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 < 3; z++)
{
switch (z)
{
case x: System.out.print("0 ");
case x-1: System.out.print("1 ");
case x-2: System.out.print("2 ");
}
}
}
}
  1.    0 1 2
  2.    0 1 2 1 2 2
  3.    2 1 0 1 0 0
  4.    2 1 2 0 1 2
 Discuss Question
Answer: Option D. -> 2 1 2 0 1 2

The case expressions are all legal because x is marked final, which means the

 expressions can be evaluated at compile time. In the first iteration of the for loop

 case x-2 matches, so 2 is printed. In the second iteration, x-1 is matched so 1

 and 2 are printed (remember, once a match is found all remaining statements

 are executed until a break statement is encountered). In the third iteration, x

 is matched. So 0 1 and 2 are printed.


Question 40.


What will be the output of the program?


int i = l, j = -1;
switch (i)
{
case 0, 1: j = 1; /* Line 4 */
case 2: j = 2;
default: j = 0;
}
System.out.println("j = " + j);
  1.    j = -1
  2.    j = 0
  3.    j = 1
  4.    Compilation fails.
 Discuss Question
Answer: Option D. -> Compilation fails.

The case statement takes only a single argument. The case statement on line 4 is 

given two arguments so the compiler complains.


Latest Videos

Latest Test Papers