Here is a listing of C++ Questions & Answers focuses 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) resolution operator
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 is given to the private and protected, We can’t access them by using direct member access operator.
4. 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
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 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: 42
b) recta area: 20 rectb area: 34
c) recta area: 30 rectb area: 21
d) recta area: 30 rectb area: 33
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: 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.
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 its compliance.
8. 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 7View Answer
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.
9. Which special character is used to mark the end of class?
a) ;
b) :
c) #
d) $
View Answer
Explanation: Similar to ending any statement, a class is also terminated with semicolon(;).
10. 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) 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.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]
- Practice Computer Science MCQs
- Check Programming Books
- Apply for C++ Internship
- Check C++ Books
- Check Computer Science Books