ProgrammingVery hardTCS NQT Prime · Infosys SP
What is printed?
What is printed?
class A {
int a = f("a");
{ f("i"); }
A() { f("c"); }
int f(String s) { System.out.print(s); return 1; }
int b = f("b");
}
// main: new A();- A. aibc
- B. abic
- C. cabi
- D. iabc
Correct answer: A. aibc
Field initializers and instance initializer blocks run in textual order (a, i, b) before the constructor body (c).
More OOP with C++ and Java questions
- What is printed? java class A { static int x = init("Ax"); static int init(String s) { System.out.print(s); return 1; } } class B extends…
- What is printed? java List<Integer> l = new ArrayList<>(List.of(10, 20, 30, 1)); l.remove(1); l.remove(Integer.valueOf(1))…
- What is printed? java Object[] o = new String[1]; try { o[0] = 1; System.out.print("T"); } catch (ArrayStoreException e) {…
- What is printed? java int i = 1; i = i++ + ++i; int j = 5; j += j++ 2; System.out.print(i + " " + j);
- What is printed? java short s = 1; s = (short) (s + 1); char c = 'A'; c += 1; c++; System.out.print(s + " " + c + " " + (c + 1));
- What is printed? java int i = 0; System.out.print(true ? 'a' : 0); System.out.print(true ? 'a' : i);