This set of Object Oriented Programming online test focuses on “OOP Basic Concepts”.
1. Which was the first purely object oriented programming language developed?
a) Java
b) C++
c) SmallTalk
d) Kotlin
View Answer
Answer: c
Explanation: 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
View Answer
Answer: c
Explanation: 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
View Answer
Answer: a
Explanation: 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
View Answer
Answer: b
Explanation: 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
View Answer
Answer: c
Explanation: 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
View Answer
Answer: b
Explanation: 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
View Answer
Answer: d
Explanation: 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
View Answer
Answer: b
Explanation: 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
View Answer
Answer: d
Explanation: 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
View Answer
Answer: a
Explanation: 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
View Answer
Answer: a
Explanation: 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
View Answer
Answer: b
Explanation: 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
View Answer
Answer: d
Explanation: 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
View Answer
Answer: c
Explanation: 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
View Answer
Answer: a
Explanation: 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.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming for online tests, here is complete set of 1000+ Multiple Choice Questions and Answers.
Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Polymorphism”.
1. Which among the following best describes polymorphism?
a) It is the ability for a message/data to be processed in more than one form
b) It is the ability for a message/data to be processed in only 1 form
c) It is the ability for many messages/data to be processed in one way
d) It is the ability for undefined message/data to be processed in at least one way
View Answer
Answer: a
Explanation: It is actually the ability for a message / data to be processed in more than one form. The word polymorphism indicates many-forms. So if a single entity takes more than one form, it is known as polymorphism.
2. What do you call the languages that support classes but not polymorphism?
a) Class based language
b) Procedure Oriented language
c) Object-based language
d) If classes are supported, polymorphism will always be supported
View Answer
Answer: c
Explanation: The languages which support classes but doesn’t support polymorphism, are known as object-based languages. Polymorphism is such an important feature, that is a language doesn’t support this feature, it can’t be called as a OOP language.
3. Which among the following is the language which supports classes but not polymorphism?
a) SmallTalk
b) Java
c) C++
d) Ada
View Answer
Answer: d
Explanation: Ada is the language which supports the concept of classes but doesn’t support the polymorphism feature. It is an object-based programming language. Note that it’s not an OOP language.
4. If same message is passed to objects of several different classes and all of those can respond in a different way, what is this feature called?
a) Inheritance
b) Overloading
c) Polymorphism
d) Overriding
View Answer
Answer: c
Explanation: The feature defined in question defines polymorphism features. Here the different objects are capable of responding to the same message in different ways, hence polymorphism.
5. Which class/set of classes can illustrate polymorphism in the following code?
abstract class student
{
public : int marks;
calc_grade();
}
class topper:public student
{
public : calc_grade()
{
return 10;
}
};
class average:public student
{
public : calc_grade()
{
return 20;
}
};
class failed{ int marks; };
a) Only class student can show polymorphism
b) Only class student and topper together can show polymorphism
c) All class student, topper and average together can show polymorphism
d) Class failed should also inherit class student for this code to work for polymorphism
View Answer
Answer: c
Explanation: Since Student class is abstract class and class topper and average are inheriting student, class topper and average must define the function named calc_grade(); in abstract class. Since both the definition are different in those classes, calc_grade() will work in different way for same input from different objects. Hence it shows polymorphism.
6. Which type of function among the following shows polymorphism?
a) Inline function
b) Virtual function
c) Undefined functions
d) Class member functions
View Answer
Answer: b
Explanation: Only virtual functions among these can show polymorphism. Class member functions can show polymorphism too but we should be sure that the same function is being overloaded or is a function of abstract class or something like this, since we are not sure about all these, we can’t say whether it can show polymorphism or not.
7. In case of using abstract class or function overloading, which function is supposed to be called first?
a) Local function
b) Function with highest priority in compiler
c) Global function
d) Function with lowest priority because it might have been halted since long time, because of low priority
View Answer
Answer: b
Explanation: Function with highest priority is called. Here, it’s not about the thread scheduling in CPU, but it focuses on whether the function in local scope is present or not, or if scope resolution is used in some way, or if the function matches the argument signature. So all these things define which function has the highest priority to be called in runtime. Local function could be one of the answer but we can’t say if someone have used pointer to another function or same function name.
8. Which among the following can’t be used for polymorphism?
a) Static member functions
b) Member functions overloading
c) Predefined operator overloading
d) Constructor overloading
View Answer
Answer: a
Explanation: Static member functions are not property of any object. Hence it can’t be considered for overloading/overriding. For polymorphism, function must be property of object, not only of class.
9. What is output of the following program?
class student
{
public : int marks;
void disp()
{
cout<<”its base class”
};
class topper:public student
{
public :
void disp()
{
cout<<”Its derived class”;
}
}
void main() { student s; topper t;
s.disp();
t.disp();
}
a) Its base classIts derived class
b) Its base class Its derived class
c) Its derived classIts base class
d) Its derived class Its base class
View Answer
Answer: a
Explanation: You need to focus on how the output is going to be shown, no space will be given after first message from base class. And then the message from derived class will be printed. Function disp() in base class overrides the function of base class being derived.
10. Which among the following can show polymorphism?
a) Overloading ||
b) Overloading +=
c) Overloading <<
d) Overloading &&
View Answer
Answer: c
Explanation: Only insertion operator can be overloaded among all the given options. And the polymorphism can be illustrated here only if any of these is applicable of being overloaded. Overloading is type of polymorphism.
11. Find the output of the following program.
class education
{
char name[10];
public : disp()
{
cout<<”Its education system”;
}
class school:public education
{
public: void dsip()
{
cout<<”Its school education system”;
}
};
void main()
{
school s;
s.disp();
}
}
a) Its school education system
b) Its education system
c) Its school education systemIts education system
d) Its education systemIts school education system
View Answer
Answer: a
Explanation: Notice that the function name in derived class is different from the function name in base class. Hence when we call the disp() function, base class function is executed. No polymorphism is used here.
12. Polymorphism is possible in C language.
a) True
b) False
View Answer
Answer: a
Explanation: It is possible to implement polymorphism in C language, even though it doesn’t support class. We can use structures and then declare pointers which in turn points to some function. In this way we simulate the functions like member functions but not exactly member function. Now we can overload these functions, hence implementing polymorphism in C language.
13. Which problem may arise if we use abstract class functions for polymorphism?
a) All classes are converted as abstract class
b) Derived class must be of abstract type
c) All the derived classes must implement the undefined functions
d) Derived classes can’t redefine the function
View Answer
Answer: c
Explanation: The undefined functions must be defined is a problem, because one may need to implement few undefined functions from abstract class, but he will have to define each of the functions declared in abstract class. Being useless task, it is a problem sometimes.
14. Which among the following is not true for polymorphism?
a) It is feature of OOP
b) Ease in readability of program
c) Helps in redefining the same functionality
d) Increases overhead of function definition always
View Answer
Answer: d
Explanation: It never increases function definition overhead, one way or another if you don’t use polymorphism, you will use the definition in some other way, so it actually helps to write efficient codes.
15. If 2 classes derive one base class and redefine a function of base class, also overload some operators inside class body. Among these two things of function and operator overloading, where is polymorphism used?
a) Function overloading only
b) Operator overloading only
c) Both of these are using polymorphism
d) Either function overloading or operator overloading because polymorphism can be applied only once in a program
View Answer
Answer: d
Explanation: Both of them are using polymorphism. It is not necessary that polymorphism can be used only once in a program, it can be used anywhere, any number of times in a single program.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming (OOPs), here is complete set of 1000+ Multiple Choice Questions and Answers.
Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Constructors”.
1. Which among the following is called first, automatically, whenever an object is created?
a) Class
b) Constructor
c) New
d) Trigger
View Answer
Answer: b
Explanation: Constructors are the member functions which are called automatically whenever an object is created. It is a mandatory functions to be called for an object to be created as this helps in initializing the object to a legal initial value for the class.
2. Which among the following is not a necessary condition for constructors?
a) Its name must be same as that of class
b) It must not have any return type
c) It must contain a definition body
d) It can contains arguments
View Answer
Answer: c
Explanation: Constructors are predefined implicitly, even if the programmer doesn’t define any of them. Even if the programmer declares a constructor, it’s not necessary that it must contain some definition.
3. Which among the following is correct?
a) class student{ public: int student(){} };
b) class student{ public: void student (){} };
c) class student{ public: student{}{} };
d) class student{ public: student(){} };
View Answer
Answer: d
Explanation: The constructors must not have any return type. Also, the body may or may not contain any body. Defining default constructor is optional, if you are not using any other constructor.
4. In which access should a constructor be defined, so that object of the class can be created in any function?
a) Public
b) Protected
c) Private
d) Any access specifier will work
View Answer
Answer: a
Explanation: Constructor function should be available to all the parts of program where the object is to be created. Hence it is advised to define it in public access, so that any other function is able to create objects.
5. How many types of constructors are available for use in general (with respect to parameters)?
a) 2
b) 3
c) 4
d) 5
View Answer
Answer: a
Explanation: Two types of constructors are defined generally, namely, default constructor and parameterized constructor. Default constructor is not necessary to be defined always.
6. If a programmer defines a class and defines a default value parameterized constructor inside it.
He has not defined any default constructor. And then he try to create the object without passing arguments, which among the following will be correct?
a) It will not create the object (as parameterized constructor is used)
b) It will create the object (as the default arguments are passed)
c) It will not create the object (as the default constructor is not defined)
d) It will create the object (as at least some constructor is defined)
View Answer
Answer: b
Explanation: It will create the object without any problem, because the default arguments use the default value if no value is passed. Hence it is equal to default constructor with zero parameters. But it will not create the object if signature doesn’t match.
7. Default constructor must be defined, if parameterized constructor is defined and the object is to be created without arguments.
a) True
b) False
View Answer
Answer: a
Explanation: If the object is create without arguments and only parameterized constructors are used, compiler will give an error as there is no default constructor defined. And some constructor must be called so as to create an object in memory.
8. If class C inherits class B. And B has inherited class A. Then while creating the object of class C, what will be the sequence of constructors getting called?
a) Constructor of C then B, finally of A
b) Constructor of A then C, finally of B
c) Constructor of C then A, finally B
d) Constructor of A then B, finally C
View Answer
Answer: d
Explanation: While creating the object of class C, its constructor would be called by default. But, if the class is inheriting some other class, firstly the parent class constructor will be called so that all the data is initialized that is being inherited.
9. In multiple inheritance, if class C inherits two classes A and B as follows, which class constructor will be called first?
class A{ };
class B{ };
class C: public A, public B{ };
a) A()
b) B()
c) C()
d) Can’t be determined
View Answer
Answer: a
Explanation: Constructor of class A will be called first. This is because the constructors in multiple inheritance are called in the sequence in which they are written to be inherited. Here A is written first, hence it is called first.
10. Which among the following is true for copy constructor?
a) The argument object is passed by reference
b) It can be defined with zero arguments
c) Used when an object is passed by value to a function
d) Used when a function returns an object
View Answer
Answer: b
Explanation: It can’t be defined with zero number of arguments. This is because to copy one object to another, the object must be mentioned so that compiler can take values from that object.
11. If the object is passed by value to a copy constructor?
a) Only public members will be accessible to be copied
b) That will work normally
c) Compiler will give out of memory error
d) Data stored in data members won’t be accessible
View Answer
Answer: c
Explanation: Compiler runs out of memory. This is because while passing the argument by value, a constructor of the object will be called. That in turn called another object constructor for values, and this goes on. This is like a constructor call to itself, and this goes on infinite times, hence it must be passed by reference, so that the constructor is not called.
12. Which object will be created first?
class student
{
int marks;
};
student s1, s2, s3;
a) s1 then s2 then s3
b) s3 then s2 then s1
c) s2 then s3 then s1
d) all are created at same time
View Answer
Answer: a
Explanation: The objects are created in the sequence of how they are written. This happens because the constructors are called in the sequence of how the objects are mentioned. This is done in sequence.
13. Which among the following helps to create a temporary instance?
a) Implicit call to a default constructor
b) Explicit call to a copy constructor
c) Implicit call to a parameterized constructor
d) Explicit call to a constructor
View Answer
Answer: d
Explanation: Explicit call to a constructor can let you create a temporary instance. This is because the temporary instances doesn’t have any name. Those are deleted from memory as soon as their reference is removed.
14. Which among the following is correct for the class defined below?
class student
{
int marks;
public: student(){}
student(int x)
{
marks=x;
}
};
main()
{
student s1(100);
student s2();
student s3=100;
return 0;
}
a) Object s3, syntax error
b) Only object s1 and s2 will be created
c) Program runs and all objects are created
d) Program will give compile time error
View Answer
Answer: c
Explanation: It is a special case of constructor with only 1 argument. While calling a constructor with one argument, you are actually implicitly creating a conversion from the argument type to the type of class. Hence you can directly specify the value of that one argument with assignment operator.
15. For constructor overloading, each constructor must differ in ___________ and __________
a) Number of arguments and type of arguments
b) Number of arguments and return type
c) Return type and type of arguments
d) Return type and definition
View Answer
Answer: a
Explanation: Each constructor must differ in the number of arguments it accepts and the type of arguments. This actually defines the constructor signature. This helps to remove the ambiguity and define a unique constructor as required.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming (OOPs), here is complete set of 1000+ Multiple Choice Questions and Answers.
Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Overloading Constructors”.
1. Which among the following best describes constructor overloading?
a) Defining one constructor in each class of a program
b) Defining more than one constructor in single class
c) Defining more than one constructor in single class with different signature
d) Defining destructor with each constructor
View Answer
Answer: c
Explanation: If more than one constructors are defined in a class with same signature, then that results in error. The signatures must be different. So that the constructors can be differentiated.
2. Can constructors be overloaded in derived class?
a) Yes, always
b) Yes, if derived class has no constructor
c) No, programmer can’t do it
d) No, never
View Answer
Answer: d
Explanation: The constructor must be having the same name as that of a class. Hence a constructor of one class can’t even be defined in another class. Since the constructors can’t be defined in derived class, it can’t be overloaded too, in derived class.
3. Does constructor overloading include different return types for constructors to be overloaded?
a) Yes, if return types are different, signature becomes different
b) Yes, because return types can differentiate two functions
c) No, return type can’t differentiate two functions
d) No, constructors doesn’t have any return type
View Answer
Answer: d
Explanation: The constructors doesn’t have any return type. When we can’t have return type of a constructor, overloading based on the return type is not possible. Hence only parameters can be different.
4. Which among the following is possible way to overload constructor?
a) Define default constructor, 1 parameter constructor and 2 parameter constructor
b) Define default constructor, zero argument constructor and 1 parameter constructor
c) Define default constructor, and 2 other parameterized constructors with same signature
d) Define 2 default constructors
View Answer
Answer: a
Explanation: All the constructors defined in a class must have a different signature in order to be overloaded. Here one default and other parameterized constructors are used, wherein one is of only one parameter and other accepts two. Hence overloading is possible.
5. Which constructor will be called from the object created in the code below?
class A
{
int i;
A()
{
i=0; cout<<i;
}
A(int x=0)
{
i=x; cout<<I;
}
};
A obj1;
a) Default constructor
b) Parameterized constructor
c) Compile time error
d) Run time error
View Answer
Answer: c
Explanation: When a default constructor is defined and another constructor with 1 default value argument is defined, creating object without parameter will create ambiguity for the compiler. The compiler won’t be able to decide which constructor should be called, hence compile time error.
6. Which among the following is false for a constructor?
a) Constructors doesn’t have a return value
b) Constructors are always user defined
c) Constructors are overloaded with different signature
d) Constructors may or may not have any arguments being accepted
View Answer
Answer: b
Explanation: The constructors are not always user defined. The construct will be provided implicitly from the compiler if the used doesn’t defined any constructor. The implicit constructor makes all the string values null and allocates memory space for each data member.
7. When is the constructor called for an object?
a) As soon as overloading is required
b) As soon as class is derived
c) As soon as class is created
d) As soon as object is created
View Answer
Answer: d
Explanation: The constructor is called as soon as the object is created. The overloading comes into picture as to identify which constructor have to be called depending on arguments passed in the creation of object.
8. Which among the following function can be used to call default constructor implicitly in java?
a) this()
b) that()
c) super()
d) sub()
View Answer
Answer: a
Explanation: The function this() can be used to call the default constructor from inside any other constructor. This helps to further reuse the code and not to write the redundant data in all the constructors.
9. Why do we use constructor overloading?
a) To use different types of constructors
b) Because it’s a feature provided
c) To initialize the object in different ways
d) To differentiate one constructor from another
View Answer
Answer: c
Explanation: The constructors are overloaded to initialize the objects of a class in different ways. This allows us to initialize the object with either default values or used given values. If data members are not initialized then program may give unexpected results.
10. If programmer have defined parameterized constructor only, then __________________
a) Default constructor will not be created by the compiler implicitly
b) Default constructor will be created by the compiler implicitly
c) Default constructor will not be created but called at runtime
d) Compile time error
View Answer
Answer: a
Explanation: When the programmer doesn’t specify any default constructor and only defines some parameterized constructor. The compiler doesn’t provide any default constructor implicitly. This is because it is assumed that the programmer will create the objects only with constructors.
11. Which among the following is not valid in java?
a) Constructor overloading
b) Recursive constructor call
c) Default value constructors
d) String argument constructor
View Answer
Answer: b
Explanation: Java doesn’t provide the feature to recursively call the constructor. This is to eliminate the out of memory error in some cases that arises unexpectedly. That is an predefined condition for constructors in java.
12. Which constructor will be called from the object obj2 in the following program?
class A
{
int i;
A()
{
i=0;
}
A(int x)
{
i=x+1;
}
A(int y, int x)
{
i=x+y;
}
};
A obj1(10);
A obj2(10,20);
A obj3;
a) A(int x)
b) A(int y)
c) A(int y, int x)
d) A(int y; int x)
View Answer
Answer: c
Explanation: The two argument constructor will be called as we are passing 2 arguments to the object while creation. The arguments will be passed together and hence compiler resolves that two argument constructor have to be called.
13. What are we only create an object but don’t call any constructor for it in java?
a) Implicit constructor will be called
b) Object is initialized to some null values
c) Object is not created
d) Object is created but points to null
View Answer
Answer: d
Explanation: The object becomes a reference object which can be initialized will another object. Then this object will indirectly become another name of the object being assigned. If not assigned, it simply points to null address.
14. Which among the following is false?
a) Constructor can’t be overloaded in Kotlin
b) Constructors can’t be called recursively in java
c) Constructors can be overloaded in C++
d) Constructors overloading depends on different signatures
View Answer
Answer: a
Explanation: Kotlin language allows constructor overloading. This is a basic feature for the constructors. The constructor overloading allows the object to be initialized according to the user.
15. Which is correct syntax?
a) classname objectname= new() integer;
b) classname objectname= new classname;
c) classname objectname= new classname();
d) classname objectname= new() classname();
View Answer
Answer: c
Explanation: The syntax for object creating in java with calling a constructor for is it is as in option c. The syntax must contain the classname followed by the object name. The keyword new must be used and then the constructor call with or without the parameters as required.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming (OOPs), here is complete set of 1000+ Multiple Choice Questions and Answers.
Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Access Specifiers”.
1. How many types of access specifiers are provided in OOP (C++)?
a) 1
b) 2
c) 3
d) 4
View Answer
Answer: c
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 together
View Answer
Answer: d
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 three
View Answer
Answer: a
Explanation: 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
View Answer
Answer: b
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 default
View Answer
Answer: c
Explanation: 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
View Answer
Answer: c
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) Default
View Answer
Answer: b
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 A
View Answer
Answer: d
Explanation: 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
View Answer
Answer: b
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 added
View Answer
Answer: a
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 also
View Answer
Answer: c
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.
View Answer
Answer: a
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) Public
View Answer
Answer: d
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) Public
View Answer
Answer: a
Explanation: 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
View Answer
Answer: d
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.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming (OOPs), here is complete set of 1000+ Multiple Choice Questions and Answers.
Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
This set of Object Oriented Programming Question Bank focuses 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
View Answer
Answer: c
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.
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
View Answer
Answer: a
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.
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
View Answer
Answer: d
Explanation: 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
View Answer
Answer: a
Explanation: 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
View Answer
Answer: c
Explanation: 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
View Answer
Answer: a
Explanation: 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
View Answer
Answer: a
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.
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
View Answer
Answer: b
Explanation: 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
View Answer
Answer: a
Explanation: 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
View Answer
Answer: d
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.
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
View Answer
Answer: d
Explanation: 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
View Answer
Answer: b
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.
13. Protected access is same as default access that is given implicitly in java if no specifier is mentioned.
a) True
b) False
View Answer
Answer: b
Explanation: 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
View Answer
Answer: a
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.
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
View Answer
Answer: c
Explanation: 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.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice Object Oriented Programming Question Bank, here is complete set of 1000+ Multiple Choice Questions and Answers.
Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Data Members”.
1. What is the term used to indicate the variable and constants of a class?
a) Data members
b) Variables of class
c) Data characters
d) Constants
View Answer
Answer: a
Explanation: The variables inside a class are termed data members of the class. It is not a mandatory rule but variables are used to refer to usual variables used in functions or globally. The term is given because the values stored in those variables represent some kind of data related to class.
2. Data members ________________ (C++)
a) Can be initialized with declaration in classes
b) Can be initialized only with help of constructors
c) Can be initialized either in declaration or by constructor
d) Can’t be initialized
View Answer
Answer: b
Explanation: The data members are not property of class, those are property of the instances of the class. And the memory for the data members are not reserved until a constructor is called. Hence we use constructors for their initialization after the memory is reserved.
3. Which among the following is true for data members?
a) Private data members can be initialized with declaration in class
b) Static members are initialized in constructors
c) Protected data members can be initialized in class directly
d) Static data members are defined outside class, not in constructor
View Answer
Answer: d
Explanation: Static members are not property of instances of classes. Those are shared by all the object of classes. Hence those are defined outside the constructor, so as to make them common for all the objects.
4. What should be done for data member to be of user defined structure type?
a) The structure must have been defined before class.
b) The structure must have been defined after the class definition
c) The structure must be predefined
d) The structure type data members can’t be used
View Answer
Answer: a
Explanation: The structure must have been defined prior to its use. If the structure is not defined, then the memory space will not be allocated for its members. This leads to undefined use of new data types.
5. How many data members can a class contain?
a) 27
b) 255
c) 1024
d) As many as required
View Answer
Answer: d
Explanation: Any class can have as many data members as required. The only restriction that may arise is when there is not enough memory space. This gives flexibility to define a class with best properties possible.
6. How to access data members of a class?
a) Dot operator
b) Arrow operator
c) Dot or arrow as required
d) Dot, arrow or direct call
View Answer
Answer: c
Explanation: The data members can never be called directly. Dot operator is used to access the members with help of object of class. Arrow is usually used if pointers are used.
7. To create a pointer to a private data member of a class, outside the class, which among the following is correct?
a) Return the address of the private data member using a member function
b) Access the private member using a pointer outside class
c) Declare the member as pointer inside the class
d) Not possible to create pointer to a private member
View Answer
Answer: a
Explanation: We can call a public member function and return the address of any private data member. Though the pointer being returned must be defined inside class itself. And the returned address can be stored in a pointer.
8. Which among the following is true for use of setter() and getter() function?
a) Considered best for manipulating data values
b) Considered the only proper way to manipulate the values
c) Considered specially for private members manipulation
d) Considered a red flag, and not recommended for large scale use
View Answer
Answer: d
Explanation: This concept of getter and setter functions is not acceptable if used too much. This is considered to be inappropriate in OOP perspective. Though it is commonly used, it doesn’t work according to OOP concepts at some higher level of understanding.
9. What is the output of following code?
int n=10; // global
class A()
{
private : int n;
public : int m;
A()
{
n=100; m=50;
}
void disp()
{
cout<<”n”<<m<<n;
};
a) 1050100
b) 1005010
c) n5010
d) n50100
View Answer
Answer: d
Explanation: In cout we have specified n as a string to be printed. And m is a variable so its value gets printed. And global variable will not be used since local variable have more preference.
10. The static member functions can only use ________
a) Static data members
b) Private data members
c) Protected data members
d) Constant data members
View Answer
Answer: a
Explanation: The static member functions can only access static data members. This is because the static member function can’t work with the properties that change object to object. It is mandatory that only the common properties of all the objects be used. And only static data members are common to all as those are property of class.
11. A class can have self-referential data members.
a) True
b) False
View Answer
Answer: b
Explanation: The data members in a class can never refer to own class type. This is not possible because the data members should have some memory allocated for its object before the self-reference is used, but class must call constructor for that. Hence not possible.
12. What is the keyword used to make data members have same value?
a) static
b) const
c) double
d) abstract
View Answer
Answer: b
Explanation: The keyword const can be used anywhere to make the variable have same value all the time. This restriction is made to use the same value whenever required. Also, this can restrict accidental changes.
13. Which data members can be inherited but are private to a class?
a) Private
b) Protected
c) Protected and Static
d) Privately inherited
View Answer
Answer: b
Explanation: Static members inheritance also depends on the type of specifier they have. Only the protected members can be inherited but remain private to class. If static members are defined in private access, they won’t be allowed for inheritance.
14. The arguments passed to member functions by reference are considered as data members of class.
a) True
b) False
View Answer
Answer: b
Explanation: This is a wrong statement. As only the data defined inside class is considered as its member. But even if a variable is passed by reference it would be the same variable that is outside the class. Hence it can’t be considered class member.
15. Which among the following is not allowed for data member declaration?
a) int a;
b) static int a;
c) abstract a;
d) Boolean a;
View Answer
Answer: c
Explanation: The abstract keyword in the declaration of data members is not allowed. This is because the abstract keyword features can’t be used with the data members of the class. We can have all other syntax given, but not abstract.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming (OOPs), here is complete set of 1000+ Multiple Choice Questions and Answers.
Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Local Class”.
1. What are local classes?
 This is somewhat similar concept that we use to denote variables of a function. The classes declared inside functions will be local to them.
2. All member functions of a local class must be ___________
a) Defined outside class body
b) Defined outside the function definition
c) Defined inside the class body
d) Defined at starting of program
View Answer
Answer: c
Explanation: There is a restriction on where the member functions of the local class should be define. Those must be defined inside the class body only. This is to reduce the ambiguity and complexity of program.
3. Can local class members access/use the general local variables (except static, abstract etc.) of the function in which it is defined?
a) Yes, it can access with arrow operator
b) No, it can’t access with dot operator
c) Yes, it can access using dot operator
d) No, it can’t access In anyway
View Answer
Answer: d
Explanation: The local variables of the functions are not available to the member functions of the class. This is done to reduce the ambiguity in variables and their access rules.
4. Which type of data can a local class access from the function in which it is defined?
a) Static and extern
b) Abstract and static
c) Void and extern
d) Const and static
View Answer
Answer: a
Explanation: The local classes have this feature to access the static and extern variables of the function in which those are defined. This feature is available since these type of data are common to the program and is created only one time. Run time creation and destruction of these variables is not done. The only restriction that may apply is those members must be constants.
5. Local classes can access the type names and enumerators defined by the enclosing function.
a) True
b) False
View Answer
Answer: a
Explanation: This is a little tricky part with local classes. Though the local class can’t access the general variables of the function but can access the types that are defined inside the function. This is because the whole definition of that type would be existing inside the class.
6. Can static variables be declared inside a local class?
a) Yes, with public access specifier
b) Yes, anywhere as required
c) No, not possible in private access specifier
d) No, not possible anyway
View Answer
Answer: d
Explanation: No, the static variables can’t be declared inside a local class. This is because each time the function is called, all the variables get created again and are destroyed as soon as the function is returned. This would have been possible id the static variable was of function.
7. All the member functions of local classes are __________ by default.
a) Static
b) Inline
c) Abstract
d) Virtual
View Answer
Answer: c
Explanation: All the members are defined inside the class body. And when the member functions are defined inside the class body, they are made inline by default. If the definition is too complex, those are made normal functions.
8. The enclosing function has no special access to the members of the local class.
a) True
b) False
View Answer
Answer: a
Explanation: This is a rule that the enclosing function doesn’t have any special access to the members of the local class. This is done to maintain the security of class members. And to adhere to the rules of OOP.
9. Which language can use inheritance with local classes?
a) Kotlin
b) Java
c) SmallTalk
d) SAP ABAP
View Answer
Answer: d
Explanation: Other language might support inheritance with local classes but those doesn’t provide all the proper features of inheritance. Language SAP ABAP provides a way to implement inheritance with local classes efficiently.
10. How many local classes can be defined inside a single function?
a) Only 1
b) Only 3
c) Only 5
d) As many as required
View Answer
Answer: d
Explanation: The local classes can be defined as required. There is no restriction on the number of local classes that can be defined inside a function. But all those classes must follow the rules and restrictions.
11. All the data members of local class must be ___________
a) Defined with declaration
b) Defined in constructor
c) Declared and defined in constructor
d) Declared using a member function
View Answer
Answer: b
Explanation: The data members follow the same rules as of simple classes. Hence the data members must be declared first. Then their definition must be given using the constructors.
12. Can two different functions have local class with same name?
a) Yes, since local
b) No, names must be different
c) No, scope doesn’t work here
d) No, ambiguity arises
View Answer
Answer: a
Explanation: The local classes can have same name if they belong to different functions. The classes would be local to those specific functions and hence can have same name. This is same as that of local variables concept.
13. What is the scope of local class?
a) Within the class only
b) Within the function
c) Within the program
d) One time creation and live till end of program
View Answer
Answer: b
Explanation: The scope of a local class is limited only within the function definition. The function can use the class as usual as local variables. The class gets destroyed as soon as the function is returned.
14. Can a function, other than the enclosing function of local class, access the class members?
a) Yes, using object
b) Yes, using direct call
c) Yes, using pointer
d) No, can’t access
View Answer
Answer: d
Explanation: The local classes are local to the specific enclosing function. Other functions can’t access the class. Even if the pointers are used, the class must be alive when the pointer is used. But this will not happen if the enclosing function is returned.
15. Which among the following is the main advantage of using local classes?
a) Make program more efficient
b) Makes program execution faster
c) Helps to add extra functionality to a function
d) Helps to add more members to a function
View Answer
Answer: c
Explanation: The closest answer is to add more functionalities to a function or to make some specific functions to be generic. Adding more members to a function can be done directly but to add some special functionality that are encapsulated, can be done using local classes.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming (OOPs), here is complete set of 1000+ Multiple Choice Questions and Answers.
Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
This set of Object Oriented Programming Interview Questions and Answers focuses on “Passing and Returning Object with Functions”.
1. In how many ways can an object be passed to a function?
a) 1
b) 2
c) 3
d) 4
View Answer
Answer: c
Explanation: The objects can be passed in three ways. Pass by value, pass by reference and pass by address. These are the general ways to pass the objects to a function.
2. If an object is passed by value _____________
a) A new copy of object is created implicitly
b) The object itself is used
c) Address of the object is passed
d) A new object is created with new random values
View Answer
Answer: a
Explanation: When an object is passed by value, a new object is created implicitly. This new object uses the implicit values assignment, same as that of the object being passed.
3. Pass by address passes the address of object _________ and pass by reference passes the address of the object _________
a) Explicitly, explicitly
b) Implicitly, implicitly
c) Explicitly, Implicitly
d) Implicitly, explicitly
View Answer
Answer: c
Explanation: Pass by address uses the explicit address passing to the function whereas pass by reference implicitly passes the address of the object.
4. If an object is passed by reference, the changes made in the function ___________
a) Are reflected to the main object of caller function too
b) Are reflected only in local scope of the called function
c) Are reflected to the copy of the object that is made during pass
d) Are reflected to caller function object and called function object also
View Answer
Answer: a
Explanation: When an object is passed by reference, its address is passed implicitly. This will make changes to the main function whenever any modification is done.
5. Constructor function is not called when an object is passed to a function, will its destructor be called when its copy is destroyed?
a) Yes, depending on code
b) Yes, must be called
c) No, since no constructor was called
d) No, since same object gets used
View Answer
Answer: b
Explanation: Even though the constructor is not called when the object is passed to a function, the copy of the object is still created, where the values of the members are same. When the object have to be destroyed, the destructor is called to free the memory and resources that the object might have reserved.
6. When an object is returned by a function, a _______________ is automatically created to hold the return value.
a) Temporary object
b) Virtual object
c) New object
d) Data member
View Answer
Answer: a
Explanation: The temporary object is created. It holds the return value. The values gets assigned as required, and the temporary object gets destroyed.
7. Is the destruction of temporary object safe (while returning object)?
a) Yes, the resources get free to use
b) Yes, other objects can use the memory space
c) No, unexpected side effects may occur
d) No, always gives rise to exceptions
View Answer
Answer: c
Explanation: The destruction of temporary variable may give rise to unexpected logical errors. Consider the destructor which may free the dynamically allocated memory. But this may abort the program if another is still trying to copy the values from that dynamic memory.
8. How to overcome the problem arising due to destruction of temporary object?
a) Overloading insertion operator
b) Overriding functions can be used
c) Overloading parenthesis or returning object
d) Overloading assignment operator and defining copy constructor
View Answer
Answer: d
Explanation: The problem can be solved by overloading the assignment operator to get the values that might be getting returned while the destructor free the dynamic memory. Defining copy constructor can help us to do this in even simpler way.
9. How many objects can be returned at once?
a) Only 1
b) Only 2
c) Only 16
d) As many as required
View Answer
Answer: a
Explanation: Like any other value, only one object can be returned at ones. The only possible way to return more than one object is to return address of an object array. But that again comes under returning object pointer.
10. What will be the output of the following code?
Class A
{
int i;
public : A(int n)
{
i=n; cout<<”inside constructor ”;
}
~A()
{
cout<<”destroying ”<<i;
}
void seti(int n)
{
i=n;
}
int geti()
{
return I;
}
};
void t(A ob)
{
cout<<”something ”;
}
int main()
{
A a(1);
t(a);
cout<<”this is i in main ”;
cout<<a.geti();
}
a) inside constructor something destroying 2this is i in main destroying 1
b) inside constructor something this is i in main destroying 1
c) inside constructor something destroying 2this is i in main
d) something destroying 2this is i in main destroying 1
View Answer
Answer: a
Explanation: Although the object constructor is called only ones, the destructor will be called twice, because of destroying the copy of the object that is temporarily created. This is the concept of how the object should be passed and manipulated.
11. It is necessary to return the object if it was passed by reference to a function.
a) Yes, since the object must be same in caller function
b) Yes, since the caller function needs to reflect the changes
c) No, the changes are made automatically
d) No, the changes are made explicitly
View Answer
Answer: c
Explanation: Having the address being passed to the function, the changes are automatically made to the main function. In all the cases if the address is being used, the same memory location will be updated with new values.
12. How many objects can be passed to a function simultaneously?
a) Only 1
b) Only an array
c) Only 1 or an array
d) As many as required
View Answer
Answer: d
Explanation: There is no limit to how many objects can be passed. This works in same way as that any other variable gets passed. Array and object can be passed at same time also.
13. If an object is passed by address, will be constructor be called?
a) Yes, to allocate the memory
b) Yes, to initialize the members
c) No, values are copied
d) No, temporary object is created
View Answer
Answer: c
Explanation: A copy of all the values is created. If the constructor is called, there will be a compile time error or memory shortage. This happens because each time a constructor is called, it try to call itself again and that goes infinite times.
14. Is it possible that an object of is passed to a function, and the function also have an object of same name?
a) No, Duplicate declaration is not allowed
b) No, 2 objects will be created
c) Yes, Scopes are different
d) Yes, life span is different
View Answer
Answer: a
Explanation: There can’t be more than one variable or object with the same name in same scope. The scope is same, since the object is passed, it becomes local to function and hence function can’t have one more object of same name.
15. Passing an object using copy constructor and pass by value are same.
a) True
b) False
View Answer
Answer: b
Explanation: The copy constructor is used to copy the values from one object to other. Pass by values is not same as copy constructor method. Actually the pass by value method uses a copy constructor to copy the values in a local object.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming for Interviews, here is complete set of 1000+ Multiple Choice Questions and Answers.
Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Object Array”.
1. What is an array of objects?
a) An array of instances of class represented by single name
b) An array of instances of class represented by more than one name
c) An array of instances which have more than 2 instances
d) An array of instances which have different types
View Answer
Answer: a
Explanation: The array of objects an array of instances of a class. The array is represented by a single name. The array name is itself a pointer. Array name represents the first object.
2. Which among the following is a mandatory condition for array of objects?
a) All the objects should be of different class
b) All the objects should be of same program classes
c) All the objects should be of same class
d) All the objects should have different data
View Answer
Answer: c
Explanation: The objects of an array must be of same class. This is mandatory because array is set of same type of elements. The objects of same class are considered to be of same type.
3. What is the type of elements of array of objects?
a) Class
b) Void
c) String
d) Null
View Answer
Answer: a
Explanation: The class itself is the type of elements of array of objects. All the objects possess the same properties. Like any other primitive data type, the objects are of their respective class type.
4. If array of objects is declared as given below, which is the limitation on objects?
Class_name arrayName[size];
a) The objects will have same values
b) The objects will not be initialized individually
c) The objects can never be initialized
d) The objects will have same data
View Answer
Answer: b
Explanation: If the syntax given, is used to declare the array of objects, then the objects can’t be initialized individually. All the objects will have to be initialized after this declaration.
5. Which is the condition that must be followed if the array of objects is declared without initialization, only with size of array?
a) The class should have separate constructor for each object
b) The class must have no constructors
c) The class should not have any member function
d) The class must have a default or zero argument constructor
View Answer
Answer: d
Explanation: The class must have a default/zero argument constructor. Since the declaration is done by only specifying the size of array, the class must have default a construct to be called by default to reserve memory for each object. Also, we can’t specify the arguments in this type of declaration hence the class should provide a default initialization.
6. When are the array of objects without any initialization useful?
a) When object data is not required just after the declaration
b) When initialization of object data is to be made by the compiler
c) When object data doesn’t matter in the program
d) When the object should contain garbage data
View Answer
Answer: a
Explanation: Sometimes the object data is not mandatory to be used just after the declaration or may be the program requires the data to be updated according to what user inputs. Hence only declaration us also useful.
7. If constructor arguments are passed to objects of array then ____________ if the constructors are overloaded.
a) It is mandatory to pass same number of arguments to all the objects
b) It is mandatory to pass same type of arguments to all the objects
c) It is not mandatory to call same constructor for all the objects
d) It is mandatory to call same constructor for all the constructors
View Answer
Answer: c
Explanation: It is not mandatory to call the same constructor for all the objects in an array if initialized with the declaration. The objects can be passed with different set of arguments in the same syntax, separated by commas.
8. How the objects of array can be denoted?
a) Indices
b) Name
c) Random numbers
d) Alphabets
View Answer
Answer: a
Explanation: Different objects in an array can be denoted with the indices of array. The first object is denoted by 0. And the further indices denote the next objects in sequence of array.
9. The objects in an object array _______________________
a) Can be created without use of constructor
b) Can be created without calling default constructor
c) Can’t be created with use of constructor
d) Can’t be created without calling default constructor
View Answer
Answer: b
Explanation: The objects need some constructor to get the memory spaced reserved for those. If the default constructor is not used then we can use some arguments constructor which will reserve the memory for the objects. The objects can be passed with constructor arguments during declaration.
10. The Object array is created in _____________________
a) Heap memory
b) Stack memory
c) HDD
d) ROM
View Answer
Answer: a
Explanation: If the object arrays are declared dynamically, then the memory will be reserved on heap. The memory for objects will be on stack only if some constructor or some call and return tasks are happening. The program doesn’t run on HDD and ROM is not used for the execution of programs.
11. If an array of objects is of size 10 and a data value have to be retrieved from 5th object then ________________ syntax should be used.
a) Array_Name[4].data_variable_name;
b) Data_Type Array_Name[4].data_variable_name;
c) Array_Name[4].data_variable_name.value;
d) Array_Name[4].data_variable_name(value);
View Answer
Answer: a
Explanation: The array name with the index of fifth element is called, i.e. index 4. Then the dot operator is used to access the data member of that object. This Allows us to access the data members of all the objects in an object array.
12. Can we have two dimensional object array?
a) Yes, always
b) Yes, only if primitive type array
c) No, since two indices are impossible
d) No, never
View Answer
Answer: a
Explanation: A two dimensional array can always be created. There is no rule that only primitive type objects can have more than one dimension. The object array can also be made 2 dimensional.
13. From which index does the array of objects start?
a) 0
b) 1
c) 2
d) 3
View Answer
Answer: a
Explanation: The index must start from 0. The index ends at size – 1 index. This is because the index is always till n-1 where n is the total number of beads.
14. Two dimensional array can’t be initialized with the declaration.
a) True
b) False
View Answer
Answer: b
Explanation: The two dimensional arrays can also be initialized using curly brackets. For each set, values in curly bracket. And then another bracket is added at first and end. This ensures that all the code belongs to the user.
15. Is an array of characters always a string?
a) Yes, always
b) Yes, if each character is terminated by null
c) No, since each character is terminated by null
d) No, never
View Answer
Answer: d
Explanation: The character arrays are not the same as string. The string once created then remains the same. The character array values may change.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming (OOPs), here is complete set of 1000+ Multiple Choice Questions and Answers.
Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Abstract Class”.
1. Which among the following best describes abstract classes?
a) If a class has more than one virtual function, it’s abstract class
b) If a class have only one pure virtual function, it’s abstract class
c) If a class has at least one pure virtual function, it’s abstract class
d) If a class has all the pure virtual functions only, then it’s abstract class
View Answer
Answer: c
Explanation: The condition for a class to be called abstract class is that it must have at least one pure virtual function. The keyword abstract must be used while defining abstract class in java.
2. Can abstract class have main() function defined inside it?
a) Yes, depending on return type of main()
b) Yes, always
c) No, main must not be defined inside abstract class
d) No, because main() is not abstract function
View Answer
Answer: b
Explanation: This is a property of abstract class. It can define main() function inside it. There is no restriction on its definition and implementation.
3. If there is an abstract method in a class then, ________________
a) Class must be abstract class
b) Class may or may not be abstract class
c) Class is generic
d) Class must be public
View Answer
Answer: a
Explanation: It is a rule that if a class have even one abstract method, it must be an abstract class. If this rule was not made, the abstract methods would have got skipped to get defined in some places which are undesirable with the idea of abstract class.
4. If a class is extending/inheriting another abstract class having abstract method, then _______________________
a) Either implementation of method or making class abstract is mandatory
b) Implementation of the method in derived class is mandatory
c) Making the derived class also abstract is mandatory
d) It’s not mandatory to implement the abstract method of parent class
View Answer
Answer: a
Explanation: Either of the two things must be done, either implementation or declaration of class as abstract. This is done to ensure that the method intended to be defined by other classes gets defined at every possible class.
5. Abstract class A has 4 virtual functions. Abstract class B defines only 2 of those member functions as it extends class A. Class C extends class B and implements the other two member functions of class A. Choose the correct option below.
a) Program won’t run as all the methods are not defined by B
b) Program won’t run as C is not inheriting A directly
c) Program won’t run as multiple inheritance is used
d) Program runs correctly
View Answer
Answer: d
Explanation: The program runs correctly. This is because even class B is abstract so it’s not mandatory to define all the virtual functions. Class C is not abstract but all the virtual functions have been implemented will that class.
6. Abstract classes can ____________________ instances.
a) Never have
b) Always have
c) Have array of
d) Have pointer of
View Answer
Answer: a
Explanation: When an abstract class is defined, it won’t be having the implementation of at least one function. This will restrict the class to have any constructor. When the class doesn’t have constructor, there won’t be any instance of that class.
7. We ___________________ to an abstract class.
a) Can create pointers
b) Can create references
c) Can create pointers or references
d) Can’t create any reference, pointer or instance
View Answer
Answer: c
Explanation: Even though there can’t be any instance of abstract class. We can always create pointer or reference to abstract class. The member functions which have some implementation inside abstract itself can be used with these references.
8. Which among the following is an important use of abstract classes?
a) Header files
b) Class Libraries
c) Class definitions
d) Class inheritance
View Answer
Answer: b
Explanation: The abstract classes can be used to create a generic, extensible class library that can be used by other programmers. This helps us to get some already implemented codes and functions that might have not been provided by the programming language itself.
9. Use of pointers or reference to an abstract class gives rise to which among the following feature?
a) Static Polymorphism
b) Runtime polymorphism
c) Compile time Polymorphism
d) Polymorphism within methods
View Answer
Answer: b
Explanation: The runtime polymorphism is supported by reference and pointer to an abstract class. This relies upon base class pointer and reference to select the proper virtual function.
10. The abstract classes in java can _________________
a) Implement constructors
b) Can’t implement constructor
c) Can implement only unimplemented methods
d) Can’t implement any type of constructor
View Answer
Answer: a
Explanation: The abstract classes in java can define a constructor. Even though instance can’t be created. But in this way, only during constructor chaining, constructor can be called. When instance of concrete implementation class is created, it’s known as constructor chaining.
11. Abstract class can’t be final in java.
a) True
b) False
View Answer
Answer: a
Explanation: If an abstract class is made final in java, it will stop the abstract class from being extended. And if the class is not getting extended, there won’t be another class to implement the virtual functions. Due to this contradicting fact, it can’t be final in java.
12. Can abstract classes have static methods (Java)?
a) Yes, always
b) Yes, but depends on code
c) No, never
d) No, static members can’t have different values
View Answer
Answer: a
Explanation: There is no restriction on declaring static methods. The only condition is that the virtual functions must have some definition in the program.
13. It is _________________________ to have an abstract method.
a) Not mandatory for an static class
b) Not mandatory for a derived class
c) Not mandatory for an abstract class
d) Not mandatory for parent class
View Answer
Answer: c
Explanation: Derived, parent and static classes can’t have abstract method (We can’t say what type of these classes is). And for abstract class it’s not mandatory to have abstract method. But if any abstract method is there inside a class, then class must be abstract type.
14. How many abstract classes can a single program contain?
a) At most 1
b) At least 1
c) At most 127
d) As many as required
View Answer
Answer: d
Explanation: There is no restriction on the number of abstract classes that can be defined inside a single program. The programs can use as many abstract classes as required. But the functions with no body must be implemented.
15. Is it necessary that all the abstract methods must be defined from an abstract class?
a) Yes, depending on code
b) Yes, always
c) No, never
d) No, if function is not used, no definition is required
View Answer
Answer: b
Explanation: That is the rule of programming language that each function declared, must have some definition. There can’t be some abstract method that remains undefined. Even if it’s there, it would result in compile time error.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming (OOPs), here is complete set of 1000+ Multiple Choice Questions and Answers.
Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Derived Class”.
1. Which among the following is best definition of a derived class?
a) A child class
b) A class which inherits one or more classes
c) A class with keyword derived
d) A class with more than one constructor
View Answer
Answer: b
Explanation: Any class which inherits one or more classes is a derived class. The only condition is it must inherit at least one class in order to be called as a derived class.
2. Which among the following is inherited by a derived class from base class?
a) Data members only
b) Member functions only
c) All the members except private members
d) All the members of base class
View Answer
Answer: c
Explanation: The class inheriting another class, inherits all the data members and member functions that are not private. This is done to ensure the security features with maximum flexibility.
3. If there is a derived class in a program, how many classes must be in that program?
a) 1
b) 2
c) 3
d) 4
View Answer
Answer: b
Explanation: If there is a derived class in a program, there must be at least 2 classes in that program. One is a base class and another derived class. Hence at least 2 classes must be there.
4. Which members can never be accessed in derived class from the base class?
a) Private
b) Protected
c) Public
d) All except private
View Answer
Answer: d
Explanation: There is no restriction for a derived class to access the members of the base class until and unless the members are private. Private member are declared so that those members are not accessible outside the class.
5. How many types of inheritance are supported in C++ for deriving a class?
a) 1
b) 2
c) 3
d) 4
View Answer
Answer: c
Explanation: There are three types of inheritance possible. Private inheritance, protected inheritance, and public inheritance. The inheritance defines the access specifier to be used with the inherited members in the derived class.
6. How many derived class can a single base class have?
a) 1
b) 2
c) 3
d) As many are required
View Answer
Answer: d
Explanation: There is no restriction on how many classes can inherit a single base class. Hence there can be as many derived classes as required in a program from a single base class.
7. Which among the following is correct?
a) Friend function of derived class can access non-private members of base class
b) Friend function of base class can access derived class members
c) Friend function of derived class can access members of only derived class
d) Friend function can access private members of base class of a derived class
View Answer
Answer: a
Explanation: The friend function of a class can access the non-private members of base class. The reason behind is that the members of base class gets derived into the derived class and hence become members of derived class too. Hence a friend function can access all of those.
8. If a class is being derived using more than two base classes, which inheritance will be used?
a) Single
b) Multi-level
c) Hierarchical
d) Multiple
View Answer
Answer: d
Explanation: The statement given is the definition of multiple inheritance with respect to the derived class. The concept can be illustrated with many other samples but the main aspects are base class and derived class only.
9. Derived class is also known as ______________ class.
a) Subclass
b) Small class
c) Big class
d) Noticeable class
View Answer
Answer: a
Explanation: It is just another name given to the derived classes. This is used while denoting all the derived classes subsequent to a superclass.
10. If class A is derived from another derived class B which is derived from class C, which class will have maximum level of abstraction?
a) Class A
b) Class B
c) Class C
d) All have the same level of abstraction
View Answer
Answer: c
Explanation: The abstraction level of class C will be maximum. This is because the parent class have higher level of abstraction. Hence the parent of all other class will have maximum level of abstraction.
11. If base class is an abstract class then derived class ______________ the undefined functions.
a) Must define
b) Must become another abstract class or define
c) Must become parent class for
d) Must implement 2 definitions of
View Answer
Answer: b
Explanation: The function must be defined in the program which are not defined in the base class. Hence the class must be defined as abstract of implement the function definition in it.
12. How many classes can be derived from a derived class?
a) Only 1
b) At most 1
c) At least 1
d) As many as required
View Answer
Answer: d
Explanation: When a class is to be derived from another derived class, the derived class behaves as a normal base class hence there are no restriction on how many class can be derived from a derived class. The derived class again behaves as a normal superclass.
13. The members of a derived class can never be derived.
a) True
b) False
View Answer
Answer: b
Explanation: This is not true that the members of a derived class can’t be derived. All the classes are considered to be a normal class when used for derivation. The members can be derived with respect to their access specifiers.
14. Which feature is not related to the derived classes among the following?
a) Inheritance
b) Encapsulation
c) Run time memory management
d) Compile time function references
View Answer
Answer: c
Explanation: The memory management is the feature that is not necessary for derived classes that will be a part of whole program. The functions references must be resolved for their proper use if inheritance is used.
15. Deriving a class in such a way that that the base class members are not available for further inheritance is known as ___________________
a) Public inheritance
b) Protected inheritance
c) Protected or private inheritance
d) Private inheritance
View Answer
Answer: d
Explanation: The private members of a class can never be derived to another class. When a class derives another class using private inheritance, all the members become private members of the derived class. Hence these member won’t be available for further inheritance.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming (OOPs), here is complete set of 1000+ Multiple Choice Questions and Answers.
Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer
Answer: d
Explanation: 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
View Answer
Answer: a
Explanation: 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
View Answer
Answer: b
Explanation: 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{ };
View Answer
Answer: a
Explanation: 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
View Answer
Answer: d
Explanation: 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
View Answer
Answer: c
Explanation: 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*/ };
View Answer
Answer: c
Explanation: 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
View Answer
Answer: c
Explanation: 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
View Answer
Answer: a
Explanation: 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
View Answer
Answer: a
Explanation: 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
View Answer
Answer: c
Explanation: 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
View Answer
Answer: c
Explanation: 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
View Answer
Answer: a
Explanation: 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
View Answer
Answer: a
Explanation: 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
View Answer
Answer: d
Explanation: 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.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming (OOPs), here is complete set of 1000+ Multiple Choice Questions and Answers.
Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Multiple Inheritance”.
1. Multiple inheritance is ____________________
a) When a class is derived from another class
b) When a class is derived from two or more classes
c) When a class is derived from other two derived classes
d) When a class is derived from exactly one class
View Answer
Answer: b
Explanation: The multiple inheritance is used when a class is being derived using two base classes or more. This way a single class can have features of more than one classes inherited into a single unit. This lets us combine two class members into a single class.
2. Which problem arises due to multiple inheritance, if hierarchical inheritance is used previously for its base classes?
a) Diamond
b) Circle
c) Triangle
d) Loop
View Answer
Answer: a
Explanation: The diamond problem arises when multiple inheritance is used. This problem arises because the same name member functions get derived into a single class. Which in turn creates ambiguity in calling those methods.
3. How many classes should a program contain to implement the multiple inheritance?
a) Only 1
b) At least 1
c) At least 3
d) Exactly 3
View Answer
Answer: c
Explanation: For the implementation of multiple inheritance, there must be at least 3 classes in a program. At least 2 base classes and one class to inherit those two classes. If lesser, it becomes single level inheritance.
4. Which programming language restricts the use of multiple inheritance?
a) C++
b) PHP
c) SmallTalk
d) Java
View Answer
Answer: d
Explanation: Java doesn’t allow use of multiple inheritance with classes. But this can be done by using the interfaces. This is more secure and unambiguous way to implement multiple inheritance.
5. Is it possible to have all the abstract classes as base classes of a derived class from those?
a) Yes, always
b) Yes, only if derived class implements all the methods
c) No, because abstract classes doesn’t have constructors
d) No, never
View Answer
Answer: b
Explanation: The condition for abstract class applies same here too. All the undefined functions must be defined. Hence all the base classes can be abstract but derived class must implement all those undefined functions.
6. If class A inherits class B and class C as “class A: public class B, public class C {// class body ;}; ”, which class constructor will be called first?
a) Class A
b) Class B
c) Class C
d) All together
View Answer
Answer: b
Explanation: The constructors of parent class will be called first. In that, the constructor of the classes will be called in the same sequence as that mentioned in class definition inheritance. Since class B is mentioned first for inheritance, its constructor will be called first.
7. Why does diamond problem arise due to multiple inheritance?
a) Methods with same name creates ambiguity and conflict
b) Methods inherited from the superclass may conflict
c) Derived class gets overloaded with more than two class methods
d) Derived class can’t distinguish the owner class of any derived method
View Answer
Answer: a
Explanation: All the derived classes can distinguish the base class members, but if a method is being inherited to the base classes from another class which again gets inherited into same class (diamond shape), that may create conflict in using the function from two available.
8. How many base classes can a derived class have which is implementing multiple inheritance?
a) Only 2
b) At least 2
c) At most 2
d) As many as required
View Answer
Answer: d
Explanation: The classes can derive from as many classes as required since the multiple inheritance feature is made to combine or group together the functions that are from different classes. This make the derived class stronger in terms of its flexibility.
9. How to overcome diamond problem?
a) Using alias name
b) Using seperate derived class
c) Using virtual keyword with same name function
d) Can’t be done
View Answer
Answer: c
Explanation: To overcome the ambiguity and conflict we can use keyword virtual. This will help us to differentiate the functions with same name that came to last derived class in diamond problem.
10. When multiple inheritance is used, which class object should be used in order to access all the available members of parent and derived class?
a) Derived class object
b) Parent class objects
c) Use Abstract derived class
d) Derive a class from derived class
View Answer
Answer: a
Explanation: The derived class object can access all of its own members. It can also access the available members of the parent classes, because the members are derived into the derived class.
11. If all the members of all the base classes are private then _____________
a) There won’t be any use of multiple inheritance
b) It will make those members public
c) Derived class can still access them in multiple inheritance
d) Compile time error
View Answer
Answer: a
Explanation: The derived class will not be able to access any members of the base classes. Since private member’s are not inheritable. It leads to no use of multiple inheritance.
12. Is it compulsory to have constructor for all the classes involved in multiple inheritance?
a) Yes, always
b) Yes, only if no abstract class is involved
c) No, only classes being used should have a constructor
d) No, they must not contain constructors
View Answer
Answer: b
Explanation: The constructors must be defined in every class. If class is abstract, it won’t have any constructor but other classes must have constructor. Either implicit or explicit.
13. If a class contains 2 nested class and is being inherited by another class, will there be any multiple inheritance?
a) No, only single level inheritance is used
b) No, only multilevel inheritance is used
c) Yes, because 3 classes are involved
d) Yes, because more than 1 classes are being derived
View Answer
Answer: a
Explanation: When a class having nested classes is being derived into another class. It indirectly means a simple class is being inherited to another class. This is single level inheritance.
14. Which members can’t be accessed in derived class in multiple inheritance?
a) Private members of base
b) Public members of base
c) Protected members of base
d) All the members of base
View Answer
Answer: a
Explanation: The private member’s are available for only the class containing those members. Derived classes will have access to protected and public members only.
15. Can the derived class be made abstract if multiple inheritance is used?
a) No, because other classes must be abstract too
b) Yes, if all the functions are implemented
c) Yes, if all the methods are predefined
d) No, since constructors won’t be there
View Answer
Answer: d
Explanation: The derived class must not be abstract. This is because the abstract classes doesn’t have constructor and hence we won’t be having the capability to have instances. This will restrict the use of multiple inheritance.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming (OOPs), here is complete set of 1000+ Multiple Choice Questions and Answers.
Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
This set of Object Oriented Programming Questions and Answers for Entrance exams focuses on “Virtual Functions”.
1. Virtual function is ______ class function which expected to be redefined in ______ class, so that when reference is made to derived class object using pointer then we can call virtual function to execute ________ class definition version.
a) Base, derived, derived
b) Derived, Derived, Derived
c) Base, derived, base
d) Base, base, derived
View Answer
Answer: a
Explanation: The functions which may give rise to ambiguity due to inheritance, can be declared virtual. So that whenever derived class object is referred using pointer or reference to the base class methods, we can still call the derived class methods using virtual function. Hence this differentiates those methods from each other.
2. What does a virtual function ensure for an object, among the following?
a) Correct method is called, regardless of the class defining it
b) Correct method is called, regardless of the object being called
c) Correct method is called, regardless of the type of reference used for function call
d) Correct method is called, regardless of the type of function being called by objects
View Answer
Answer: c
Explanation: It is property of the virtual function and one of their main use. Its use ensure that the correct method is called even though it is been called from different pointer or references. This also decreases chance of mistakes in program.
3. Virtual functions are mainly used to achieve _____________
a) Compile time polymorphism
b) Interpreter polymorphism
c) Runtime polymorphism
d) Functions code polymorphism
View Answer
Answer: c
Explanation: It is used to achieve runtime polymorphism. The functions which are inherited and overridden, so at runtime the correct function is executed. The correct function call is made from the intended class.
4. Which keyword is used to declare virtual functions?
a) virtual
b) virt
c) anonymous
d) virtually
View Answer
Answer: a
Explanation: The virtual keyword is used to declare virtual functions. Anonymous keyword is used with classes and have a different meaning. The virtual functions are used to call the intended function of the derived class.
5. Where the virtual function should be defined?
a) Twice in base class
b) Derived class
c) Base class and derived class
d) Base class
View Answer
Answer: d
Explanation: The virtual function should be declared in base class. So that when the derived class inherits from the base class, the functions can be differentiated from the one in base class and another in derived class.
6. The resolving of virtual functions is done at ______________
a) Compile time
b) Interpret time
c) Runtime
d) Writing source code
View Answer
Answer: c
Explanation: The resolving of virtual functions that are to be called is done at run time. The base class and the derived classes may contain different definitions and different variables, so all these things are resolved at run time and decided which function is to be called.
7. In which access specifier should a virtual function be defined?
a) Private
b) Public
c) Protected
d) Default
View Answer
Answer: b
Explanation: The virtual functions must be defined in public section of a class. This is to ensure that the virtual function is available everywhere in the program. Also to avoid any error while resolving the method.
8. Virtual functions can never be made _______________
a) Static function
b) Parameterized function
c) Default argument function
d) Zero parameter function
View Answer
Answer: a
Explanation: The virtual function must not be static. Those functions are the property of individual objects and not of a class as a whole. The functions should not be made common for all the objects of that class.
9. Virtual functions can’t be made friend function of other classes.
a) True
b) False
View Answer
Answer: a
Explanation: The friend functions can access the private members also. This may hinder the security of class members. This is why the functions should not be made friend functions of other class.
10. Which is a must condition for virtual function to achieve runtime polymorphism?
a) Virtual function must be accessed with direct name
b) Virtual functions must be accessed using base class object
c) Virtual function must be accessed using pointer or reference
d) Virtual function must be accessed using derived class object only
View Answer
Answer: c
Explanation: The virtual functions must be called using pointer or reference. This is mandatory so that the intended function gets executed while resolving the method at runtime. The must not be any ambiguity between the method of parent class and derived class.
11. Which among the following is true for virtual functions?
a) Prototype must be different in base and derived class
b) Prototype must be same in base class and derived class
c) Prototype must be given only in base class
d) Prototype must have different signature in base and derived class
View Answer
Answer: b
Explanation: The prototype must be the same. Because the function is to be overridden in the derived class. If the function prototype is different in derived class then it will not override the base class function and hence virtual function concept won’t work here.
12. The virtual functions must be declared and defined in _____________ class and overridden in ___________ class.
a) Base, base
b) Derived, derived
c) Derived, base
d) Base, derived
View Answer
Answer: d
Explanation: The virtual functions must be declared and defined in base class. The functions can be redefined in derived class. If redefined in derived class then it overrides the base class function definition.
13. It is __________ to redefine the virtual function in derived class.
a) Necessary
b) Not necessary
c) Not acceptable
d) Good practice
View Answer
Answer: b
Explanation: It is not necessary to redefine the virtual function in the derived class. If not defined, the base class function definition is used but if defined, the intended definition is used according to need. It is not about good coding practice as it should be redefined only if needed.
14. Which among the following is true?
a) A class may have virtual destructor but not virtual constructor
b) A class may have virtual constructor but not virtual destructor
c) A class may have virtual constructor and virtual constructor
d) A class may have either virtual destructor or virtual constructor
View Answer
Answer: a
Explanation: Any class can contain virtual destructor. But is not possible to define a virtual constructor. The reason behind is that the destructors can be overridden but constructors should not be.
15. If virtual function of base class is redefined in derived class then ________________
a) It must be declared virtual in derived class also
b) It may or may not be declared virtual in derived class
c) It can must not be declared virtual in derived class
d) It must be declared normally in derived class
View Answer
Answer: b
Explanation: The virtual functions may or may not be declared virtual in derived class. This is because if the overriding function defined in derived class is not declared virtual explicitly, the compiler makes it virtual implicitly.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming for Entrance exams, here is complete set of 1000+ Multiple Choice Questions and Answers.
Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
This set of Object Oriented Programming Interview Questions and Answers for freshers focuses on “Overriding Member Functions”.
1. Which among the following best describes member function overriding?
a) Member functions having same name in base and derived classes
b) Member functions having same name in base class only
c) Member functions having same name in derived class only
d) Member functions having same name and different signature inside main function
View Answer
Answer: a
Explanation: The member function which is defined in base class and again in the derived class, is overridden by the definition given in the derived class. This is because the preference is given more to the local members. When derived class object calls that function, definition from the derived class is used.
2. Which among the following is true?
a) Inheritance must not be using when overriding is used
b) Overriding can be implemented without using inheritance
c) Inheritance must be done, to use overriding are overridden
d) Inheritance is mandatory only if more than one functions
View Answer
Answer: c
Explanation: The inheritance must be used in order to use function overriding. If inheritance is not used, the functions can only be overloaded. There must be a base class and a derived class to override the function of base class.
3. Which is the correct condition for function overriding?
a) The declaration must not be same in base and derived class
b) The declaration must be exactly the same in base and derived class
c) The declaration should have at least 1 same argument in declaration of base and derived class
d) The declaration should have at least 1 different argument in declaration of base and derived class
View Answer
Answer: b
Explanation: For a function to be over ridden, the declaration must be exactly the same. There must not be any different syntax used. This will ensure that the function to be overridden is only the one intended from to be overridden from the derived class.
4. Exactly same declaration in base and derived class includes______________
a) Only same name
b) Only same return type and name
c) Only same return type and argument list
d) All the same return type, name and parameter list
View Answer
Answer: d
Explanation: Declaration includes the whole prototype of the function. The return type name and the parameter list must be same in order to confirm that the function is same in derived and the base class. And hence can be overridden.
5. Which among function will be overridden from the function defined in derived class below:
class A
{
int i;
void show()
{
cout<<i;
}
void print()
{
cout <<i;
}
};
class B
{
int j;
void show()
{
cout<<j;
}
};
a) show()
b) print()
c) show() and print()
d) Compile time error
View Answer
Answer: a
Explanation: The declaration must be exactly same in the derived class and base class. The derived class have defined show() function with exactly same declaration. This then shows that the function in base class is being overridden if show() is called from the object of class B.
6. How to access the overridden method of base class from the derived class?
a) Using arrow operator
b) Using dot operator
c) Using scope resolution operator
d) Can’t be accessed once overridden
View Answer
Answer: c
Explanation: Scope resolution operator :: can be used to access the base class method even if overridden. To access those, first base class name should be written followed by the scope resolution operator and then the method name.
7. The functions to be overridden _____________
a) Must be private in base class
b) Must not be private base class
c) Must be private in both derived and base class
d) Must not be private in both derived and base class
View Answer
Answer: b
Explanation: If the function is private in the base class, derived class won’t be able to access it. When the derived class can’t access the function to be overridden then it won’t be able to override it with any definition.
8. Which language doesn’t support the method overriding implicitly?
a) C++
b) C#
c) Java
d) SmallTalk
View Answer
Answer: b
Explanation: The feature of method overriding is not provided in C#. To override the methods, one must use override or virtual keywords explicitly. This is done to remove accidental changes in program and unintentional overriding.
9. In C# ____________________
a) Non – virtual or static methods can’t be overridden
b) Non – virtual and static methods only can be overridden
c) Overriding is not allowed
d) Overriding must be implemented using C++ code only
View Answer
Answer: a
Explanation: The non-virtual and static methods can’t be overridden in C# language. The restriction is made from the language implicitly. Only the methods that are abstract, virtual or override can be overridden.
10. In Delphi ______________
a) Method overriding is done implicitly
b) Method overriding is not supported
c) Method overriding is done with directive override
d) Method overriding is done with the directive virtually
View Answer
Answer: c
Explanation: This is possible but only if the method to be overridden is marked as dynamic or virtual. It is inbuilt restriction of programming language. This is done to reduce the accidental or unintentional overriding.
11. What should be used to call the base class method from the derived class if function overriding is used in Java?
a) Keyword super
b) Scope resolution
c) Dot operator
d) Function name in parenthesis
View Answer
Answer: a
Explanation: The keyword super must be used to access base class members. Even when overriding is used, super must be used with the dot operator. The overriding is possible.
12. In Kotlin, the function to be overridden must be ______________
a) Private
b) Open
c) Closed
d) Abstract
View Answer
Answer: b
Explanation: The function to be overridden must be open. This is a condition in Kotlin for any function to be overridden. This avoids accidental overriding.
13. Abstract functions of a base class _________________
a) Are overridden by the definition in same class
b) Are overridden by the definition in parent class
c) Are not overridden generally
d) Are overridden by the definition in derived class
View Answer
Answer: d
Explanation: The functions declared to be abstract in base class are redefined in derived classes. That is, the functions are overridden by the definitions given in the derived classes. This must be done to give at least one definition to each undefined function.
14. If virtual functions are defined in the base class then _______________
a) It is not necessary for derived classes to override those functions
b) It is necessary for derived classes to override those functions
c) Those functions can never be derived
d) Those functions must be overridden by all the derived classes
View Answer
Answer: a
Explanation: The derived classes doesn’t have to redefine and override the base class functions. If one definition is already given it is not mandatory for any derived class to override those functions. The base class definition will be used.
15. Which feature of OOP is exhibited by the function overriding?
a) Inheritance
b) Abstraction
c) Polymorphism
d) Encapsulation
View Answer
Answer: c
Explanation: The polymorphism feature is exhibited by function overriding. Polymorphism is the feature which basically defines that same named functions can have more than one functionalities.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming for Interviews, here is complete set of 1000+ Multiple Choice Questions and Answers.
Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer
Answer: b
Explanation: 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
View Answer
Answer: a
Explanation: 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
View Answer
Answer: c
Explanation: 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
View Answer
Answer: a
Explanation: 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
View Answer
Answer: d
Explanation: 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
View Answer
Answer: c
Explanation: 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
View Answer
Answer: a
Explanation: 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
View Answer
Answer: a
Explanation: 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
View Answer
Answer: d
Explanation: 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()
View Answer
Answer: c
Explanation: 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
View Answer
Answer: a
Explanation: 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
View Answer
Answer: b
Explanation: 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) |
View Answer
Answer: d
Explanation: 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
View Answer
Answer: c
Explanation: 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
View Answer
Answer: d
Explanation: 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.