0

Control Flow

Description: Control Flow
Number of Questions: 15
Created by:
Tags: Control Flow Java/Oracle /C/C++ Basics of Java Java
Attempted 0/15 Correct 0 Score 0

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


Correct Option: D
Explanation:

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 }

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.


Correct Option: A
Explanation:

The compiler will complain because of incompatible types (line 4), the if expects a boolean but it gets an integer.

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


Correct Option: D
Explanation:

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.

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


Correct Option: D
Explanation:

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.

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


Correct Option: C
Explanation:

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.

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


Correct Option: D
Explanation:

The scope of variable i is only for for(int i=0;i<=s;i++);  line .so that   System.out.println(i);  line   gives compile time error (cannot find symbol for variable I )

What will be the output of the program? public class ObjComp { public static void main(String [] args ) { int result = 0; ObjComp oc = new ObjComp(); Object o = oc;

if (o == oc)
result = 1; if (o != oc)
result = result + 10; if (o.equals(oc) )
result = result + 100; if (oc.equals(o) )
result = result + 1000;

System.out.println("result = " + result); } }

  1. 1

  2. 10

  3. 101

  4. 1101


Correct Option: D
Explanation:

Even though o and oc are reference variables of different types, they are both referring to the same object. This means that == will resolve to true and that the defaultequals() method will also resolve to true.

What will be the output of the program? String s = "hello"; Object o = s; if( o.equals(s) ) { System.out.println("A"); } else { System.out.println("B"); } if( s.equals(o) ) { System.out.println("C"); } else { System.out.println("D"); }

  1. A
  2. B
  3. C
  4. D
  1. 1 and 3

  2. 2 and 4

  3. 3 and 4

  4. 1 and 2


Correct Option: A

x = 0; if (x1.hashCode() != x2.hashCode() ) x = x + 1; if (x3.equals(x4) ) x = x + 10; if (!x5.equals(x6) ) x = x + 100; if (x7.hashCode() == x8.hashCode() ) x = x + 1000; System.out.println("x = " + x); and assuming that the equals() and hashCode() methods are property implemented, if the output is "x = 1111", which of the following statements will always be true?

  1. x2.equals(x1)

  2. x3.hashCode() == x4.hashCode()

  3. x5.hashCode() != x6.hashCode()

  4. x8.equals(x7)


Correct Option: B
Explanation:

By contract, if two objects are equivalent according to the equals() method, then thehashCode() method must evaluate them to be . Option A is incorrect because if the hashCode() values are not equal, the two objects must not be equal. Option C is incorrect because if equals() is not true there is no guarantee of any result from hashCode(). Option D is incorrect because hashCode() will often return  even if the two objects do not evaluate to equals() being true.

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.


Correct Option: D
Explanation:

Using the integer 1 in the while statement, or any other looping or conditional construct for that matter, will result in a compiler error. This is old C Program syntax, not valid Java. A, B and C are incorrect because line 1 is valid (Java is case sensitive so While is a valid class name). Line 8 is also valid because an equation may be placed in a String operation as shown.

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("4 "); case 5: System.out.println("5 "); default: assert z < 10; }

if ( z < 10 ) assert z > 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


Correct Option: D
Explanation:

Assert statements should not cause side effects. Line 22 changes the value of z if the assert statement is false. Option A is fine; a second expression in an assert statement is not required. Option B is fine because it is perfectly acceptable to call a method with the second expression of an assert statement. Option C is fine because it is proper to call an assert statement conditionally.

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


Correct Option: D
Explanation:

The case statement takes only a single argument. The case statement on line 4 is given two arguments so the compiler complains.

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


Correct Option: A
Explanation:

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.

What will be the output of the program? class Tree { } class Pine extends Tree { } class Oak extends Tree { } public class Forest1 { public static void main (String [] args) { Tree tree = new Pine(); if( tree instanceof Pine ) System.out.println ("Pine"); else if( tree instanceof Tree ) System.out.println ("Tree"); else if( tree instanceof Oak ) System.out.println ( "Oak" ); else System.out.println ("Oops "); } }

  1. Pine

  2. Tree

  3. Forest

  4. Oops


Correct Option: A
Explanation:

The program prints "Pine".

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


Correct Option: C
Explanation:

In the first two iterations x is incremented once and y is not because of the short circuit&& operator. In the third and forth iterations x and y are each incremented, and in the fifth iteration x is doubly incremented and y is incremented.

- Hide questions