ProgrammingModerateWipro · Capgemini pattern
A parent class declares protected void run(). Which overriding declaration in a subclass compiles?
- A. private void run()
- B. public void run()
- C. void run()
- D. protected int run()
Correct answer: B. public void run()
An override may keep or widen access but never narrow it: protected can become public, but not package-private or private. Changing the return type from void to int is not a valid override either.
More OOP with C++ and Java questions
- 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)…
- Which member function fails to compile? cpp class C { int a = 0; mutable int hits = 0; public: int get() const { hits++; return a; } void…
- Which C++ operator can be overloaded only as a non-static member function, never as a free function?