Object Oriented Programming - Very
Short Questions (1 mark each)
1. What is the output?
int a = 5;
cout << a++ + ++a;
2. Identify the error:
class Test {
int x;
void show() {
cout << x;
}
};
Test t;
3. Predict the output:
int a = 10;
int &ref = a;
ref += 5;
cout << a;
4. Find the error:
class Sample {
void display();
};
Sample::display() {
cout << "Hello";
}
5. What is the output?
void print(int x) { cout << "Int"; }
void print(double x) { cout << "Double"; }
print(4.5f);
6. Output of this overloading:
void show(char x) { cout << "Char"; }
void show(int x) { cout << "Int"; }
show('A');
7. Output for virtual function:
class Base {
public:
virtual void show() { cout << "Base"; }
};
class Derived : public Base {
public:
void show() { cout << "Derived"; }
};
Base *b = new Derived();
b->show();
8. Predict output:
class Test {
public:
Test() { cout << "C"; }
~Test() { cout << "D"; }
};
int main() {
Test t;
}
9. Find the error:
class A {
public:
A() {}
~A() {}
};
class B : virtual A {};
10. What will be the output?
class A {
public:
void display() { cout << "A"; }
};
class B : public A {
public:
void display() { cout << "B"; }
};
A *ptr = new B;
ptr->display();
11. Output:
void fun(int x) { cout << "int"; }
void fun(char x) { cout << "char"; }
fun('A');
12. What will happen?
int &ref = *(new int(10));
cout << ref;
13. Output of this template:
template <typename T>
void swapVal(T &a, T &b) {
T temp = a;
a = b;
b = temp;
}
int x = 3, y = 4;
swapVal(x, y);
cout << x << y;
14. Find the logical error:
class A {
int x;
public:
A() { x = 0; }
A(int a) { x = a; }
};
A obj;
15. Output:
int a = 10;
int *p = &a;
*p = 20;
cout << a;
16. Error in this code:
class A {
int x;
A() {}
};
int main() {
A obj;
}
17. Output of this file handling snippet:
ifstream fin("test.txt");
char ch;
fin >> ch;
cout << ch;
18. What is the output?
class Base {
public:
void show() { cout << "Base"; }
};
class Derived : public Base {
public:
void show(int) { cout << "Derived"; }
};
Derived d;
d.show();
19. Error?
int a = 5;
int *p = &a;
delete p;
20. Output:
void test(int x = 10, int y = 20) {
cout << x + y;
}
test(5);
21. Output:
class A {
public:
A() { cout << "A"; }
};
class B {
A a;
public:
B() { cout << "B"; }
};
B b;
22. Error in the code:
class A {
A() {}
};
A obj;
23. Output:
try {
throw 5;
} catch (double e) {
cout << "Double";
} catch (...) {
cout << "Catch-all";
}
24. Output:
class A {
public:
virtual void f() = 0;
};
class B : public A {
public:
void f() { cout << "B"; }
};
B b;
b.f();
25. What happens here?
class A {
public:
A(int) { cout << "int"; }
A(double) { cout << "double"; }
};
A obj(3.14f);
26. Identify the problem:
void display(int x, int y = 10, int z = 20);
display(5, 6, 7, 8);
27. Output:
int x = 5;
int y = x++;
cout << x << y;
28. Error?
void func(int &x) {
x += 2;
}
func(5);
29. Output:
class Base {
public:
void show() { cout << "Base"; }
};
class Derived : public Base {
public:
void show() { cout << "Derived"; }
};
Base *b = new Derived;
b->show();
30. Output of function overloading:
void disp(int a, int b = 20) { cout << a + b; }
void disp(int a) { cout << a; }
disp(10);