Source Files, Classes and Operator Overloading in C++
Source Files, Classes and Operator Overloading in C++
Here is a listing of C++ questions and puzzles on “Linkage” along with answers, explanations and/or
solutions:
Answer: c
Explanation: There are three types of linkage in c++. They are internal linkage, external linkage and no
linkage.
Answer: a
Explanation: None.
#ifndef Exercise_H
#define Exercise_H
#endif
2.
#include <iostream>
#include "exe.h"
return 0;
a) 842
b) 843
c) compile time error
d) none of the mentioned
View Answer
Answer: a
Explanation: In this program, we have created a header file and linked that into the source program and
we are post incrementing that because of that it is printed as 842.
Output:
$ g++ link.cpp
$ a.out
842
5. What is the defualt type oof linkage that are available for identifires?
a) internal
b) external
c) no linkage
d) none of the mentioned
View Answer
Answer: b
Explanation: None.
Answer: b
Explanation: Extern keyword is used to represent identifiers from other programs.
Answer: c
Explanation: By defining a function’s prototype in another file means, we can inherit all the features
from the source function.
Answer: c
Explanation: None.
Here is a listing of C++ interview questions on “Header Files Usage” along with answers, explanations
and/or solutions:
Answer: b
Explanation: None.
Answer: a
Explanation: The include keyword is used to include all the required things to execute the given code in
the program.
Answer: a
Explanation: The iostream.h is used in the older versions of c++ and iostream is evolved from it in the std
namespace.
#include <iostream>
int main()
char name[30];
gets(name);
puts(name);
return 0;
a) jobsjobs
b) jobs
c) compile time error
d) program will not run
View Answer
Answer: c
Explanation: In this program,we need to string header file to run this program.
Answer: b
Explanation: The iomanip header file is used to correct the precision of the values.
Answer: c
Explanation: None.
Answer: b
Explanation: None.
9. Which of the following header files is required for creating and reading data files?
a) ofstream.h
b) fstream.h
c) ifstream.h
d) console.h
View Answer
Answer: b
Explanation: In this fstream.h header file is used for accessing the files only.
#include <iostream>
#include <stdarg.h>
va_list Numbers;
va_start(Numbers, Count);
int Sum = 0;
va_end(Numbers);
return (Sum/Count);
}
int main()
return 0;
a) 4
b) 5
c) 6
d) compile time error
View Answer
Answer: a
Explanation: In this program, we are finding the average of first 10 numbers using stdarg header file
Output:
$ g++ std.cpp
$ a.out
4
Here is a listing of C++ quiz on “Classes” along with answers, explanations and/or solutions:
Answer: C
Explanation: The classes in c++ are used to manipulate both data and functions.
2. How many specifiers are present in access specifiers in class?
a) 1
b) 2
c) 3
d) 4
View Answer
Answer: c
Explanation: There are three types of access specifiers. They are public, protected and private.
Answer: b
Explanation: None.
4. Which other keywords are also used to declare the class other than class?
a) struct
b) union
c) object
d) both struct & union
View Answer
Answer: d
Explanation: Struct and union take the same definition of class but differs in the access techniques.
#include <iostream>
class rect
int x, y;
public:
int area ()
{
return (x * y);
};
x = a;
y = b;
int main ()
rect rect;
return 0;
a) rect area:12
b) rect area: 12
c) rect area:24
d) none of the mentioned
View Answer
Answer: b
Explanation: In this program, we are calculating the area of rectangle based on given values.
Output:
$ g++ class.cpp
$ a.out
rect area: 12
#include <iostream>
class CDummy
public:
int isitme (CDummy& param);
};
if (¶m == this)
return true;
else
return false;
int main ()
CDummy a;
CDummy *b = &a;
if (b->isitme(a))
else
cout<<"not execute";
return 0;
a) execute
b) not execute
c) none of the mentioned
d) both execute & not execute
View Answer
Answer: a
Explanation: In this program, we are just pointing the pointer to a object and printing execute if it is
correctly pointed.
Output:
$ g++ class1.cpp
$ a.out
execute
Answer: a
Explanation: None.
Answer: b
Explanation: None.
Answer: a
Explanation: Once the object is declared means, the constructor are also declared by default.
10. When struct is used instead of the keyword class means, what will happen in the program?
a) access is public by default
b) access is private by default
c) access is protected by default
d) none of the mentioned
View Answer
Answer: a
Explanation: None.
Multiple Choice Questions C++ - User Defined Types - Sanfoundry
by Manish
This section on C++ MCQs (multiple choice questions) focuses on “User Defined Types”. One
shall practice these MCQs to improve their C++ programming skills needed for various
interviews (campus interviews, walkin interviews, company interviews), placements, entrance
exams and other competitive exams. These questions can be attempted by anyone focusing on
learning C++ programming language. They can be a beginner, fresher, engineering graduate or
an experienced IT professional. Our multiple choice questions come with detailed explanation of
the answers which helps in better understanding of C++ concepts.
Here is a listing of C multiple choice questions on “User Defined Types” along with answers,
explanations and/or solutions:
Answer: c
Explanation: None.
Answer: a
Explanation: By using typedef, we can create a type of pre-existing type only not our own type
of data.
Answer: d
Explanation: Union is used to define the data types of our choice and it will store the data type in
one location make them accessible.
4. What is the output of this program?
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. typedef int num;
6. num a = 10, b = 15;
7. num c = a + b + a - b;
8. cout << c;
9. return 0;
10. }
a) 20
b) 15
c) 30
d) 25
View Answer
Answer: a
Explanation: In this program, we are manipulating the numbers and printing the result using
user-defined data types.
Output:
$ g++ user.cpp
$ a.out
20
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int i;
6. enum month
7. {
8. JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,DEC
9. };
10. for (i = JAN; i <= DEC; i++)
11. cout << i;
12. return 0;
13. }
a) 012345678910
b) 0123456789
c) 01234567891011
d) none of the mentioned
View Answer
Answer: a
Explanation: In this program, we are defined the data types as enumerator and printing its value
in a order.
Output:
$ g++ user1.cpp
$ a.out
012345678910
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. typedef int num;
6. typedef char let;
7. let w = "steve";
8. num a = 10, b = 15;
9. num c = a + w;
10. cout << c;
11. return 0;
12. }
a) 10steve
b) steve10
c) compile time error
d) compile but not run
View Answer
Answer: c
Explanation: Error: invalid conversion from ‘const char*’ to ‘let {aka char}’.
Answer: a
Explanation: None.
Answer: b
Explanation: We are defining the user-defined data type to be availed only inside that program, if
we want to use anywhere means we have to define those types in the header file.
10. How many types of models are available to create the user-defined data type?
a) 1
b) 2
c) 3
d) 4
View Answer
Answer: b
Explanation: There are two types of models. They are references to built-in types and multipart
types.
Here is a listing of C++ quiz on “Objects” along with answers, explanations and/or solutions:
Answer: c
Explanation: Objects in the method can be accessed using direct member access operator which is (.).
3. Which of these following members are not accessed by using direct member access operator?
a) public
b) private
c) protected
d) both private & protected
View Answer
Answer: d
Explanation: Because of the access given to the private and protected, We can’t access them by using
direct member access operator.
#include <iostream>
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;
return 0;
a) 210
b) 213
c) 215
d) 217
View Answer
Answer: b
Explanation: In the above program, we are calculating the area of the cube by using the cube formula
Output:
$ g++ obj1.cpp
$ a.out
213
#include <iostream>
class Rect
int x, y;
public:
int area ()
return (x * y);
};
x = a;
y = b;
int main ()
return 0;
Answer: a
Explanation: We are calculating the area of rectangle by two objects.
Answer: d
Explanation: None.
#include <iostream>
class sample
private:
int var;
public:
void input()
void output()
};
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) None of the mentioned
View Answer
Answer: c
Explanation: While using private member, you can’t access it variable.
Answer: a
Explanation: None.
#include <iostream>
class number
int i;
public:
int geti();
};
int number::geti()
return i;
void number::puti(int j)
{
i = j;
int main()
number s;
s.puti(10);
return 0;
a) 10
b) 11
c) 20
d) 22
View Answer
Answer: a
Explanation: We are getting the number and copying it to j and printing it.
Output:
$ g++ obj2.cpp
$ a.out
10
This section on C++ questions and puzzles focuses on “Operator Functions”. One shall practice
these questions and puzzles to improve their C++ programming skills needed for various
interviews (campus interviews, walkin interviews, company interviews), placements, entrance
exams and other competitive exams. These programming puzzles can be attempted by anyone
focusing on learning C++ programming language. They can be a beginner, fresher, engineering
graduate or an experienced IT professional. Our C++ questions come with detailed explanation
of the answers which helps in better understanding of C++ concepts.
Here is a listing of C++ questions and puzzles on “Operator Functions” along with answers,
explanations and/or solutions:
Answer: b
Explanation: None.
Answer: a
Explanation: None.
Answer: a
Explanation: We have to declare the operator function by using operator, operator sign. Example
“operator +” where operator is a keyword and + is the symbol need to be overloaded.
1. #include <iostream>
2. using namespace std;
3. class sample
4. {
5. public:
6. int x, y;
7. sample() {};
8. sample(int, int);
9. sample operator + (sample);
10. };
11. sample::sample (int a, int b)
12. {
13. x = a;
14. y = b;
15. }
16. sample sample::operator+ (sample param)
17. {
18. sample temp;
19. temp.x = x + param.x;
20. temp.y = y + param.y;
21. return (temp);
22. }
23. int main ()
24. {
25. sample a (4,1);
26. sample b (3,2);
27. sample c;
28. c = a + b;
29. cout << c.x << "," << c.y;
30. return 0;
31. }
a) 5, 5
b) 7, 3
c) 3, 7
d) None of the mentioned
View Answer
Answer: b
Explanation: In this program, we are adding the first number of a with first number of b by using
opertor function and also we are adding second number by this method also.
Output:
$ g++ oper.cpp
$ a.out
7, 3
1. #include <iostream>
2. using namespace std;
3. class Box
4. {
5. double length;
6. double breadth;
7. double height;
8. public:
9. double getVolume(void)
10. {
11. return length * breadth * height;
12. }
13. void setLength( double len )
14. {
15. length = len;
16. }
17. void setBreadth( double bre )
18. {
19. breadth = bre;
20. }
21. void setHeight( double hei )
22. {
23. height = hei;
24. }
25. Box operator+(const Box& b)
26. {
27. Box box;
28. box.length = this->length + b.length;
29. box.breadth = this->breadth + b.breadth;
30. box.height = this->height + b.height;
31. return box;
32. }
33. };
34. int main( )
35. {
36. Box Box1;
37. Box Box2;
38. Box Box3;
39. double volume = 0.0;
40. Box1.setLength(6.0);
41. Box1.setBreadth(7.0);
42. Box1.setHeight(5.0);
43. Box2.setLength(12.0);
44. Box2.setBreadth(13.0);
45. Box2.setHeight(10.0);
46. volume = Box1.getVolume();
47. cout << "Volume of Box1 : " << volume <<endl;
48. volume = Box2.getVolume();
49. cout << "Volume of Box2 : " << volume <<endl;
50. Box3 = Box1 + Box2;
51. volume = Box3.getVolume();
52. cout << "Volume of Box3 : " << volume <<endl;
53. return 0;
54. }
Answer: a
Explanation: In this program, we finding the box3 area by adding box1 and box2.
Output:
$ g++ oper1.cpp
$ a.out
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400
a) operator+
operator+=
b) operator+=
operator+
c) operator+
operator+
d) none of the mentioned
View Answer
Answer: a
Explanation: We are using two operator functions and executing them and result is printed
according to the order.
Output:
$ g++ oper2.cpp
$ a.out
operator+
operator+=
1. #include <iostream>
2. using namespace std;
3. class myclass
4. {
5. public:
6. int i;
7. myclass *operator->()
8. {return this;}
9. };
10. int main()
11. {
12. myclass ob;
13. ob->i = 10;
14. cout << ob.i << " " << ob->i;
15. return 0;
16. }
a) 10 10
b) 11 11
c) error
d) runtime error
View Answer
Answer: a
Explanation: In this program, -> operator is used to describe the member of the class and so we
are getting this output.
Output:
$ g++ char4.cpp
$ a.out
10 10
Answer: d
Explanation: None.
9. Operator overloading is
a) making c++ operator works with objects
b) giving new meaning to existing operator
c) making new operator
d) adding operation to the existing operators
View Answer
Answer: d
Explanation: Operator overloading is the way adding operation to the existing operators.
a) 5
b) 6
c) error
d) runtime error
View Answer
Answer: c
Explanation: In this program, there will arise an ambiguous overload for 5.
Here is a listing of C++ interview questions on “Complex Number Type” along with answers,
explanations and/or solutions:
Answer: b
Explanation: None.
Answer: b
Explanation: We can declare the complex number by using complex(3,4) where 3 is a real number and 4
is imaginary part.
Answer: c
Explanation: There are three real types in complex numbers. They are float complex, double complex,
long double complex.
#include <iostream>
#include <complex>
int main()
c2 = pow(c1, 2.0);
return 0;
a) (-240, 128)
b) (240, 128)
c) (240, 120)
d) None of the mentioned
View Answer
Answer: a
Explanation: In this program, we are finding the square of the complex number.
Output:
$ g++ comp.cpp
$ a.out
(-240,128)
#include <iostream>
#include <complex>
int main()
c_double *= 2;
c_double = c_int;
return 0;
a) (2, 3)
b) (4, 5)
c) (8, 15)
d) None of the mentioned
View Answer
Answer: b
Explanation: We are just copying the value of c_int into c_double, So it’s printing as (4,5).
Output:
$ g++ comp1.cpp
$ a.out
(4,5)
#include <iostream>
#include <complex>
i = i * 6 / 3;
cout << i;
return 0;
a) (4, 6)
b) (2, 3)
c) (6, 12)
d) None of the mentioned
View Answer
Answer: a
Explanation: We are multiplying the complex number by 2.
Output:
$ g++ comp2.cpp
$ a.out
(4,6)
#include <iostream>
#include <complex>
int main()
complex<double> c1(4.0,3.0);
complex<float> c2(polar(5.0,0.75));
return 0;
a) c1: (4,3)(7.65844,6.40819)
b) c1: (4,3)(7,6)
c) both c1: (4,3)(7.65844,6.40819) & c1: (4,3)(7,6)
d) None of the mentioned
View Answer
Answer: a
Explanation: We are adding the two complex numbers and printing the result.
Output:
$ g++ comp3.cpp
$ a.out
c1: (4,3)(7.65844,6.40819)
#include <iostream>
#include <complex>
int main()
return 0;
a) (4.0, 3.0)
b) (6.12132, 3.70711)
c) (5.0, 0.75)
d) None of the mentioned
View Answer
Answer: b
Explanation: In this program, we are adding both complex number and finding the square root of it.
Output:
$ g++ comp4.cpp
$ a.out
(6.12132,3.70711)
Answer: d
Explanation: None.
#include <iostream>
#include <complex>
int main ()
return 0;
a) 2
b) 20
c) 40
d) None of the mentioned
View Answer
Answer: a
Explanation: imag part will return the imaginery part of the complex number.
Output:
$ g++ comp5.cpp
$ a.out
2
Answer: d
Explanation: Conversion operator doesn’t have any return type not even void.
Answer: a
Explanation: It is used to check that operators and operands are compatible after conversion.
Answer: a
Explanation: None.
#include <iostream>
class sample1
float i, j;
};
class sample2
{
int x, y;
public:
x = a;
y = b;
int result()
return x + y;
};
int main ()
sample1 d;
sample2 * padd;
return 0;
a) 20
b) runtime error
c) random number
d) runtime error or random number
View Answer
Answer: d
Explanation: As it assigns to a reference to an object of another incompatible type using explicit type-
casting.
Output:
$ g++ con.cpp
$ a.out
14032334
#include <iostream>
class sample
public:
sample(int i) : m_i(i) { }
public:
return m_i + i;
return m_i;
private:
int m_i;
};
int f(char c)
return c;
int main()
sample f(2);
cout << f(2);
return 0;
a) 3
b) 4
c) 5
d) None of the mentioned
View Answer
Answer: b
Explanation: In this program, we are adding its value with it itself, So only we got the output as 4.
Output:
$ g++ con1.cpp
$ a.out
4
#include <iostream>
#include <cmath>
class Complex
private:
double real;
double imag;
public:
{}
double mag()
return getMag();
operator double ()
{
return getMag();
private:
double getMag()
};
int main()
return 0
a) 5 5
b) 4 5
c) 6 6
d) None of the mentioned
View Answer
Answer: a
Explanation: In this program, we are calculating the magnitude value by two ways.
Output:
$ g++ con3.cpp
$ a.out
55
#include <iostream>
#include <string>
class test
{
public:
operator string ()
return "Converted";
};
int main()
test t;
string s = t;
return 0;
a) converted
b) error
c) run time error
d) none of the mentioned
View Answer
Answer: a
Explanation: In this program, We casted the string to the object of the class.
Output:
$ g++ con4.cppp
$ a.out
converted
#include <iostream>
int main()
double a = 21.09399;
float b = 10.20;
int c ;
c = (int) a;
cout << c ;
c = (int) b;
cout << c ;
return 0;
a) 2110
b) 1210
c) 21
d) None of the mentioned
View Answer
Answer: a
Explanation: In this program, we casted the data type to integer.
Output:
$ g++ con5.cpp
$ a.out
2110
Answer: b
Explanation: There are two types of user-defined conversions.They are conversion by constructor,
Conversion functions.
Answer: a
Explanation: None.
Multiple Choice Questions C++ - Friends - Sanfoundry
by Manish
This section on C++ MCQs (multiple choice questions) focuses on “Friends”. One shall practice
these MCQs to improve their C++ programming skills needed for various interviews (campus
interviews, walkin interviews, company interviews), placements, entrance exams and other
competitive exams. These questions can be attempted by anyone focusing on learning C++
programming language. They can be a beginner, fresher, engineering graduate or an experienced
IT professional. Our multiple choice questions come with detailed explanation of the answers
which helps in better understanding of C++ concepts.
Here is a listing of C multiple choice questions on “Friends” along with answers, explanations
and/or solutions:
Answer: a
Explanation: Friend is used to access private and protected members of a class from outside the
same class.
Answer: b
Explanation: None.
Answer: a
Explanation: In option a, the class2 is the friend of class1 and it can access all the private and
protected members of class1.
4. What is the output of this program?
1. #include <iostream>
2. using namespace std;
3. class Box
4. {
5. double width;
6. public:
7. friend void printWidth( Box box );
8. void setWidth( double wid );
9. };
10. void Box::setWidth( double wid )
11. {
12. width = wid;
13. }
14. void printWidth( Box box )
15. {
16. box.width = box.width * 2;
17. cout << "Width of box : " << box.width << endl;
18. }
19. int main( )
20. {
21. Box box;
22. box.setWidth(10.0);
23. printWidth( box );
24. return 0;
25. }
a) 40
b) 5
c) 10
d) 20
View Answer
Answer: d
Explanation: We are using the friend function for printwidth and multiplied the width value by 2,
So we got the output as 20
Output:
$ g++ friend.cpp
$ a.out
20
1. #include <iostream>
2. using namespace std;
3. class sample
4. {
5. int width, height;
6. public:
7. void set_values (int, int);
8. int area () {return (width * height);}
9. friend sample duplicate (sample);
10. };
11. void sample::set_values (int a, int b)
12. {
13. width = a;
14. height = b;
15. }
16. sample duplicate (sample rectparam)
17. {
18. sample rectres;
19. rectres.width = rectparam.width * 2;
20. rectres.height = rectparam.height * 2;
21. return (rectres);
22. }
23. int main ()
24. {
25. sample rect, rectb;
26. rect.set_values (2, 3);
27. rectb = duplicate (rect);
28. cout << rectb.area();
29. return 0;
30. }
a) 20
b) 16
c) 24
d) None of the mentioned
View Answer
Answer: c
Explanation: In this program, we are using the friend function for duplicate function and
calculating the area of the rectangle.
Output:
$ g++ friend1.cpp
$ a.out
24
1. #include <iostream>
2. using namespace std;
3. class sample;
4. class sample1
5. {
6. int width, height;
7. public:
8. int area ()
9. {
10. return (width * height);}
11. void convert (sample a);
12. };
13. class sample
14. {
15. private:
16. int side;
17. public:
18. void set_side (int a)
19. {
20. side = a;
21. }
22. friend class sample1;
23. };
24. void sample1::convert (sample a)
25. {
26. width = a.side;
27. height = a.side;
28. }
29. int main ()
30. {
31. sample sqr;
32. sample1 rect;
33. sqr.set_side(6);
34. rect.convert(sqr);
35. cout << rect.area();
36. return 0;
37. }
a) 24
b) 35
c) 16
d) 36
View Answer
Answer: d
Explanation: In this program, we are using the friend for the class and calculating the area of the
square.
Output:
$ g++ friend2.cpp
$ a.out
36
1. #include <iostream>
2. using namespace std;
3. class base
4. {
5. int val1, val2;
6. public:
7. int get()
8. {
9. val1 = 100;
10. val2 = 300;
11. }
12. friend float mean(base ob);
13. };
14. float mean(base ob)
15. {
16. return float(ob.val1 + ob.val2) / 2;
17. }
18. int main()
19. {
20. base obj;
21. obj.get();
22. cout << mean(obj);
23. return 0;
24. }
a) 200
b) 150
c) 100
d) 300
View Answer
Answer: a
Explanation: In this program, We are finding the mean value by declaring the function mean as a
friend of class base.
Output:
$ g++ friend3.cpp
$ a.out
200
1. #include <iostream>
2. using namespace std;
3. class sample
4. {
5. private:
6. int a, b;
7. public:
8. void test()
9. {
10. a = 100;
11. b = 200;
12. }
13. friend int compute(sample e1);
14. };
15. int compute(sample e1)
16. {
17. return int(e1.a + e1.b) - 5;
18. }
19. int main()
20. {
21. sample e;
22. e.test();
23. cout << compute(e);
24. return 0;
25. }
a) 100
b) 200
c) 300
d) 295
View Answer
Answer: d
Explanation: In this program, we are finding a value from the given function by using the friend
for compute function.
Output:
$ g++ friend4.cpp
$ a.out
295
Answer: c
Explanation: None.
Answer: a
Explanation: The keyword friend is placed only in the function declaration of the friend function
and not in the function definition because it is used toaccess the member of a class.
This section on C++ quiz focuses on “Large Objects”. One shall practice these quizzes to
improve their C++ programming skills needed for various interviews (campus interviews, walkin
interviews, company interviews), placements, entrance exams and other competitive exams.
These questions can be attempted by anyone focusing on learning C++ programming language.
They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our
C++ quiz comes with detailed explanation of the answers which helps in better understanding of
C++ concepts.
Here is a listing of C++ quiz on “Large Objects” along with answers, explanations and/or
solutions:
1. How to store the large objects in c++ if it extents its allocated memory?
a) memory heap
b) stack
c) queue
d) none of the mentioned
View Answer
Answer: a
Explanation: None.
2. When we are using heap operations what do we need to do to save the memory?
a) rename the objects
b) delete the objects after processing
c) both rename & delete the objects
d) none of the mentioned
View Answer
Answer: b
Explanation: when you allocate memory from the heap, you must remember to clean up objects
when you’re done! Failure to do so is called a memory leak.
Answer: c
Explanation: Because vector is mainly used to store large objects for game
programming and other operations etc.
1. #include <iostream>
2. using namespace std;
3. class sample
4. {
5. public:
6. sample()
7. {
8. cout << "X::X()" << endl;
9. }
10. sample( sample const & )
11. {
12. cout << "X::X( X const & )" << endl;
13. }
14. sample& operator=( sample const & )
15. {
16. cout << "X::operator=(X const &)" << endl;
17. }
18. };
19. sample f()
20. {
21. sample tmp;
22. return tmp;
23. }
24. int main()
25. {
26. sample x = f();
27. return 0;
28. }
Answer: c
Explanation: As we are passing the object without any attributes it will return as X::X().
Output:
$ g++ large.cpp
$ a.out
X::X()
Answer: d
Explanation: None.
Answer: d
Explanation: None.
Answer: a
Explanation: None.
Answer: c
Explanation: In virtual memory, We can keep track of all the objects and access them much
faster than any another.
Answer: b
Explanation: Because by using pass by reference we need to pass only address location, So it can
save alot of memory.
C++ Programming Questions and Answers – Essential Operators
This section on online C++ test focuses on “Essential Operators”. One shall practice these test questions
to improve their C++ programming skills needed for various interviews (campus interviews, walkin
interviews, company interviews), placements, entrance exams and other competitive exams. These
questions can be attempted by anyone focusing on learning C++ programming language. They can be a
beginner, fresher, engineering graduate or an experienced IT professional. Our online C++ test questions
come with detailed explanation of the answers which helps in better understanding of C++ concepts.
Here is a listing of online C++ test questions on “Essential Operators” along with answers, explanations
and/or solutions:
Answer: d
Explanation: None.
Answer: b
Explanation: In assignment operation, the flow of execution will be from right to left only.
Answer: c
Explanation: When we want to modify the value of a variable by performing an operation on the value
currently stored, We will use compound assignment statement. In this option, a -=5 is equal to a = a-5.
int main ()
int a, b;
a = 10;
b = 4;
a = b;
b = 7;
cout << a;
cout << b;
return 0;
a) a:4 b:7
b) a:10 b:4
c) a:4 b:10
d) None of the mentioned
View Answer
Answer: a
Explanation: In this program, we are reassigning the values of a and b because of this we got the output
as a:4 b:7
Output:
$ g++ ess.cpp
$ a.out
a:4 b:7
#include <iostream>
int main ()
{
int a, b, c;
a = 2;
b = 7;
c = (a > b) ? a : b;
cout << c;
return 0;
a) 2
b) 7
c) 9
d) 14
View Answer
Answer: b
Explanation: We are using the ternary operator to evaluate this expression. It will return first option, if
first condition is true otherwise it will return second
Output:
$ g++ ess1.cpp
$ a.out
7
#include <iostream>
int main()
int a = 0;
int b = 10;
if ( a && b )
else
{
cout << "false"<< endl ;
return 0;
a) true
b) false
c) error
d) none of the mentioned
View Answer
Answer: b
Explanation: && is called as Logical AND operator, if there is no zero in the operand means, it will be
true otherwise false.
Output:
$ g++ ess2.cpp
$ a.out
false
Answer: b
Explanation: None.
Answer: b
Explanation: | operator is used to find the ‘or’ of given values.
Answer: b
Explanation: None.
#include <iostream>
int main()
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = a + b * c / d;
return 0;
a) 50
b) 60
c) 70
d) 90
View Answer
Answer: a
Explanation: In this program, the value e is evaluated by precedence ad we got the output as 50.
Output:
$ g++ ess4.cpp
$ a.out
50
Here is a listing of C++ interview questions on “Subscripting” along with answers, explanations and/or
solutions:
Answer: c
Explanation: None.
2. How many arguments will the subscript operator will take for overloading?
a) 1
b) 2
c) 0
d) as many as possible
View Answer
Answer: a
Explanation: The subscript operator overload takes only one argument, but it can be of any type.
Answer: a
Explanation: None.
#include <iostream>
private:
int arr[SIZE];
public:
safe()
register int i;
arr[i] = i;
int &operator[](int i)
if (i > SIZE)
return arr[0];
return arr[i];
};
int main()
safe A;
return 0;
}
Answer: a
Explanation: In this program, We are returning the elements in the specified array location and if it is out
of bound means it will return the first element.
Output:
$ g++ sub.cpp
$ a.out
5Index out of bounds
0
#include <iostream>
class numbers
private:
int m_nValues[10];
public:
};
return m_nValues[nValue];
int main()
numbers N;
N[5] = 4;
cout << N[5];
return 0;
a) 5
b) 4
c) 3
d) 6
View Answer
Answer: b
Explanation: In this program, We are getting the values and returning it by overloading the subscript
operator.
Output:
$ g++ sub1.cpp
$ a.out
4
#include <iostream>
class safearray
private:
int arr[limit];
public:
if (n == limit - 1)
int temp;
temp = arr[n];
arr[n + 1] = temp;
return arr[n];
};
int main()
safearray sa1;
sa1[j] = j*10;
cout << "Element " << j << " is " << temp;
return 0;
a) 0102030
b) 1020300
c) 3020100
d) error
View Answer
Answer: a
Explanation: In this program, we are returning the array element by the multiple of 10.
Output:
$ g++ sub2.cpp
$ a.out
0102030
#include <iostream>
class A
public:
int x;
int& operator[](int n)
return x;
return x;
};
int z = a[2];
int main()
A a(7);
a[3] = 8;
int z = a[2];
foo(a);
return 0;
a) 110
b) 111
c) 011
d) 001
View Answer
Answer: d
Explanation: In this program, we overloading the operator[] by using subscript operator.
Output:
$ g++ sub3.cpp
$ a.out
001
#include <iostream>
class sample
private:
int* i;
int j;
public:
~sample ();
};
return i[n];
}
sample::sample (int j)
j = j;
sample::~sample ()
delete [] i;
int main ()
sample m (5);
m [0] = 25;
m [1] = 20;
m [2] = 15;
m [3] = 10;
m [4] = 5;
return 0;
a) 252015105
b) 510152025
c) 51015
d) None of the mentioned
View Answer
Answer: a
Explanation: In this program, we are printing the array in the reverse order by using subscript operator.
Output:
$ g++ sub4.cpp
$ a.out
252015105
9. What do we need to do to pointer for overloading the subscript operator?
a) reference pointer
b) dereference pointer
c) store it in heap
d) none of the mentioned
View Answer
Answer: b
Explanation: If you have a pointer to an object of some class type that overloads the subscript operator,
you have to dereference that pointer in order to free the memory.
Answer: a
Explanation: The reason is that operator[] always takes exactly one parameter, but operator() can take
any number of parameters.
This section on C++ quiz focuses on “Function Call”. One shall practice these quizzes to
improve their C++ programming skills needed for various interviews (campus interviews, walkin
interviews, company interviews), placements, entrance exams and other competitive exams.
These questions can be attempted by anyone focusing on learning C++ programming language.
They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our
C++ quiz comes with detailed explanation of the answers which helps in better understanding of
C++ concepts.
Here is a listing of C++ quiz on “Function Call” along with answers, explanations and/or
solutions:
Answer: a
Explanation: None.
Answer: d
Explanation: It will modifies how the operator is to be interpreted when applied to objects of a
given type.
1. #include
2. using namespace std;
3. class Distance
4. {
5. private:
6. int feet;
7. int inches;
8. public:
9. Distance()
10. {
11. feet = 0;
12. inches = 0;
13. }
14. Distance(int f, int i)
15. {
16. feet = f;
17. inches = i;
18. }
19. Distance operator()(int a, int b, int c)
20. {
21. Distance D;
22. D.feet = a + c + 10;
23. D.inches = b + c + 100 ;
24. return D
25. }
26. void displayDistance()
27. {
28. cout << feet << inches << endl;
29. }
30. };
31. int main()
32. {
33. Distance D1(11, 10), D2;
34. cout << "First Distance : ";
35. D1.displayDistance();
36. D2 = D1(10, 10, 10);
37. cout << "Second Distance :";
38. D2.displayDistance();
39. return 0;
40. }
Answer: a
Explanation: We are calculating the foot and inches by using the function call operator.
Output:
$ g++ call.cpp
$ a.out
First Distance : 1110
Second Distance :30120
1. #include
2. using namespace std;
3. void duplicate (int& a, int& b, int& c)
4. {
5. a *= 2;
6. b *= 2;
7. c *= 2;
8. }
9. int main ()
10. {
11. int x = 1, y = 3, z = 7;
12. duplicate (x, y, z);
13. cout << x << y << z;
14. return 0;
15. }
a) 1468
b) 2812
c) 2614
d) none of the mentioned
View Answer
Answer: c
Explanation: We are passing the values by reference and modified the data on the function block.
Output:
$ g++ call1.cpp
$ a.out
2614
1. #include
2. using namespace std;
3. class three_d
4. {
5. int x, y, z;
6. public:
7. three_d() { x = y = z = 0; }
8. three_d(int i, int j, int k) { x = i; y = j; z = k; }
9. three_d operator()(three_d obj);
10. three_d operator()(int a, int b, int c);
11. friend ostream &operator<<(ostream &strm, three_d op);
12. };
13. three_d three_d::operator()(three_d obj)
14. {
15. three_d temp;
16. temp.x = (x + obj.x) / 2;
17. temp.y = (y + obj.y) / 2;
18. temp.z = (z + obj.z) / 2;
19. return temp;
20. }
21. three_d three_d::operator()(int a, int b, int c)
22. {
23. three_d temp;
24. temp.x = x + a;
25. temp.y = y + b;
26. temp.z = z + c;
27. return temp;
28. }
29. ostream &operator<<(ostream &strm, three_d op) {
30. strm << op.x << ", " << op.y << ", " << op.z << endl;
31. return strm;
32. }
33. int main()
34. {
35. three_d objA(1, 2, 3), objB(10, 10, 10), objC;
36. objC = objA(objB(100, 200, 300));
37. cout << objC;
38. return 0;
39. }
Answer: a
Explanation: In this program, We are using the function call operator to calculate the value of
objc.
Output:
$ g++ call2.cpp
$ a.out
55, 106, 156
1. #include
2. #include
3. using namespace std;
4. class Complex
5. {
6. private:
7. float real;
8. float imag;
9. public:
10. Complex():real(0), imag(0){}
11. Complex operator ()(float re, float im)
12. {
13. real += re;
14. imag += im;
15. return *this;
16. }
17. Complex operator() (float re)
18. {
19. real += re;
20. return *this;
21. }
22. void display()
23. {
24. cout << "(" << real << "," << imag << ")" << endl;
25. }
26. };
27. int main()
28. {
29. Complex c1, c2;
30. c2 = c1(3.2, 5.3);
31. c1(6.5, 2.7);
32. c2(1.9);
33. cout << "c2=";c1.display();
34. cout << "c2=";c2.display();
35. return 0;
36. }
a) c2=(9.7,8)
c2=(5.1,5.3)
b) c2=(9,8)
c2=(5,5)
c) c2=(4.7,8)
c2=(2.1,5.3)
d) none of the mentioned
View Answer
Answer: a
Explanation: In this program, We are returning the real and imaginary part of the complex
number by using function call operator.
Output:
$ g++ call3.cpp
$ a.out
c2=(9.7,8)
c2=(5.1,5.3)
Answer: b
Explanation: None.
1. #include
2. using namespace std;
3. int operate (int a, int b)
4. {
5. return (a * b);
6. }
7. float operate (float a, float b)
8. {
9. return (a / b);
10. }
11. int main ()
12. {
13. int x = 5, y = 2;
14. float n = 5.0, m = 2.0;
15. cout << operate (x, y);
16. cout << operate (n, m);
17. return 0;
18. }
a) 119
b) 102.5
c) 123.4
d) none of the mentioned
View Answer
Answer: b
Explanation: In this program, We are overloading the function and getting the output as 10 and
2.5 by division and multiplication.
Output:
$ g++ call3.cpp
$ a.out
102.5
Answer: a
Explanation: None.
This section on C question bank focuses on “Dereferencing”. One shall practice these questions
to improve their C++ programming skills needed for various interviews (campus interviews,
walkin interviews, company interviews), placements, entrance exams and other competitive
exams. These questions can be attempted by anyone focusing on learning C++ programming
language. They can be a beginner, fresher, engineering graduate or an experienced IT
professional. Our C question bank comes with detailed explanation of the answers which helps in
better understanding of C++ concepts.
Here is a listing of C question bank on “Dereferencing” along with answers, explanations and/or
solutions:
1. Which is used to tell the computer that where a pointer is pointing to?
a) dereference
b) reference
c) heap operations
d) none of the mentioned
View Answer
Answer: a
Explanation: None.
Answer: c
Explanation: Derefencing is using a pointer with asterix. For example, *(abc).
Answer: a
Explanation: None.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a, b;
6. int* c;
7. c = &a;
8. a = 200;
9. b = 200;
10. *c = 100;
11. b = *c;
12. cout << *c << " " << b;
13. return 0;
14. }
a) 100 200
b) 100 0
c) 200 200
d) 100 100
View Answer
Answer: d
Explanation: In this program, We are making the assignments and invoking the both b and c
values as 100 by dereference operator.
Output:
$ g++ def.cpp
$ a.out
100 100
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int x;
6. int *p;
7. x = 5;
8. p = &x;
9. cout << *p;
10. return 0;
11. }
a) 5
b) 10
c) memory address
d) none of the mentioned
View Answer
Answer: a
Explanation: In this program, we are copying the memory location of x into p and then printing
the value in the address.
Output:
$ g++ def1.cpp
$ a.out
5
1. #include <iostream>
2. using namespace std;
3. int main ()
4. {
5. int a;
6. int * ptr_b;
7. int ** ptr_c;
8. a = 1;
9. ptr_b = &a;
10. ptr_c = &ptr_b;
11. cout << a << "\n";
12. cout << *ptr_b << "\n";
13. cout << *ptr_c << "\n";
14. cout << **ptr_c << "\n";
15. return 0;
16. }
a) 1
1
0xbffc9924
1
b) 1
1
1
0xbffc9924
c) 1
0xbffc9924
1
1
d) none of the mentioned
View Answer
Answer: a
Explanation: In this program, We are printing the values and memory address
by using the pointer and derefernce operator.
Output:
$ g++ def2.cpp
$ a.out
1
1
0xbffc9924
1
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int x = 9;
6. int* p = &x;
7. cout << sizeof(p);
8. return 0;
9. }
a) 4
b) 2
c) Depends on compiler
d) None of the mentioned
View Answer
Answer: c
Explanation: The size of a datatype mainly depends on complier only.
Output:
$ g++ def3.cpp
$ a.out
4
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. double arr[] = {5.0, 6.0, 7.0, 8.0};
6. double *p = (arr+2);
7. cout << *p << endl;
8. cout << arr << endl;
9. cout << *(arr+3) << endl;
10. cout << *(arr) << endl;
11. cout << *arr+9 << endl;
12. return 0;
13. }
a) 7
0xbf99fc98
8
5
14
b) 7
8
0xbf99fc98
5
14
c) 0xbf99fc98
d) none of the mentioned
View Answer
Answer: a
Explanation: In this program, We are printing the values that are pointed by pointer and also the
dereference oerator.
Output:
$ g++ def5.cpp
$ a.out
7
0xbf99fc98
8
5
14
Answer: b
Explanation: It operates on a pointer variable, and returns an l-value equivalent to the value at
the pointer address.
Answer: a
Explanation: None.
Here is a listing of C++ programming questions on “Increment and Decrement” along with answers,
explanations and/or solutions:
Answer: c
Explanation: None.
2. How many types are there in increment/decrement operator?
a) 1
b) 2
c) 3
d) 4
View Answer
Answer: b
Explanation: There are two types of increment/decrement. They are postfix and prefix.
Answer: a
Explanation: None.
#include <stdio.h>
int main()
int a = 21;
int c ;
c = a++;
cout << c;
return 0;
a) 21
b) 22
c) 23
d) 20
View Answer
Answer: a
Explanation: value of ‘a’ will be stored in c and then only it will be incremented.
Output:
$ g++ incre.cpp
$ a.out
21
#include <stdio.h>
int main()
int x = 5, y = 5;
return 0;
a) 55
b) 64
c) 46
d) 45
View Answer
Answer: b
Explanation: The values will be preincemented and predecremented, So it will print as 64.
Output:
$ g++ incre2.cpp
$ a.out
64
#include <stdio.h>
int main()
int x = 5, y = 5, z;
x = ++x; y = --y;
z = x++ + y--;
cout << z;
return 0;
a) 10
b) 11
c) 9
d) 12
View Answer
Answer: a
Explanation: In this program, the increment and decrement of evaluation of z will not be accounted
because they are post.
Output:
$ g++ incre3.cpp
$ a.out
10
#include <stdio.h>
int main()
int x = 5, y = 5, z;
x = ++x; y = --y;
z = x + ++x;
cout << z;
return 0;
a) 11
b) 12
c) 13
d) 14
View Answer
Answer: d
Explanation: In this program, we are adding the x value after preincrementing two times.
Output:
$ g++ incre4.cpp
$ a.out
14
#include <stdio.h>
int main()
int num1 = 5;
int num2 = 3;
int num3 = 2;
num1 = num2++;
num2 = --num3;
return 0;
a) 532
b) 235
c) 312
d) 311
View Answer
Answer: d
Explanation: In this program, We are preincrementing and postincrementing the operands and saving it.
Output:
$ g++ incre5.cpp
$ a.out
311
Answer: b
Explanation: None.
Here is a listing of online C++ quiz on “String Class” along with answers, explanations and/or solutions:
Answer: b
Explanation: C++ provides following two types of string representations. They are C-style character
string and string class type with Standard C++.
a) #include<ios>
b) #include<str>
c) #include<string>
View Answer
Answer: c
Explanation: None.
Answer: c
Explanation: Both will return the number of characters that conform the string’s content.
#include <iostream>
#include <cstring>
int main ()
char str3[10];
int len ;
len = strlen(str1);
return 0;
a) 5
b) 55
c) 11
d) 10
View Answer
Answer: d
Explanation: In the program, We are concatanating the str1 and str2 and printing
it’s total length. So the length is 10.
Output:
$ g++ stri.cpp
$ a.out
10
#include <iostream>
#include <string>
int main ()
string::reverse_iterator r;
return 0;
a) microsoft
b) micro
c) tfosorcim
d) tfos
View Answer
Answer: c
Explanation: ‘rbegin’ is used to reverse the given the string.
Output:
$ g++ stri1.cpp
$ a.out
tfosorcim
#include <string>
int main ()
size_t f;
f = str.rfind(key);
if (f != string::npos)
return 0;
Answer: d
Explanation: rfind is used to find the characters in the string and replace is used to replace with certain
characters.
Output:
$ g++ stri2.cpp
$ a.out
everbody does like this
#include <iostream>
#include <string>
int main ()
{
string str ("steve jobs is legend");
string::iterator it;
return 0;
a) jobs is
b) steve legend
c) steve
d) none of the mentioned
View Answer
Answer: b
Explanation: In this program, We are leaving the first 5 characters and last 7 characters and we are
erasing the remaining the characters.
Output:
$ g++ stri3.cpp
$ a.out
steve legend
#include <iostream>
#include <string>
int main ()
return 0;
}
a) M
b) Microsoft
c) Micro
d) runtime error
View Answer
Answer: d
Explanation: This program will terminate because the cout element is out of range.
#include <iostream>
#include <string>
int main ()
return 0;
a) 61073741820
b) 51073741820
c) 6 and max size depends on compiler
d) none of the mentioned
View Answer
Answer: c
Explanation: In this program, We are printing the capacity and max size of the string.
Output:
$ g++ stri5.cpp
$ a.out
61073741820
10. Which method do we use to append more than one character at a time?
a) append
b) operator+=
c) data
d) both append & operator+=
View Answer
Answer: d
Explanation: None.