ProgrammingHardIBM · Deloitte pattern
class D : private B { ... }; where B has public int x, protected int y and private int z. Which is correct?
- A. Inside D's member functions x, y and z are accessible
- B. None of x, y, z is accessible inside D
- C. x stays public in D, so main can use d.x
- D. Inside D's member functions x and y are accessible, but main cannot access x through a D object
Correct answer: D. Inside D's member functions x and y are accessible, but main cannot access x through a D object
Private inheritance turns B's public and protected members into private members of D: usable inside D, hidden from outside. B's private z is never accessible to a derived class.
More OOP with C++ and Java questions
- Which of these is a checked exception in Java?
- A parent class declares protected void run(). Which overriding declaration in a subclass compiles?
- Parent declares void read() throws IOException. Which override in a subclass compiles?
- What is printed? java Set<String> t = new TreeSet<>(List.of("apple", "Banana", "cherry")); System.out.print(t);
- What happens when this Java code is compiled? java class P { P(int x) { } } class C extends P { C() { } }
- What is printed? java // static methods of the main class: static void f(int... a) { System.out.print("V"); } static void f(int a, int b)…