C++ MCQ’s

C++ MCQ’s Home Page

C++ Basic 1
C++ Basic 2
C++ Basic 3
C++ Basic 4
C++ Basic 5
C++ OOPs Concepts 1
C++ OOPs Concepts 2
C++ OOPs Concepts 3
C++ OOPs Concepts 4
C++ OOPs Concepts 5

 

Cpp Language MCQ’S | OOPs Concepts Set 4

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

#include<iostream>
using namespace std;
class Base {
public:
Base()
{ cout<<“Constructing Base \n”; }
~Base()
{ cout<<“Destructing Base \n”; }
};
class Derived: public Base {
public:
Derived()
{ cout<<“Constructing Derived \n”; }
~Derived()
{ cout<<“Destructing Derived \n”; }
};

int main(void)
{
Derived *d = new Derived();
Base *b = d;
delete b;
return 0;
}

a)
Constructing Base
Constructing Derived
Destructing Base
b)
Constructing Base
Constructing Derived
Destructing Derived
Destructing Base
c)
Constructing Base
Constructing Derived
Destructing Base
Destructing Derived
d)
Constructing Derived
Constructing Base
Destructing Base
Destructing Derived

 

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

#include<iostream>
using namespace std;
class Base {
public:
Base()
{ cout<<“Constructing Base \n”; }
virtual~Base()
{ cout<<“Destructing Base \n”; }
};
class Derived: public Base {
public:
Derived()
{ cout<<“Constructing Derived \n”; }
~Derived()
{ cout<<“Destructing Derived \n”; }
};

int main(void)
{
Derived *d = new Derived();
Base *b = d;
delete b;
return 0;
}

a)
Constructing Base
Constructing Derived
Destructing Base
b)
Constructing Base
Constructing Derived
Destructing Derived
Destructing Base
c)
Constructing Base
Constructing Derived
Destructing Base
Destructing Derived
d)
Constructing Derived
Constructing Base
Destructing Base
Destructing Derived

 

23.
What is the correct syntax of declaring array of pointers of integers of size 10 in C++?

a) int arr = new int[10];
b) int **arr = new int*[10];
c) int *arr = new int[10];
d) int *arr = new int*[10];

 

24.
Which of the following is correct about new and malloc?

i) new is an operator whereas malloc is a function
ii) new calls constructor malloc does not
iii) new returns required pointer whereas malloc returns void pointer and needs to be typecast
a) i and ii
b) ii and iii
c) i and iii
d) i, ii and iii

 

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

#include <iostream>
using namespace std;

class A
{
int a;
A() { a = 5;}
};

int main()
{
A *obj = new A;
cout << obj->a;
}

a) 5
b) Garbage value
c) Compile-time error
d) Run-time error

 

Visitor Counter

0051026