Objects - C++ Questions and Answers - Sanfoundry
Objects - C++ Questions and Answers - Sanfoundry
Objects
This section on C++ Multiple Choice Questions focuses on “Objects”. 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 “Objects” along with answers, explanations
and/or solutions:
Answer: a
Explanation: In class, only all the listed items except class will be declared.
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
Answer: d
Explanation: Because of the access is given to the private and protected, We can’t access them by
using direct member access operator.
advertisement
1. #include <iostream>
2. using namespace std;
3. class Box
4. {
5. public :
6. double length;
7. double breadth;
8. double height;
9. };
10. int main( )
11. {
12. Box Box1;
13. double volume;
14. Box1.height = 5;
15. Box1.length = 6;
16. Box1.breadth = 7.1;
17. volume = Box1.height * Box1.length * Box1.breadth;
18. cout << "Volume of Box1 : " << volume <<endl;
19. return 0;
20. }
a) 210
b) 213
c) 215
d) 217
View Answer
Answer: b
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
advertisement
1. #include <iostream>
2. using namespace std;
3. class Rect
4. {
5. int x, y;
6. public:
7. void set_values (int,int);
8. int area ()
9. {
10. return (x * y);
11. }
12. };
13. void Rect::set_values (int a, int b)
14. {
15. x = a;
16. y = b;
17. }
18. int main ()
19. {
20. Rect recta, rectb;
21. recta.set_values (5, 6);
22. rectb.set_values (7, 6);
23. cout << "recta area: " << recta.area();
24. cout << "rectb area: " << rectb.area();
25. return 0;
26. }
Answer: a
Explanation: We are calculating the area of rectangle by two objects.
advertisement
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
Answer: d
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.
Answer: d
Explanation: Because a class may contain any number of objects according to its compliance.
1. #include <iostream>
2. using namespace std;
3. class sample
4. {
5. private:
6. int var;
7. public:
8. void input()
9. {
10. cout << var;
11. }
12. void output()
13. {
14. cout << "Variable entered is ";
15. cout << var << "\n";
16. }
17. };
18. int main()
19. {
20. sample object;
21. object.input();
22. object.output();
23. object.var();
24. return 0;
25. }
a)
Enter an integer 5
Variable entered is 5
b) Runtime error
c) Error
d)
Enter an integer 7
Variable entered is 7
View Answer
Answer: c
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.
Answer: a
Explanation: Similar to ending any statement, a class is also terminated with semicolon(;).
a) 10
b) 11
c) 20
d) 22
View Answer
Answer: a
Explanation: We are getting the number and copying it to j and printing it.
Output:
$ g++ obj2.cpp
$ a.out
10
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]
« Prev - C++ Programming Questions and » Next - C++ Programming Questions and Answers
Answers – User Defined Types – Operator Functions
Related Posts:
advertisement
Recommended Articles:
1. Object Oriented Programming using C++ Questions and Answers – Private Member Functions
2. Object Oriented Programming using C++ Questions and Answers – Pointer to Objects
3. Object Oriented Programming using C++ Questions and Answers – Public Member Functions
4. Object Oriented Programming using C++ Questions and Answers – Access Specifiers
5. C++ Programming Questions and Answers – Function Objects
6. C++ Programming Questions and Answers – Access Control
7. Java Questions & Answers – Access Control – 2
8. Object Oriented Programming using C++ Questions and Answers – Types of Member Functions
9. Object Oriented Programming using C++ Questions and Answers – Objects
10. Object Oriented Programming using C++ Questions and Answers – Assigning Objects
advertisement
Additional Resources:
Java Programs on Classes and Objects
Object Oriented Programming MCQ Questions
C++ Programming MCQ Questions
Java Programming MCQ Questions
R Programming MCQ Questions
Popular Pages:
Programming MCQ Questions
Java Programming Examples
C Programming Interview Questions
C Programming MCQ Questions
Python Programming Examples
Name
Subscribe
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!
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.