0% found this document useful (0 votes)
85 views44 pages

N2N CPP MCQ Level2

Uploaded by

Mehra saab
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)
85 views44 pages

N2N CPP MCQ Level2

Uploaded by

Mehra saab
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/ 44

Click the test name to view my attempt.

MCQ Test C/C++: Templates

Q1. What is the output of this C++ program?


#include <iostream>
#include <string>
using namespace std;
template < typename T >
void print_mydata(T output)
{
cout << output << endl;
}
int main()
{
double d = 5.5;
string s("Hello World");
print_mydata( d );
print_mydata( s );
return 0;
}
Ans -
5.5
Hello World

Q2. What is the validity of template parameters in C++?


Ans - Inside that block only

Q3. Which of the following is not correct (in C++) ?


1. Class templates and function templates are instantiated in the same
way
2. Class templates differ from function templates in the way they are
initiated
3. Class template is initiated by defining an object using the template
argument
4. Class templates are generally used for storage classes
Ans - (2), (3), (4)

Q4. What will be the output of this C++ code?


#include <iostream>
using namespace std;

template<int n> struct funStruct


{
static const int val = 2*funStruct<n-1>::val;
};

template<> struct funStruct<0>


{
static const int val = 1 ;
};

int main()
{
cout << funStruct<10>::val << endl;
return 0;
}
Ans - 1024

Q5. Which of the following is true about templates in C++?


1) Template is a feature of C++ that allows us to write one code for
different data types.
2) We can write one function that can be used for all data types including
user defined types like sort(), max(), min() etc.
3) We can write one class or struct that can be used for all data types
including user defined types like Linked List, Stack, Queue etc.
4) Template is an example of compile time polymorphism.
Ans - 1, 2, 3 and 4

Q6. What will be the output of the given C++ code?


#include <iostream>
using namespace std;

template <typename T>


void fun(const T&x)
{
static int count = 0;
cout << "x = " << x << " count = " << count << endl;
++count;
return;
}

int main()
{
fun<int> (1);
cout << endl;
fun<int>(1);
cout << endl;
fun<double>(1.1);
cout << endl;
return 0;
}
Ans –
x = 1 count = 0
x = 1 count = 1
x = 1.1 count = 0

Q7. How to declare a template in C++?


Ans - template <>

Q8. What is the output of this C++ program?


#include <iostream>
using namespace std;
template <class type>
class Test
{
public:
Test(){};
~Test(){};
type Funct1(type Var1)
{
return Var1;
}
type Funct2(type Var2)
{
return Var2;
}
};
int main()
{
Test<int> Var1;
Test<double> Var2;
cout << Var1.Funct1(200);
cout << Var2.Funct2(3.123);
return 0;
}
Ans - 2003.123

Q9. What is the output of this C++ program?


#include <iostream>
using namespace std;
template <class T>
class A
{
public:
A(int a): x(a) {}
protected:
int x;
};
template <class T>
class B: public A<char>
{
public:
B(): A<char>::A(100)
{
cout << x * 2 << endl;
}
};
int main()
{
B<char> test;
return 0;
}
Ans - 200

Q10. What is meant by template parameter in C++?


Ans - It can be used to pass a type as argument
MCQ Test C/C++: Operator Overloading

Q1. Correct way of declaring operator overloaded function in C++ is:-


Ans - return-type operator operator-sign(arguments){}

Q2. What is the output of this C++ program?


#include <iostream>
using namespace std;
class sample{
public:
int x, y;
sample() {};
sample(int, int);
sample operator + (sample);
};
sample::sample (int a, int b){
x = a;
y = b;
}
sample sample::operator+ (sample param){
sample temp;
temp.x = x + param.x;
temp.y = y + param.y;
return (temp);
}
int main ()
{
sample a (4,1);
sample b (3,2);
sample c;
c = a + b;
cout << c.x << "," << c.y;
return 0;
}
Ans - 7.3

Q3. Operator overloading in C++ is ____________


Ans - Adding operation to the existing operators

Q4. What is the output of this C++ program?


#include <iostream>
using namespace std;
class Integer{
int i;
public:
Integer(int ii) : i(ii) {}
const Integer
operator+(const Integer& rv) const
{
cout << "operator+" << endl;
return Integer(i + rv.i);
}
Integer&
operator+=(const Integer& rv)
{
cout << "operator+=" << endl;
i += rv.i;
return *this;
}
};
int main()
{
int i = 1, j = 2, k = 3;
k += i + j;
Integer ii(1), jj(2), kk(3);
kk += ii + jj;
}
Ans -
operator+
operator+=
operator+=

Q5. What is the output of this program?


#include <iostream>
using namespace std;
class myclass
{
public:
int i;
myclass *operator->()
{return this;}
};
int main()
{
myclass ob;
ob->i = 10;
cout << ob.i << " " << ob->i;
return 0;
}
Ans - 10 10

Q6. Which of the following operators can’t be overloaded in C++?


Ans - ::

Q7. In C++, when an operator is given user-defined meaning, then it is


called ____________
Ans - Operator overloading

Q8. What is the output of this C++ program?


#include <iostream>
using namespace std;
class Box
{
double length;
double breadth;
double height;
public:
double getVolume(void)
{
return length * breadth * height;
}
void setLength( double len )
{
length = len;
}
void setBreadth( double bre )
{
breadth = bre;
}
void setHeight( double hei )
{
height = hei;
}
Box operator+(const Box& b)
{
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
};
int main( )
{
Box Box1;
Box Box2;
Box Box3;
double volume = 0.0;
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
Box3 = Box1 + Box2;
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume <<endl;
return 0;
}
Ans -
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400

Q9. What is the output of this C++ program?


#include <iostream>
using namespace std;
ostream & operator<<(ostream & i, int n)
{
return i;
}
int main()
{
cout << 5 << endl;
cin.get();
return 0;
}
Ans - Compilation error

Q10. Which of the following statements is NOT valid about operator


overloading
Ans - None of the mentioned
MCQ Test C/C++: Constructor and Destructor 1

Q1. State whether the following statements about the constructor are
True or False.
i) Constructors should always be declared in the private section.
ii) Constructors are invoked automatically when the objects are created.
Ans - False, True

Q2. When an object is created and initialized at the same time, a ……………….
gets called.
Ans - Copy constructor

Q3. …………….. constructor will not do anything and defined just to satisfy
the compiler.
Ans - Implicit

Q4. A constructor that accepts no parameters is called the …………….


Ans - Default constructor

Q5. The constructors that can take arguments are called ……………...
Ans - Parameterized constructor

Q6. What is the output of following C++ program?


#include <iostream>
using namespace std;
class Point{
int x, y;
public:
Point(const Point &p) { x = p.x; y = p.y; }
int getX() { return x; }
int getY() { return y; }
};
int main(){
Point p1;
Point p2 = p1;
cout << "x = " << p2.getX() << " y = " << p2.getY();
return 0;
}
Ans - Compiler Error
Q7. A ………………….. is used to declare and initialize an object from another
object.
Ans - Copy constructor

Q8. Constructors cannot be inherited, through a derived class can call


the ………………. constructor.
Ans - Base class

Q9. Constructors are used to


Ans - Initalize the objects

Q10. C++ provides a special ………………… called the constructor, which enables
an object to initialize itself when it is created.
Ans - Member function

Q11. A constructor has the same …………….. as that of class.


Ans - Name

Q12. In C++, ……………………. creates objects even through it was not defined in
the class.
Ans - Implicit constructor

Q13. When a copy constructor may be called?


Ans - All of the above

Q14. The ………………… constructor can be called with either one argument or
no arguments.
Ans - Default argument

Q15. Constructors are normally used to …………….. and to allocate memory.


Ans - Initialize objects
MCQ Test C/C++: Constructor and Destructor 2

Q1. What will be the output of this C++ code?


#include<iostream>
using namespace std;
class Base
{
public :
int x, y;
public:
Base(int i, int j){ x = i; y = j; }
};
class Derived : public Base
{
public:
Derived(int i, int j):x(i), y(j) {}
void print() {cout << x <<" "<< y; }
};
int main(void)
{
Derived q(10, 10);
q.print();
return 0;
}
Ans - Compiler Error

Q2. Which of the following statements are not true about destructor?
1. It is invoked when object goes out of the scope
2. Like constructor, it can also have parameters
3. It can be virtual
4. It should always be declared in private section
5. It bears same name as that of the class preceded by Lambda sign.
Ans - Only 2, 4, 5
Q3. Consider the below C++ program.
#include<iostream>
using namespace std;
class A
{
public:
A(){ cout <<"1";}
A(const A &obj){ cout <<"2";}
};
class B: virtual A
{
public:
B(){cout <<"3";}
B(const B & obj){cout<<"4";}
};
class C: virtual A
{
public:
C(){cout<<"5";}
C(const C & obj){cout <<"6";}
};
class D:B,C
{
public:
D(){cout<<"7";}
D(const D & obj){cout <<"8";}
};
int main()
{
D d1;
D d(d1);
}
Which of the below is not printed?

Ans - All of the above


Q4. What will be the output of the following C++ program?
#include<iostream>
using namespace std;
class Base1
{
public:
Base1()
{
cout << "Base1's constructor called" << endl;
}
};
class Base2
{
public:
Base2()
{
cout<<"Base2's constructor called"<<endl;
}
};
class Derived: public Base1, public Base2
{
public:
Derived()
{
cout<<"Derived's constructor called"<<endl;
}
};
int main()
{
Derived d;
return 0;
}
Ans –
Base1′s constructor called
Base2′s constructor called
Derived’s constructor called

Q5. A ……………. takes a reference to an object of the same class as itself as


an argument.
Ans - Copy constructor

Q6. Destructor is a member function whose name is same as the class


name but is preceded by a ……….....
Ans - Tilde

Q7. In case of inheritance where both base and derived class are having
constructors, when an object of derived class is created then
___________ .
Ans - Constructor of base class will be executed first followed by derived
class

Q8. Assume class TEST. Which of the following statements is/are


responsible to invoke copy constructor?
Ans - Both (a) and (b)

Q9. Which of the following are true about constructors?


1. A class can have more than one constructor.
2. They can be inherited.
3. Their address can be referred.
4. Constructors can be declared in protected section of the class.
5. Constructors cannot return values.
Ans - 1,4,5

Q10. What will be the output of the following C++ code?


#include<iostream>
using namespace std;
class Base {
private:
int i, j;
public:
Base(int _i = 0, int _j = 0): i(_i), j(_j) { }
};
class Derived: public Base {
public:
void show(){
cout<<" i = "<<i<<" j = "<<j;
}
};
int main(void) {
Derived d;
d.show();
return 0;
}
Ans - Compiler Error: i and j are private in Base

Q11. If default constructor is not defined, then how the objects of the
class will be created?
Ans - Compiler provides its default constructor to build the object

Q12. The process of initializing through a copy constructor is known as


……………
Ans - Copy initialization

Q13. What will be the output of the following C++ program?


#include <iostream>
using namespace std;
class Base1 {
public:
~Base1() { cout << "Base1's destructor" << endl; }
};
class Base2 {
public:
~Base2() { cout << "Base2's destructor" << endl; }
};
class Derived: public Base1, public Base2 {
public:
~Derived() { cout << "Derived's destructor" << endl; }
};
int main()
{
Derived d;
return 0;
}
Ans -
Derived's destructor
Base2's destructor
Base1's destructor

Q14. A destructor is used to destroy the objects that have been created by
a ………………..
Ans - Constructor

Q15. ………………….. with a constructor (or destructor) cannot be used as a


member of a union.
Ans - Object
MCQ Test C/C++: Object Oriented Programming 1

Q1. What is meant by type_info?


Ans - Used to hold the type information returned by the typeid operator

Q2. In C++, which one of the following statements is correct when a class
grants friend status to another class?
Ans - All member functions of the class granted friendship have
unrestricted access to the members of the class granting the friendship.

Q3. Objects are ______________ of a class.


Ans – Instance

Q4. Which operator is used to access the members of a class object in


C++?
Ans - Direct member access operator

Q5. An object is an instance of a _________.


Ans - class

Q6. In C++, if abstract class is inherited by derived class, then


_______________ .
Ans - All of these

Q7. In C++, which other keywords are used to declare composite data
types other than 'class' keyword?
Ans - both struct & union

Q8. What is the Run-Time Type Information?


Ans - Information about an object’s data type at runtime

Q9. Find the wrong statement about abstract Class in C++.


Ans - We can’t create pointers to an abstract class.

Q10. Composition is also called as


Ans - Both A and C
Q11. Which of the following is a valid class declaration in C++?
Ans - class A { int x; };

Q12. Syntax for Pure Virtual Function is ______________ .


Consider show() as the function name.
Ans - virtual void show()=0

Q13. Which operators are part of RTTI?


Ans - both dynamic_cast() & typeid

Q14. Which of the followings is/are automatically added to every class, if


we do not write our own?
Ans - All of the above

Q15. In C++, which of these following members cannot be accessed by


using direct member access operator?
Ans - both private & protected

Q16. In C++, if a function is friend of a class, which one of the following is


wrong?
Ans - Friend functions are members of a class.

Q17. The access specifier of the classes in C++ programs by default are :
Ans – private

Q18. How many access specifiers are there in class in C++?


Ans – 3

Q19. Which is used to describe the function using placeholder types in


C++?
Ans - Template type parameters

Q20. In C++, which operator is used to define the member of a class


externally?
Ans - ::

Q21. Which of the following is true regarding classes in C++?


Ans - Objects of a class do not share non-static members. Every object
has its own copy
Q22. In C++, when struct is used instead of the keyword class, what will
happen in the program?
Ans - Access specifier is public by default

Q23. A class can have __________ objects.


Ans - as many as possible

Q24. Which of the following is true about virtual functions in C++?


Ans – All of these

Q25. In C++, when a data member of the new class is an object of another
class, it is called as
Ans - New class is a composite of other objects

Q26. To which type of class, We can apply RTTI?


Ans – Polymorphic

Q27. In C++, a virtual function that has no definition within the base class
is called ____________.
Ans - Pure virtual function

Q28. Which data type values are placed in the base class?
Ans - Default type values

Q29. What members can a class can hold?


Ans - Both data & functions

Q30. Which of the following is dependent on template parameter in C++?


Ans - Base class
MCQ Test C/C++: Object Oriented Programming 2

Q1. From where is the template class derived?


Ans - Regular non-templated C++ class or templated class

Q2. What is the output of the following C++ program?


#include <iostream>
using namespace std;
class Box
{
public :
double length;
double breadth;
double height;
};
int main( )
{
Box Box1;
double volume;
Box1.height = 5;
Box1.length = 6;
Box1.breadth = 7.1;
volume = Box1.height * Box1.length * Box1.breadth;
cout << volume <<endl;
return 0;
}
Ans – 213

Q3. What is the output of this C++ program?


#include <iostream>
using namespace std;
namespace Box1
{
int a = 4;
}
namespace Box2
{
int a = 13;
}
int main ()
{
int a = 16;
Box1::a;
Box2::a;
cout << a;
return 0;
}
Ans – 16

Q4. What is the syntax of friend function in C++?


Ans - friend datatype function_name ();

Q5. What can be passed as a non-type template parameter during


compile time?
Ans - constant expression

Q6. At which time does the static_cast can be applied?


Ans - Compile-time construct

Q7. What will be the output of the following C++ program?


#include<iostream>
using namespace std;
class X
{
public:
int x;
};
int main()
{
X a = {10};
X b = a;
cout << a.x << " " << b.x;
return 0;
}
Ans – 10 10

Q8. What is the output of this C++ program(if size of int is 4 bytes)?
#include<iostream>
using namespace std;
class Test
{
static int x;
int *ptr;
int y;
};
int main()
{
Test t;
cout << sizeof(t) << " ";
cout << sizeof(Test *);
}
Ans – 8 4

Q9.
What is the output of this C++ program?
#include <iostream>
using namespace std;
class Rect
{
int x, y;
public:
void set_values (int,int);
int area ()
{
return (x * y);
}
};
void Rect::set_values (int a, int b)
{
x = a;
y = b;
}
int main ()
{
Rect recta, rectb;
recta.set_values (5, 6);
rectb.set_values (7, 6);
cout << "recta area: " << recta.area();
cout << "rectb area: " << rectb.area();
return 0;
}
Ans - recta area: 30rectb area: 42

Q10. What will be the output of this C++ program?


#include <iostream>
using namespace std;
int i;
class A
{
public:
~A()
{
i=10;
}
};
int foo()
{
i=3;
A ob;
return i;
}
int main()
{
cout << foo() << endl;
return 0;
}
Ans – 3

Q11. What is the output of this C++ program?


#include<iostream>
using namespace std;
class P {
public:
void print() { cout <<"Inside P"; }
};
class Q : public P {
public:
void print() { cout <<"Inside Q"; }
};
class R: public Q { };
int main(void)
{
R r;
r.print();
return 0;
}
Ans - Inside Q

Q12. What is the output of this C++ program?


#include <iostream>
using namespace std;
class CDummy
{
public:
int isitme (CDummy& param);
};
int CDummy::isitme (CDummy& param)
{
if (&param == this)
return true;
else
return false;
}
int main ()
{
CDummy a;
CDummy *b = &a;
if (b->isitme(a))
{
cout << "execute";
}
else
{
cout<<"not execute";
}
return 0;
}
Ans - execute

Q13. What is the output of this C++ program?


#include <iostream>
using namespace std;
class sample
{
private:
int var;
public:
void input()
{
cout << var;
}
void output()
{
cout << "Variable entered is ";
cout << var << "\n";
}
};
int main()
{
sample object;
object.input();
object.output();
object.var();
return 0;
}
Ans - Compilation error

Q14. When the inheritance is private, the private methods in base class
are __________ in the derived class (in C++).
Ans – inaccessible

Q15. In C++, runtime polymorphism is achieved by


Ans - Virtual function

Q16. What is the output of this C++ program(If int size is 4 bytes)?
#include<iostream>
using namespace std;
class base {
int arr[10];
};
class b1: public base { };
class b2: public base { };
class derived: public b1, public b2 {};
int main(void)
{
cout << sizeof(derived);
return 0;
}
Ans – 80
Q17. What is the output of this C++ program?
#include <iostream>
using namespace std;
class rect
{
int x, y;
public:
void val (int, int);
int area ()
{
return (x * y);
}
};
void rect::val (int a, int b)
{
x = a;
y = b;
}
int main ()
{
rect rect;
rect.val (3, 4);
cout << "rect area: " << rect.area();
return 0;
}
Ans - rect area: 12

Q18. What is the output of following C++ program?


#include<iostream>
using namespace std;
class Empty {};
int main()
{
cout << sizeof(Empty);
return 0;
}
Ans - A non-zero value
Q19. Which keyword can be used in template in C++?
Ans - both class & typename

Q20. What is the output of this C++ program?


#include<iostream>
using namespace std;
class Base {};
class Derived: public Base {};
int main()
{
Base *bp = new Derived;
Derived *dp = new Base;
}
Ans - Compiler Error in line " Derived *dp = new Base;"
MCQ Test C/C++: Object Oriented Programming 3

Q1. What will be the output of this C++ program?


#include<iostream>
using namespace std;
class Base {
public:
int fun() { cout << "Base::fun() called"; }
int fun(int i) { cout << "Base::fun(int i) called"; }
};
class Derived: public Base {
public:
int fun() { cout << "Derived::fun() called"; }
};
int main() {
Derived d;
d.Base::fun(5);
return 0;
}
Ans - Base::fun(int i) called

Q2. What will be the output of this C++ program?


#include<iostream>
using namespace std;
class Base
{
public:
void show()
{
cout<<" In Base ";
}
};
class Derived: public Base
{
public:
int x;
void show()
{
cout<<"In Derived ";
}
Derived()
{
x = 10;
}
};
int main(void)
{
Base *bp, b;
Derived d;
bp = &d;
bp->show();
cout << bp->x;
return 0;
}
Ans - Compiler Error in line " cout << bp->x"

Q3. What is the output of this C++ program?


#include <iostream>
using namespace std;
class class0
{
public:
virtual ∼∼class0(){ }
protected:
char p;
public:
char getChar();
};
class class1 : public class0
{
public:
void printChar();
};
void class1::printChar()
{
cout << "True" << endl;
}
int main()
{
class1 c;
c.printChar();
return 1;
}
Ans – True

Q4. What is the output of this C++ program?


#include <iostream>
using namespace std;
class Base
{
public:
Base(){}
~Base(){}
protected:
private:
};
class Derived:public Base
{
public:
Derived(){}
Derived(){}
private:
protected:
};
int main()
{
cout << "The program exceuted" << endl;
}
Ans - Compilation error

Q5. What is the output of this C++ program?


#include <iostream>
using namespace std;
template<typename type>
class Test
{
public:
Test()
{
};
∼∼Test()
{
};
type Funct1(type Var1)
{
return Var1;
}
type Funct2(type Var2)
{
return Var2;
}
};
int main()
{
Test<int> Var1;
Test<float> Var2;
cout << Var1.Funct1(200) << endl;
cout << Var2.Funct2(3.123) << endl;
return 0;
}
Ans - 200
3.123

Q6. How many kinds of template parameters are there in C++?


Ans – 3

Q7. What is meant by template parameter in C++?


Ans - It can be used to pass a type as argument
Q8. What will be the output of this C++ program?
#include<iostream>
using namespace std;
class Base1
{
public:
char c;
};
class Base2
{
public:
int c;
};

class Derived: public Base1, public Base2


{
public:
void show() { cout << c; }
};
int main(void)
{
Derived d;
d.show();
return 0;
}
Ans - Compiler Error in "cout << c;"

Q9. What is the output of this C++ program?


#include <iostream>
using namespace std;
class Base
{
public:
Base ( )
{
cout << "1";
}
∼∼Base ( )
{
cout << "2";
}
};
class Derived : public Base
{
public:
Derived ( )
{
cout << "3";
}
∼∼Derived ( )
{
cout << "4";
}
};
int main( )
{
Derived x;
}
Ans – 1342

Q10. What is the output of this C++ program?


#include <iostream>
using namespace std;
template<typename T>class clsTemplate
{
public:
T value;
clsTemplate(T i)
{
this->value = i;
}
void test()
{
cout << value << endl;
}
};
class clsChild : public clsTemplate<char>
{
public:
clsChild(): clsTemplate<char>( 0 )
{
}
clsChild(char c): clsTemplate<char>( c )
{
}
void test2()
{
test();
}
};
int main()
{
clsTemplate <int> a( 42 );
clsChild b( 'A' );
a.test();
b.test();
return 0;
}
Ans – 42
A

Q11. How many number of pointer (*) does C have against a pointer
variable declaration?
Ans – No limits

Q12. What is the validity of template parameters in C++?


Ans - Inside that block only

Q13. How many kinds of entities are directly parameterized in C++


through templates?
Ans – 3
Q14. What is the output of this C++ program?
#include <iostream>
using namespace std;
template <class T>
class A
{
public:
A(int a): x(a) { }
protected:
int x;
};
template <class T>
class B: public A<char>
{
public:
B(): A<char>::A(100)
{
cout << x * 2 << endl;
}
};
int main()
{
B<char> test;
return 0;
}
Ans – 200

Q15. What is the output of this C++ program?


#include <iostream>
using namespace std;
template<typename type>
type Max(type Var1, type Var2)
{
return Var1 > Var2 ? Var1:Var2;
}
int main()
{
int p;
p = Max(100, 200);
cout << p << endl;
return 0;
}
Ans – 200

Q16. What is the output of this C++ program?


#include <iostream>
using namespace std;
template <class type>
class Test
{
public:
Test();
∼∼Test();
type Data(type);
};
template <class type>
type Test<type>::Data(type Var0)
{
return Var0;
}
template <class type>
Test<type>::Test()
{
}
template <class type>
Test<type>::~Test()
{
}
int main(void)
{
Test<char> Var3;
cout << Var3.Data('K') << endl;
return 0;
}
Ans – K
Q17. What will be the output of this C++ program?
#include<iostream>
using namespace std;
class Base
{
protected:
int a;
public:
Base() {a = 0;}
};
class Derived1: public Base
{
public:
int c;
};
class Derived2: public Base
{
public:
int c;
};
class DerivedDerived: public Derived1, public Derived2
{
public:
void show() { cout << a; }
};
int main(void)
{
DerivedDerived d;
d.show();
return 0;
}
Ans - Compiler Error in Line "cout << a;"

Q18. Which operator is used for input stream in C++?


Ans - >>
Q19. What is the output of following C++ program?
#include <iostream>
#include<string>
using namespace std;
class Base
{
public:
virtual string print() const
{
return "This is Base class";
}
};
class Derived : public Base
{
public:
virtual string print() const
{
return "This is Derived class";
}
};
void describe(Base p)
{
cout << p.print() << endl;
}
int main()
{
Base b;
Derived d;
describe(b);
describe(d);
return 0;
}
Ans -
This is Base class
This is Base class
Q20. What will be the output of this C++ program?
#include<iostream>
using namespace std;
class Base
{
public:
int fun() { cout << "Base::fun() called"; }
int fun(int i) { cout << "Base::fun(int i) called"; }
};
class Derived: public Base
{
public:
int fun() { cout << "Derived::fun() called"; }
};
int main()
{
Derived d;
d.fun(5);
return 0;
}
Ans – Compiler Error

You might also like