0% found this document useful (0 votes)
174 views

Objects - C++ Questions and Answers - Sanfoundry

Uploaded by

badoublejay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
174 views

Objects - C++ Questions and Answers - Sanfoundry

Uploaded by

badoublejay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

C++ Programming Questions and Answers –

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:

1. Where does the object is created?


a) class
b) constructor
c) destructor
d) attributes
View Answer

Answer: a
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

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

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

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:

Note: Join free Sanfoundry classes at Telegram or Youtube

$ g++ obj1.cpp
$ a.out
213

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

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

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

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.

7. How many objects can present in a single class?


a) 1
b) 2
c) 3
d) as many as possible
View Answer

Answer: d
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?

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.

9. Which special character is used to mark the end of class?


a) ;
b) :
c) #
d) $
View Answer

Answer: a
Explanation: Similar to ending any statement, a class is also terminated with semicolon(;).

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


1. #include <iostream>
2. using namespace std;
3. class number
4. {
5. int i;
6. public:
7. int geti();
8. void puti(int j);
9. };
10. int number::geti()
11. {
12. return i;
13. }
14. void number::puti(int j)
15. {
16. i = j;
17. }
18. int main()
19. {
20. number s;
21. s.puti(10);
22. cout << s.geti( );
23. return 0;
24. }

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

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]

« Prev - C++ Programming Questions and » Next - C++ Programming Questions and Answers
Answers – User Defined Types – Operator Functions

Related Posts:

Apply for C++ Internship


Check C++ Books
Practice Computer Science MCQs
Check Computer Science Books
Apply for Computer Science Internship

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

Subscribe: C++ Newsletter

Name

Email

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!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest

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.

About | Certifications | Internships | Jobs | Privacy Policy | Terms | Copyright | Contact

     

© 2011-2024 Sanfoundry. All Rights Reserved.

You might also like