ProgrammingHardProduct-company placement · Java
Which statement about the protected modifier is true in Java but NOT in C++?
- A. A protected member is accessible to any class in the same package, even one that is not a subclass
- B. A protected member is accessible in subclasses
- C. A protected member is inaccessible to unrelated classes in other packages or files
- D. A protected member can be overridden if it is a method
Correct answer: A. A protected member is accessible to any class in the same package, even one that is not a subclass
Java's protected = package access + subclass access. C++ has no packages: protected members are visible only to the class, its friends and derived classes.
More OOP with C++ and Java questions
- What is printed? java // file a/P.java package a; public class P { void f() { System.out.print("P"); } // package-private public void…
- What is printed? java String r = Stream.of(3, 1, 3, 2).distinct().sorted() .map(String::valueOf).collect(Collectors.joining("-"))…
- What is printed? java System.out.print(Optional.ofNullable(null).map(Object::toString).orElse("none") + " ")…
- What happens when this is compiled? java interface I { void a(); void b(); } abstract class P implements I { public void a() { } } class C…
- Which line fails to compile? cpp struct P { int x = 3; ostream& operator<<(ostream& os) { return os << x; } }; int main() { P p; cout <<…
- What is wrong with this program? cpp struct Buf { int p; Buf() : p(new int[4]) { } ~Buf() { delete[] p; } }; int main() { Buf a; Buf b =…