Test Series - oops

Test Number 20/78

Q: 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
Solution: 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.
Q: Which among the following have least security according to the access permissions allowed?
A. Private
B. Default
C. Protected
D. Public
Solution: 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.
Q: Which among the following can be used for outermost class access specifier in java?
A. Private
B. Public
C. Default
D. Default or Public
Solution: 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.
Q: 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
Solution: 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.
Q: How many public class(s) (outermost) can be there in a java program?
A. 1
B. 2
C. 3
D. As required
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: Class A can have instances as it has public default constructor. Class will have its own constructors defined. Hence both classes can have instances.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: Which specifier allows to secure the public members of base class in inherited classes?
A. Private
B. Protected
C. Public
D. Private and Protected
Solution: 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.

You Have Score    /10