Question 61
HardWhat is the output of this code?
#include <iostream>
using std;
{
:
() { (); }
{ cout << ; }
};
: Base {
:
() { (); }
{ cout << ; }
};
{
Der d;
}
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 << ; }
};
: Base {
:
() { (); }
{ cout << ; }
};
{
Der d;
}
Der Der
Der Base
Base Der
Base Base
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 void f(int x = 1) { cout << "B" << x; }
};
class Der : public Base {
public:
void f(int x = 2) override { cout << "D" << x; }
};
int main() {
Der d;
Base &b = d;
b.f();
}
D1
B1
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 : public A {
public:
void show() override { cout << "B"; }
};
int main() {
B b;
A a = b;
A &r = b;
A *p = &b;
a.show(); r.show(); p->show();
}
BBB
BAB
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;
void f(long x) { cout << "long "; }
void f(double x) { cout << "double "; }
void f(int *p) { cout << "ptr "; }
int main() {
f(5L);
f(5.0f);
f(nullptr);
}
Compilation error
long double ptr
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class V:
def __init__(self, x): self.x = x
def __add__(self, o): return V(self.x + (o.x if isinstance(o, V) else o))
def __radd__(self, o): return self + o
def __repr__(self): return f"V({self.x})"
print(sum([V(1), V(2), V(3)]))
6
V(3)
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class A:
def __eq__(self, o): return True
__hash__ = None
a, b = A(), A()
try:
print(a == b, len({a}))
except TypeError:
print(a == b, "unhashable")
True unhashable
True 2
Sign up free to see the answer and a step-by-step explanation.
What is the difference between static and dynamic binding?
They are the same
Static binding chooses the function at compile time; dynamic binding chooses it at run time from the object's real type
Dynamic binding happens at compile time
Static binding applies only to virtual functions
Sign up free to see the answer and a step-by-step explanation.
How does a vtable make virtual calls work in C++?
Each object stores copies of its functions
The compiler inlines every virtual call
Each polymorphic object holds a pointer to its class's table of function addresses, which the call looks up
The program searches all classes at run time
Sign up free to see the answer and a step-by-step explanation.
Why can't a C++ constructor be virtual?
C++ forbids all virtual functions
The object's dynamic type (and vtable) isn't set up until construction runs
Constructors have no names
It would be slower
Sign up free to see the answer and a step-by-step explanation.
What is double dispatch, and which pattern implements it?
Overloading; the Factory pattern
Choosing behaviour based on the run-time types of two objects; the Visitor pattern
Calling a method twice; the Singleton pattern
Static binding; the Builder pattern
Sign up free to see the answer and a step-by-step explanation.
B2
D2
AAA
ABB
double double ptr
long long ptr
TypeError
V(6)
False unhashable
True 1