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. Where is the derived class is derived from?
a) derived
b) base
c) both derived & base
d) None of the mentioned

View Answer

Answer: b [Reason:] Because derived inherits functions and variables from base.

2. Pick out the correct statement.
a) A derived class’s constructor cannot explicitly invokes its base class’s constructor
b) A derived class’s destructor cannot invoke its base class’s destructor
c) A derived class’s destructor can invoke its base class’s destructor
d) None of the mentioned

View Answer

Answer: b [Reason:] Destructors are automatically invoked when a object goes out of scope or when a dynamically allocated object is deleted. Inheritance does not change this behavior. This is the reason a derived destructor cannot invoke its base class destructor.

3. Which of the following can derived class inherit?
a) members
b) functions
c) both members & functions
d) none of the mentioned

View Answer

Answer: c

4. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class A
  4.     {
  5.         public:
  6.         A(int n )
  7.         {
  8.             cout << n;
  9.         }
  10.     };
  11.     class B: public A
  12.     {
  13.         public:
  14.         B(int n, double d)
  15.         : A(n)
  16.         {
  17.             cout << d;
  18.         }    
  19.     };
  20.     class C: public B
  21.     {
  22.         public:
  23.         C(int n, double d, char ch)
  24.         : B(n, d)
  25.         {
  26.             cout <<ch;
  27.         }
  28.     };
  29.     int main()
  30.     {
  31.         C c(5, 4.3, 'R');
  32.         return 0;
  33.     }

a) 54.3R
b) R4.35
c) 4.3R5
d) None of the mentioned

View Answer

Answer: a [Reason:] In this program, We are passing the value and manipulating by using the derived class. Output: $ g++ der.cpp $ a.out 54.3R

5. What is the output of this program?

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

a) 3 4
b) 4 3
c) 4
d) 3

View Answer

Answer: b [Reason:] In this program, We are passing the values and assigning it to i and j and we are printing it. Output: $ g++ der1.cpp $ a.out 4 3

6. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class Base
  4.     {
  5.         public:
  6.         int m;
  7.         Base(int n=0)
  8.         : m(n)
  9.         {
  10.             cout << "Base" << endl;
  11.         }
  12.     };
  13.     class Derived: public Base
  14.     {
  15.         public:
  16.         double d;
  17.         Derived(double de = 0.0)
  18.         : d(de)
  19.         {
  20.             cout << "Derived" << endl;
  21.         }
  22.     };
  23.     int main()
  24.     {
  25.         cout << "Instantiating Base" << endl;
  26.         Base cBase;
  27.         cout << "Instantiating Derived" << endl;
  28.         Derived cDerived;
  29.         return 0;
  30.     }

a) Instantiating Base
Base
Instantiating Derived
Base
Derived
b) Instantiating Base
Instantiating Derived
Base
Derived
c) Instantiating Base
Base
Instantiating Derived
Base
d) None of the mentioned

View Answer

Answer: a [Reason:] In this program, We are printing the execution order of the program. Output: $ g++ der2.cpp $ a.out Instantiating Base Base Instantiating Derived Base Derived

7. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class Parent
  4.     {
  5.         public:
  6.         Parent (void) 
  7.         {     
  8.             cout << "Parent()n";
  9.         }
  10.         Parent (int i) 
  11.         { 
  12.             cout << "Parent("<< i << ")n"; 
  13.         };
  14.         Parent (void) 
  15.         { 
  16.             cout << "~Parent()n";
  17.         }; 
  18.     };
  19.     class Child1 : public Parent { };
  20.     class Child2 : public Parent
  21.     {
  22.         public:
  23.         Child2 (void) 
  24.         {
  25.             cout << "Child2()n";
  26.         }
  27.         Child2 (int i) : Parent (i) 
  28.         {
  29.             cout << "Child2(" << i << ")n"; 
  30.         }
  31.         ~Child2 (void) 
  32.         {
  33.             cout << "~Child2()n"; 
  34.         }
  35.     };
  36.     int main (void)
  37.     {
  38.         Child1 a;
  39.         Child2 b;
  40.         Child2 c(42);
  41.         return 0;
  42.     }

a) Parent()
Parent()
Child2()
Parent(42)
Child2(42)
~Child2()
~Parent()
~Child2()
~Parent()
~Parent()
b) error
c) runtime error
d) none of the mentioned

View Answer

Answer: b [Reason:] In this program, We got an error in overloading because we didn’t invoke the destructor of parent.

8. What is the output of this program?

  1.     #include<iostream>
  2.     using namespace std;
  3.     class X 
  4.     {
  5.         int m;
  6.         public:
  7.         X() : m(10)
  8.         {                                                       
  9.         }
  10.         X(int mm): m(mm)
  11.         {
  12.         }
  13.         int getm()
  14.         {
  15.             return m;
  16.         }
  17.     };
  18.     class Y : public X 
  19.     {
  20.         int n;
  21.         public:
  22.         Y(int nn) : n(nn) {}                                                
  23.         int getn() { return n; }
  24.     };
  25.     int main()
  26.     {
  27.         Y yobj( 100 );
  28.         cout << yobj.getm() << " " << yobj.getn() << endl;
  29.     }

a) 10 100
b) 100 10
c) 10 10
d) 100 100

View Answer

Answer: a [Reason:] In this program, We are passing the value and getting the result by derived class. Output: $ g++ der5.cpp $ a.out 10 100

9. Which operator is used to declare the destructor?
a) #
b) ~
c) @
d) $

View Answer

Answer: b

10. Which constructor will initialize the base class data member?
a) derived class
b) base class
c) class
d) none of the mentioned

View Answer

Answer: b [Reason:] Because it is having the proper data set to initialize, Otherwise it will throw a error.

Set 2

1. Void pointer can point to which type of objects?
a) int
b) float
c) double
d) all of the mentioned

View Answer

Answer: d [Reason:] Because it doesn’t know the type of object it is pointing to, So it can point to all objects.

2. When does the void pointer can be dereferenced?
a) when it doesn’t point to any value
b) when it cast to another type of object
c) using delete keyword
d) none of the mentioned

View Answer

Answer: b [Reason:] By casting the pointer to another data type, it can dereferenced from void pointer.

3. The pointer can point to any variable that is not declared with which of these?
a) const
b) volatile
c) both const & volatile
d) static

View Answer

Answer: c

4. A void pointer cannot point to which of these?
a) methods in c++
b) class member in c++
c) all of the mentioned
d) none of the mentioned

View Answer

Answer: d

5. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int func(void *Ptr);
  4.     int main()
  5.     {
  6.         char *Str = "abcdefghij";
  7.         func(Str);
  8.         return 0;
  9.     }
  10.     int func(void *Ptr)
  11.     {
  12.         cout << Ptr;
  13.         return 0;
  14.     }

a) abcdefghij
b) address of string “abcdefghij”
c) compile time error
d) runtime error

View Answer

Answer: b [Reason:] Even though it is a void pointer, we gets the address. Output: $ g++ b.cpp $ a.out 0x8048714

6. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int *p;
  6.         void *vp;
  7.         if (vp == p);
  8.             cout << "equal";
  9.         return 0;
  10.     }

a) equal
b) no output
c) compile error
d) runtime error

View Answer

Answer: a [Reason:] The void pointer is easily converted to any other type of pointer, so these are equal. Output: $ g++ poi4.cpp $ a.out equal

7. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int i;
  6.         char c;
  7.         void *data;
  8.         i = 2;
  9.         c = 'd';
  10.         data = &i;
  11.         cout << "the data points to the integer value" << data;
  12.         data = &c;
  13.         cout << "the data now points to the character" << data;
  14.         return 0;
  15.     }

a) 2d
b) two memory addresses
c) both of the mentioned
d) none of the mentioned

View Answer

Answer: b [Reason:] Because the data points to the address value of the variables only, So it is printing the memory address of these two variable. Output: $ g++ poi2.cpp $ a.out the data points to the integer value0xbfc81824 the data now points to the character0xbfc8182f

8. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int n = 5;
  6.         void *p = &n;
  7.         int *pi = static_cast<int*>(p);
  8.         cout << *pi << endl;
  9.         return 0;
  10.     }

a) 5
b) 6
c) compile time error
d) runtime error

View Answer

Answer: a [Reason:] We just casted this from void to int, so it prints 5 Output: $ g++ poi1.cpp $ a.out 5

9. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int a = 5, c;
  6.         void *p = &a;
  7.         double b = 3.14;
  8.         p = &b;
  9.         c = a + b;
  10.         cout << c << 'n' << p;
  11.         return 0;
  12.     }

a) 8, memory address
b) 8.14
c) memory address
d) none of the mentioned

View Answer

Answer: a [Reason:] In this program, we are just adding the two values and printing it. Output: $ g++ poi.cpp $ a.out 8 0xbfef0378

10. What we can’t do on a void pointer?
a) pointer arithemetic
b) pointer functions
c) both of the mentioned
d) none of the mentioned

View Answer

Answer: a [Reason:] Because void pointer is used to cast the variables only, So pointer arithemetic can’t be done in a void pointer.

Set 3

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

2. Which is used to do the dereferencing?
a) pointer without asterix
b) value without asterix
c) pointer with asterix
d) value with asterix

View Answer

Answer: c [Reason:] Derefencing is using a pointer with asterix. For example, *(abc).

3. Pick out the correct option.
a) References automatically dereference without needing an extra character
b) References automatically dereference with an extra character
c) Reference will not dereference
d) None of the mentioned

View Answer

Answer: a

4. What is the output of this program?

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

5. What is the output of this program?

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

6. What is the output of this program?

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

7. What is the output of this program?

  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 [Reason:] The size of a datatype mainly depends on complier only. Output: $ g++ def3.cpp $ a.out 4

8. What is the output of this program?

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

9. What does the dereference operator will return?
a) rvalue equivalent to the value at the pointer address
b) lvalue equivalent to the value at the pointer address
c) it will return nothing
d) none of the mentioned

View Answer

Answer: b [Reason:] It operates on a pointer variable, and returns an l-value equivalent to the value at the pointer address.

10. Pick out the correct statement.
a) The NULL pointer dereference occurs where a pointer that is expected to be a valid address but instead is equal to NULL
b) The NULL pointer dereference occurs where a pointer that is expected to be a valid address but instead is equal to memory address
c) All of the mentioned
d) None of the mentioned

View Answer

Answer: a

Set 4

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

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

3. Which container in c++ will take large objects?
a) string
b) class
c) vector
d) none of the mentioned

View Answer

Answer: c [Reason:] Because vector is mainly used to store large objects for game programming and other operations etc.

4. What is the output of this program?

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

a) X::operator=(X const &)
b) X::X( X const & )
c) X::X()
d) None of the mentioned

View Answer

Answer: c [Reason:] As we are passing the object without any attributes it will return as X::X(). Output: $ g++ large.cpp $ a.out X::X()

5. How to stop your program from eating so much ram?
a) Find a way to work with the data one at a time
b) Declare it in program memory, instead of on the stack
c) Use the hard drive, instead of RAM
d) All of the mentioned

View Answer

Answer: d

6. Which option is best to eliminate the memory problem?
a) use smart pointers
b) use raw pointers
c) use virtual destructor
d) use smart pointers & virtual destructor

View Answer

Answer: d [Reason:] Virtual destructor means is that the object is destructed in reverse order in which it was constructed and the smart pointer will delete the object from memory when the object goes out of scope.

7. What is the size of the heap?
a) 10MB
b) 500MB
c) 1GB
d) Size of the heap memory is limited by the size of the RAM and the swap memory

View Answer

Answer: d

8. How to unlimit the size of the stack?
a) setrlimit()
b) unlimit()
c) both setrlimit() & unlimit()
d) none of the mentioned

View Answer

Answer: a

9. In linux, how does the heaps and stacks are managed?
a) ram
b) secondary memory
c) virtual memory
d) none of the mentioned

View Answer

Answer: c [Reason:] In virtual memory, We can keep track of all the objects and access them much faster than any another.

10. Which is used to pass the large objects in c++?
a) pass by value
b) pass by reference
c) both pass by value & reference
d) none of the mentioned

View Answer

Answer: b [Reason:] Because by using pass by reference we need to pass only address location, So it can save alot of memory.

Set 5

1. Where does the object is created?
a) class
b) constructor
c) destructor
d) attributes

View Answer

Answer: a [Reason:] In class only all the listed items except class will be declared.

2. How to access the object in the class?
a) scope resolution operator
b) ternary operator
c) direct member access operator
d) none of the mentioned

View Answer

Answer: c [Reason:] 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 [Reason:] Because of the access given to the private and protected, We can’t access them by using direct member access operator.

4. What is the output of the following program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class Box
  4.     {
  5.         public :
  6.         double length;
  7.         double breadth;
  8.         double height;
  9.     };
  10.     int main( )
  11.     {
  12.         Box Box1;
  13.         double volume;
  14.         Box1.height = 5;
  15.         Box1.length = 6;
  16.         Box1.breadth = 7.1;
  17.         volume = Box1.height * Box1.length * Box1.breadth;
  18.         cout << "Volume of Box1 : " << volume <<endl;
  19.         return 0;
  20.     }

a) 210
b) 213
c) 215
d) 217

View Answer

Answer: b [Reason:] In the above program, we are calculating the area of the cube by using the cube formula Output: $ g++ obj1.cpp $ a.out 213

5. What is the output of the program?

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

a) recta area: 30 rectb area: 42
b) recta area: 20 rectb area: 34
c) recta area: 30 rectb area: 21
d) none of the mentioned

View Answer

Answer: a [Reason:] We are calculating the area of rectangle by two objects.

6. Pick out the other definition of objects.
a) member of the class
b) associate of the class
c) attribute of the class
d) instance of the class

View Answer

Answer: d

7. How many objects can present in a single class?
a) 1
b) 2
c) 3
d) as many as possible

View Answer

Answer: d [Reason:] Because a class may contain any number of objects according to it’s compliance.

8. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class sample
  4.     {
  5.         private:
  6.         int var;
  7.         public:
  8.         void input()
  9.         {
  10.            cout << var;
  11.         }
  12.         void output()
  13.         {
  14.            cout << "Variable entered is ";
  15.            cout << var << "n";
  16.         }
  17.     };
  18.     int main()
  19.     {
  20.         sample object;
  21.         object.input();
  22.         object.output();
  23.         object.var();
  24.         return 0;
  25.     }

a) Enter an integer 5
Variable entered is 5
b) Runtime error
c) Error
d) None of the mentioned

View Answer

Answer: c [Reason:] While using private member, you can’t access it variable.

9. Which special character is used to mark the end of class?
a) ;
b) :
c) #
d) $

View Answer

Answer: a

10. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class number
  4.     {
  5.         int i;
  6.         public:
  7.         int geti();
  8.         void puti(int j);
  9.     };
  10.     int number::geti()
  11.     {
  12.         return i;
  13.     }
  14.     void number::puti(int j)
  15.     {
  16.         i = j;
  17.     }
  18.     int main()
  19.     {
  20.         number s;
  21.         s.puti(10);
  22.         cout << s.geti( );
  23.         return 0;
  24.     }

a) 10
b) 11
c) 20
d) 22

View Answer

Answer: a [Reason:] We are getting the number and copying it to j and printing it. Output: $ g++ obj2.cpp $ a.out 10