Here is a listing of 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
View Answer
Explanation: 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) none of the mentioned
View Answer
Explanation: 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
View Answer
Explanation: Because of the access given to the private and protected, We can’t access them by using direct member access operator.
4. What is the output of the following program?
#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
View Answer
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
5. What is the output of the program?
#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: 42
b) recta area: 20 rectb area: 34
c) recta area: 30 rectb area: 21
d) none of the mentioned
View Answer
Explanation: 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
View Answer
Explanation: None.
7. How many objects can present in a single class?
a) 1
b) 2
c) 3
d) as many as possible
View Answer
Explanation: Because a class may contain any number of objects according to it’s compliance.
8. What is the output of this program?
#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) None of the mentioned
View Answer
Explanation: While using private member, you can’t access it variable.
9. Which special character is used to mark the end of class?
a) ;
b) :
c) #
d) $
View Answer
Explanation: None.
10. What is the output of this program?
#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) 10
b) 11
c) 20
d) 22
View Answer
Explanation: We are getting the number and copying it to j and printing it.
Output:
$ g++ obj2.cpp
$ a.out
10
Sanfoundry Global Education & Learning Series – C++ Programming Language.
Here’s the list of Best Reference Books in C++ Programming Language.
To practice all features of C++ programming language, here is complete set on 1000+ Multiple Choice Questions and Answers on C++.