Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) 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
Answer: a
Clarification: 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
Answer: c
Clarification: 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.
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
Answer: a
Clarification: 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.
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
Answer: d
Clarification: 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
Answer: b
Clarification: 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.
6. Which access specifier is/are most secure during inheritance?
a) Private
b) Default
c) Protected
d) Private and default
Answer: a
Clarification: 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
Answer: c
Clarification: 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
