ProgrammingVery hardInfosys DSE · Wipro Elite
What is printed?
What is printed?
class A {
static int x = init("Ax");
static int init(String s) { System.out.print(s); return 1; }
}
class B extends A { static int y = init("By"); }
// main:
System.out.print(B.x);
System.out.print(B.y);- A. AxBy11
- B. Ax1By1
- C. By1Ax1
- D. Ax1Ax1By1
Correct answer: B. Ax1By1
B.x refers to a field of A, so only A is initialized then; B is initialized on first use of B.y.
More OOP with C++ and Java questions
- 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);
- What is printed? java class T { static int v = f(); static int f() { throw new RuntimeException(); } } // main: for (int k = 0; k < 2…