C++ Programming Questions and Answers – Objects

C++ Multiple Choice Questions focuses on “Objects”. One shall practice these questions to improve their C++ programming skills needed for various 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++ questions comes with the detailed explanation of the answers which helps in better understanding of C++ concepts.

C++ Questions & Answers focuses on “Objects” along with answers, explanations and/or solutions:

Q 1. Where does the object is created?
A. class
B. constructor
C. destructor
D. attributes

Show Answer Answer:-A. class
Explanation In class, only all the listed items except class will be declared.

Q 2. How to access the object in the class?
A. direct member access operator
B. ternary operator
C. scope resolution operator
D. resolution operator

Show Answer Answer:-A. direct member access operator
Explanation Objects in the method can be accessed using direct member access operator which is (.).

Q 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

Show Answer Answer:-D. both private & protected
Explanation Because of the access is given to the private and protected, We can’t access them by using direct member access operator.

Q 4. Which special character is used to mark the end of class?
A. ;
B. :
C. #
D. $

Show Answer Answer:-A. ;
Explanation Similar to ending any statement, a class is also terminated with semicolon(;).

Q 5. Pick out the other definition of objects.
A. member of the class
B. instance of the class
C. attribute of the class
D. associate of the class

Show Answer Answer:-B.instance of the class
Explanation 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.

Q 6. How many objects can present in a single class?
A. 1
B. 2
C. 3
D. as many as possible

Show Answer Answer:-D. as many as possible
Explanation Because a class may contain any number of objects according to its compliance.

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

#include <iostream>

using namespace std;

class sample

{

private:

int var;

public:

void input()

{

cout << var;

}

void output()

{

cout << “Variable entered is “;

cout << var << “\n“;

}

};

int main()

{

sample object;

object.input();

object.output();

object.var();

return 0;

}

A. Enter an integer 5
Variable entered is 5

B. Runtime error
C. Error
D. Enter an integer 7
Variable entered is 7

Show Answer Answer:-C. Error
Explanation 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.

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

#include <iostream>

using namespace std;

class number

{

int i;

public:

int geti();

void puti(int j);

};

int number::geti()

{

return i;

}

void number::puti(int j)

{

i = j;

}

int main()

{

number s;

s.puti(10);

cout << s.geti( );

return 0;

}

A. 8
B. 10
C. 21
D. 26

Show Answer Answer:-B. 10
Explanation We are getting the number and copying it to j and printing it. Output: $ g++ obj2.cpp $ a.out 10

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

#include <iostream>

using namespace std;

class Rect

{

int x, y;

public:

void set_values (int,int);

int area ()

{

return (x * y);

}

};

void Rect::set_values (int a, int b)

{

x = a;

y = b;

}

int main ()

{

Rect recta, rectb;

recta.set_values (5, 6);

rectb.set_values (7, 6);

cout << “recta area: ” << recta.area();

cout << “rectb area: ” << rectb.area();

return 0;

}

A. recta area: 30 rectb area: 33
B. recta area: 20 rectb area: 34
C. recta area: 30 rectb area: 21
D. recta area: 30 rectb area: 42

Show Answer Answer:-D. recta area: 30 rectb area: 42
Explanation We are calculating the area of rectangle by two objects.

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

#include <iostream>

using namespace std;

class Box

{

public :

double length;

double breadth;

double height;

};

int main( )

{

Box Box1;

double volume;

Box1.height = 5;

Box1.length = 6;

Box1.breadth = 7.1;

volume = Box1.height * Box1.length * Box1.breadth;

cout << “Volume of Box1 : ” << volume <<endl;

return 0;

}

A. 210
B. 213
C. 215
D. 217

Show Answer Answer:-B. 213
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

Comments

Leave a Reply

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

You cannot copy content of this page