Question 61
HardWhat is the output of this code?
#include <iostream>
using std;
{
:
() { cout << ; }
~() { cout << ; }
};
: A {
:
() { cout << ; }
~() { cout << ; }
};
{
A *p = ();
p;
}
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Object-Oriented Programming
What is the output of this code?
#include <iostream>
using std;
{
:
() { cout << ; }
~() { cout << ; }
};
: A {
:
() { cout << ; }
~() { cout << ; }
};
{
A *p = ();
p;
}
BAAB
ABAB
ABBA
AB~A
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:
A() { cout << "A"; }
~A() { cout << "~A"; }
};
class B : public A {
public:
B() { cout << "B"; }
~B() { cout << "~B"; }
};
int main() {
A *p = new B();
delete p;
}
ABBA
AB~A
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: int v = 1; };
class B : virtual public A {};
class C : virtual public A {};
class D : public B, public C {};
int main() {
D d;
d.B::v = 5;
cout << d.C::v;
}
0
5
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: int v = 1; };
class B : public A {};
class C : public A {};
class D : public B, public C {};
int main() {
D d;
d.B::v = 5;
cout << d.C::v;
}
0
1
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class A:
def __init__(self):
print("A", end="")
class B(A):
def __init__(self):
print("B", end="")
super().__init__()
class C(A):
def __init__(self):
print("C", end="")
super().__init__()
class D(B, C):
def __init__(self):
print("D", end="")
super().__init__()
D()
DCBA
DBACA
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class Base:
def greet(self):
return "base:" + self.name()
def name(self):
return "B"
class Child(Base):
def name(self):
return "C"
print(Child().greet())
base:C
AttributeError
Sign up free to see the answer and a step-by-step explanation.
A class Square extends Rectangle and overrides setWidth to also change the height. Code that sets width 4 and height 5 then expects area 20 gets 25. Which principle is broken?
Liskov Substitution
Open/Closed
Single Responsibility
Interface Segregation
Sign up free to see the answer and a step-by-step explanation.
Why does Java allow multiple inheritance of interfaces but not of classes?
It is a JVM bug
Interfaces are faster
Classes are too large
Interfaces (traditionally) carry no state, so there is no ambiguity about which fields to inherit
Sign up free to see the answer and a step-by-step explanation.
A Java class implements two interfaces that both provide a default method hello(). What must it do?
Make hello() static
Nothing; Java picks one
Override hello(), optionally calling A.super.hello() or B.super.hello()
Rename one interface
Sign up free to see the answer and a step-by-step explanation.
In C++, private inheritance (class D : private B) models which relationship?
'Has-a' through a pointer
No relationship
'Implemented in terms of', not 'is-a'
'Is-a'
Sign up free to see the answer and a step-by-step explanation.
ABAB
B~B
1
Compilation error
5
Compilation error
DBA
DBCA
C
base:B