Question 51
MediumWhat does the 'protected' keyword on a field allow in C++?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Object-Oriented Programming
What does the 'protected' keyword on a field allow in C++?
Access only from the same file
Access only from friend classes
Access from anywhere
Access from the class and its derived classes, but not from outside
Sign up free to see the answer and a step-by-step explanation.
In Python, how does a child class call the parent's version of method save()?
self.parent.save()
parent.save()
base.save()
super().save()
Sign up free to see the answer and a step-by-step explanation.
What is the main problem of the 'fragile base class' situation?
Changes to a base class can unexpectedly break subclasses that depend on its details
Subclasses cannot add methods
Base classes can't have constructors
Base classes are deleted at run time
Sign up free to see the answer and a step-by-step explanation.
Can a subclass in Java override a private method of its parent?
No; private methods are not inherited, so a method with the same name is a new method
Only if it is static
Yes, always
Only in the same package
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
#include <iostream>
using namespace std;
class A { public: virtual void show() { cout << "A"; } };
class B : virtual public A { public: void show() override { cout << "B"; } };
class C : virtual public A { };
class D : public B, public C { };
int main() {
D d;
d.show();
return 0;
}
A
Error
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
#include <iostream>
using namespace std;
class Base {
public:
~Base() { cout << "Base destroyed"; }
};
class Derived : public Base {
public:
~Derived() { cout << "Derived destroyed "; }
};
int main() {
Base* p = new Derived();
delete p;
return 0;
}
Derived destroyed Base destroyed
Derived destroyed
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
#include <iostream>
using namespace std;
class Base {
public:
virtual ~Base() { cout << "Base destroyed"; }
};
class Derived : public Base {
public:
~Derived() override { cout << "Derived destroyed "; }
};
int main() {
Base* p = new Derived();
delete p;
return 0;
}
Base destroyed
Derived destroyed Base destroyed
Sign up free to see the answer and a step-by-step explanation.
When should composition be preferred over inheritance?
When the relationship is HAS-A, or behaviour needs to vary without a rigid class hierarchy
Only when classes are final
When a class has no fields
Always, inheritance should never be used
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class Parent { int x = 10; }
class Child extends Parent { int x = 20; }
public class Main {
public static void main(String[] args) {
Parent p = new Child();
System.out.println(p.x);
}
}
Error
0
Sign up free to see the answer and a step-by-step explanation.
What does the 'final' keyword on a class mean in Java?
The class cannot be instantiated
It must be abstract
All its methods are static
The class cannot be extended
Sign up free to see the answer and a step-by-step explanation.
AB
B
Nothing
Base destroyed
Derived destroyed
Base destroyed Derived destroyed
10
20