Predict Java program output
class Output { final static short i = 2; public static int j = 0; public static void main(String [] args) { for (int k = 0; k < 3; k++) { switch (k) { case i : System.out.print(" 1 "); case i - 1 : System.out.print(" 2 "); default : System.out.print(" 0 "); } } } }0 2 0 1 2 0
0 2 0 1 2 0
class Test { protected int x, y; } class Main { public static void main(String args [ ]) { Test t = new Test(); System.out.print("x = " +t.x); } }
0, 0 (Default constructors initialize integral variables as 0 in Java)
// filename: Test.java class Test { int x = 10; public static void main(String[] args) { Test t = new Test(); System.out.println(t.x); } }
10
class Output { final static short i = 2; public static int j = 0; public static void main(String [] args) { for (int k = 0; k < 3; k++) { switch (k) { case i: System.out.print(" 0 "); case i-1: System.out.print(" 1 "); case i-2: System.out.print(" 2 "); } } } }
2 1 2 0 1 2
class Test { public static void main(String args [ ]) { System.out.print(fun()); } static int fun() { return 20; } }
20
public class Final { final int assign = 30; public static void main(String[] args) { final int result = 20; final int assign; Final f = new Final(); assign = 20; System.out.println(assign); System.out.println(f.assign); System.out.println(f.process(result)); } int process(int a) { return a + 5; } }
20 30 25
package main; class Base { public void Print() { System.out.println("Base"); } } class Derived extends Base { public void Print() { System.out.println("Derived"); } } class Main{ public static void DoPrint( Base o ) { o.Print(); } public static void main(String[] args) { Base x = new Base(); Base y = new Derived(); Derived z = new Derived(); DoPrint(x); DoPrint(y); DoPrint(z); } }
Base Derived Derived
class Test2 { public static void main(String[] args) { String str[] = { "Geeks", "for", "Geeks" }; for (int i = 0; i < str.length; i++) System.out.print(str[i]); } } A)GeeksforGeeks B)Error C)Geeks D)GfG
GeeksforGeeks
class Test { public static void main(String args [ ]) { for(int = i = 0; true; i++) { System.out.print("Hello"); break; } } }
Hello
class Test1 { Test1(int x) { System.out.println("Constructor called " + x); } } // This class contains an instance of Test1 class Test2 { Test1 t1 = new Test1(10); Test2(int i) { t1 = new Test1(i); } public static void main(String[] args) { Test2 t2 = new Test2(5); } }
The output of the program is Constructor called 10 Constructor called 5.
public class Final { int a = 30; public static void main(String[] args) { final int assign; Final b = new Final(); process(b); System.out.println(b.a); process(b); assign = b.a; System.out.println(assign); } public static void process(Final a) { a.a = a.a + 5; } }
35 40
/ filename: Test.java class Test { int y = 2; int x = y+2; public static void main(String[] args) { Test m = new Test(); System.out.println("x = " + m.x + ", y = " + m.y); } }
4
// filename: Test.java public class Test { int x = 2; Test(int i) { x = i; } public static void main(String[] args) { Test t = new Test(5); System.out.println("x = " + t.x); } }
5
class Test { public static void main(String args [ ]) { for(int = i = 0; 1; i++) { System.out.print("Hello"); break; } } }
error