ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
class T {
static { System.out.print("S"); }
{ System.out.print("I"); }
T() { System.out.print("C"); }
}
// in main:
new T();
new T();- A. ICSIC
- B. SCIC
- C. SICIC
- D. SICSIC
Correct answer: C. SICIC
The static block runs once, when the class is first initialised. For every object the instance initialiser block runs before the constructor body. So S, then IC twice.
More OOP with C++ and Java questions
- What is printed? java // static methods of the main class: static int f() { int x = 1; try { return x; } finally { x = 5; } } static int…
- What is printed? java System.out.print(1 + 2 + "3" + 4 + 5);
- What is printed? java // static methods of the main class: static void change(StringBuilder sb) { sb.append("X"); sb = new…
- What is printed? java List<Integer> l = new ArrayList<>(List.of(1, 2, 3)); l.remove(1); System.out.print(l + " ")…
- What is printed? java List<Integer> list = new ArrayList<>(List.of(1, 2, 3)); for (Integer i : list) if (i == 2) list.remove(i)…
- What happens with this C++ code? cpp void f(int a, int b = 0) { cout << "two"; } void f(int a) { cout << "one"; } int main() { f(5); }