Question 21
EasyWhat is the output of this code?
class Cat:
def ():
:
():
a [Cat(), Dog()]:
(a.speak(), end=)
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 Cat:
def ():
:
():
a [Cat(), Dog()]:
(a.speak(), end=)
Meow Meow
Error
Meow Woof
Woof Meow
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, other):
return V(self.x + other.x)
print((V(2) + V(3)).x)
Error
6
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;
int f(int x) { return x * 2; }
double f(double x) { return x / 2; }
int main() {
cout << f(4) << " " << f(4.0);
}
8 2
2 8
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"; }
virtual ~A() {}
};
class B : public A {
public:
void show() override { cout << "B"; }
};
int main() {
A* p = new B();
p->show();
delete p;
}
A
AB
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:
void show() { cout << "A"; }
};
class B : public A {
public:
void show() { cout << "B"; }
};
int main() {
B b;
A* p = &b;
p->show();
}
AB
Compilation error
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class Bag:
def __init__(self):
self.items = [1, 2, 3]
def __len__(self):
return len(self.items)
print(len(Bag()))
Error
6
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class P:
def __str__(self):
return "Point"
print(P())
<main.P object>
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 Shape {
public:
void draw() { cout << "Shape"; }
};
class Circle : public Shape {
public:
void draw() { cout << "Circle"; }
};
int main() {
Shape* s = new Circle();
s->draw();
delete s;
return 0;
}
Error
Shape
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;
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
int main() {
cout << add(2, 3) << " " << add(2.5, 5.0);
return 0;
}
7.5 5
5 7.5
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class Cat:
def speak(self):
return "Meow"
class Dog:
def speak(self):
return "Woof"
print(" ".join(a.speak() for a in [Cat(), Dog()]))
Sound Sound
Meow Woof
Sign up free to see the answer and a step-by-step explanation.
23
5
8 8
2 2
Compilation error
B
A
B
3
1
P
Point
Shape Circle
Circle
5 7
Error
Woof Meow
Error