Object Oriented MCQ Set 1
1. How many types of access specifiers are provided in OOP (C++)?
a) 1
b) 2
c) 3
d) 4
Answer
Answer: c [Reason:] 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 together
Answer
Answer: d [Reason:] 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 three
Answer
Answer: a [Reason:] 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) Public
Answer
Answer: b [Reason:] Default access is used if the programmer doesn’t specify the specifier. This acts in similar way as that of private. But since nothing is specified we call it 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 default
Answer
Answer: c [Reason:] 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 also
Answer
Answer: c [Reason:] 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) Default
Answer
Answer: b [Reason:] 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 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 A
Answer
Answer: d [Reason:] 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 protected
Answer
Answer: b [Reason:] 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 added
Answer
Answer: a [Reason:] All the data members are counted to calculate the size of an object of a class. The data member’s 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 also
Answer
Answer: c [Reason:] 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.
Answer
Answer: a [Reason:] 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) Public
Answer
Answer: d [Reason:] 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) Public
Answer
Answer: a [Reason:] 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) Public
Answer
Answer: d [Reason:] 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.
Object Oriented MCQ Set 2
1. Which among the following are valid ways of overloading the operators?
a) Only using friend function
b) Only using member function
c) Either member functions or friend functions can be used
d) Operators can’t be overloaded
Answer
Answer: c [Reason:] The operators can be overloaded by using the member function or even the friend functions can be used. This is because both of these can access all the data members of a class.
2. Which among the following is mandatory condition for operators overloading?
a) Overloaded operator must be member function of the left operand
b) Overloaded operator must be member function of the right operand
c) Overloaded operator must be member function of either left or right operand
d) Overloaded operator must not be dependent on the operands
Answer
Answer: a [Reason:] The operator to be overloaded must be made the member function of the operand on left side of expressions to be used. This allows the compiler to identify whether the overloading has to be used or not. This rule also reduces the ambiguity in code.
3. When the operator to be overloaded becomes the left operand member then ________
a) The right operand acts as implicit object represented by *this
b) The left operand acts as implicit object represented by *this
c) Either right or left operand acts as implicit object represented by *this
d) *this pointer is not applicable in that member function
Answer
Answer: b [Reason:] The left operand becomes the object that is referred by *this pointer in the member function that will be called while using operator overloading. This is done to point to a specific object on which the overloading will be applied.
4. If the left operand is pointed by *this pointer, what happens to other operands?
a) Other operands are passed as function return type
b) Other operands are passed to compiler implicitly
c) Other operands must be passed using another member function
d) Other operands are passed as function arguments
Answer
Answer: d [Reason:] The operands that are used during overloading expect the left operand, can be passed as function arguments. Those are then referred in function definition with the names specified in the argument list.
5. If a friend overloaded operator have to be changed to member overloaded operator, which operator should be used with the class name?
a) Scope resolution operator
b) Colon
c) Arrow operator
d) Dot operator
Answer
Answer: a [Reason:] The scope resolution operator can be used followed by the class name. Then the operator keyword with the operator symbol that should be overloaded. This is done to use member function instead of friend function.
6. What is the syntax to overload an operator?
a) className::operator
b) className:operator
c) className.operator
d) className->operator
Answer
Answer: a [Reason:] The class name is followed by the scope resolution operator. This is done to specify the class to which the function should belong to. Then the keyword operator should be used in order to indicate the operator that is to be overloaded. Then come the parameters list to specify other operands.
7. Why the left parameter is removed from parameter list?
a) Because it is of no use
b) Because it is never used in definitions
c) Because it becomes parameter pointed by *this
d) Because it can’t be referred by *this pointer
Answer
Answer: c [Reason:] The left object is removed from being passed as a parameter, because it is implicitly passed. It is passed implicitly because it is considered the object with respect to which the overloading function is being called.
8. Which object’s members can be called directly while overloading operator function is used (In function definition)?
a) Left operand’s members
b) Right operand’s members
c) All operand members
d) None of the members
Answer
Answer: a [Reason:] This is because the left operand is passed implicitly. It is pointed by *this. This in turn means we can use the direct member names of the object because those are again converted to a syntax containing *this pointer implicitly.
9. If left operand’s member is specified directly in the function definition, which is the correct implicit conversion of that syntax?
a) *this className
b) *this parameterObject
c) *this returnedObject
d) *this object
Answer
Answer: d [Reason:] Since the left operands are passed implicitly, those object members can be accessed directly in the function definition. The compiler converts the syntax into the syntax that can be processed. The implicitly converted syntax contains *this pointer followed by the objectName that is left operand in the expression.
10. When the friend operator overloading is converted into member operator overloading ___
a) Two parameters of friend function remains same parameters in member operator overloading
b) Two parameters of friend function becomes only one parameter of member function
c) Two parameters of friend function are removed while using member function
d) Two parameters of friend function are made 4 in member operator overloading
Answer
Answer: b [Reason:] The friend function would accept two arguments if some binary operator is overloaded. When we try to convert that definition to member operator overloading then it becomes only one parameter. The reason behind is that the left operand is passed implicitly while using the member functions.
11. Where in the parameter list is the implicit *this is added?
a) Right most parameter
b) Anywhere in parameter list
c) Left most parameter
d) Not added to parameter list
Answer
Answer: c [Reason:] The left operand is passed implicitly by the compiler to the member function. But this is done, when the compiler adds the calling object as *this to the parameter list. It is always added as the left most parameter, i.e. the first parameter of the function.
12. Which operator among the following can be overloading using only member function?
a) Assignment operator
b) Addition operator
c) Subtraction operator
d) Multiplication and division operator
Answer
Answer: a [Reason:] Only the assignment operator among the options given must be overloaded using the member functions. The assignment operator can’t be overloaded using friend function. This is a restriction in the programming languages to make the programs more resistant towards errors.
13. Which operator among the following can be overloaded using both friend function and member function?
a) Assignment operator
b) Subscript
c) Member selection (arrow operator)
d) Modulus operator
Answer
Answer: d [Reason:] Only the modulus operator among the given operators can be overloaded using either friend function or member function. Other operators must be overloaded using only the member functions.
14. All the operators can be overloaded using the member function operator overloading.
a) True
b) False
Answer
Answer: b [Reason:] It is not the case that all the operators can be overloaded using the member operator overloading. There are some cases where the operators must be overloaded using the friend function only. The reason behind is that the left operand should be passed *this pointer, but the left operand in these cases might be object of some other class. Hence can’t be done.
15. Which operator among the following must be overloaded using the friend function?
a) << operator only
b) >> operator only
c) Both << and >> operators
d) It’s not mandatory to use friend function in any case
Answer
Answer: c [Reason:] In some cases it is mandatory to use the friend functions for overloading the operators. Here both the << and >> operators must be overloaded using friend function because the left operand is object of some other class and the right operand is usually of some different type.
Object Oriented MCQ Set 3
1. When value of an object is assigned to another object, ___
a) It becomes invalid statement
b) Its values gets copied into another object
c) Its values gets address of the existing values
d) The compiler doesn’t execute that statement
Answer
Answer: b [Reason:] The values get copied to another object. No address is assigned to the object values. This is uses copy constructor to copy the values.
2. If an object is created and another object is assigned to it, then ____
a) Copy constructor is called to copy the values
b) Object is copied directly to the object
c) Reference to another object is created
d) The new object is initialized to null values
Answer
Answer: c [Reason:] The new object created, refers to the same address of the previously created object. Now whenever new object changes any data member value, it will affect the previously existing object.
3. How the argument passed to a function get initialized?
a) Assigned using copy constructor at time of passing
b) Copied directly
c) Uses addresses always
d) Doesn’t get initialized
Answer
Answer: a [Reason:] The arguments get initialized using the copy constructor. There is need of assigning the value of all the members of an object to the local object of the function.
4. Predict the output of the program.
class A { public int i; }; void main() { A x; A y=x; x.i=10; y.i=20; y.i++; y.i=20; cout&l;<tx.i; }
a) 10
b) 20
c) 21
d) 0
Answer
Answer: b [Reason:] The expected output may be 10 because the value of member of object x is printed. But when object x is assigned to y, y points to the same address where x is stored. So actually both objects x and y point to the same location and refers to the same object.
5. If programmer doesn’t define any copy assignment operator then ____
a) Compiler gives an error
b) Program fails at run time
c) Compiler gives an implicit definition
d) Compiler can’t copy the member values
Answer
Answer: c [Reason:] While defining a copy constructor, we use reference const parameter, those are used for the assignment. The assignment operator may or may not be defined by the programmer, if not, compiler implicitly defines member wise copy assignment operator.
6. Declaring a copy constructor doesn’t suppresses the compiler generated copy assignment operator.
a) True
b) False
Answer
Answer: a [Reason:] Even if the programmer doesn’t define or even if they define the copy constructor. The compiler still generates a copy assignment operator. It doesn’t gets suppressed.
7. In copy constructor definition, if non const values are accepted only ________
a) Only const objects will be accepted
b) Only non – const objects are accepted
c) Only const members will not get copied
d) Compiler generates an error
Answer
Answer: b [Reason:] Only the non – const objects will be accepted by the compiler. If a const object is passed, the compiler produces an error. To reduce that, we use const argument in definition, so that both const and non – const objects are accepted.
8. How many objects can be assigned to a single address?
a) Only 1
b) At most 7
c) At most 3
d) As many as required
Answer
Answer: d [Reason:] The memory address can be referenced by more than one object. There is no maximum number defined. Any number of objects can reference to the same address.
9. Use of assignment operator ____
a) Changes its use, when used at declaration and in normal assignment
b) Doesn’t changes its use, whatever the syntax might be
c) Assignment takes place in declaration and assignment syntax
d) Doesn’t work in normal syntax, but only with declaration
Answer
Answer: a [Reason:] The assignment operator if used at declaration then it uses copy constructor for the copying of objects. If used in simple assignment syntax then it uses copy assignment function.
10. If more than one object refer to the same address, any changes made
a) Can be made visible to specific objects
b) Will be specific to one object only
c) From any object will be visible in all
d) Doesn’t changes the values of all objects
Answer
Answer: c [Reason:] At a memory address, only one object can be referenced. All the other objects which refer to the same memory address make changes for all of the objects referring that address.
11. How to make more than one object refer to the same object?
a) Initialize it to null
b) Initialize the object with another at declaration
c) Use constructor to create new object
d) Assign the address directly
Answer
Answer: b [Reason:] The object must get initialized with another object at time of declaration only. We don’t have to create a new object we just have to get name of new object because there after same address will be referred.
12. We can assign ______
a) Value of one reference variable to another
b) Value of any object to another
c) Value of any type to any object
d) Value of non – reference to another reference
Answer
Answer: a [Reason:] Only the reference value can be assigned to another reference value. This is because both deal with the address. There is no type mismatch hence we can assign them.
13. Assigning reference to an object _____
a) Will create another copy of the object
b) Will create two different copies of the object
c) Will not create any other copy of the object
d) Will not refer to the object
Answer
Answer: c [Reason:] When an object is assigned with another object. Same memory location is used. There is no other copy of the object created.
14. Which among the following is true?
a) We can use direct assignment for any object
b) We can use direct assignment only for different class objects
c) We must not use direct assignment
d) We can use direct assignment to same class objects
Answer
Answer: d [Reason:] The direct assignment can be used with the same class objects. There is no restriction on them. But better if the program have a predefined copy assignment operator.
15. Assigning objects takes place while passing the arguments.
a) True
b) False
Answer
Answer: b [Reason:] The actual assignment doesn’t take place as the object might have got passed by reference. Also even if not by reference, the copy constructor is called to copy the values into the new object and not exactly the assignment operator.
Object Oriented MCQ Set 4
1. Which is most appropriate definition of a base class?
a) It is parent of any of its derived class
b) It is child of one of the parent class
c) It is most basic class of whole program
d) It is class with maximum number of members
Answer
Answer: a [Reason:] A class which is parent of another class, or from which other classes can be derived, is known as a base class. It is mandatory that a class must have at least one derived class to be called as a base class.
2. A base class is also known as _______ class.
a) Basic
b) Inherited
c) Super
d) Sub
Answer
Answer: c [Reason:] A class which is being derived by other classes, is called as super class. This concept is clearly used in java as we call the functions of a base class by using the keyword super as required.
3. An abstract class is always a ____ class.
a) Base
b) Derived
c) Template
d) Nested
Answer
Answer: a [Reason:] Every abstract class is a base class. It must be so, because the functions which are not defined inside the abstract class, must be defined in the derived classes. Hence it becomes a base class.
4. How many base classes can a single class inherit in java?
a) 1
b) 2
c) 3
d) As many as required
Answer
Answer: a [Reason:] In java, multiple inheritance is not supported, which leads to the fact that a class can have only 1 parent class if inheritance is used. Only if interfaces are used then the class can implement more than one base class.
5. How to make a derived class a base class?
a) Change name of the class
b) Use keyword base
c) Make a class derive from it
d) Can’t be done
Answer
Answer: c [Reason:] Making another class derive from it will make that class as base class. It is not necessary that we have to write a different code for it. If at least one class derives that class, it becomes the base class for the new class.
6. If a base class is being derived by two other classes, which inheritance will that be called?
a) Single
b) Multiple
c) Multi-level
d) Hierarchical
Answer
Answer: d [Reason:] When more than one classes are being derived from a single parent class, the inheritance is known as hierarchical inheritance. This is usually useful when the base class is higher abstraction of its derived classes.
7. Which among the following must be in a base class?
a) Data members
b) Member functions
c) Access specifiers
d) Nothing
Answer
Answer: d [Reason:] Even a class which doesn’t have any members can be a base class. It is not mandatory to have any member or attribute in base class.
8. Which type of members can’t be accessed in derived classes of a base class?
a) Protected
b) Private
c) Public
d) All can be accessed
Answer
Answer: b [Reason:] The private members can be accessed only inside the base class. If the class is derived by other classes. Those members will not be accessible. This concept of OOP is made to make the members more secure.
9. If a class is enclosing more than one class, than it can be called as base class of those classes.
a) True
b) False
Answer
Answer: b [Reason:] When a class have more than one nested classes, it is known as enclosing class. It can’t be called as parent or base class since there is no inheritance involved.
10. Base class have ____ of abstraction.
a) Higher degree
b) Lower degree
c) Intermediate
d) Minimum degree
Answer
Answer: b [Reason:] A base class will have lesser information as compared to those of derived classes. Since derived classes inherit the base class properties and then add on their own features, they elaborate more hence have lower degree of abstraction.
11. Always the base class constructors are called _____ constructor of derived class.
a) Before
b) After
c) Along
d) According to priority of
Answer
Answer: a [Reason:] When the base class object is created, its constructor will be called for sure. But if derived class constructor is called, first base class constructor is called and then derived class constructor is taken into consideration.
12. Can we call methods of base class using constructor of the derived class?
a) Yes, always
b) Yes, but not always
c) No, never
d) No, but we can call in some cases
Answer
Answer: a [Reason:] If the function is defined in the base class, it can always be called from the constructor of its derived class. Since the constructors are not private, they can be accessed in derived class even if those are protected.
13. If a base class is inherited from another class and then one class derives it, which inheritance is shown?
a) Multiple
b) Single
c) Hierarchical
d) Multi-level
Answer
Answer: d [Reason:] If a base class is inherited from another class, single inheritance is shown. But when one more class inherits the derived class, this becomes a multi-level inheritance.
14. How many base classes can a single derived class have in C++?
a) 1
b) 2
c) 3
d) As many as required
Answer
Answer: d [Reason:] This is because C++ allows the multiple inheritance. A derived class can have more than one base class and hence can derive all of their features.
15. If a base class is added with few new members, its subclass must also be modified. (True/False)
a) True
b) False
Answer
Answer: b [Reason:] The base class can be added with new members without affecting the sub classes. This is because the sub classes may get some more features inherited but it won’t use them. But the base class will be able to use the new members as would be required.
Object Oriented MCQ Set 5
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
Answer: b [Reason:] 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) Vairbale 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
Answer: a [Reason:] 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
Answer: a [Reason:] The variables can be defined only once. Once the variable is defined, then it can’t be declared again. The definiton 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 new program
c) The source file must be included in the new file
d) The source file doesn’t matter for extern variables
Answer
Answer: c [Reason:] 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. Onlt 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 definiton of variables
c) Both declaration and definiton of variables
d) Neither declaration nor definition
Answer
Answer: a [Reason:] 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 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
Answer: d [Reason:] 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 funcion 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 cant be made extern
Answer
Answer: c [Reason:] 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. (True/False)
a) True
b) False
Answer
Answerr: b [Reason:] The statement is false. The variables are not extern by defalut. 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. Due to what, is the memory allocated for the extern variables?
a) Declaration
b) Definition
c) Including file
d) Memory is not allocated for extern variables
Answer
Answer: b [Reason:] The memory for the extern variables are allocaed 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
Answer: a [Reason:] 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
Answer: b [Reason:] 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
Answer: d [Reason:] The program gives the compiler time error. There is no definiton 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
Answer: a [Reason:] 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 inlcuded 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
Answer: c [Reason:] 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 definiton 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 program
d) Because those are visible throughout the program
Answer
Answer: a [Reason:] 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.