0% found this document useful (0 votes)
2K views24 pages

C++ Output Based Questions - Set 2 CL1.1C++

The document provides 16 questions related to object oriented programming in C++. For each question, it predicts the output of a code snippet and provides the answer. It encourages readers to practice the questions for exam preparation and provides contact details to get help with any doubts regarding the questions.

Uploaded by

Lakshay Chauhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views24 pages

C++ Output Based Questions - Set 2 CL1.1C++

The document provides 16 questions related to object oriented programming in C++. For each question, it predicts the output of a code snippet and provides the answer. It encourages readers to practice the questions for exam preparation and provides contact details to get help with any doubts regarding the questions.

Uploaded by

Lakshay Chauhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

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 [email protected] 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:-
[email protected] 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


[email protected]
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
[email protected]
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
[email protected]
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


[email protected]
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


[email protected]
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


[email protected]
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


[email protected]
{
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


[email protected]
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


[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
};

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


[email protected]
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


[email protected]
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
[email protected]
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
[email protected]
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


[email protected]
}
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
[email protected]
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


[email protected]
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


[email protected]
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


[email protected]
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
[email protected]
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
[email protected]
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


[email protected]

You might also like