Control Flow
Description: Control Flow | |
Number of Questions: 15 | |
Created by: Adhira Saini | |
Tags: Control Flow Java/Oracle /C/C++ Basics of Java Java |
What will be the output of the program? int x = l, y = 6; while (y--) { x++; } System.out.println(x = + x + y = + y);
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?
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);
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 */
What will be the output of the program? int x = 3; int y = 1; if (x = y) /* Line 3 */ { System.out.println(x = + x); }
class MyClass { public static void main() { int s=2; for(int i=0;i<=s;i++); { System.out.print(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); } }
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"); }
- A
- B
- C
- D
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?
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?
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?
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);
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);
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 "); } }
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); } }