Control Flow

Java control flow statements including loops, conditional statements, and switch statements

11 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

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
Question 2 Multiple Choice (Single Answer)

public void test(int x)
{
int odd = 1;
if(odd) /* Line 4 */
{
System.out.println(odd);
}
else
{
System.out.println(even);
}
}

Which statement is true?

  1. compilation fails.
  2. odd will always be output.
  3. even will always be output.
  4. odd will be output for odd values of x, and even for even values.
Question 3 Multiple Choice (Single Answer)

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
Question 4 Multiple Choice (Single Answer)

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
Question 5 Multiple Choice (Single Answer)

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. no output
Question 6 Multiple Choice (Single Answer)

class MyClass
{
public static void main()
{
int s=2;
for(int i=0;i<=s;i++);
{
System.out.print(i);
}
}
}

  1. 01
  2. undefined loop
  3. Run time error
  4. compile time error
  5. 012
Question 7 Multiple Choice (Single Answer)

public class While
{
public void loop()
{
int x= 0;
while ( 1 ) /* Line 6 /
{
System.out.print("x plus one is " + (x + 1)); /
Line 8 */
}
}
}
Which statement is true?

  1. There is a syntax error on line 1.
  2. There are syntax errors on lines 1 and 6.
  3. There are syntax errors on lines 1, 6, and 8.
  4. There is a syntax error on line 6.
Question 8 Multiple Choice (Single Answer)

public class Test2
{
public static int x;
public static int foo(int y)
{
return y * 2;
}
public static void main(String [] args)
{
int z = 5;
assert z > 0; /* Line 11 /
assert z > 2: foo(z); /
Line 12 /
if ( z < 7 )
assert z > 4; /
Line 14 */

    switch (z) 
    {
        case 4: System.out.println(&quot;4 &quot;);
        case 5: System.out.println(&quot;5 &quot;);
        default: assert z &lt; 10;
    }

    if ( z &lt; 10 )
        assert z &gt; 4: z++; /* Line 22 */
    System.out.println(z);
}

}

Which line is an example of an inappropriate use of assertions?

  1. Line 11
  2. Line 12
  3. Line 14
  4. Line 22
Question 9 Multiple Choice (Single Answer)

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
Question 10 Multiple Choice (Single Answer)

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
Question 11 Multiple Choice (Single Answer)

What will be the output of the program?
class Test
{
public static void main(String [] args)
{
int x= 0;
int y= 0;
for (int z = 0; z < 5; z++)
{
if (( ++x > 2 ) && (++y > 2))
{
x++;
}
}
System.out.println(x + " " + y);
}
}

  1. 5 2
  2. 5 3
  3. 6 3
  4. 6 4