Question 31
MediumWhat is the output of this code?
class Vec:
def ():
.x, .y = x, y
():
Vec(.x + o.x, .y + o.y)
v = Vec(, ) + Vec(, )
((v.x, v.y))
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?
class Vec:
def ():
.x, .y = x, y
():
Vec(.x + o.x, .y + o.y)
v = Vec(, ) + Vec(, )
((v.x, v.y))
(3, 7)
(4, 6)
(1, 2, 3, 4)
Error
Sign up free to see the answer and a step-by-step explanation.
Can a method be overloaded only by changing its return type?
Only for static methods
Only in Java
Yes, always
No, the parameter list must differ
Sign up free to see the answer and a step-by-step explanation.
Which methods cannot be overridden in Java?
static, final and private methods
Methods returning void
Methods with parameters
public methods
Sign up free to see the answer and a step-by-step explanation.
What is dynamic method dispatch?
Compiling methods faster
Overloading a method
Choosing which overridden method to call at run time from the object's actual type
Calling a method with no arguments
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 Shape {
public:
virtual double area() { return 0; }
};
class Sq : public Shape {
double s;
public:
Sq(double x) : s(x) {}
double area() override { return s * s; }
};
int main() {
Shape *a[2] = { new Shape(), new Sq(3) };
cout << a[0]->area() + a[1]->area();
}
18
3
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(int x) { cout << "int "; }
void f(double x) { cout << "double "; }
void f(char x) { cout << "char "; }
int main() {
f(5);
f(5.0);
f('5');
}
int double char
double double char
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 print() { cout << "B"; }
};
class Der : public Base {
public:
void print() { cout << "D"; }
};
void call(Base b) { b.print(); }
void callRef(Base &b) { b.print(); }
int main() {
Der d;
call(d);
callRef(d);
}
BB
BD
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 V {
public:
int x, y;
V(int a, int b) : x(a), y(b) {}
V operator+(const V &o) const { return V(x + o.x, y + o.y); }
};
int main() {
V a(1, 2), b(3, 4);
V c = a + b + a;
cout << c.x << "," << c.y;
}
5,6
5,8
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class Dog:
def speak(self): return "Woof"
class Robot:
def speak(self): return "Beep"
print(" ".join(x.speak() for x in [Dog(), Robot(), Dog()]))
Error: Robot is not an animal
Beep Beep Beep
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class M:
def __init__(self, v): self.v = v
def __add__(self, o): return M(self.v * o.v)
def __eq__(self, o): return self.v == o.v
print((M(2) + M(3)).v, M(6) == M(2) + M(3))
5 True
6 False
Sign up free to see the answer and a step-by-step explanation.
0
9
int int int
int double int
DB
DD
3,4
4,6
Woof Beep Woof
Woof Woof Woof
6 True
5 False