Object Oriented Programming using C++ Questions and Answers – Private Access Specifier

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Private Access Specifier”.

1. If a function has to be called only by using other member functions of the class, what should be the access specifier used for that function?
a) Private
b) Protected
c) Public
d) Default
View Answer

Answer: a
Explanation: The function should be made private. In this way, the function will be available to be called only from the class member functions. Hence the function will be secure from the outside world.

2. Which among the following is correct for the code given below?

class student
{  
	private: student()
	{  
	}
	public : student( int x) 
	{ 
		marks =x; 
	}
};

a) The object can never be created
b) The object can be created without parameters
c) Only the object with only 1 parameter can be created
d) Only the object with some parameters can be created
View Answer

Answer: c
Explanation: For creating object without parameters, the default constructor must be defined in public access. But here, only parameterized constructor is public, hence the objects being created with only one parameter will only be allowed.
advertisement
advertisement

3. Which among the following is true for the code given below?

class A
{
	private : int marks; char name[20];
	public :
	A(int x=100)
	{ 
		marks=x;  
	}
};

a) Objects can be created with one parameter or without parameter
b) Object can be created only with one parameter
c) Object can be created with more than one parameter
d) Objects can be create only without parameter
View Answer

Answer: a
Explanation: The constructor here has a default argument constructor. Hence we can pass one parameter, but that is optional. If an object is created without parameter, the default value will be used in the constructor definition.
advertisement

4. Which among the following is correct to call a private member from outside the class?
a) object.memberfunction( parameters );
b) object->memberfunction( parameters );
c) object->memberfunction( parameteres); or object.memberfunction( parameters );
d) Not possible
View Answer

Answer: d
Explanation: The private member function will not be accessible from outside the class. Hence any syntax will not work to access the private members. If you have the address of the member, may be you can access those members, but that is a totally different case and concept.

5. If private members have to be accessed directly from outside the class but the access specifier must not be changed, what should be done?
a) Specifier must be changed
b) Friend function should be used
c) Other public members should be used
d) It is not possible
View Answer

Answer: b
Explanation: For calling the function directly, we can’t use another function because that will be indirect call. Using friend function, we can access the private members directly.
advertisement

6. Which access specifier is/are most secure during inheritance?
a) Private
b) Default
c) Protected
d) Private and default
View Answer

Answer: a
Explanation: The private members are most secure in inheritance. The default members can still be in inherited in special cases, but the private members can’t be accessed in any case.

7. Choose the correct option for the code given below.
class A{ static int c=0; public: A(){ c++; } };
a) Constructor will make c=1 for each object created
b) Constructor will make c=0 for each object created
c) Constructor will keep number of objects created
d) Constructor will just initialize c=0 then increment by 1
View Answer

Answer: c
Explanation: The constructor is using a static member to keep the count of the number of objects created. This is done because the variable c is static and hence the value will be common for all the objects created.

8. Which option is false for the following code?

class A
{
	private : int sum(int x, int y)
	{ 
		return x+y; 
	}
	public: A()
	{  
	}
	A(int x, int y)
	{ 
		cout<<sum(x,y);  
	}
};

a) Constructor can be created with zero argument
b) Constructor prints sum, if two parameters are passed with object creation
c) Constructor will give error if float values are passed
d) Constructor will take 0 as default value of parameters if not passed
View Answer

Answer: d
Explanation: Constructor is not having any default arguments hence no default value will be given to any parameters. Only integer values must be passed to the constructor if we need the sum as output, otherwise if float values are passed, type mismatch will be shown as error.

9. Which member will never be used from the following class?

class A()
{ 
	int marks; char name[20];
	public : A()
	{ 
		marks=100; 
	}
	void disp()
	{ 
		cout<<”Marks=&lt'<marks;
		cout<<”Student”;
	}
};

a) name variable will never be used
b) marks variable will never be used
c) constructor will never be used
d) disp() function will never be used
View Answer

Answer: a
Explanation: Variable name will never be used. It is a private member. None other than class members can access name, also, neither the constructor nor the disp() function are accessing the variable name. Hence it will never be accessible.

10. Private member functions can be overloaded.
a) True
b) False
View Answer

Answer: a
Explanation: The private functions can also be overloaded. This can be done in usual way by having the same name of the member function and having different signature. Only thing is, they must be accessed from members of class only.

11. Which among the following is true?
a) Private member functions can’t be overloaded
b) Private member functions can be overridden
c) Private member functions can’t be overloaded with a public member
d) Private member function can’t be overridden
View Answer

Answer: d
Explanation: The private member functions can be overloaded but they can’t be overridden. This is because, overriding means a function with same name in derived class, gets more priority when called from object of derived class. Here, the member function is private so there is no way that it can be overridden.

12. Which data member in following code will be used whenever an object is created?

Class A
{ 
   int x; int y; int z;
   public : A()
   { 
      y=100; x=100*y; 
   }
};

a) x will be used
b) y will be used
c) z will be used
d) All will be used
View Answer

Answer: c
Explanation: Whenever an object will be created, the constructor will be called. Inside constructor we are using the data members x and y. Hence these two will always be used with each object creation.

13. Which member can be considered most secure in the code below?

class A()
{
	int a;
	private : int b;
	protected : int c;
	public : int d;
};

a) a
b) b
c) c
d) d
View Answer

Answer: b
Explanation: The default variables can be inherited in some special cases but the public members can never be inherited. Hence the most secure data member in the class is b.

14. Which among the following is correct for the code given below?

class A
{
	private : A()
	{ 
	}
	public : A(int x)
	{  
	}
};
A a;
A b(100);

a) Program will give compile time error
b) Program will run fine
c) Program will give runtime error
d) Program will give logical error
View Answer

Answer: a
Explanation: The program will be giving a compile time error as the default constructor is private in class. And, the logical errors are usually runtime so we can’t say that the program will give logical error. The program will not run.

15. Which among the following is correct?
a) Private specifier must be used before public specifier
b) Private specifier must be used before protected specifier
c) Private specifier must be used first
d) Private specifier can be used anywhere in class
View Answer

Answer: d
Explanation: The private specifier can be used anywhere in the class as required. It is not a rule to mention the private members first and then others. It is just followed to write first for better readability.

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming (OOPs) using C++, 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.