ProgrammingVery hardGATE CS · TCS Digital · Advanced
What is printed?
What is printed?
System.out.print(true ? 1 : 2.0); System.out.print(true ? 'a' : 0); int i = 0; System.out.print(true ? 'a' : i);
- A. 1a97
- B. 1.0a97
- C. 1.0aa
- D. 1.0a0
Correct answer: B. 1.0a97
Mixing int and double promotes to double (1.0). A char with an int constant that fits in char stays char ('a'). With a non-constant int variable the result type is int, so 'a' is printed as 97.
More OOP with C++ and Java questions
- What is printed? java static int f() { try { throw new RuntimeException(); } finally { return 1; } } static int g() { int x = 1; try {…
- What is printed? java class K { int v; K(int v) { this.v = v; } public int hashCode() { return v; } public boolean equals(Object o) {…
- What is printed? java Number[] n = new Integer[2]; n[0] = 1; try { n[1] = 2.5; } catch (ArrayStoreException e) { System.out.print("ASE ")…
- What is printed? java String a = "ab"; String b = "a"; String c = b + "b"; final String d = "a"; String e = d + "b"; System.out.print((a…
- What is printed? java interface A { default String m() { return "A"; } } interface B extends A { default String m() { return "B"; } }…
- What is printed? java class P { String n = "P"; static String s() { return "Ps"; } String t() { return "Pt"; } } class C extends P {…