C++ Programming Questions and Answers – Classes – 1

This section on 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, company 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.

Here is a listing of C++ Questions & Answers focuses on “Classes” along with answers, explanations and/or solutions:

1. What does a class in C++ holds?
a) data
b) functions
c) both data & functions
d) arrays
View Answer

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

2. How many specifiers are present in access specifiers in class?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: c
Explanation: 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) !!$
View Answer

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

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

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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  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: 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
View Answer

Answer: b
Explanation: In this program, we are calculating the area of rectangle based on given values.

$ g++ class.cpp
$ a.out
rect area: 12

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

  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) error
d) both execute & not execute
View Answer

Answer: a
Explanation: In this program, we are just pointing the pointer to a object and printing execute if it is correctly pointed.

$ g++ class1.cpp
$ a.out

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

8. The data members and functions of a class in C++ are by default ____________
a) protected
b) private
c) public
d) public & protected
View Answer

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

9. Constructors are used to ____________
a) initialize the objects
b) construct the data members
c) both initialize the objects & construct the data members
d) delete the objects
View Answer

Answer: a
Explanation: 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) access is denied
View Answer

Answer: a
Explanation: For structures, by default all the data members and member functions are public.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.