C++ Output Based Questions - Set 2 CL1.1C++
C++ Output Based Questions - Set 2 CL1.1C++
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
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.
int main()
{
int *ptr = new int(5);
cout << *ptr;
return 0;
}
Output: 5
int main()
{
Test obj;
int x = 40;
obj.setX(x);
obj.print();
return 0;
}
Output: x=40
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;
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 B: public A
{
public:
};
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{
#include <iostream>
using namespace std;
int a = 90;
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