C++ Programming Questions and Answers – Classes – 2

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Classes – 2”.

1. Which category of data type a class belongs to?
a) Fundamental data type
b) Derived data type
c) User defined derived data type
d) Atomic data type
View Answer

Answer: c
Explanation: Fundamental/Atomic data type includes int, char, float, double and void. Derived data type includes arrays, pointers, references, function and constants. User defined derived data type includes class, structure, union and enumeration.

2. Which operator a pointer object of a class uses to access its data members and member functions?
a) .
b) ->
c) :
d) ::
View Answer

Answer: b
Explanation: ->(arrow operator) is used by a pointer object to access members of its class.

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

advertisement
advertisement
#include <iostream>
#include <string>
using namespace std;
class A
{
	int a;
 
   public:
	int assign(int i) const {
		a = i;
	}
	int return_value() const {
		return a;
	}
};
int main(int argc, char const *argv[])
{
	A obj;
	obj.assign(5);
	cout<<obj.return_value();
}

a) 5
b) 10
c) Error
d) Segmentation fault
View Answer

Answer: c
Explanation: As the assign() is a constant function and a constant function cannot change the state of an object and as in the assign function we are trying to modify the member a of the object therefore the program gives error.
Note: Join free Sanfoundry classes at Telegram or Youtube

4. What is the correct syntax of accessing a static member of a Class?

---------------------------
Example class:
class A
{
	public:
		static int value;
}
---------------------------

a) A.value
b) A::value
c) A->value
d) A^value
View Answer

Answer: b
Explanation: Scope resolution operator(::) is used to access a static member of a class.
advertisement

5. How the objects are self-referenced in a member function of that class.
a) Using a special keyword object
b) Using this pointer
c) Using * with the name of that object
d) By passing self as a parameter in the member function
View Answer

Answer: b
Explanation: In Classes objects are self-referenced using this pointer inside the member functions. for example this->value to access the data member value of that object.
advertisement

6. What does a mutable member of a class mean?
a) A member that can never be changed
b) A member that can be updated only if it not a member of constant object
c) A member that can be updated even if it a member of constant object
d) A member that is global throughout the class
View Answer

Answer: c
Explanation: Mutable members are those which can be updated even if it a member of a constant object. You can change their value even from a constant member function of that class.

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

#include <iostream>
#include <string>
using namespace std;
 
class A
{
	mutable int a;
 
        public:
	int assign(int i) const {
		a = i;
	}
	int return_value() const {
		return a;
	}
 
};
 
int main(int argc, char const *argv[])
{
	A obj;
	obj.assign(5);
	cout<<obj.return_value();
}

a) 5
b) Error
c) Segmentation fault
d) Undefined value
View Answer

Answer: a
Explanation: As a is mutable member of the class it’s value can be modified whether it is a part of constant object or not. It can be modified even inside a constant member function. Hence, the program tuns fine and does not gives any error.

8. Pick the incorrect statement about inline functions in C++?
a) They reduce function call overheads
b) These functions are inserted/substituted at the point of call
c) Saves overhead of a return call from a function
d) They are generally very large and complicated function
View Answer

Answer: d
Explanation: Inline are functions that are expanded when it is called. The whole code of the inline function gets inserted/substituted at the point of call. In this, they help in reducing the function call overheads. Also they save overhead of a return call from a function. Inline functions are generally kept small.

9. Inline functions are avoided when ____________________________
a) function contains static variables
b) function have recursive calls
c) function have loops
d) all of the mentioned
View Answer

Answer: d
Explanation: Inline functions are avoided in all the above cases as whole inline code is copied to the point of call so compiler avoids to make large functions as inline. Even if you yourself mention inline but the function is large compiler ignores your request of inline and treats that function as a normal function.

10. Pick the correct statement.
a) Macros and inline functions are same thing
b) Macros looks like function calls but they are actually not
c) Inline functions looks like function but they are not
d) Inline function are always large
View Answer

Answer: b
Explanation: Macros in C++ looks like function calls but actually they are not function calls.

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

#include <iostream> 
using namespace std; 
class S 
{ 
	int m; 
   public: 
	#define MAC(S::m)
};
 
int main(int argc, char const *argv[])
{
	cout<<"Hello World";
	return 0;
}

a) Hello World
b) Error
c) Segmentation Fault
d) Blank Space
View Answer

Answer: b
Explanation: Macros cannot access the private member of a class therefore #define MAC(S::m) will give an error.

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

#include <iostream>
#include <string>
using namespace std;
 
class A
{
	static int a;
 
   public:
	void change(int i){
		a = i;
	}
	void value_of_a(){
		cout<<a;
	}
};
 
int main(int argc, char const *argv[])
{
	A a1 = A();
	a1.change(5);
	a1.value_of_a();
	return 0;
}

a) 5
b) Garbage value
c) Error
d) Segmentation fault
View Answer

Answer: c
Explanation: Every static member of a class is initialised before its use. As ‘a’ is a static member of the class and is not initialised so the program will give error.

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

#include <iostream>
#include <string>
using namespace std;
class A
{
	static int a;
    public:
	void change(int i){
		a = i;
	}
	void value_of_a(){
		cout<<a;
	}
};
 
int A::a = 5;
 
int main(int argc, char const *argv[])
{
	A a1 = A();
	A a2 = A();
	A a3 = A();
	a1.change(10);
	a1.value_of_a();
	a2.value_of_a();
	a3.value_of_a();
	return 0;
}

a) 1055
b) 555
c) 101010
d) 51010
View Answer

Answer: c
Explanation: As ‘a’ is a static member of the class so it is a type of global variable to the class i.e. any change made by one object is reflected back to all the other objects. Hence when a is changed to 10 by object a1, so value of ‘a’ becomes 10 for each object and 3 times 10 is printed.

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

#include <iostream>
#include <string>
using namespace std;
class A
{
	int a = 5;
    public:
	void change(int i){
		a = i;
	}
	static void value_of_a(){
		cout<<a;
	}
};
 
int main(int argc, char const *argv[])
{
	A a1 = A();
	a1.change(10);
	a1.value_of_a();
	return 0;
}

a) 10
b) Error
c) Segmentation Fault
d) 5
View Answer

Answer: b
Explanation: As value_of_a() is a static function and static member can access only static members therefore the program will give error.

15. Which functions of a class are called inline functions?
a) All the functions containing declared inside the class
b) All functions defined inside or with the inline keyword
c) All the functions accessing static members of the class
d) All the functions that are defined outside the class
View Answer

Answer: b
Explanation: All the functions defined inside the class or functions having inline keyword before them are inline functions of a class provided they are small and simple otherwise compiler ignores the request of inline.

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]

advertisement
advertisement
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 - Founder & CTO at Sanfoundry
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.