C++ Programming Questions and Answers – Classes – 2

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

Q 1. Which category of data type a class belongs to?
A. Fundamental data type
B. Derived data type
C. Atomic data type
D. User defined derived data type

Show Answer Answer:-D. User defined derived data type
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.

Q 2. Which operator a pointer object of a class uses to access its data members and member functions?
A. .
B. :
C. ::
D.->

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

Q 3. 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

Show Answer Answer:-B. A::value
Explanation Scope resolution operator(::) is used to access a static member of a class.

Q 4. How the objects are self-referenced in a member function of that class.
A. Using this pointer
B. Using a special keyword object
C. Using * with the name of that object
D. By passing self as a parameter in the member function

Show Answer Answer:-A. Using this pointer
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.

Q 5. 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

Show Answer Answer:-C. A member that can be updated even if it a member of constant object
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.

Q. 6. 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

Show Answer Answer:-B. All functions defined inside or with the inline keyword
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.

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

Show Answer Answer:-D. They are generally very large and complicated function
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.

Q 8. Inline functions are avoided when ____________________________
A. function contains static variables
B. function have recursive calls
C. function have loops
D. all of the mentioned

Show Answer Answer:-D. all of the mentioned
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.

Q 9. Pick the correct statement.
A. Macros and inline functions are same thing
B. Inline function are always large
C. Inline functions looks like function but they are not
D. Macros looks like function calls but they are actually not

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

Q 10. 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

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

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

#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

Show Answer Answer:-C. Error
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.

Q 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

Show Answer Answer:-C. Error
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.

Q 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. 5553
C. 51010
D. 101010

Show Answer Answer:-D. 101010
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.

Q 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. 5
B. 10
C. Segmentation Fault
D. Error

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

Q 15. 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

Show Answer Answer:-A. 5
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.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page