Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
Filter by Categories
nmims post
Objective Type Set
Online MCQ Assignment
Question Solution
Solved Question
Uncategorized

Multiple choice question for engineering

Set 1

1. What does your class can hold?
a) data
b) functions
c) both data & functions
d) none of the mentioned

View Answer

Answer: C [Reason:] 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 [Reason:] There are three types of access specifiers. They are public, protected and private.

3. Which is used to define the member of a class externally?
a) :
b) ::
c) #
d) none of the mentioned

View Answer

Answer: b

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 [Reason:] Struct and union take the same definition of class but differs in the access techniques.

5. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class rect
  4.     {
  5.         int x, y;
  6.         public:
  7.         void val (int, int);
  8.         int area ()
  9.         {
  10.             return (x * y);
  11.         }
  12.     };
  13.     void rect::val (int a, int b)
  14.     {
  15.         x = a;
  16.         y = b;
  17.     }
  18.     int main ()
  19.     {
  20.         rect rect;
  21.         rect.val (3, 4);
  22.         cout << "rect area: " << rect.area();
  23.         return 0;
  24.     }

a) rect area:12
b) rect area: 12
c) rect area:24
d) none of the mentioned

View Answer

Answer: b [Reason:] In this program, we are calculating the area of rectangle based on given values. Output: $ g++ class.cpp $ a.out rect area: 12

6. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class CDummy
  4.     {
  5.         public:
  6.         int isitme (CDummy& param);
  7.     };
  8.     int CDummy::isitme (CDummy& param)
  9.     {
  10.         if (&param == this)
  11.             return true;
  12.         else
  13.             return false;
  14.     }
  15.     int main ()
  16.     {
  17.         CDummy a;
  18.         CDummy *b = &a;
  19.         if (b->isitme(a)) 
  20.         {
  21.             cout << "execute";
  22.         }
  23.         else
  24.         {
  25.             cout<<"not execute";
  26.         }
  27.         return 0;
  28.     }

a) execute
b) not execute
c) none of the mentioned
d) both execute & not execute

View Answer

Answer: a [Reason:] 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

7. Which of the following is a valid class declaration?
a) class A { int x; };
b) class B { }
c) public class A { }
d) object A { int x; };

View Answer

Answer: a

8. The fields in the class in c++ program are by default
a) protected
b) private
c) public
d) none of the mentioned

View Answer

Answer: b

9. Constructors are used to
a) initalize the objects
b) construct the data members
c) both initalize the objects & construct the data members
d) none of the mentioned

View Answer

Answer: a [Reason:] 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

Set 2

1. What is the return type of the conversion operator?
a) void
b) int
c) float
d) no return type

View Answer

Answer: d [Reason:] Conversion operator doesn’t have any return type not even void.

2. Why we use the “dynamic_cast” type conversion?
a) result of the type conversion is a valid
b) to be used in low memory
c) result of the type conversion is a invalid
d) none of the mentioned

View Answer

Answer: a [Reason:] It is used to check that operators and operands are compatible after conversion.

3. How many parameters does a conversion operator may take?
a) 0
b) 1
c) 2
d) as many as possible

View Answer

Answer: a

4. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class sample1 
  4.     {
  5.         float i, j;
  6.     };
  7.     class sample2 
  8.     {
  9.         int x, y;
  10.         public:
  11.         sample2 (int a, int b) 
  12.         {
  13.              x = a; 
  14.              y = b;
  15.         }
  16.         int result() 
  17.         { 
  18.              return x + y;
  19.          }
  20.     };
  21.     int main () 
  22.     {
  23.         sample1 d;
  24.         sample2 * padd;
  25.         padd = (sample2*) &d;
  26.         cout < result();
  27.         return 0;
  28.     }

a) 20
b) runtime error
c) random number
d) runtime error or random number

View Answer

Answer: d [Reason:] As it assigns to a reference to an object of another incompatible type using explicit type-casting. Output: $ g++ con.cpp $ a.out 14032334

5. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class sample
  4.     {
  5.         public:
  6.         sample(int i) : m_i(i) { }
  7.         public:
  8.         int operator()(int i = 0) const 
  9.         { 
  10.             return m_i + i; 
  11.         }
  12.         operator int () const   
  13.         { 
  14.             return m_i; 
  15.         }
  16.         private:
  17.         int m_i;
  18.         friend int g(const sample&);
  19.     };
  20.     int f(char c)
  21.     {
  22.         return c;
  23.     }
  24.     int main()
  25.     {
  26.         sample f(2);
  27.         cout << f(2);
  28.         return 0;
  29.     }

a) 3
b) 4
c) 5
d) None of the mentioned

View Answer

Answer: b [Reason:] 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

6. What is the output of this program?

  1.     #include <iostream>
  2.     #include <cmath>
  3.     using namespace std;
  4.     class Complex
  5.     {
  6.         private:
  7.         double real;
  8.         double imag;
  9.         public:
  10.         Complex(double r = 0.0, double i = 0.0) : real(r), imag(i)
  11.         {}
  12.         double mag()
  13.         {  
  14.             return getMag();
  15.         }
  16.         operator double ()
  17.         {
  18.             return getMag();
  19.         }
  20.         private:
  21.         double getMag()
  22.         {
  23.             return sqrt(real * real + imag * imag);
  24.         }
  25.     };
  26.     int main()
  27.     {
  28.         Complex com(3.0, 4.0);
  29.         cout << com.mag();
  30.         cout << com;
  31.         return 0
  32.     }

a) 5 5
b) 4 5
c) 6 6
d) None of the mentioned

View Answer

Answer: a [Reason:] In this program, we are calculating the magnitude value by two ways. Output: $ g++ con3.cpp $ a.out 55

7. What is the output of this program?

  1.     #include <iostream>
  2.     #include <string>
  3.     using namespace std;
  4.     class test
  5.     {
  6.         public:
  7.         operator string () 
  8.         {  
  9.             return "Converted";
  10.         }
  11.     };
  12.     int main()
  13.     {
  14.         test t;
  15.         string s = t;
  16.         cout << s << endl;
  17.         return 0;
  18.     }

a) converted
b) error
c) run time error
d) none of the mentioned

View Answer

Answer: a [Reason:] In this program, We casted the string to the object of the class. Output: $ g++ con4.cppp $ a.out converted

8. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         double a = 21.09399;
  6.         float b = 10.20;
  7.         int c ;
  8.         c = (int) a;
  9.         cout << c ;
  10.         c = (int) b;
  11.         cout << c ;
  12.         return 0;
  13.     }

a) 2110
b) 1210
c) 21
d) None of the mentioned

View Answer

Answer: a [Reason:] In this program, we casted the data type to integer. Output: $ g++ con5.cpp $ a.out 2110

9. How types are there in user defined conversion?
a) 1
b) 2
c) 3
d) 4

View Answer

Answer: b [Reason:] There are two types of user-defined conversions.They are conversion by constructor, Conversion functions.

10. Pick out the correct syntax of operator conversion.
a) operator float()const
b) operator float()
c) operator const
d) none of the mentioned

View Answer

Answer: a

Set 3

Here is a listing of online C++ quiz on “Design of Class Hierarchies” along with answers, explanations and/or solutions:

1. Which interface determines how your class will be used by other program?
a) public
b) private
c) protected
d) none of the mentioned

View Answer

Answer: a [Reason:] If we invoked the interface as public means, We can access the program from other programs also.

2. Pick out the correct statement about override.
a) Overriding refers to a derived class function that has the same name and signature as a base class virtual function
b) Overriding has different names
c) Overriding refers to a derived class
d) None of the mentioned

View Answer

Answer: a

3. How many ways of reusing are there in class hierarchy?
a) 1
b) 2
c) 3
d) 4

View Answer

Answer: b [Reason:] Class hierarchies promote reuse in two ways. They are code sharing and interface sharing.

4. How many types of class are there in c++?
a) 1
b) 2
c) 3
d) 4

View Answer

Answer: c [Reason:] There are three types of classes. They are abstract base classes, concrete derived classes, standalone classes.

5. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class BaseClass 
  4.     {
  5.         int i;
  6.         public:
  7.         void setInt(int n);
  8.         int getInt();
  9.     };
  10.     class DerivedClass : public BaseClass
  11.     {
  12.         int j;
  13.         public:
  14.         void setJ(int n);
  15.         int mul();
  16.     };
  17.     void BaseClass::setInt(int n)
  18.     {
  19.         i = n;
  20.     }
  21.     int BaseClass::getInt()
  22.     {
  23.         return i;
  24.     }
  25.     void DerivedClass::setJ(int n)
  26.     {
  27.         j = n;
  28.     }
  29.     int DerivedClass::mul()
  30.     {
  31.         return j * getInt();
  32.     }
  33.     int main()
  34.     {
  35.         DerivedClass ob;
  36.         ob.setInt(10);       
  37.         ob.setJ(4);          
  38.         cout << ob.mul();    
  39.         return 0;
  40.     }

a) 10
b) 4
c) 40
d) None of the mentioned

View Answer

Answer: c [Reason:] In this program, We are multiplying the value 10 and 4 by using inheritance. Output: $ g++ des.cpp $ a.out 40

6. Pick out the correct statement about multiple inheritance.
a) Deriving a class from one direct base class
b) Deriving a class from more than one direct base class
c) Deriving a class from more than one direct derived class
d) None of the mentioned

View Answer

Answer: b [Reason:] In multiple inheritance, We are able to derive a class from more than one base class.

7. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class BaseClass 
  4.     {
  5.         int x;
  6.         public:
  7.         void setx(int n) 
  8.         {
  9.             x = n;
  10.         }
  11.         void showx() 
  12.         {
  13.             cout << x ;
  14.         }
  15.     };
  16.     class DerivedClass : private BaseClass
  17.     {
  18.         int y;
  19.         public:
  20.         void setxy(int n, int m)
  21.         {
  22.             setx(n);      
  23.             y = m;
  24.         }
  25.         void showxy() 
  26.         {
  27.             showx();       
  28.             cout << y << 'n';
  29.         }
  30.     };
  31.     int main()
  32.     {
  33.         DerivedClass ob;
  34.         ob.setxy(10, 20);
  35.         ob.showxy();
  36.         return 0;
  37.     }

a) 10
b) 20
c) 1020
d) None of the mentioned

View Answer

Answer: c [Reason:] In this program, We are passing the values from the main class and printing it on the inherited classes. Output: $ g++ des2.cpp $ a.out 1020

8. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class BaseClass 
  4.     {
  5.         public:
  6.         virtual void myFunction()
  7.         {
  8.             cout << "1";
  9.         }
  10.     };
  11.     class DerivedClass1 : public BaseClass 
  12.     {
  13.         public:
  14.         void myFunction()
  15.         {
  16.             cout << "2";
  17.         }
  18.     };
  19.    class DerivedClass2 : public DerivedClass1 
  20.     {
  21.         public:
  22.         void myFunction()
  23.         {
  24.             cout << "3";
  25.         }
  26.     };
  27.     int main()
  28.     {
  29.         BaseClass *p;
  30.         BaseClass ob;
  31.         DerivedClass1 derivedObject1;
  32.         DerivedClass2 derivedObject2;
  33.         p = &ob;
  34.         p -> myFunction();
  35.         p = &derivedObject1;
  36.         p -> myFunction();
  37.         p = &derivedObject2;
  38.         p -> myFunction();
  39.         return 0;
  40.     }

a) 123
b) 12
c) 213
d) 321

View Answer

Answer: a [Reason:] We are passing the objects and executing them in a certain order and we are printing the program flow. Output: $ g++ des3.cpp $ a.out 123

9. What does inheriatance allows you to do?
a) create a class
b) create a hierarchy of classes
c) access methods
d) none of the mentioned

View Answer

Answer: b

10. What is the syntax of inheritance of class?
a) class name
b) class name : access specifer
c) class name : access specifer class name
d) none of the mentioned

View Answer

Answer: c

Set 4

1. What is the use of function call operator?
a) overloading the methods
b) overloading the objects
c) overloading the parameters
d) none of the mentioned

View Answer

Answer: b

2. Pick out the correct statement.
a) virtual functions does not give the ability to write a templated function
b) virtual functions does not give the ability to rewrite a templated function
c) virtual functions does give the ability to write a templated function
d) none of the mentioned

View Answer

Answer: a

3. What will happen when the function call operator is overloaded?
a) It will not modify the functions
b) It will modify the functions
c) It will modify the object
d) It will modify the operator to be interpreted

View Answer

Answer: d [Reason:] It will modifies how the operator is to be interpreted when applied to objects of a given type.

4. What is the output of this program?

  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.     }

a) First Distance : 1110
Second Distance :30120
b) First Distance : 110
Second Distance :3020
c) First Distance : 1115
Second Distance :30125
d) none of the mentioned

View Answer

Answer: a [Reason:] 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

5. What is the output of this program?

  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 [Reason:] We are passing the values by reference and modified the data on the function block. Output: $ g++ call1.cpp $ a.out 2614

6. What is the output of this program?

  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.     }

a) 55, 106, 156
b) 55, 106
c) 55, 106, 159
d) none of the mentioned

View Answer

Answer: a [Reason:] 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

7. What is the output of this program?

  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 [Reason:] 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)

8. In which form does the function call operator can be overloaded?
a) static member function
b) non-static member function
c) dynamis_cast
d) static_cast

View Answer

Answer: b

9. What is the output of this program?

  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 [Reason:] 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

10. What is the use of functor?
a) It makes the object “callable” like a function
b) It makes the class “callable” like a function
c) It makes the attribute “callable” like a function
d) None of the mentioned

View Answer

Answer: a

Set 5

1. Which operator works only with integer variables?
a) increment
b) decrement
c) both increment & decrement
d) none of the mentioned

View Answer

Answer: c

2. How many types are there in increment/decrement operator?
a) 1
b) 2
c) 3
d) 4

View Answer

Answer: b [Reason:] There are two types of increment/decrement. They are postfix and prefix.

3. Pick out the correct statement.
a) Increment operator ++ adds 1 to its operand
b) Increment operator ++ adds 2 to its operand
c) Decrement operator ++ subtracts 1 to its operand
d) None of the mentioned

View Answer

Answer: a

4. What is the output of this program?

  1.     #include <stdio.h> 
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int a = 21;
  6.         int c ;
  7.         c = a++;
  8.         cout << c;  
  9.         return 0;
  10.     }

a) 21
b) 22
c) 23
d) 20

View Answer

Answer: a [Reason:] value of ‘a’ will be stored in c and then only it will be incremented. Output: $ g++ incre.cpp $ a.out 21

5. What is the output of this program?

  1.     #include <stdio.h> 
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int x = 5, y = 5;
  6.         cout << ++x << --y << endl;
  7.         return 0;
  8.     }

a) 55
b) 64
c) 46
d) 45

View Answer

Answer: b [Reason:] The values will be preincemented and predecremented, So it will print as 64. Output: $ g++ incre2.cpp $ a.out 64

6. What is the output of this program?

  1.     #include <stdio.h> 
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int x = 5, y = 5, z;
  6.         x = ++x; y = --y;
  7.         z = x++ + y--;
  8.         cout << z;
  9.         return 0;
  10.     }

a) 10
b) 11
c) 9
d) 12

View Answer

Answer: a [Reason:] 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

7. What is the output of this program?

  1.     #include <stdio.h>  
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int x = 5, y = 5, z;
  6.         x = ++x; y = --y;
  7.         z = x + ++x;
  8.         cout << z;
  9.         return 0;
  10.     }

a) 11
b) 12
c) 13
d) 14

View Answer

Answer: d [Reason:] In this program, we are adding the x value after preincrementing two times. Output: $ g++ incre4.cpp $ a.out 14

8. What is the output of this program?

  1.     #include <stdio.h> 
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int num1 = 5;
  6.         int num2 = 3;
  7.         int num3 = 2;
  8.         num1 = num2++;
  9.         num2 = --num3;
  10.         cout << num1 << num2 << num3;
  11.         return 0;
  12.     }

a) 532
b) 235
c) 312
d) 311

View Answer

Answer: d [Reason:] In this program, We are preincrementing and postincrementing the operands and saving it. Output: $ g++ incre5.cpp $ a.out 311

9. Pick out the correct statement
a) Preincrement is faster than postincrement
b) postincrement is faster than preincrement
c) pretincrement is slower than postincrement
d) none of the mentioned

View Answer

Answer: a [Reason:] Because preincrement take one byte instruction & post increment takes two byte instruction.

10. Which concepts does the preincrement uses?
a) call by value
b) call by reference
c) queue
d) none of the mentioned

View Answer

Answer: b