250+ TOP MCQs on Protected Access Specifier and Answers

Object Oriented Programming Question Bank 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
Answer: c
Clarification: 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.

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
Answer: a
Clarification: 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.

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
Answer: d
Clarification: 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.

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
Answer: a
Clarification: 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)?

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
Answer: c
Clarification: 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.

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
Answer: a
Clarification: 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
Answer: a
Clarification: 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
Answer: b
Clarification: 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
Answer: a
Clarification: 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
Answer: d
Clarification: 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
Answer: d
Clarification: 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
Answer: b
Clarification: 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
Answer: b
Clarification: 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
Answer: a
Clarification: 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
Answer: c
Clarification: 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.

To practice Object Oriented Programming Question Bank,

250+ TOP MCQs on Inheritance and Answers

Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) on “Inheritance”.

1. Which among the following best describes the Inheritance?
a) Copying the code already written
b) Using the code already written once
c) Using already defined functions in programming language
d) Using the data and functions into derived segment
Answer: d
Clarification: It can only be indicated by using the data and functions that we use in derived class, being provided by parent class. Copying code is nowhere similar to this concept, also using the code already written is same as copying. Using already defined functions is not inheritance as we are not adding any of our own features.

2. How many basic types of inheritance are provided as OOP feature?
a) 4
b) 3
c) 2
d) 1
Answer: a
Clarification: There are basically 4 types of inheritance provided in OOP, namely, single level, multilevel, multiple and hierarchical inheritance. We can add one more type as Hybrid inheritance but that is actually the combination any types of inheritance from the 4 basic ones.

3. Which among the following best defines single level inheritance?
a) A class inheriting a derived class
b) A class inheriting a base class
c) A class inheriting a nested class
d) A class which gets inherited by 2 classes
Answer: b
Clarification: A class inheriting a base class defines single level inheritance. Inheriting an already derived class makes it multilevel inheritance. And if base class is inherited by 2 other classes, it is multiple inheritance.

4. Which among the following is correct for multiple inheritance?
a) class student{public: int marks;}s; class stream{int total;}; class topper:public student, public stream{ };
b) class student{int marks;}; class stream{ }; class topper: public student{ };
c) class student{int marks;}; class stream:public student{ };
d) class student{ }; class stream{ }; class topper{ };
Answer: a
Clarification: Class topper is getting derived from 2 other classes and hence it is multiple inheritance. Topper inherits class stream and class student publicly and hence can use its features. If only few classes are defined, there we are not even using inheritance (as in option class student{ }; class stream{ }; class topper{ };).

5. Which programming language doesn’t support multiple inheritance?
a) C++ and Java
b) C and C++
c) Java and SmallTalk
d) Java
Answer: d
Clarification: Java doesn’t support multiple inheritance. But that feature can be implemented by using the interfaces concept. Multiple inheritance is not supported because of diamond problem and similar issues.

6. Which among the following is correct for a hierarchical inheritance?
a) Two base classes can be used to be derived into one single class
b) Two or more classes can be derived into one class
c) One base class can be derived into other two derived classes or more
d) One base class can be derived into only 2 classes
Answer: c
Clarification: One base class can be derived into the other two derived classes or more. If only one class gets derived by only 2 other classes, it is also hierarchical inheritance, but it is not a mandatory condition, because any number of derived classes can be there.

7. Which is the correct syntax of inheritance?
a) class derived_classname : base_classname{ /*define class body*/ };
b) class base_classname : derived_classname{ /*define class body*/ };
c) class derived_classname : access base_classname{ /*define class body*/ };
d) class base_classname :access derived_classname{ /*define class body*/ };
Answer: c
Clarification: Firstly, keyword class should come, followed by the derived class name. Colon is must followed by access in which base class has to be derived, followed by the base class name. And finally the body of class. Semicolon after the body is also must.

8. Which type of inheritance leads to diamond problem?
a) Single level
b) Multi-level
c) Multiple
d) Hierarchical
Answer: c
Clarification: When 2 or more classes inherit the same class using multiple inheritance and then one more class inherits those two base classes, we get a diamond like structure. Here, ambiguity arises when same function gets derived into 2 base classes and finally to 3rd level class because same name functions are being inherited.

9. Which access type data gets derived as private member in derived class?
a) Private
b) Public
c) Protected
d) Protected and Private
Answer: a
Clarification: It is a rule, that when a derived class inherits the base class in private access mode, all the members of base class gets derived as private members of the derived class.

10. If a base class is inherited in protected access mode then which among the following is true?
a) Public and Protected members of base class becomes protected members of derived class
b) Only protected members become protected members of derived class
c) Private, Protected and Public all members of base, become private of derived class
d) Only private members of base, become private of derived class
Answer: a
Clarification: As the programming language rules apply, all the public and protected members of base class becomes protected members of derived class in protected access mode. It can’t be changed because it would hinder the security of data and may add vulnerability in the program.

11. Members which are not intended to be inherited are declared as ________________
a) Public members
b) Protected members
c) Private members
d) Private or Protected members
Answer: c
Clarification: Private access specifier is the most secure access mode. It doesn’t allow members to be inherited. Even Private inheritance can only inherit protected and public members.

12. While inheriting a class, if no access mode is specified, then which among the following is true? (in C++)
a) It gets inherited publicly by default
b) It gets inherited protected by default
c) It gets inherited privately by default
d) It is not possible
Answer: c
Clarification: If the access mode is not specified during inheritance, the class is inherited privately by default. This is to ensure the security of data and to maintain OOP features. Hence it is not mandatory to specify the access mode if we want the class to be inherited privately.

13. If a derived class object is created, which constructor is called first?
a) Base class constructor
b) Derived class constructor
c) Depends on how we call the object
d) Not possible
Answer: a
Clarification: First the base class constructor is invoked. When we create a derived class object, the system tries to invoke its constructor but the class is derived so first the base class must be initialized, hence in turn the base class constructor is invoked before the derived class constructor.

14. The private members of the base class are visible in derived class but are not accessible directly.
a) True
b) False
Answer: a
Clarification: Consider that a variable is private in base class and the derived class uses public inheritance to inherit that class. Now if we also have a global variable of same name as that of base class private variable, neither the global variable nor the base class private variable will be accessible from derived class. This is because we can’t have 2 variables with same name in same local scope. Hence the private members are accessible but not directly.

15. How can you make the private members inheritable?
a) By making their visibility mode as public only
b) By making their visibility mode as protected only
c) By making their visibility mode as private in derived class
d) It can be done both by making the visibility mode public or protected
Answer: d
Clarification: It is not mandatory that you have to make the visibility mode either public or protected. You can do either of those. That will give you permission to inherit the private members of base class.

250+ TOP MCQs on Exception Handling and Answers

Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) on “Exception Handling”.

1. What is an exception?
a) Problem arising during compile time
b) Problem arising during runtime
c) Problem in syntax
d) Problem in IDE
Answer: b
Clarification: The problems that might occur during execution of a program are known as exceptions. The exceptions are unexpected sometimes and can be predicted. Also, the exceptions should be always considered for a better program.

2. Why do we need to handle exceptions?
a) To prevent abnormal termination of program
b) To encourage exception prone program
c) To avoid syntax errors
d) To save memory
Answer: a
Clarification: The exceptions should be handled to prevent any abnormal termination of a program. The program should keep running even if it gets interrupted in between. The program should preferable show the error occurred and then retry the process or just continue the program further.

3. An exception may arise when _______________
a) Input is fixed
b) Input is some constant value of program
c) Input given is invalid
d) Input is valid
Answer: c
Clarification: The exceptions may arise because the input given by the user might not be of the same type that a program can manage. If the input is invalid the program gets terminated.

4. If a file that needs to be opened is not found in the target location then _____________
a) Exception will be produced
b) Exceptions are not produced
c) Exception might get produced because of syntax
d) Exceptions are not produced because of logic
Answer: a
Clarification: The exceptions are produced when anything unexpected happened. The program might not be able to find a file in the target location and hence program produces an exceptions. The exception produced, then terminates the program.

5. Which is the universal exception handler class?
a) Object
b) Math
c) Errors
d) Exceptions
Answer: d
Clarification: Any type of exception can be handled by using class Exceptions. An object of this class is created which can manipulate the exception data. The data can be used to display the error or to run the program further based on error produced.

6. What are two exception classes in hierarchy of java exceptions class?
a) Runtime exceptions only
b) Compile time exceptions only
c) Runtime exceptions and other exceptions
d) Other exceptions
Answer: c
Clarification: The exceptions class is having two other derived classes which are of runtime exception handler and for other type of exceptions handling. The runtime exception handler is used to handle the exceptions produced during run time and same with case of other exceptions.

7. Which are the two blocks that are used to check error and handle the error?
a) Try and catch
b) Trying and catching
c) Do and while
d) TryDo and Check
Answer: a
Clarification: Two blocks that are used to check for errors and to handle the errors are try and catch block. The code which might produce some exceptions is placed inside the try block and then the catch block is written to catch the error that is produced. The error message or any other processing can be done in catch block if the error is produced.

8. There can be a try block without catch block but vice versa is not possible.
a) True
b) False
Answer: a
Clarification: The try block may or may not have any catch block. But a catch block can’t be there in a program if there is no try block. It is like else-block can only be written if and only if if-block is present in the program.

9. How many catch blocks can a single try block can have?
a) Only 1
b) Only 2
c) Maximum 127
d) As many as required
Answer: d
Clarification: There is no limit on the number of catch blocks corresponding to a try block. This is because the error can be of any type and for each type, a new catch block can be defined. This is to make sure all type of exceptions can be handled.

10. Which among the following is not a method of Throwable class?
a) public String getMessage()
b) public Throwable getCause()
c) public Char toString()
d) public void printStackTrace()
Answer: c
Clarification: Actually all the functions are available in throwable class. But the return type given in the option is wrong. The function toString returns string value. Hence the return type must be a String and not a char.

11. To catch the exceptions ___________________
a) An object must be created to catch the exception
b) A variable should be created to catch the exception
c) An array should be created to catch all the exceptions
d) A string have to be created to store the exception
Answer: a
Clarification: The object must be created of a specific class of which the error has occurred. If the type of error is unknown then we can use an object of class Exceptions. This object will be able to handle any kind of exception that a program might produce.

12. Multiple catch blocks __________________
a) Are mandatory for each try block
b) Can be combined into a single catch block
c) Are not possible for a try block
d) Can never be associated with a single try block
Answer: b
Clarification: The separate catch blocks for a single try block can be combined into a single catch block. All type of errors can be then handled in s single block. The type still have to be specified for the errors that might be produced.

13. Which symbol should be used to separate the type of exception handler classes in a single catch block?
a) ?
b) ,
c) –
d) |
Answer: d
Clarification: A pipe symbol can be used to separate different type of exceptions. The exceptions should always be given in proper sequence to ensure that no code remains unreachable. If not done properly the code might never be used in a program.

14. Which class is used to handle the input and output exceptions?
a) InputOutput
b) InputOutputExceptions
c) IOExceptions
d) ExceptionsIO
Answer: c
Clarification: There is a specific class to handle each type of exceptions that might be produced in a program. The input and output exceptions can be handled by an object of class IOExcceptions. This class handles all type of input and output exceptions.

15. Why do we use finally block?
a) To execute the block if exception occurred
b) To execute a code when exception is not occurred
c) To execute a code whenever required
d) To execute a code with each and every run of program
Answer: d
Clarification: Sometimes there is a need to execute a set of code every time the program runs. Even if the exception occurs and even if it doesn’t, there can be some code that must be executed at end of the program. That code is written in finally block. This block is always executed regardless of exceptions occurring.

250+ TOP MCQs on Extern Variable and Answers

Object Oriented Programming Questions Campus interviews on “Extern Variable”.

1. What is extern variable?
a) Variables to be used that are declared in another object file
b) Variables to be used that are declared in another source file
c) Variables to be used that are declared in another executable file
d) Variables to be used that are declared in another program
Answer: b
Clarification: The variables that are declared in another source file can be accessed in other files using extern variables. The extern variables must be mentioned explicitly. The source file is included to use its variables.

2. Which among the following is a correct statement for variables?
a) Variable can be declared many times
b) Variable can be declared only one time
c) Variable declaration can’t be done more than ones
d) Variable declaration is always done more than one time
Answer: a
Clarification: The variables can be declared any number of times. There is no restriction on how many times a single variables can be declared. Declaration is just an indication that the variable will be used in the program.

3. Which among the following is true for the variables?
a) Variable can be defined only once
b) Variable can be defined any number of times
c) Variable must be defined more than one time
d) Variable can be defined in different files
Answer: a
Clarification: The variables can be defined only once. Once the variable is defined, then it can’t be declared again. The definition of a variable is actual allocation of memory for the variable.

4. To use extern variable _____________________
a) The source file must not be included in the new file code
b) The source file itself must be used for a new program
c) The source file must be included in the new file
d) The source file doesn’t matter for extern variables
Answer: c
Clarification: The source file must be included in the file which needs to use the extern variable. This is done to ensure that the variables that are already declared can be used again. Only the declarations are used from one file to another.

5. What does a header file contain for an extern variable?
a) Only declaration of variables
b) Only definition of variables
c) Both declaration and definition of variables
d) Neither declaration nor definition
Answer: a
Clarification: The header file only contains the declaration of variables that are extern. It doesn’t contain any static variable definitions.

6. Which condition is true if the extern variable is used in a file?
a) All the header files declare it
b) Only few required files declare it
c) All header files declared it if required
d) Only one header file should declare it
Answer: d
Clarification: Only one header file should declare the extern variable to be used. There must not be more than one file declaring the same extern variable. This is to ensure that there is no ambiguity in using the extern variable.

7. Whenever a function is declared in a program _____________________
a) extern can be used only in some special cases
b) extern can’t be used
c) function is extern by default
d) it can’t be made extern
Answer: c
Clarification: Even if we don’t specify a function to be extern, by default all the functions are exter. The compiler adds the keyword at the beginning of the function declaration. If there is an extern function to be used then it will be used otherwise the new function only will be used.

8. Even if a variable is not declared as extern, it is extern by default.
a) True
b) False
Answerr: b
Clarification: The statement is false. The variables are not extern by default. If those are made extern by default, then the memory will never be allocated for those extern variables. Hence we make the variables extern explicitly.

9. Which of the following results in the allocation of memory for the extern variables?
a) Declaration
b) Definition
c) Including file
d) Memory is not allocated for extern variables
Answer: b
Clarification: The memory for the extern variables are allocated due to their definition. When the variables are declared, it only indicates the compiler that the variable is going to be used somewhere. But definition makes the compiler to allocate the memory for the variables.

10. Which is the correct syntax for extern variable declaration?
a) extern data_type variable_name;
b) extern variable_name;
c) data_type variable_name extern;
d) extern (data_type)variable_name;
Answer: a
Clarification: The syntax firstly contains the keyword extern. Then the data type of the variable is given. Then the variabel name is mentioned by which it will be used in the program.

11. Which is the correct syntax for extern function declaration?
a) extern function_name(argument_list);
b) extern return_type function_name(argument_list);
c) extern (return_type)function_name(argument_list);
d) return_type extern function_name(argument_list);
Answer: b
Clarification: The syntax must contain the keyword extern first, to denote that the function is extern. Though the function are extern by default but among the given choices, it should contain the keyword, for explicit declaration. Then the usual function declaration follows.

12. What will be the output of the program?

extern int var;
int main(void)
{
  var = 10;
  var++;
  cout<<var;
}

a) 10
b) 11
c) Run time error
d) Compile time error
Answer: d
Clarification: The program gives the compiler time error. There is no definition given for the extern variables. This is not allowed and hence we get a compile time error.

13. If the definition is given in the header file that we include then ________________
a) The program can run successfully
b) Also the program should define the extern variable
c) The extern variable must contain two definitions
d) Extern variable can’t be used in the program
Answer: a
Clarification: The program runs successfully. This is because only one definition of any variable is allowed. And hence the definition from the source file that is included will be used.

14. If extern variable is initialized with the declaration then _______________________
a) Also the header file with definition is required
b) The header file with definition must be included
c) There is no need to include any other header file for definition
d) The extern variable produces compile time error
Answer: c
Clarification: When the value for the extern variable is defined with its declaration, then there is no need to include any file for the definition of the variable. The Initialization acts as a definition for the extern variable in the file itself.

15. Why are functions extern by default?
a) Because functions are always private
b) Because those are not visible throughout the program
c) Because those can’t be accessed in all parts of the program
d) Because those are visible throughout the program
Answer: a
Clarification: The program have all of its functions visible throughout the program usually. Also, there is no specific value that a function must contain. Hence the functions are extern by default.

Object Oriented Programming for Campus Interviews,

250+ TOP MCQs on OOPs Basic Concepts and Answers

Object Oriented Programming online test on “OOP Basic Concepts”.

1. Which was the first purely object oriented programming language developed?
a) Java
b) C++
c) SmallTalk
d) Kotlin
Answer: c
Clarification: SmallTalk was the first programming language developed which was purely object oriented. It was developed by Alan Kay. OOP concept came into the picture in 1970’s.

2. Which of the following best defines a class?
a) Parent of an object
b) Instance of an object
c) Blueprint of an object
d) Scope of an object
Answer: c
Clarification: A class is Blueprint of an object which describes/ shows all the functions and data that are provided by an object of a specific class. It can’t be called as parent or instance of an object. Class in general describes all the properties of an object.

3. Who invented OOP?
a) Alan Kay
b) Andrea Ferro
c) Dennis Ritchie
d) Adele Goldberg
Answer: a
Clarification: Alan Kay invented OOP, Andrea Ferro was a part of SmallTalk Development. Dennis invented C++ and Adele Goldberg was in team to develop SmallTalk but Alan actually had got rewarded for OOP.

4. What is the additional feature in classes that was not in structures?
a) Data members
b) Member functions
c) Static data allowed
d) Public access specifier
Answer: b
Clarification: Member functions are allowed inside a class but were not present in structure concept. Data members, static data and public access specifiers were present in structures too.

5. Which is not feature of OOP in general definitions?
a) Code reusability
b) Modularity
c) Duplicate/Redundant data
d) Efficient Code
Answer: c
Clarification: Duplicate/Redundant data is dependent on programmer and hence can’t be guaranteed by OOP. Code reusability is done using inheritance. Modularity is supported by using different code files and classes. Codes are more efficient because of features of OOP.

6. Pure OOP can be implemented without using class in a program. (True or False)
a) True
b) False
Answer: b
Clarification: It’s false because for a program to be pure OO, everything must be written inside classes. If this rule is violated, the program can’t be labelled as purely OO.

7. Which Feature of OOP illustrated the code reusability?
a) Polymorphism
b) Abstraction
c) Encapsulation
d) Inheritance
Answer: d
Clarification: Using inheritance we can reuse the code already written and also can avoid creation of many new functions or variables, as that can be done one time and be reused, using classes.

8. Which language does not support all 4 types of inheritance?
a) C++
b) Java
c) Kotlin
d) Small Talk
Answer: b
Clarification: Java doesn’t support all 4 types of inheritance. It doesn’t support multiple inheritance. But the multiple inheritance can be implemented using interfaces in Java.

9. How many classes can be defined in a single program?
a) Only 1
b) Only 100
c) Only 999
d) As many as you want
Answer: d
Clarification: Any number of classes can be defined inside a program, provided that their names are different. In java, if public class is present then it must have the same name as that of file.

10. When OOP concept did first came into picture?
a) 1970’s
b) 1980’s
c) 1993
d) 1995
Answer: a
Clarification: OOP first came into picture in 1970’s by Alan and his team. Later it was used by some programming languages and got implemented successfully, SmallTalk was first language to use pure OOP and followed all rules strictly.

11. Why Java is Partially OOP language?
a) It supports usual declaration of primitive data types
b) It doesn’t support all types of inheritance
c) It allows code to be written outside classes
d) It does not support pointers
Answer: a
Clarification: As Java supports usual declaration of data variables, it is partial implementation of OOP. Because according to rules of OOP, object constructors must be used, even for declaration of variables.

12. Which concept of OOP is false for C++?
a) Code can be written without using classes
b) Code must contain at least one class
c) A class must have member functions
d) At least one object should be declared in code
Answer: b
Clarification: In C++, it’s not necessary to use classes, and hence codes can be written without using OOP concept. Classes may or may not contain member functions, so it’s not a necessary condition in C++. And, an object can only be declared in a code if its class is defined/included via header file.

13. Which header file is required in C++ to use OOP?
a) iostream.h
b) stdio.h
c) stdlib.h
d) OOP can be used without using any header file
Answer: d
Clarification: We need not include any specific header file to use OOP concept in C++, only specific functions used in code need their respective header files to be included or classes should be defined if needed.

14. Which of the two features match each other?
a) Inheritance and Encapsulation
b) Encapsulation and Polymorphism
c) Encapsulation and Abstraction
d) Abstraction and Polymorphism
Answer: c
Clarification: Encapsulation and Abstraction are similar features. Encapsulation is actually binding all the properties in a single class or we can say hiding all the features of object inside a class. And Abstraction is hiding unwanted data (for user) and showing only the data required by the user of program.

15. Which feature allows open recursion, among the following?
a) Use of this pointer
b) Use of pointers
c) Use of pass by value
d) Use of parameterized constructor
Answer: a
Clarification: Use of this pointer allows an object to call data and methods of itself whenever needed. This helps us call the members of an object recursively, and differentiate the variables of different scopes.

250+ TOP MCQs on Public Access Specifier and Answers

Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) on “Public Access Specifier”.

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

class A
{
	int marks;
	public : disp()
	{ 
		cout&lt;&lt;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
Answer: a
Clarification: 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.

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
Answer: a
Clarification: 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
Answer: d
Clarification: 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.

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
Answer: d
Clarification: 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
Answer: b
Clarification: 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.

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
Answer: a
Clarification: 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
Answer: a
Clarification: 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.

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
Answer: c
Clarification: 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
Answer: a
Clarification: 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
Answer: d
Clarification: 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
Answer: c
Clarification: 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
Answer: d
Clarification: 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
Answer: c
Clarification: 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
Answer: a
Clarification: 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
Answer: d
Clarification: 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.