ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
class Parent {
protected int score = 50;
int getScore() { return score; }
}
class Child extends Parent {
Child() { score += 10; }
}
class GrandChild extends Child {
GrandChild() { score *= 2; }
}
// in main:
System.out.println(new GrandChild().getScore());- A. 120
- B. 100
- C. 60
- D. 50
Correct answer: A. 120
Construction order is Parent (score=50) -> Child (score=50+10=60) -> GrandChild (score=60*2=120); getScore() is inherited unchanged and simply reads the final shared score field: 120.
More OOP with C++ and Java questions
- What is printed? java class Base { void greet() { System.out.print("Base "); helper(); } private void helper() {…
- What is printed? cpp struct Base { virtual void save() { cout << "BaseSave "; } }; struct Derived : Base { void save() override { cout <<…
- What is printed? java class Animal { String sound() { return "..."; } } class Dog extends Animal { @Override String sound() { return…
- What is printed? cpp struct Base { virtual int value() const { return 10; } }; struct Derived : Base { int value() const override { return…
- What is printed? java interface Payable { double rate(); default double monthly(double hours) { return hours rate(); } } class Intern…
- What is printed? cpp struct Base { virtual void print() const { cout << "Base"; } friend ostream& operator<<(ostream &os, const Base &b) {…