ProgrammingVery hardProduct-company placement · Java
What is printed?
What is printed?
class C { static { System.out.print("init "); } }
// in main:
C[] a = new C[2];
Class<?> k = C.class;
System.out.print("x ");
new C();- A. x init
- B. init x
- C. init init x
- D. init x init
Correct answer: A. x init
Creating an array of C and evaluating the class literal C.class do not trigger class initialisation; the first instance creation does.
More OOP with C++ and Java questions
- What is printed? java try { try { System.out.print("1"); throw new Exception("a"); } catch (Exception e) { System.out.print("2"); throw…
- What happens when this is compiled (inside a method declared throws IOException)? java List<String> files = List.of("a.txt")…
- Which line fails to compile? java List<?> l = new ArrayList<String>(); l.add(null); // L1 l.add("x"); // L2 Object o = l.isEmpty() ? null…
- Which line fails to compile? java class Fruit implements Comparable<Fruit> { public int compareTo(Fruit o) { return 0; } } class Apple…
- Which line fails to compile? java // Java 16 or later Object o = "hello"; if (o instanceof String s && s.length() > 3)…
- What is printed? java Set<Short> s = new HashSet<>(); for (short i = 0; i < 5; i++) { s.add(i); s.remove(i - 1); }…