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

«
»

This set of Object Oriented Programming using C++ Question Bank focuses on “Protected Access Specifier”.

1. Which among the following best describes the protected specifier?
a) Members are most secure and can’t be used outside class
b) Members are secure but can be used outside the class
c) Members are secure as private, but can be inherited
d) Members are secure like private, but can’t be inherited
View Answer

Answer: c
Explanation: The members which are made protected, are most secure if inheritance is not used. But, this facility is provided to keep those members private and with that, they can be inherited by other classes. This is done to make the code more flexible.
advertisement

2. If a constructor is defined in protected access, then?
a) It’s instance can be created inside the subclasses
b) It’s instance can be created anywhere in the program
c) It’s instance can be created inside the subclasses and main() function
d) It’s instance can be created inside the parent class only
View Answer

Answer: a
Explanation: The instances will be allowed to be created only inside the sub classes. This is because the protected members will be inherited and hence the constructor too. This will allow the subclasses to call the constructor whenever an object is created.

3. For the following code, choose the correct option.

advertisement
advertisement
class A
{  
	int marks;
	protected : A()
	{ 
		marks=100; 
	}
	public : A( int x)
	{ 
		marks=x;  
	}
};

a) The instances can be created only in subclasses
b) The instances can be created only in main() function
c) The instances can be created only in parent class
d) The instances can be created anywhere in the program
View Answer

Answer: d
Explanation: The instances can be created anywhere in the program. The only restriction will be on which constructor will have to be called. The instances with zero arguments will be allowed to be created only inside the subclasses, but the instances with one argument can be created anywhere in the program.
advertisement

4. If the protected members are to be made accessible only to the nearest subclass and no further subclasses, which access specifier should be used in inheritance?
a) The sub class should inherit the parent class privately
b) The sub class should inherit the parent class as protected
c) The sub class should inherit the parent class as public
d) The sub class can use any access modifier
View Answer

Answer: a
Explanation: The sub class should use private inheritance. This will allow only the nearest sub classes to inherit the protected members and then those members will become private. Hence further inheritance of those members will not be possible.

5. What will be the output of the following code (all header files and required things are included)?

advertisement
class A
{
	int marks;
	protected : A(int x)
	{ 
		marks=x; 
	}
	public : A()
	{ 
		marks=100; 
	}
}
class B
{
	A a;
	A b=100;
};
main()
{
	A a, b=100;
	B c;
}

a) Program runs fine
b) Program gives runtime error
c) Program gives compile time error
d) Program gives logical error
View Answer

Answer: c
Explanation: The objects being created with assignment value are allowed, if the constructor accepts only 1 argument. But main() function will not be able to create the object here with assignment, as the constructor which accepts one argument is in protected mode in the class.
advertisement

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

class A
{
	protected : int marks;
	public : 
	A()
	{ 
		marks=100; 
	}
	disp()
	{ 
		cout<<”marks=<<marks; 
	}
};
class B: protected A
{
};
B b;
b.disp();

a) Object b can’t access disp() function
b) Object b can access disp() function inside its body
c) Object b can’t access members of class A
d) Program runs fine
View Answer

Answer: a
Explanation: The object of class B can’t access the members of A outside the class. This is because the class is being inherited in protected access, so all the members will become protected in subclass B.

7. Protected members differ from default members as _______
a) Protected members can be accessed outside package using inheritance, but default can’t
b) Default members can be accessed outside package using inheritance, but protected can’t
c) Protected members are allowed for inheritance but Default members are not allowed
d) Both are same
View Answer

Answer: a
Explanation: The protected members are allowed in the same package but can also be accessed in other packages using inheritance. But the default members can never be accessible in other packages.

8. If all the members are defined in protected specifier then? (Constructors not considered)
a) Instance of class can’t be created
b) Instance of class can be created anywhere
c) Instance of class can be created only in subclasses
d) Instance of class can be created only in main() function
View Answer

Answer: b
Explanation: The instances can be created anywhere in the program. This is because the constructors are not considered among the members defined in protected mode. Hence the default implicit constructor will be used whenever an object is created.

9. Which among the following is correct for the code given?

class A
{
    private: int marks;
    A()
    { 
    }
    Public : disp()
    { 
          cout<< marks; 
     }
};
class B: public A
{
}
B b;

a) Instance of B will not be created
b) Instance of B will be created
c) Program gives compile time error
d) Program gives runtime error
View Answer

Answer: a
Explanation: Instance of B will not be created. When you try to create an instance of B, First the constructor of parent class will be called, but the parent class constructor is private, hence it won’t be able to initialize and allocate memory for parent class members. In turn, the object of B will not be created.

10. If protected inheritance is used then _____
a) Public members become public in subclass
b) Protected members become public in subclass
c) Protected members become protected in subclass
d) Protected and Public members become protected in subclass
View Answer

Answer: d
Explanation: The protected and public members of the parent class will become the protected members in subclass. This is predefined rule of inheritance. The reason behind is to maintain the level of security in subclass too.

11. If protected members are to be accessed from outside the class then__________
a) Members must be inherited publicly in subclass
b) Members must accessed using class pointers
c) Members must be accessed as usual
d) Members must be made public
View Answer

Answer: d
Explanation: The members must be made public, otherwise it is not possible. In every case, the protected members will act as private members if it’s about access specifier. It will only be inherited, that too will lead to make those members protected again, in subclasses.

12. Which among the following can use protected access specifier?
a) Members which may be used in other packages
b) Members which have to be secure and should be used by other packages/subclass
c) Members which have to be accessed from anywhere in the program
d) Members which have to be as secure as private but can be used by main() function
View Answer

Answer: b
Explanation: The members which have to be secure and might get used in other packages or subclasses can use protected access. This also allows the members to be safe from accidental modification.

13. Protected access is same as default access that is given implicitly in java if no specifier is mentioned.
a) True
b) False
View Answer

Answer: b
Explanation: The statement given is true. The clear difference is protected members are available in other packages also, but the default members are available within the package only.

14. If a class have default constructor defined in private access, and one parameter constructor in protected mode, how will it be possible to create instance of object?
a) Define a constructor in public access with different signature
b) Directly create the object in the subclass
c) Directly create the object in main() function
d) Not possible
View Answer

Answer: a
Explanation: If a new constructor is defined in public access. That will be available to the whole program. Only restriction will be of the way to use it.

15. What will be the output of the program given below?

class A
{
	Public : A(int a=0)
	{ 
		cout<<new A”;
        }
};
A a;
A b;
A c;

a) new A new A new A
b) newAnewAnewA
c) new Anew Anew A
d) new A new Anew A
View Answer

Answer: c
Explanation: The constructor has a default argument. Whenever the object is created, the constructor will be called and print the message in its definition. Since the argument is default valued, it is not mandatory to pass anything to the new object.

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

To practice Object Oriented Programming Question Bank using C++, here is complete set of 1000+ Multiple Choice Questions and Answers.

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!

advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter