OBJECT ORIENTED PROGRAMMING USING C++
Kindly read the instructions carefully
1. All these questions are important for examination point of view,
so practice them well.
2. If you have any doubt or facing any problem regarding these
questions you can mail us at coderslodgeofficial@gmail.com or
drop a message in our WhatsApp or telegram group.
3. If you want to support us, give your valuable feedback so that
next time we can improve while interacting with you.
4. Reminder-Practice all questions well it will build your concept
clear, and you can easily score good in your exams.
Connect with us
• If you want to join our WhatsApp community then mail us at:-
coderslodgeofficial@gmail.com with your preferred branch(with year) and
college name.
• Join our Telegram group:- https://t.me/coderslodgeofficial
• Like our Facebook page :- https://www.facebook.com/coderslodge
• Follow us on Instagram:-https://www.instagram.com/coderslodge/
• Follow us on Twitter:- https://twitter.com/CodersLodge
• Follow us on Linkedin:- https://www.linkedin.com/company/coderslodge
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
1. Predict the output of following C++ program.
#include<iostream>
using namespace std;
class A
{
public:
virtual void fun() {cout << "A" << endl ;}
};
class B: public A
{
public:
virtual void fun() {cout << "B" << endl;}
};
class C: public B
{
public:
virtual void fun() {cout << "C" << endl;}
};
int main()
{
A *a = new C;
A *b = new B;
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
a->fun();
b->fun();
return 0;
}
Output: C
B
2. Predict the output of following C++ program.
#include<iostream>
using namespace std;
class A
{
public :
int x=20;
};
class B
{
public :
int x=10;
};
int main()
{
A obj1;
B obj2;
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
obj1 = obj2;
cout<< obj1.x;
cout<<endl;
return 0;
}
Output: The program will not generate output due to
compilation error.
3. Predict the output of following C++ program.
#include<iostream>
using namespace std;
int main()
{
int *ptr = new int(5);
cout << *ptr;
return 0;
}
Output: 5
4. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 6, c;
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
c = (a > b) ? a : b;
cout << c;
return 0;
}
a) 6
b) 5
c) 4
d) 7
Answer: a
5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int n = 15;
for ( ; ;)
cout << n;
return 0;
}
a) error
b) 15
c) infinite times of printing n
d) none of the mentioned
Answer: c
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
6. Predict the output of following C++ program.
#include<iostream>
using namespace std;
class Test
{
private:
int x;
public:
void setX (int x) { Test::x = x; }
void print() { cout << "x = " << x << endl; }
};
int main()
{
Test obj;
int x = 40;
obj.setX(x);
obj.print();
return 0;
}
Output: x=40
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
7. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int fun(int=0, int = 0);
int main()
{
cout << fun(5);
return 0;
}
int fun(int x, int y) { return (x+y); }
a) -5
b) 0
c) 10
d) 5
Answer: d
8. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int operate (int a, int b)
{
return (a * b);
}
float operate (float a, float b)
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
{
return (a / b);
}
int main()
{
int x = 5, y = 2;
float n = 5.0, m = 2.0;
cout << operate(x, y) <<"\t";
cout << operate (n, m);
return 0;
}
a) 10.0 5.0
b) 5.0 2.5
c) 10.0 5
d) 10 2.5
Answer: d
9. Predict the output of following C++ program
#include <iostream>
using namespace std;
class A
{
int id;
static int count;
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
public:
A()
{
count++;
id = count;
cout << "constructor called " << id << endl;
}
~A()
{
cout << "destructor called " << id << endl;
}
};
int A::count = 0;
int main()
{
A a[2];
return 0;
}
Output: constructor called 1
constructor called 2
destructor called 2
destructor called 1
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
10. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
struct a
{
int count;
};
struct b
{
int* value;
};
struct c : public a, public b
{
};
int main()
{
c* p = new c;
p->value = 0;
cout << "Inherited";
return 0;
}
a) Inherited
b) Error
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
c) Runtime error
d) inherited
Answer: a
11. Predict the output of following C++ program.
#include <iostream>
using namespace std;
class A
{
public:
void print() { cout << "A::print()"; }
};
class B : private A
{
public:
void print() { cout << "B::print()"; }
};
class C : public B
{
public:
void print() { A::print(); }
};
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
int main()
{
C b;
b.print();
}
Output: Compiler Error: ‘A’ is not an accessible base of
‘C’
12. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A
{
int a, b;
float d;
public:
void change(int i){
a = i;
}
void value_of_a(){
cout<<a;
}
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
};
class B: private A
{
};
int main(int argc, char const *argv[])
{
B b;
cout<<sizeof(B);
return 0;
}
a) 8
b) 12
c) Error
d) Segmentation fault
Answer: b
13. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A{
float d;
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
public:
virtual void func(){
cout<<"Hello this is class A\n";
}
};
class B: public A{
int a = 15;
public:
void func(){
cout<<"Hello this is class B\n";
}
};
int main(int argc, char const *argv[])
{
B b;
b.func();
return 0;
}
a) Hello this is class B
b) Hello this is class A
c) Error
d) Segmentation fault
Answer: a
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
14. Predict the output of following C++ program.
#include<iostream>
using namespace std;
class Base {
protected:
int x;
public:
Base (int i){ x = i;}
};
class Derived : public Base {
public:
Derived (int i):Base(i) { }
void print() { cout << x; }
};
int main()
{
Derived d(10);
d.print();
}
Output: 10
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
15. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class Mammal
{
public:
virtual void Define(){
cout<<"I'm a Mammal\n";
}
};
class Human: public Mammal
{
private:
void Define(){
cout<<"I'm a Human\n";
}
};
int main(int argc, char const *argv[])
{
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
Mammal *M = new Mammal();
Human H;
M = &H;
M->Define();
return 0;
}
a) Error
b) Segmnetation fault
c) I’m a Human
d) Garbage Value
Answer: c
16. Predict the output of following C++ program.
#include <iostream>
using std::cout;
class Test
{
public:
Test();
~Test();
};
Test::Test()
{
cout << "Constructor is executed\n";
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
}
Test::~Test()
{
cout << "Destructor is executed\n";
}
int main()
{
delete new Test();
return 0;
}
Output: Constructor is executed
Destructor is executed
17. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A
{
int a;
public:
A(){
a = 0;
}
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
void show(){
a++;
cout<<"a: "<<a<<endl;
}
};
class B: public A
{
public:
};
int main(int argc, char const *argv[])
{
B b;
b.show();
return 0;
}
a) a: 1
b) a: 0
c) Error
d) Segmentation fault
Answer: a
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
18. What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
void func(int a, int b)
{
if(b == 0){
throw "This value of b will make the product zero. "
"So please provide positive values.\n";
}
else{
cout<<"Product of "<<a<<" and "<<b<<" is:
"<<a*b<<endl;
}
}
int main()
{
try{
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
func(5,0);
}
catch(const char* e){
cout<<e;
}
}
a) 0
b) 5
c) This value of b will make the product zero. So please provide
positive values.
d) Product of 5 and 0 is: 0
Answer: c
19. What will be the output of this program?
#include <iostream>
using namespace std;
int a = 90;
int fun(int x, int *y = &a)
{
*y = x + *y;
return x + *y;
}
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
int main()
{
int a = 5, b = 10;
a = fun(a);
cout << a << " " << b << endl;
b = fun(::a,&a);
cout << a << " " << b << endl;
return 0;
}
Output: 100 10
195 290
20. What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class A
{
int a;
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
public:
A(){}
};
class B: public A
{
int b;
public:
B(){}
};
void func()
{
B b;
throw b;
}
int main()
{
try{
func();
}
catch(A a){
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
cout<<"Caught A Class\n";
}
catch(B b){
cout<<"Caught B Class\n";
}
}
a) Caught B Class
b) Caught A Class
c) Compile-time error
d) Run-time error
Answer: b
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com