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

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

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

class A
{
	int marks;
	public : disp()
	{ 
		cout<<marks; 
	}
}
class B: protected A
{
	char name[20];
}
A a; a.disp();
B b; b.disp();

a) Only object of class A can access disp() function
b) Only object of class B can access disp() function
c) Both instances can access disp() function
d) Accessing disp() outside class is not possible
View Answer

Answer: a
Explanation: The object of class A can access the disp() function. This is because the disp() function is public in definition of class A. But it can’t be accessed from instance of class B because the disp() function is protected in class B, since it was inherited as protected.
advertisement
advertisement

2. If the members have to be accessed from anywhere in the program and other packages also, which access specifier should be used?
a) Public
b) Private
c) Protected
d) Default
View Answer

Answer: a
Explanation: The access specifier must be public so as to access the members outside the class and anywhere within the program without using inheritance. This is a rule, predefined for the public members.

3. Which among the following have least security according to the access permissions allowed?
a) Private
b) Default
c) Protected
d) Public
View Answer

Answer: d
Explanation: The public members are available to the whole program. This makes the members most vulnerable to accidental changes, which may result in unwanted modification and hence unstable programming.
Note: Join free Sanfoundry classes at Telegram or Youtube

4. Which among the following can be used for outermost class access specifier in java?
a) Private
b) Public
c) Default
d) Default or Public
View Answer

Answer: d
Explanation: Either default or public access specifier must be used for outermost classes. Private can be used with inner classes. This is done so that all the members can access and use the utmost class and that program execution can be done from anywhere. Inner classes can be made private for security.

5. We can reduce the visibility of inherited methods.
a) True
b) False
View Answer

Answer: b
Explanation: The statement given is false. This is because when we inherit the members they can either be made more secure or be at same access. But the visibility reduction is not possible, for example, if a member is protected in parent class, then it can only be made protected or private in subclass and not public in any case.
advertisement

6. If members of a super class are public, then________
a) All those will be available in subclasses
b) None of those will be available in subclasses
c) Only data members will be available in subclass
d) Only member functions will be available in subclass
View Answer

Answer: a
Explanation: All the members will be available in subclasses. Though it is not guaranteed whether the members will be available in subsequent subclasses from the first subclass.

7. How many public class(s) (outermost) can be there in a java program?
a) 1
b) 2
c) 3
d) As required
View Answer

Answer: a
Explanation: There can be only one public class in a java program. The public class name must match the name of file. And there can’t be more than one class with same name in a single program in same scope. Hence it is not possible to have more than one public class in java program.
advertisement

8. What is the output of the following code?

package pack1;
class A
{
	public A()
	{ 
		System.out.print(“object created”); 
	}   
}
package pack2;
import pack1.*;
class B
{
	A a=new A();
}

a) Output is: object created
b) Output is: object createdobject created
c) Compile time error
d) Run time error
View Answer

Answer: c
Explanation: The program will give compile time error. Class A is defined with default access specifier. This directly means that class A will be available within package only. Even if the constructor is public, the object will not be created.

9. Which among the following for public specifier is false?
a) The static members can’t be public
b) The public members are available in other packages too
c) The subclasses can inherit the public members privately
d) There can be only one public class in java program
View Answer

Answer: a
Explanation: The static members are not property of any object of the class. Instead, those are treated as property of class. This allows us to have public static members too.

10. A class has its default constructor defined as public. Class B inherits class A privately. The class ___________
a) B will not be able to have instances
b) Only A can have instances
c) Only B can have instances
d) Both classes can have instances
View Answer

Answer: d
Explanation: Class A can have instances as it has public default constructor. Class will have its own constructors defined. Hence both classes can have instances.

11. Which specifier can be used to inherit protected members as protected in subclass but public as public in subclass?
a) Private
b) Default
c) Public
d) Protected
View Answer

Answer: c
Explanation: The specifier that can make protected member’s protected in subclass and public member’s public in subclass, is public. This is done to maintain the security level of protected members of parent class.

12. Which among the following is true for public class?
a) There can be more than one public class in a single program
b) Public class members can be used without using instance of class
c) Public class is available only within the package
d) Public classes can be accessed from any other class using instance
View Answer

Answer: d
Explanation: The public class is a usual class. There is no special rule but the members of the class can be accessed from other classes using instance of the class. This is usually useful to define main() function.

13. If a class doesn’t have public members, then________
a) None of its members will be able to get inherited
b) None of its instance creation will be allowed
c) None of its member function can be called outside the class
d) None of its data members will be able to get initial value
View Answer

Answer: c
Explanation: According to rule of private, protected and default access specifiers, none of the members under these specifiers will be able to get invoked outside the class. We are not sure about the members of class specifically so other options doesn’t give a fixed answer.

14. In multi-level inheritance(all public), the public members of parent/superclass will ________
a) Will continue to get inherited subsequently
b) Will not be inherited after one subclass inheritance
c) Will not be available to be called outside class
d) Will not be able to allocated with any memory space
View Answer

Answer: a
Explanation: The public inheritance makes the public members of the base class, public in derived classes. This can be used when the same feature have to be redefined with each new class inheriting the base class.

15. Which specifier allows to secure the public members of base class in inherited classes?
a) Private
b) Protected
c) Public
d) Private and Protected
View Answer

Answer: d
Explanation: Both the private and protected specifiers can make the public members of the base class more secure. This is useful if we stop using the parent class members and use the classes which inherited the parent class, so as to secure data better.

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.