250+ TOP MCQs on Objects and Answers

C++ quiz on “Objects” along with answers, explanations and/or solutions:

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

Answer: a
Clarification: 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) resolution operator

Answer: c
Clarification: 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

Answer: d
Clarification: Because of the access is given to the private and protected, We can’t access them by using direct member access operator.

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

  1.     #include 
  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

Answer: b
Clarification: 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 will be the output of the following C++ code?

  1.     #include 
  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) recta area: 30 rectb area: 33

Answer: a
Clarification: 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

Answer: d
Clarification: An Object represents an instance of a class i.e. a variable of that class type having access to its data members and member functions from outside if allowed.

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

Answer: d
Clarification: Because a class may contain any number of objects according to its compliance.

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

  1.     #include 
  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)

Enter an integer 7
Variable entered is 7

View Answer

Answer: c
Clarification: There is no member function var() in the class hence the program will through an error stating var is a private data member and it cannot be used as a function.

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

Answer: a
Clarification: Similar to ending any statement, a class is also terminated with semicolon(;).

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

  1.     #include 
  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

Answer: a
Clarification: We are getting the number and copying it to j and printing it.
Output:

$ g++ obj2.cpp
$ a.out
10

Leave a Reply

Your email address will not be published. Required fields are marked *