C++ Programming Questions and Answers – Classes – 1

C++ Multiple Choice Questions focuses on “Classes”. One shall practice these questions to improve their C++ programming skills needed for various interviews campus interviews, walk-in 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 “Classes” along with answers, explanations and/or solutions:

Q 1. What does a class in C++ holds?
A. data
B. functions
C. arrays
D. both data & functions

Show Answer Answer:-D. both data & functions
Explanation The classes in C++ encapsulates(i.e. put together) all the data and functions related to them for manipulation.

Q 2. How many specifiers are present in access specifiers in class?
A. 1
B. 2
C. 3
D. 4

Show Answer Answer:-C. 3
Explanation There are three types of access specifiers. They are public, protected and private.

Q 3. Which is used to define the member of a class externally?
A. :
B. ::
C. #
D. !!$

Show Answer Answer:-B. ::
Explanation :: operator is used to define the body of any class function outside the class.

Q 4. Which other keywords are also used to declare the class other than class?
A. struct
B. union
C. object
D. both struct & union

Show Answer Answer:-D. both struct & union
Explanation Struct and union take the same definition of class but differs in the access techniques.

Q 5. 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; };

Show Answer Answer:-A. class A { int x; };
Explanation A class declaration terminates with semicolon and starts with class keyword. only option (a) follows these rules therefore class A { int x; }; is correct.

Q 6. The data members and functions of a class in C++ are by default ____________
A. protected
B. public
C. private
D. public & protected

Show Answer Answer:-C.private
Explanation By default all the data members and member functions of class are private.

Q 7. Constructors are used to ____________
A. delete the objects
B. construct the data members
C. both initialize the objects & construct the data members
D. initialize the objects

Show Answer Answer:-D. initialize the objects
Explanation Once the object is declared means, the constructor are also declared by default.

Q 8. When struct is used instead of the keyword class means, what will happen in the program?
A. access is private by default
B. access is public by default
C. access is protected by default
D. access is denied

Show Answer Answer:-B. access is public by default
Explanation For structures, by default all the data members and member functions are public.

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

#include <iostream>

using namespace std;

class rect

{

int x, y;

public:

void val (int, int);

int area ()

{

return (x * y);

}

};

void rect::val (int a, int b)

{

x = a;

y = b;

}

int main ()

{

rect rect;

rect.val (3, 4);

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

return 0;

}

A. rect area: 24
B. rect area: 12
C. compile error because rect is as used as class name and variable name in line #20
D. rect area: 56

Show Answer Answer:-B. rect area: 12
Explanation In this program, we are calculating the area of rectangle based on given values. Output: $ g++ class.cpp $ a.out rect area: 12

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

#include <iostream>

using namespace std;

class CDummy

{

public:

int isitme (CDummy& param);

};

int CDummy::isitme (CDummy& param)

{

if (&param == this)

return true;

else

return false;

}

int main ()

{

CDummy a;

CDummy *b = &a;

if (b->isitme(a))

{

cout << “execute”;

}

else

{

cout<<“not execute”;

}

return 0;

}

A. execute
B. not execute
C. error
D. both execute & not execute

Show Answer Answer:-A. execute
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

Comments

Leave a Reply

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

You cannot copy content of this page