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
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
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
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
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?
#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
View Answer
Explanation: In this program, we are calculating the area of rectangle based on given values.
Output:
$ g++ class.cpp $ a.out rect area: 12
6. 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 (¶m == 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
View Answer
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
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
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
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
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
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.
- Check C++ Books
- Apply for C++ Internship
- Apply for Computer Science Internship
- Apply for Information Technology Internship
- Check Programming Books