Question 61
HardWhat is the output of this code?
#include <iostream>
using std;
{
:
= ;
~() = ;
{ * (); }
};
: Shape {
s;
:
( x) : (x) {}
{ s * s; }
};
{
Shape &s = ();
cout << s.();
}
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;
{
:
= ;
~() = ;
{ * (); }
};
: Shape {
s;
:
( x) : (x) {}
{ s * s; }
};
{
Shape &s = ();
cout << s.();
}
9
Compilation error
0
18
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 run() = 0;
};
void Base::run() { cout << "base-run "; }
class Impl : public Base {
public:
void run() override { Base::run(); cout << "impl-run"; }
};
int main() {
Impl i;
i.run();
}
Compilation error
base-run
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
from abc import ABC, abstractmethod
class Repo(ABC):
@abstractmethod
def get(self, k): ...
def get_or(self, k, d):
v = self.get(k)
return d if v is None else v
class Mem(Repo):
def __init__(self): self.d = {"a": 1}
def get(self, k): return self.d.get(k)
r = Mem()
print(r.get_or("a", 0), r.get_or("b", 0), isinstance(r, Repo))
1 None True
TypeError
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
from abc import ABC, abstractmethod
class A(ABC):
@property
@abstractmethod
def name(self): ...
class B(A):
name = "bee"
print(B().name)
TypeError
name
Sign up free to see the answer and a step-by-step explanation.
Java 8 added default methods to interfaces. What problem did this mainly solve?
Allowing interfaces to have instance fields
Adding new methods to widely used interfaces (such as Collection.stream()) without breaking every existing implementation
Making interfaces faster
Allowing multiple inheritance of state
Sign up free to see the answer and a step-by-step explanation.
Why should high-level modules depend on abstractions rather than concrete classes?
Because concrete classes are slower
To make code longer
Java requires it
So implementations can be swapped (for example in tests) without changing the high-level code
Sign up free to see the answer and a step-by-step explanation.
What is a 'leaky abstraction'?
An abstraction whose hidden details still affect its users, such as an ORM hiding SQL until a query is slow
A memory leak
An interface with too many methods
An abstract class with no methods
Sign up free to see the answer and a step-by-step explanation.
In C++, what makes a class abstract?
Being declared final
Having no members
Having a private constructor
Having at least one pure virtual function
Sign up free to see the answer and a step-by-step explanation.
Which is the best abstraction for code that must send notifications by email, SMS or push?
One class with a switch on a channel string
A Notifier interface with send(message), implemented once per channel
Three unrelated functions called by name
A global variable naming the channel
Sign up free to see the answer and a step-by-step explanation.
What does the Template Method pattern do?
Logs method calls
Creates objects from templates
Defines an algorithm's skeleton in a base class and lets subclasses fill in specific steps
Copies objects
Sign up free to see the answer and a step-by-step explanation.
impl-run
base-run impl-run
1 0 False
1 0 True
bee
None