ProgrammingHardProduct-company placement · Java
How many of these statements are correct in Java? (i) A subclass object contains storage for its superclass's private fields. (ii) A subclass method can read a superclass's private field directly by name. (iii) A method in a subclass with the same signature as a private superclass method does not override it. (iv) Constructors of a superclass are inherited by its subclasses.
- A. 1
- B. 2
- C. 3
- D. 4
Correct answer: B. 2
(i) and (iii) are correct: private state still exists in the object (reachable through inherited public methods), and private methods are invisible so they cannot be overridden. (ii) and (iv) are false: constructors are never inherited.
More OOP with C++ and Java questions
- What is printed? java int[][] a = {{1}, {2}}; int[][] b = a.clone(); b[0][0] = 9; b[1] = new int[]{5}; System.out.print(a[0][0] + " " +…
- What is printed? java StringBuilder[] sbs = new StringBuilder[3]; Arrays.fill(sbs, new StringBuilder()); sbs[0].append("x")…
- What is printed? java List<Integer> l = new ArrayList<>(10); System.out.print(l.size() + " "); l.set(0, 1); System.out.print(l);
- What is printed? java List<Integer> l = new ArrayList<>(List.of(2, 2, 3, 2, 4)); for (int i = 0; i < l.size(); i++) if (l.get(i) == 2)…
- What is printed? java Set<String> s = new HashSet<>(); System.out.print(s.add("a") + " " + s.add("a") + " " + s.remove("b") + " " +…
- What is printed? java Map<String, Integer> m = Map.of("a", 1, "a", 2); System.out.print(m);