Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

How many types of access specifiers are provided in OOP (C++)?(a) 1(b) 2(c) 3(d) 4The question was posed to me in an online quiz.This intriguing question comes from Access Specifiers topic in division Access Specifiers of Object Oriented Programming

Answer» RIGHT answer is (c) 3

For explanation: Only 3 TYPES of access specifiers are available. Namely, private, PROTECTED and PUBLIC. All these three can be used according to the NEED of security of members.
2.

Which among the following can be used together in a single class?(a) Only private(b) Private and Protected together(c) Private and Public together(d) All three togetherThis question was addressed to me during an online exam.I'm obligated to ask this question of Access Specifiers topic in chapter Access Specifiers of Object Oriented Programming

Answer»

The correct OPTION is (d) All THREE together

Explanation: All the classes can USE any of the SPECIFIERS as needed. There is no restriction on how many of them can be used together.

3.

Which among the following can restrict class members to get inherited?(a) Private(b) Protected(c) Public(d) All threeThe question was posed to me in an internship interview.My doubt stems from Access Specifiers in division Access Specifiers of Object Oriented Programming

Answer»

The correct option is (a) Private

To explain I would say: Private members of a CLASS can’t be inherited. These members can only be ACCESSIBLE from members of its own class only. It is USED to SECURE the data.

4.

Which access specifier is used when no access specifier is used with a member of class (java)?(a) Private(b) Default(c) Protected(d) PublicThis question was posed to me during an online interview.Enquiry is from Access Specifiers in chapter Access Specifiers of Object Oriented Programming

Answer»

The correct option is (b) DEFAULT

Easiest explanation - Default ACCESS is used if the programmer doesn’t specify the SPECIFIER. This ACTS in a similar way as that of private. But since NOTHING is specified we call it to default access.

5.

Which specifier allows a programmer to make the private members which can be inherited?(a) Private(b) Default(c) Protected(d) Protected and defaultI got this question during an interview.My query is from Access Specifiers in chapter Access Specifiers of Object Oriented Programming

Answer»

Right choice is (C) PROTECTED

The BEST I can explain: Protected access is USED to make the members private. But those members can be INHERITED. This gives both security and code reuse capability to a program.

6.

Which among the following is false?(a) Private members can be accessed using friend functions(b) Member functions can be made private(c) Default members can’t be inherited(d) Public members are accessible from other classes alsoThe question was posed to me in unit test.My doubt is from Access Specifiers topic in section Access Specifiers of Object Oriented Programming

Answer»

The correct CHOICE is (c) Default members can’t be inherited

For explanation: The default members can be inherited. Provided that they are in same package. It works in a LITTLE DIFFERENT way from PRIVATE access specifier.

7.

If a class has all the private members, which specifier will be used for its implicit constructor?(a) Private(b) Public(c) Protected(d) DefaultI had been asked this question in homework.My question is from Access Specifiers in portion Access Specifiers of Object Oriented Programming

Answer»

Correct option is (B) Public

Best explanation: The implicit constructor will always be public. Otherwise the class wouldn’t be ABLE to have instances. In TURN, no OBJECTS will be created and the class can only be USED for inheritance.

8.

If class A has add() function with protected access, and few other members in public. Then class B inherits class A privately. Will the user will not be able to call _________ from the object of class B.(a) Any function of class A(b) The add() function of class A(c) Any member of class A(d) Private, protected and public members of class AThe question was asked at a job interview.Question is from Access Specifiers topic in section Access Specifiers of Object Oriented Programming

Answer»

The CORRECT CHOICE is (d) Private, protected and PUBLIC members of class A

The explanation is: Class B OBJECT will not be able to call any of the private, protected and public members of class A. It is not only about the function ADD(), but all the members of class A will become private members of class B.

9.

Which access specifier should be used in a class where the instances can’t be created?(a) Private default constructor(b) All private constructors(c) Only default constructor to be public(d) Only default constructor to be protectedThe question was asked during an online exam.The query is from Access Specifiers topic in chapter Access Specifiers of Object Oriented Programming

Answer»

Right OPTION is (b) All private constructors

The BEST EXPLANATION: All the constructors must be MADE private. This will restrict the instance of class to be made anywhere in the program. Since the constructors are private, no instance will be ABLE to call them and hence won’t be allocated with any memory space.

10.

On which specifier’s data, does the size of a class’s object depend?(a) All the data members are added(b) Only private members are added(c) Only public members are added(d) Only default data members are addedI have been asked this question in examination.This intriguing question comes from Access Specifiers in section Access Specifiers of Object Oriented Programming

Answer»

Correct answer is (a) All the data members are added

The BEST explanation: All the data members are counted to calculate the size of an object of a class. The data member access specifier doesn’t PLAY any ROLE here. HENCE all the data size will be added.

11.

If class B inherits class A privately. And class B has a friend function. Will the friend function be able to access the private member of class A?(a) Yes, because friend function can access all the members(b) Yes, because friend function is of class B(c) No, because friend function can only access private members of friend class(d) No, because friend function can access private member of class A alsoI had been asked this question by my school principal while I was bunking the class.I want to ask this question from Access Specifiers topic in division Access Specifiers of Object Oriented Programming

Answer» RIGHT answer is (C) No, because friend function can only access private members of friend class

The explanation: The friend function of class B will not be able to access private members of class A. SINCE B is inheriting class A PRIVATELY, the members will BECOME private in class B. But private members of class A won’t be inherited at all. Hence it won’t be accessible.
12.

If an abstract class has all the private members, then _________(a) No class will be able to implement members of abstract class(b) Only single inheritance class can implement its members(c) Only other enclosing classes will be able to implement those members(d) No class will be able to access those members but can implement.I have been asked this question during an interview.The query is from Access Specifiers in division Access Specifiers of Object Oriented Programming

Answer» CORRECT answer is (a) No class will be able to implement members of abstract class

Explanation: The classes which inherit the abstract class, won’t be able to implement the members of abstract class. The PRIVATE members will not be INHERITED. This will restrict the SUBCLASSES to implement those members.
13.

Which access specifier should be used so that all the parent class members can be inherited and accessed from outside the class?(a) Private(b) Default or public(c) Protected or private(d) PublicThis question was addressed to me in a job interview.My query is from Access Specifiers topic in chapter Access Specifiers of Object Oriented Programming

Answer»

Correct answer is (d) Public

Easiest EXPLANATION - All the MEMBERS MUST be of public access. So that the members can be inherited EASILY. Also, the members will be AVAILABLE from outside the class.

14.

Which access specifier is usually used for data members of a class?(a) Private(b) Default(c) Protected(d) PublicThe question was posed to me during an online exam.The origin of the question is Access Specifiers topic in section Access Specifiers of Object Oriented Programming

Answer»

Correct choice is (a) Private

To explain: All the data MEMBERS should be made private to ENSURE the HIGHEST security of data. In special cases we can USE public or protected access, but it is advised to keep the data members private always.

15.

Which specifier should be used for member functions of a class?(a) Private(b) Default(c) Protected(d) PublicI had been asked this question during an internship interview.The origin of the question is Access Specifiers in section Access Specifiers of Object Oriented Programming

Answer» RIGHT option is (d) PUBLIC

Explanation: It is always advised that the member functions should be kept public so that those functions can be USED from out of the class. This is USUALLY DONE to ensure that the features provided by the class can be used at its maximum.
16.

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) DefaultI got this question in an interview for internship.Origin of the question is Private Access Specifier topic in chapter Access Specifiers of Object Oriented Programming

Answer»

Right option is (a) PRIVATE

Easiest 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.

17.

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 possibleI have been asked this question by my school teacher while I was bunking the class.Question is from Private Access Specifier in chapter Access Specifiers of Object Oriented Programming

Answer»

The correct choice is (d) Not possible

Best 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.

18.

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 possibleThe question was asked by my school principal while I was bunking the class.This is a very interesting question from Private Access Specifier topic in section Access Specifiers of Object Oriented Programming

Answer»

Correct answer is (b) Friend function should be used

The best 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.

19.

Which access specifier is/are most secure during inheritance?(a) Private(b) Default(c) Protected(d) Private and defaultI had been asked this question by my school principal while I was bunking the class.Origin of the question is Private Access Specifier in portion Access Specifiers of Object Oriented Programming

Answer» RIGHT choice is (a) Private

Easiest 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.
20.

Private member functions can be overloaded.(a) True(b) FalseI have been asked this question during an interview for a job.My question is from Private Access Specifier topic in portion Access Specifiers of Object Oriented Programming

Answer»

Right answer is (a) True

To explain I would say: 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.

21.

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 memberfunction can’t be overriddenI had been asked this question in semester exam.Question is from Private Access Specifier topic in division Access Specifiers of Object Oriented Programming

Answer»

The correct option is (d) Private memberfunction can’t be OVERRIDDEN

The 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.

22.

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 classI got this question in an internship interview.I need to ask this question from Private Access Specifier in portion Access Specifiers of Object Oriented Programming

Answer»

Correct CHOICE is (d) PRIVATE specifier can be used ANYWHERE in class

To explain I would say: 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.

23.

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 inheritedThe question was asked in an online interview.My question is from Protected Access Specifier topic in chapter Access Specifiers of Object Oriented Programming

Answer»

The correct option is (c) MEMBERS are secure as private, but can be inherited

The BEST 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.

24.

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 onlyI have been asked this question in an interview for internship.I need to ask this question from Protected Access Specifier topic in section Access Specifiers of Object Oriented Programming

Answer»

Right option is (a) It’s instance can be created INSIDE the subclasses

The 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.

25.

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 modifierThe question was posed to me in an interview for job.Origin of the question is Protected Access Specifier in chapter Access Specifiers of Object Oriented Programming

Answer»

The CORRECT choice is (a) The sub class should INHERIT the parent class privately

To EXPLAIN I would say: 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.

26.

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 sameI have been asked this question in an online quiz.My question is based upon Protected Access Specifier topic in portion Access Specifiers of Object Oriented Programming

Answer»

Correct choice is (a) Protected members can be accessed outside package USING INHERITANCE, but default can’t

The 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.

27.

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() functionI have been asked this question in an internship interview.This key question is from Protected Access Specifier in division Access Specifiers of Object Oriented Programming

Answer»

The correct CHOICE is (b) Instance of CLASS can be created anywhere

The explanation is: 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.

28.

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 subclassThe question was asked during a job interview.Question is from Protected Access Specifier topic in section Access Specifiers of Object Oriented Programming

Answer»

The correct ANSWER is (d) Protected and Public MEMBERS become protected in subclass

The 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.

29.

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 publicThe question was posed to me during an online interview.Origin of the question is Protected Access Specifier topic in portion Access Specifiers of Object Oriented Programming

Answer»

Right option is (d) MEMBERS MUST be made public

To explain I would say: 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.

30.

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() functionI got this question in class test.This interesting question is from Protected Access Specifier in division Access Specifiers of Object Oriented Programming

Answer»

Right answer is (b) Members which have to be SECURE and should be USED by other packages/subclass

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.

31.

Protected access is same as default access that is given implicitly in java if no specifier is mentioned.(a) True(b) FalseI had been asked this question in an online quiz.Query is from Protected Access Specifier in section Access Specifiers of Object Oriented Programming

Answer»

The correct OPTION is (b) False

To EXPLAIN I would say: 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.

32.

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 possibleThis question was addressed to me in my homework.My doubt stems from Protected Access Specifier topic in chapter Access Specifiers of Object Oriented Programming

Answer»

Right OPTION is (a) DEFINE a CONSTRUCTOR in public access with different signature

Easiest 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.

33.

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) DefaultI had been asked this question in an interview for job.Question is from Public Access Specifier in portion Access Specifiers of Object Oriented Programming

Answer»

The correct choice is (a) Public

Easiest 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.

34.

Which among the following have least security according to the access permissions allowed?(a) Private(b) Default(c) Protected(d) PublicThis question was addressed to me in exam.Origin of the question is Public Access Specifier topic in division Access Specifiers of Object Oriented Programming

Answer»

Correct ANSWER is (d) PUBLIC

The best 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.

35.

Which among the following can be used for outermost class access specifier in java?(a) Private(b) Public(c) Default(d) Default or PublicI got this question by my school teacher while I was bunking the class.I need to ask this question from Public Access Specifier topic in chapter Access Specifiers of Object Oriented Programming

Answer» CORRECT choice is (d) Default or Public

The 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.
36.

We can reduce the visibility of inherited methods.(a) True(b) FalseI had been asked this question in semester exam.The origin of the question is Public Access Specifier in division Access Specifiers of Object Oriented Programming

Answer»

Correct option is (B) False

Easy 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.

37.

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 subclassI had been asked this question in an online interview.I'd like to ask this question from Public Access Specifier topic in section Access Specifiers of Object Oriented Programming

Answer»

Correct choice is (a) All those will be AVAILABLE in subclasses

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.

38.

How many public class(s) (outermost) can be there in a java program?(a) 1(b) 2(c) 3(d) As requiredThe question was posed to me in exam.My question is taken from Public Access Specifier in chapter Access Specifiers of Object Oriented Programming

Answer»

The correct choice is (a) 1

To explain I WOULD say: 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.

39.

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 programThe question was posed to me in quiz.I'm obligated to ask this question of Public Access Specifier in section Access Specifiers of Object Oriented Programming

Answer»

The correct CHOICE is (a) The static MEMBERS can’t be PUBLIC

The explanation is: 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.

40.

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 instancesThe question was posed to me in an interview.The question is from Public Access Specifier topic in chapter Access Specifiers of Object Oriented Programming

Answer»

Correct answer is (d) Both classes can have INSTANCES

The best 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.

41.

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) ProtectedThis question was addressed to me in an international level competition.This interesting question is from Public Access Specifier topic in chapter Access Specifiers of Object Oriented Programming

Answer»

Right ANSWER is (c) Public

The 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.

42.

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 instanceI got this question in homework.I want to ask this question from Public Access Specifier in chapter Access Specifiers of Object Oriented Programming

Answer»

Right option is (d) Public classes can be accessed from any other class USING INSTANCE

To explain: 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.

43.

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 valueI had been asked this question at a job interview.My doubt stems from Public Access Specifier in division Access Specifiers of Object Oriented Programming

Answer»

The correct option is (c) None of its member function can be called outside the class

Easiest 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.

44.

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 spaceThe question was asked in my homework.My doubt is from Public Access Specifier topic in portion Access Specifiers of Object Oriented Programming

Answer»

Correct OPTION is (a) Will continue to get inherited subsequently

Easiest 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.

45.

Which specifier allows to secure the public members of base class in inherited classes?(a) Private(b) Protected(c) Public(d) Private and ProtectedI got this question by my school teacher while I was bunking the class.My question comes from Public Access Specifier topic in division Access Specifiers of Object Oriented Programming

Answer»

The CORRECT choice is (d) Private and Protected

The 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.