CPP MCQ
CPP MCQ
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char const *argv[])
{
char s1[6] = "Hello";
char s2[6] = "World";
char s3[12] = s1 + " " + s2;
cout<<s3;
return 0;
}
a) Hello
b) World
c) Error
d) Hello World
Answer: c
3. What happens if the following program is executed in C and C++?
#include <stdio.h>
int main(void)
{
int new = 5;
printf("%d", new);
}
. a) Error in C and successful execution in C++
b) Error in both C and C++
c) Error in C++ and successful execution in C
d) A successful run in both C and C++
Answer: c
4. What happens if the following program is executed in C and C++?
#include <stdio.h>
void func(void)
{
printf("Hello");
}
void main()
{
func();
func(2);
}
Answer: d
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string s = "spaces in text";
s.erase(remove(s.begin(), s.end(), ' ' ), s.end() ) ;
cout << s << endl;
}
a) spacesintext
b) spaces in text
c) spaces
d) spaces in
Answer: a
6. Which of the following C++ code will give error on compilation?
================code 1=================
#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
cout<<"Hello World";
return 0;
}
========================================
================code 2=================
#include <iostream>
int main(int argc, char const *argv[])
{
std::cout<<"Hello World";
return 0;
}
========================================
a) Code 1 only
b) Neither code 1 nor code 2
c) Both code 1 and code 2
d) Code 2 only
Answer: b
#include <iostream>
using namespace std;
int main()
{
int p;
bool a = true;
bool b = false;
int x = 10;
int y = 5;
p = ((x | y) + (a + b));
cout << p;
return 0;
}
a) 12
b) 0
c) 2
d) 16
Answer: d
8. What will be the output of the following C++ function?
1. int main()
2. {
3. register int i = 1;
4. int *ptr = &i;
5. cout << *ptr;
6. return 0;
7. }
a) Runtime error may be possible
b) Compiler error may be possible
c) 1
d) 0
Answer: b
#include<iostream>
using namespace std;
int main ()
{
int cin;
cin >> cin;
cout << "cin: " << cin;
return 0;
}
a) Segmentation fault
b) Nothing is printed
c) Error
d) cin: garbage value
Answer: d
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main(int argc, char const *argv[])
{
const char *a = "Hello\0World";
cout<<a;
return 0;
}
a) Hello
b) World
c) Error
d) Hello World
View Answer
Answer: a
11. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
char c = 74;
cout << c;
return 0;
}
a) I
b) J
c) A
d) N
View Answer
Answer: b
12. What will be the output of the following C++ program?
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
cout << setprecision(17);
double d = 0.1;
cout << d << endl;
return 0;
}
a) compile time error
b) 0.100001
c) 0.11
d) 0.10000000000000001
Answer: d
13. What is the correct syntax of accessing a static member of a class in C++?
---------------------------
Example class:
class A
{
public:
static int value;
}
---------------------------
a) A->value
b) A^value
c) A.value
d) A::value
View Answer
Answer: d
#include <iostream>
using namespace std;
int main()
{
int a = 5;
float b;
cout << sizeof(++a + b);
cout << a;
return 0;
1. }
a) 2 5
b) 4 5
c) 4 6
d) 2 6
Answer: b
15. What will be the output of the following C++ program?
#include<iostream>
using namespace std;
int main()
{
int a = 5;
auto check = [=]()
{
a = 10;
};
check();
cout<<"Value of a: "<<a<<endl;
return 0;
}
a) Segmentation fault
b) Value of a: 5
c) Value of a: 10
d) Error
Answer: d
16. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void square (int *x, int *y)
{
*x = (*x) * --(*y);
}
int main ( )
{
int number = 30;
square(&number, &number);
cout << number;
return 0;
}
a) 30
b) Error
c) Segmentation fault
d) 870
Answer: d
17. What will be the output of the following C++ program?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
std::string str ("PGDAC.");
str.back() = '!';
std::cout << str << endl;
return 0;
}
a) PGDAC!
b) PGDAC!.
c) PGDAC.
d) PGDAC.!
Answer: a
#include <iostream>
using namespace std;
int main()
{
int n = 5;
void *p = &n;
int *pi = static_cast<int*>(p);
cout << *pi << endl;
return 0;
1. }
a) 5
b) 6
c) compile time error
d) runtime error
Answer: a
19. What will be the output of the following C++ program?
#include <iostream>
using namespace std;
int main()
{
try
{
try
{
throw 20;
}
catch (int n)
{
cout << "Inner Catch\n";
throw;
}
}
catch (int x)
{
cout << "Outer Catch\n";
}
return 0;
}
a) Outer Catch
b)Inner Catch
Outer Catch
c) Error
d) Inner Catch
Answer: b
20. What will be the output of the following C++ code snippet?
#include <iostream>
using namespace std;
int operate (int a, int b)
{
return (a * b);
}
float operate (float a, float b)
{
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
b) 10 2.5
c) 10.0 5.0
d) 5.0 2.5
Answer: b
21. What will be the output of the following C++ code?
1. #include <iostream>
2. using namespace std;
3. int main ()
4. {
5. int a, b, c;
6. a = 2;
7. b = 7;
8. c = (a > b) ? a : b;
9. cout << c;
10. return 0;
11. }
a) 12
b) 14
c) 6
d) 7
Answer: d
22. What will be the output of the following C++ code snippet?
#include <stdio.h>
#include<iostream>
using namespace std;
int main ()
{
int array[] = {0, 2, 4, 6, 7, 5, 3};
int n, result = 0;
for (n = 0; n < 8; n++)
{
result += array[n];
}
cout << result;
return 0;
}
a) 21
b) 27
c) 26
d) 25
Answer: b
23. What will be the output of the following C++ program?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("Hello Students");
for (size_t i = 0; i < str.length();)
{
cout << str.at(i-1);
}
return 0;
}
a) runtime error
b) Hello
c) H
d) Hello Stude
Answer: a
24. What will be the output of the following C++ program?
#include <iostream>
using namespace std;
class A{
public:
A(){
cout<<"Constructor called\n";
}
~A(){
cout<<"Destructor called\n";
}
};
int main(int argc, char const *argv[])
{
A *a = new A[5];
delete[] a;
return 0;
}
a) Segmentation fault
b) “Constructor called” five times and then “Destructor called” five times
c) “Constructor called” five times and then “Destructor called” once
d) Error
Answer: b
25. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class polygon
{
protected:
int width, height;
public:
void set_values (int a, int b)
{
width = a; height = b;}
};
class output1
{
public:
void output (int i);
};
void output1::output (int i)
{
cout << i << endl;
}
class rectangle: public polygon, public output1
{
public:
int area ()
{
return (width * height);
}
};
class triangle: public polygon, public output1
{
public:
int area ()
{
return (width * height / 2);
}
};
int main ()
{
rectangle rect;
triangle trgl;
rect.set_values (4, 5);
trgl.set_values (4, 5);
rect.output (rect.area());
trgl.output (trgl.area());
return 0;
}
a) 20
b) 10
c)
20
10
d) 30
View Answer
Answer: c
26. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class Base
{
public:
virtual void print() const = 0;
};
class DerivedOne : public Base
{
public:
void print() const
{
cout << "DerivedOne\n";
}
};
class DerivedTwo : public Base
{
public:
void print() const
{
cout << "DerivedTwo\n";
}
};
class Multiple : public DerivedOne, public DerivedTwo
{
public:
void print() const
{
DerivedTwo :: print();
}
};
int main()
{
int i;
Multiple both;
DerivedOne one;
DerivedTwo two;
Base *array[ 3 ];
array[ 0 ] = &both;
array[ 1 ] = &one;
array[ 2 ] = &two;
array[ i ] -> print();
return 0;
}
a) DerivedOne
b) DerivedTwo
c) Error
d) DerivedThree
Answer: c
#include <iostream>
using namespace std;
class student
{
public:
int rno , m1 , m2 ;
void get()
{
rno = 15, m1 = 10, m2 = 10;
}
};
class sports
{
public:
int sm;
void getsm()
{
sm = 10;
}
};
class statement:public student,public sports
{
int tot,avg;
public:
void display()
{
tot = (m1 + m2 + sm);
avg = tot / 3;
cout << tot;
cout << avg;
}
};
int main()
{
statement obj;
obj.get();
obj.getsm();
obj.display();
}
a) 3100
b) 3010
c) 2010
d) 1010
Answer: b
28. 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
c) Runtime error
d) inherited
Answer: a
#include <iostream>
using namespace std;
class Base1
{
protected:
int SampleDataOne;
public:
Base1()
{
SampleDataOne = 100;
}
~Base1()
{
}
int SampleFunctOne()
{
return SampleDataOne;
}
};
class Base2
{
protected:
int SampleDataTwo;
public:
Base2()
{
SampleDataTwo = 200;
}
~Base2()
{
}
int SampleFunctTwo()
{
return SampleDataTwo;
}
};
class Derived1 : public Base1, public Base2
{
int MyData;
public:
Derived1()
{
MyData = 300;
}
~Derived1()
{
}
int MyFunct()
{
return (MyData + SampleDataOne + SampleDataTwo);
}
};
int main()
{
Base1 SampleObjOne;
Base2 SampleObjTwo;
Derived1 SampleObjThree;
cout << SampleObjThree.Base1 :: SampleFunctOne() << endl;
cout << SampleObjThree.Base2 :: SampleFunctTwo() << endl;
return 0;
}
a) 100
b) 200
c) Both 100 & 200
d) 150
Answer: c
#include <iostream>
using namespace std;
class X
{
public:
int a;
void f(int b)
{
cout<< b << endl;
}
};
int main()
{
int X :: *ptiptr = &X :: a;
void (X :: * ptfptr) (int) = &X :: f;
X xobject;
xobject.*ptiptr = 10;
cout << xobject.*ptiptr << endl;
(xobject.*ptfptr) (20);
}
a) 10
20
b)
20
10
b) 20
d) 10
Answer: a
31.
What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class Testpm
{
public:
void m_func1()
{
cout << "func1\n";
}
int m_num;
};
void (Testpm :: *pmfn)() = &Testpm :: m_func1;
int Testpm :: *pmd = &Testpm :: m_num;
int main()
{
Testpm ATestpm;
Testpm *pTestpm = new Testpm;
(ATestpm.*pmfn)();
(pTestpm ->* pmfn)();
ATestpm.*pmd = 1;
pTestpm ->* pmd = 2;
cout << ATestpm.*pmd << endl
<< pTestpm ->* pmd << endl;
}
a) func1
b)
func1
func1
c)
d)
func1
func1
Answer: d
#include <iostream>
using namespace std;
class bowl
{
public:
int apples;
int oranges;
};
int count_fruit(bowl * begin, bowl * end, int bowl :: *fruit)
{
int count = 0;
for (bowl * iterator = begin; iterator != end; ++ iterator)
count += iterator ->* fruit;
return count;
}
int main()
{
bowl bowls[2] = {{ 1, 2 },{ 3, 5 }};
cout << "I have " << count_fruit(bowls, bowls + 2, & bowl :: apples) << " apples\n";
cout << "I have " << count_fruit(bowls, bowls + 2, & bowl :: oranges) << " oranges\
n";
return 0;
}
a)
I have 4 apples
I have 7 oranges
b)
I have 3 apples
I have 5 oranges
c)
I have 1 apples
I have 5 oranges
d)
I have 1 apples
I have 7 oranges
Answer: a
33. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class Foo
{
public:
Foo(int i = 0){ _i = i;}
void f()
{
cout << "Executed"<<endl;
}
private:
int _i;
};
int main()
{
Foo *p = 0;
p -> f();
}
a) Executed
b) Error
c) Runtime error
d) 10
Answer: a
34. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class student
{
public:
int rno , m1 , m2 ;
protected:
void get()
{
rno = 15, m1 = 10, m2 = 10;
}
};
class sports
{
public:
int sm;
void getsm()
{
sm = 10;
}
};
class statement : public student, public sports
{
int tot, avg;
public:
void display()
{
tot = (m1 + m2 + sm);
avg = tot / 3;
cout << tot;
cout << avg;
}
void setObject()
{
get();
}
};
int main()
{
statement obj;
obj.setObject();
obj.getsm();
obj.display();
}
a) 3010
b) 1010
c) 2100
d) Error
Answer: a
35. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
struct A
{
int i;
char j;
float f;
void func();
};
void A :: func() {}
struct B
{
public:
int i;
char j;
float f;
void func();
};
void B :: func() {}
int main()
{
A a; B b;
a.i = b.i = 1;
a.j = b.j = 'c';
a.f = b.f = 3.14159;
a.func();
b.func();
cout << "Allocated";
return 0;
}
a) Allocated
b) Error
c) 3.14159
d) 1
Answer: a
#include <iostream>
using namespace std;
struct A
{
private:
int i, j, k;
public:
int f();
void g();
};
int A :: f()
{
return i + j + k;
}
void A :: g()
{
i = j = k = 0;
}
class B
{
int i, j, k;
public:
int f();
void g();
};
int B :: f()
{
return i + j + k;
}
void B :: g()
{
i = j = k = 0;
}
int main()
{
A a;
B b;
a.f();
a.g();
b.f();
b.g();
cout << "Identical results would be produced";
}
a) 50
b) Identical results would be produced
c) Error
d) Runtime error
Answer: b
#include <iostream>
using namespace std;
class Cat
{
public:
int age;
int weight;
};
int main()
{
Cat f;
f.age = 56;
cout << "Gates is " ;
cout << f.age << " years old.\n";
1. }
a) Gates is
b) Gates is 56 years old
c) Error
d) Gates is 53 years old
Answer: b
#include <iostream>
using namespace std;
struct X;
struct Y
{
void f(X*);
};
struct X
{
private:
int i;
public:
void initialize();
friend void g(X* , int);
friend void Y :: f(X*);
friend struct Z;
friend void h();
};
void X :: initialize()
{
i = 0;
}
void g(X* x, int i)
{
x -> i = i;
}
void Y :: f(X * x)
{
x -> i = 47;
cout << x->i;
}
struct Z
{
private:
int j;
public:
void initialize();
void g(X* x);
};
void Z::initialize()
{
j = 99;
}
void Z::g(X* x)
{
x -> i += j;
}
void h()
{
X x;
x.i = 100;
cout << x.i;
}
int main()
{
X x;
Z z;
z.g(&x);
cout << "Data accessed";
}
a) 99
b) 47
c) Data accessed
d) 67
Answer: c
39. 1. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A
{
static int a;
public:
void show()
{
a++;
cout<<"a: "<<a<<endl;
}
};
int A::a = 5;
Answer: c
string s1 = "Hello";
string s2 = "World";
string s3 = (s1+s2).substr(5);
a) Error because s1+s2 will result into string and no string has substr() function
b) Segmentation fault as two string cannot be added in C++
c) The statements runs perfectly
d) Run-time error
Answer: c
#include <iostream>
#include <string>
using namespace std;
class A
{
static int a;
public:
A()
{
cout<<"Object of A is created\n";
}
void show()
{
a++;
cout<<"a: "<<a<<endl;
}
};
class B
{
public:
};
Answer: d
42. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A
{
static int a;
public:
void show()
{
a++;
cout<<"a: "<<a<<endl;
}
void operator.()
{
cout<<"Objects are added\n";
}
};
class B
{
public:
};
Answer: d
43. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class complex
{
int i;
int j;
public:
complex(int a, int b)
{
i = a;
j = b;
}
complex operator+(complex c)
{
complex temp;
temp.i = this->i + c.i;
temp.j = this->j + c.j;
return temp;
}
void show(){
cout<<"Complex Number: "<<i<<" + i"<<j<<endl;
}
};
Answer: c
#include <iostream>
#include <string>
using namespace std;
class complex
{
int i;
int j;
public:
complex(){}
complex(int a, int b)
{
i = a;
j = b;
}
complex operator+(complex c)
{
complex temp;
temp.i = this->i + c.i;
temp.j = this->j + c.j;
return temp;
}
void show(){
cout<<"Complex Number: "<<i<<" + i"<<j<<endl;
}
};
Answer: a
#include <iostream>
#include <string>
using namespace std;
class Box
{
int capacity;
Box(){}
Box(double capacity){
this->capacity = capacity;
}
};
Answer: a
46. What will be the output of the following C++ code?
#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;
}
a) rect area: 24
b) rect area: 12
c) compile error because rect is as used as class name and variable name in line #20
d) rect area: 56
View Answer
Answer: b
47. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class CDummy
{
public:
int isitme (CDummy& param);
};
int CDummy::isitme (CDummy& param)
{
if (¶m == 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;
}
a) execute
b) not execute
c) error
d) both execute & not execute
Answer: a
48. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
typedef int num;
num a = 10, b = 15;
num c = a + b + a - b;
cout << c;
return 0;
}
a) 20
b) 15
c) 30
d) 25
Answer: a
49. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int i;
enum month
{
JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,DEC
};
for (i = JAN; i <= DEC; i++)
cout << i;
return 0;
}
a) 012345678910
b) 0123456789
c) 01234567891011
d) 01234567891011122
Answer: a
50. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
typedef int num;
typedef char let;
let w = "steve";
num a = 10, b = 15;
num c = a + w;
cout << c;
return 0;
}
a) 10steve
b) steve10
c) compile time error
d) compile but not run
Answer: c
51. What will be the output of the following C++ code?
#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 of Box1 : " << volume <<endl;
return 0;
}
a) 210
b) 213
c) 215
d) 217
Answer: b
#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;
}
a) recta area: 30 rectb area: 42
b) recta area: 20 rectb area: 34
c) recta area: 30 rectb area: 21
d) recta area: 30 rectb area: 33
Answer: a
#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;
}
a)
Enter an integer 5
Variable entered is 5
b) Runtime error
c) Error
d)
Enter an integer 7
Variable entered is 7
Answer: c
#include <iostream>
using namespace std;
class number
{
int i;
public:
int geti();
void puti(int j);
};
int number::geti()
{
return i;
}
void number::puti(int j)
{
i = j;
}
int main()
{
number s;
s.puti(10);
cout << s.geti( );
return 0;
1. }
a) 10
b) 11
c) 20
d) 22
Answer: a
55. What will be the output of the following C++ code?
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
int length;
char * buffer;
ifstream is;
is.open ("sample.txt", ios :: binary );
is.seekg (0, ios :: end);
length = is.tellg();
is.seekg (0, ios :: beg);
buffer = new char [length];
is.read (buffer, length);
is.close();
cout.write (buffer, length);
delete[] buffer;
return 0;
1. }
a) This is sample
b) sample
c) Error
d) Runtime error
Answer: d
56. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main ()
{
char first, second;
cout << "Enter a word: ";
first = cin.get();
cin.sync();
second = cin.get();
cout << first << endl;
cout << second << endl;
return 0;
1. }
a) first
b) second
c) returns first 2 letter or number from the entered word
d) third
Answer: c
57. What will be the output of the following C++ code?
#include<iostream>
#include <fstream>
using namespace std;
int main ()
{
ofstream outfile ("test.txt");
for (int n = 0; n < 100; n++)
{
outfile << n;
outfile.flush();
}
cout << "Done";
outfile.close();
return 0;
1. }
a) Done
b) Error
c) Runtime error
d) DoneDoneDone
Answer: a
58. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main ()
{
int a = 100;
double b = 3.14;
cout << a;
cout << endl;
cout << b << endl << a * b;
endl (cout);
return 0;
}
a) 100
b) 3.14
c) 314
d) All of the mentioned
Answer: d
59. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str;
string str2="Steve jobs";
string str3="He founded apple";
str.append(str2);
str.append(str3, 6, 3);
str.append(str3.begin() + 6, str3.end());
str.append(5,0x2e);
cout << str << '\n';
return 0;
}
a) Steve jobs
b) He founded apple
c) Steve
d) Steve jobsndended apple…..
Answer: d
60. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string name ("Jobs");
string family ("Steve");
name += " Apple ";
name += family;
name += '\n';
cout << name;
return 0;
}
a) Steve Jobs
b) Apple
c) Jobs Apple Steve
d) Apple Steve
Answer: c
61. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("Steve jobs");
cout << str.length();
return 0;
}
a) 8
b) 10
c) 12
d) 9
Answer: b
62. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class Car
{
public:
int speed;
};
int main()
{
int Car :: *pSpeed = &Car :: speed;
Car c1;
c1.speed = 1;
cout << c1.speed << endl;
c1.*pSpeed = 2;
cout << c1.speed << endl;
return 0;
1. }
a) 1
b) 2
c) Both 1 & 2
d) 4
Answer: c
63. What will be the output of the following C++ code?
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;
int main ()
{
int first[] = {10, 40, 90};
int second[] = {1, 2, 3};
int results[5];
transform ( first, first + 5, second, results, divides<int>());
for (int i = 0; i < 3; i++)
cout << results[i] << " ";
return 0;
1. }
a) 10 20
b) 20 30
c) 10 20 30
d) 20 40
Answer: c
64. What will be the output of the following C++ code?
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;
int main ()
{
int numbers[] = {3, -4, -5};
transform ( numbers, numbers + 3, numbers, negate<int>() );
for (int i = 0; i < 3; i++)
cout << numbers[i] << " ";
}
a) -3
b) 3 4 5
c) 3 -4 5
d) -3 4 5
Answer: d
65. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class A
{
public:
A(int n )
{
cout << n;
}
};
class B: public A
{
public:
B(int n, double d)
: A(n)
{
cout << d;
}
};
class C: public B
{
public:
C(int n, double d, char ch)
: B(n, d)
{
cout <<ch;
}
};
int main()
{
C c(5, 4.3, 'R');
return 0;
}
a) 54.3R
b) R4.35
c) 4.3R5
d) R2.6
Answer: a
67. What will be the output of the following C++ code?
#include<iostream>
using namespace std;
class X
{
int m;
public:
X() : m(10)
{
}
X(int mm): m(mm)
{
}
int getm()
{
return m;
}
};
class Y : public X
{
int n;
public:
Y(int nn) : n(nn) {}
int getn() { return n; }
};
int main()
{
Y yobj( 100 );
cout << yobj.getm() << " " << yobj.getn() << endl;
1. }
a) 10 100
b) 100 10
c) 10 10
d) 100 100
Answer: a
#include <iostream>
#include <string>
using namespace std;
class Box
{
int capacity;
public:
Box(int cap){
capacity = cap;
}
void show()
{
Box b(10);
cout<<"Value of capacity is: "<<b.capacity<<endl;
}
class Box
{
int capacity;
public:
void print();
friend void show();
bool compare();
friend bool lost();
};
a) 1
b) 2
c) 3
d) 4
Answer: b
70. . What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class Box
{
int capacity;
Box(){}
Box(double capacity){
this->capacity = capacity;
}
};
Answer: a
72. How many times CppBuzz.com is printed?
#include<iostream>
int main()
{
int i=0;
lbl:
std::cout<<"CppBuzz.com";
i++;
if(i<5)
{
goto lbl;
}
return 0;
}
(A) Error
(B) 5 times
(C) 4 times
(D) 6 times
Ans: B
int main()
{
const int a=10;
a++;
std::cout<<a;
return 0;
}
(A) 10
(B) 11
(C) Compilation Error
(D) Linking Error
Ans: C
int main()
{
int a, b=20;
a = 90/b;
return 0;
}
Ans: C
75. #include<iostream>
class base
{
public:
base()
{
cout<<"BCon";
}
~base()
{
cout<<"BDest ";
}
};
class derived: public base
{
public:
derived()
{ cout<<"DCon ";
}
~derived()
{ cout<<"DDest ";
}
};
int main()
{
derived object;
return 0;
}
(A) Dcon DDest
(B) Dcon DDest BCon BDest
(C) BCon DCon DDest BDest
(D) BCon DCon BDes DDest
Ans: C
int main()
{
int a = 10;
cout<<a++;
return 0;
}
(A) 10
(B) 11
(C) 12
(D) Not defined
Ans: A
#include<iostream>
int main()
{
std::cout<<"CppBuzz";;;;;
return 0;
}
(A) Compilation Error
(B) Runtime Error
(C) CppBuzz
(D) CppBuzz;;;;
Ans: C
int main()
{
int i = 0, x = 0;
do
{
if(i % 5 == 0)
{
std::cout<<x;
x++;
}
++i;
}while(i<10);
cout<<x;
return 0;
}
A) 01
(B) 012
(C) 0
(D) 0123
Ans: B
int main()
{
return 0;
}
A) 0
(B) 1
(C) 4
(D) 5
Ans: B
79. #include<iostream>
enum color{
black=-5,
blue,
red
};
int main()
{
color obj = blue;
std::cout<<obj;
return 0;
}
(A) Compilation Error
(B) 0
(C) 1
(D) -4
Ans: D
80.
#include<iostream>
using namespace std;
int main()
{
int x = 9;
while (x>0)
x--;
cout<<x;
return 0;
}
(A) 9876543210
(B) 987654321
(C) 9
(D) 0
Ans: D
2. One of the methods to stop the execution of the function is by calling the standard ________
function.
a. goto.
b. jump.
c. stop.
d. exit.
ANSWER: D
3. A ________ function is a function that has no body inside its base class.
a. inline.
b. friend.
c. constructor.
d. pure virtual.
ANSWER: D
4. In a group of nested loops, which loop is executed the most number of times?
a. The outermost loop.
b. The innermost loop.
c. All loops are executed the same number of times.
d. Cannot be determined without knowing the size of the loops bottom of form.
ANSWER: B
5. A ________ converts from an object of the type of the constructor's parameter to an object of
the class.
a. conversion function.
b. member function.
c. class conversion.
d. conversion constructors.
ANSWER: D
9. The ________ operator is used to return the value of the variable to which the pointer points.
a. reference.
b. dereference.
c. dot.
d. arrow.
ANSWER: B
11. OOP language supports object based features, inheritance and ______________
[A] Encapsulation.
[B} Polymorphism
[C} Object identity.
[A] Functions.
Answer: Option [B]
12. ___________ is one of the ways to achieve polymorphism.
[A] Inheritance
[B} Data overloading
[C} Operator overloading.
[A] Message binding.
Answer: Option [C]
13. Which of the following is not an arithmetic operator?
[A] +
[B} *
[C} -
[A] &
Answer: Option [D]
14. Which of the following is a correct comment?
[A] */ Comments */.
[B} ** Comment **.
[C} /* Comment */.
[A] { Comment }.
Answer: Option [C]
[A] formal
[B} actual
[C} value
[A] original
Answer: Option [B]
17. Which of the following will not return a value?
[A] null
[B} void
[C} empty
[A] free
Answer: Option [B]
18. Function overloading is also similar to which of the following
[A] Friend
[B} Member
[C} Nonmember
[A] Void
Answer: Option [A]
20. Which is not a loop structure?
[A] for
[B} do while.
[C} while
[A] repeat until.
Answer: Option [D]
21. Which one of the following is a built in function?
[A] stringlen()
[B} strlength().
[C} strlen().
[A] strleng().
Answer: Option [C]
22. ________ is the process of using the same name for two or more functions
[A] cin().
[B} put().
[C} get().
[A] getw().
Answer: Option [B]
24. he technique of building new classes from existing classes is called _______.
[A] inheritance
[B} overloading
[C} constructor
[A] polymorphism
Answer: Option [A]
Polymorphism
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions &
Answers (MCQs) focuses on “Polymorphism”.
Answer: a
Explanation: It is actually the ability for a message / data to be processed in more than one
form. The word polymorphism indicates many-forms. So if a single entity takes more than
one form, it is known as polymorphism.
2. What do you call the languages that support classes but not polymorphism?
a) Class based language
b) Procedure Oriented language
c) Object-based language
d) If classes are supported, polymorphism will always be supported
Answer: c
Explanation: The languages which support classes but doesn’t support polymorphism, are known
as object-based languages. Polymorphism is such an important feature, that is a language doesn’t
support this feature, it can’t be called as a OOP language.
3. Which among the following is the language which supports classes but not polymorphism?
a) SmallTalk
b) Java
c) C++
d) Ada
Answer: d
Explanation: Ada is the language which supports the concept of classes but doesn’t support the
polymorphism feature. It is an object-based programming language. Note that it’s not an OOP
language.
4. If same message is passed to objects of several different classes and all of those can respond in
a different way, what is this feature called?
a) Inheritance
b) Overloading
c) Polymorphism
d) Overriding
Answer: c
Explanation: The feature defined in question defines polymorphism features. Here the different
objects are capable of responding to the same message in different ways, hence polymorphism.
5. Which class/set of classes can illustrate polymorphism in the following code
Answer: c
Explanation: Since Student class is abstract class and class topper and average are inheriting
student, class topper and average must define the function named calc_grade(); in abstract class.
Since both the definition are different in those classes, calc_grade() will work in different way
for same input from different objects. Hence it shows polymorphism.
6. Which type of function among the following shows polymorphism?
a) Inline function
b) Virtual function
c) Undefined functions
d) Class member functions
Answer: b
Explanation: Only virtual functions among these can show polymorphism. Class member
functions can show polymorphism too but we should be sure that the same function is being
overloaded or is a function of abstract class or something like this, since we are not sure about all
these, we can’t say whether it can show polymorphism or not.
7. In case of using abstract class or function overloading, which function is supposed to be called
first?
a) Local function
b) Function with highest priority in compiler
c) Global function
d) Function with lowest priority because it might have been halted since long time, because of
low priority
Answer: b
Explanation: Function with highest priority is called. Here, it’s not about the thread scheduling in
CPU, but it focuses on whether the function in local scope is present or not, or if scope resolution
is used in some way, or if the function matches the argument signature. So all these things define
which function has the highest priority to be called in runtime. Local function could be one of the
answer but we can’t say if someone have used pointer to another function or same function
name.
8. Which among the following can’t be used for polymorphism?
a) Static member functions
b) Member functions overloading
c) Predefined operator overloading
d) Constructor overloading
Answer: a
Explanation: Static member functions are not property of any object. Hence it can’t be
considered for overloading/overriding. For polymorphism, function must be property of object,
not only of class.
9. What is output of the following program?
Answer: b
Explanation: Only virtual functions among these can show polymorphism. Class member
functions can show polymorphism too but we should be sure that the same function is being
overloaded or is a function of abstract class or something like this, since we are not sure about all
these, we can’t say whether it can show polymorphism or not.
7. In case of using abstract class or function overloading, which function is supposed to be called
first?
a) Local function
b) Function with highest priority in compiler
c) Global function
d) Function with lowest priority because it might have been halted since long time, because of
low priority
Answer: b
Explanation: Function with highest priority is called. Here, it’s not about the thread scheduling in
CPU, but it focuses on whether the function in local scope is present or not, or if scope resolution
is used in some way, or if the function matches the argument signature. So all these things define
which function has the highest priority to be called in runtime. Local function could be one of the
answer but we can’t say if someone have used pointer to another function or same function
name.
8. Which among the following can’t be used for polymorphism?
a) Static member functions
b) Member functions overloading
c) Predefined operator overloading
d) Constructor overloading
Answer: a
Explanation: Static member functions are not property of any object. Hence it can’t be
considered for overloading/overriding. For polymorphism, function must be property of object,
not only of class.
9. What is output of the following program?
class student
{
public : int marks;
void disp()
{
cout<<”its base class”
};
class topper:public student
{
public :
void disp()
{
cout<<”Its derived class”;
}
}
void main() { student s; topper t;
s.disp();
t.disp();
}
a) Its base classIts derived class
b) Its base class Its derived class
c) Its derived classIts base class
d) Its derived class Its base class
Answer: a
Explanation: You need to focus on how the output is going to be shown, no space will be given
after first message from base class. And then the message from derived class will be printed.
Function disp() in base class overrides the function of base class being derived.
10. Which among the following can show polymorphism?
a) Overloading ||
b) Overloading +=
c) Overloading <<
d) Overloading &&
Answer: c
Explanation: Only insertion operator can be overloaded among all the given options. And the
polymorphism can be illustrated here only if any of these is applicable of being overloaded.
Overloading is type of polymorphism.
11. Find the output of the following program.
class education
{
char name[10];
public : disp()
{
cout<<”Its education system”;
}
class school:public education
{
public: void dsip()
{
cout<<”Its school education system”;
}
};
void main()
{
school s;
s.disp();
}
}
a) Its school education system
b) Its education system
c) Its school education systemIts education system
d) Its education systemIts school education system
Answer: a
Explanation: Notice that the function name in derived class is different from the function name
in base class. Hence when we call the disp() function, base class function is executed. No
polymorphism is used here.
12. Polymorphism is possible in C language.
a) True
b) False
Answer: a
Explanation: It is possible to implement polymorphism in C language, even though it doesn’t
support class. We can use structures and then declare pointers which in turn points to some
function. In this way we simulate the functions like member functions but not exactly member
function. Now we can overload these functions, hence implementing polymorphism in C
language.
13. Which problem may arise if we use abstract class functions for polymorphism?
a) All classes are converted as abstract class
b) Derived class must be of abstract type
c) All the derived classes must implement the undefined functions
d) Derived classes can’t redefine the function
Answer: c
Explanation: The undefined functions must be defined is a problem, because one may need to
implement few undefined functions from abstract class, but he will have to define each of the
functions declared in abstract class. Being useless task, it is a problem sometimes.
14. Which among the following is not true for polymorphism?
a) It is feature of OOP
b) Ease in readability of program
c) Helps in redefining the same functionality
d) Increases overhead of function definition always
Answer: d
Explanation: It never increases function definition overhead, one way or another if you don’t use
polymorphism, you will use the definition in some other way, so it actually helps to write
efficient codes.
15. If 2 classes derive one base class and redefine a function of base class, also overload some
operators inside class body. Among these two things of function and operator overloading, where
is polymorphism used?
a) Function overloading only
b) Operator overloading only
c) Both of these are using polymorphism
d) Either function overloading or operator overloading because polymorphism can be applied
only once in a program
Answer: c
Explanation: Both of them are using polymorphism. It is not necessary that polymorphism can be
used only once in a program, it can be used anywhere, any number of times in a single program.
Inheritance
Answer: b
Explanation: Inheritance is the concept of OOPs in which new classes are derived from existing
classes in order to reuse the properties of classes defined earlier.
2. How many specifiers are used to derive a class?
a) 1
b) 2
c) 3
d) 4
Answer: c
Explanation: There are 3 specifiers used to derive a class. They are private, protected and public.
3. Which specifier makes all the data members and functions of base class inaccessible by the
derived class?
a) private
b) protected
c) public
d) both private and protected
Answer: a
Explanation: Private access specifier is used to make all the data members and functions of the
base class inaccessible.
4. If a class is derived privately from a base class then ______________________________
a) no members of the base class is inherited
b) all members are accessible by the derived class
c) all the members are inherited by the class but are hidden and cannot be accessible
d) no derivation of the class gives an error
Answer: c
Explanation: Whenever a class is derived, all the members of the base class is inherited by the
derived class but are not accessible by the derived class.
5. 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;
}
};
class B: private A
{
};
Answer: b
Explanation: As class B is derived from class A and class A has three members with each of 4
bytes size hence size of B equal to 3 * 4 = 12 bytes.
6. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A
{
float d;
public:
int a;
void change(int i){
a = i;
}
void value_of_a(){
cout<<a;
}
};
class B: public A
{
int a = 15;
public:
void print(){
cout<<a;
}
};
return 0;
}
a) 1010
b) 1510
c) 1515
d) 5110
Answer: b
Explanation: When change() is called it sets parents class ‘a’ variable = 10. When print() is
called then ‘a’ from class B is printed and wehn value_of_a() is called then ‘a’ from class A is
printed.
7. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A
{
float d;
public:
A(){
cout<<"Constructor of class A\n";
}
};
class B: public A
{
int a = 15;
public:
B(){
cout<<"Constructor of class B\n";
}
};
Constructor of class A
Constructor of class B
b) Constructor of class A
c) Constructor of class B
d)
Constructor of class B
Constructor of class A
Answer: a
Explanation: When a derived class is declared it calls both its constructor and the base class
constructor. It first calls the base class constructor and then its own constructor.
#include <iostream>
#include <string>
using namespace std;
class A{
float d;
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";
}
};
#include <iostream>
#include <string>
using namespace std;
class A
{
float d;
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";
}
};
#include <iostream>
#include <string>
using namespace std;
class A
{
float d;
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";
}
};
Answer: b
Explanation: As pointer object a is pointing to the object b hence the definition of virtual
function defined inside the class B will be class. This is one of the use of virtual function.
13. Which statement is incorrect about virtual function.
a) They are used to achieve runtime polymorphism
b) They are used to hide objects
c) Each virtual function declaration starts with the virtual keyword
d) All of the mentioned
Answer: b
Explanation: Virtual function are used to achieve runtime polymorphism by calling the right
function during runtime. Their declaration starts with a virtual keyword.
14. The concept of deciding which function to invoke during runtime is called
______________________
a) late binding
b) dynamic linkage
c) static binding
d) both late binding and dynamic linkage
Answer: d
Explanation: The concept of deciding which function to invoke during runtime is called late
binding or dynamic linkage. Late binding because function binding to the object is done during
runtime. Dynamic linkage because this binding is done during runtime.
15. What is a pure virtual function?
a) A virtual function defined inside the base class
b) A virtual function that has no definition relative to the base class
c) A virtual function that is defined inside the derived class
d) Any function that is made virtual
Answer: b
Explanation: A virtual function that has no definition relative to the base class is called a pure
virtual function.
Files
a. Binary
b. Text
c. Can’t say
Answer Explanation
2) It is not possible to combine two or more file opening mode in open () method.
- Published on 19 Oct 15
a. True
b. False
Answer Explanation
ANSWER: False
Explanation:
No explanation is available for this question!
3) Which of the following is not a file opening mode ____ .
- Published on 19 Jul 15
a. ios::ate
b. ios::nocreate
c. ios::noreplace
d. ios::truncate
Answer Explanation
ANSWER: ios::truncate
Explanation:
No explanation is available for this question!
4) Due to ios::trunc mode, the file is truncated to zero length.
- Published on 17 Jul 15
a. True
b. False
Answer Explanation
ANSWER: True
Explanation:
No explanation is available for this question!
5) If we have object from ofstream class, then default mode of opening the file is _____ .
- Published on 17 Jul 15
a. ios::in
b. ios::out
c. ios::in|ios::trunc
d. ios::out|ios::trunk
Answer Explanation
ANSWER: ios::out|ios::trunk
Explanation:
No explanation is available for this question!
6) __________ is return type of is_open() function.
- Published on 17 Jul 15
a. int
b. bool
c. float
d. char *
Answer Explanation
7) If we have object from fstream class, then what is the default mode of opening the file?
- Published on 17 Jul 15
a. ios::in|ios::out
b. ios::in|ios::out|ios::trunc
c. ios::in|ios::trunc
Answer Explanation
a. ofstream
b. ifstream
c. iostream
d. None of these
Answer Explanation
ANSWER: ofstream
Explanation:
No explanation is available for this question!
9) Streams that will be performing both input and output operations must be declared as class _________
- Published on 17 Jul 15
a. iostream
b. fstream
c. stdstream
d. Stdiostream
Answer Explanation
ANSWER: fstream
Explanation:
No explanation is available for this question!
10) To perform File I/O operations, we must use _____________ header file.
- Published on 17 Jul 15
a. < ifstream>
b. < ofstream>
c. < fstream>
d. Any of these
Answer Explanation
b. ios::set
c. ios::end
d. ios::beg
Answer Explanation
ANSWER: ios::set
Explanation:
No explanation is available for this question!