250+ TOP MCQs on Assigning Objects and Answers

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

1. When value of an object is assigned to another object ________________
a) It becomes invalid statement
b) Its values gets copied into another object
c) Its values gets address of the existing values
d) The compiler doesn’t execute that statement
Answer: b
Clarification: The values get copied to another object. No address is assigned to the object values. This is uses copy constructor to copy the values.

2. If an object is created and another object is assigned to it, then ________________
a) Copy constructor is called to copy the values
b) Object is copied directly to the object
c) Reference to another object is created
d) The new object is initialized to null values
Answer: c
Clarification: The new object created, refers to the same address of the previously created object. Now whenever new object changes any data member value, it will affect the previously existing object.

3. How the argument passed to a function get initialized?
a) Assigned using copy constructor at time of passing
b) Copied directly
c) Uses addresses always
d) Doesn’t get initialized
Answer: a
Clarification: The arguments get initialized using the copy constructor. There is a need of assigning the value of all the members of an object to the local object of the function.

4. Predict the output of the program.

class A
{
	public int i;
};
void main()
{
	A x;
	A y=x;
	x.i=10;
	y.i=20;
	y.i++;
	y.i=20;
	cout&l;<tx.i;
}

a) 10
b) 20
c) 21
d) 0
Answer: b
Clarification: The expected output may be 10 because the value of member of object x is printed. But when object x is assigned to y, y points to the same address where x is stored. So actually both objects x and y point to the same location and refers to the same object.

5. If programmer doesn’t define any copy assignment operator then ____________________
a) Compiler gives an error
b) Program fails at run time
c) Compiler gives an implicit definition
d) Compiler can’t copy the member values
Answer: c
Clarification: While defining a copy constructor, we use reference const parameter, those are used for the assignment. The assignment operator may or may not be defined by the programmer, if not, compiler implicitly defines member wise copy assignment operator.

6. Declaring a copy constructor doesn’t suppresses the compiler generated copy assignment operator.
a) True
b) False
Answer: a
Clarification: Even if the programmer doesn’t define or even if they define the copy constructor. The compiler still generates a copy assignment operator. It doesn’t gets suppressed.

7. In copy constructor definition, if non const values are accepted only ________
a) Only const objects will be accepted
b) Only non – const objects are accepted
c) Only const members will not get copied
d) Compiler generates an error
Answer: b
Clarification: Only the non – const objects will be accepted by the compiler. If a const object is passed, the compiler produces an error. To reduce that, we use const argument in definition, so that both const and non – const objects are accepted.

8. How many objects can be assigned to a single address?
a) Only 1
b) At most 7
c) At most 3
d) As many as required
Answer: d
Clarification: The memory address can be referenced by more than one object. There is no maximum number defined. Any number of objects can reference to the same address.

9. Use of assignment operator ____________________
a) Changes its use, when used at declaration and in normal assignment
b) Doesn’t changes its use, whatever the syntax might be
c) Assignment takes place in declaration and assignment syntax
d) Doesn’t work in normal syntax, but only with declaration
Answer: a
Clarification: The assignment operator if used at declaration then it uses copy constructor for the copying of objects. If used in simple assignment syntax then it uses copy assignment function.

10. If more than one object refer to the same address, any changes made __________
a) Can be made visible to specific objects
b) Will be specific to one object only
c) From any object will be visible in all
d) Doesn’t changes the values of all objects
Answer: c
Clarification: At a memory address, only one object can be referenced. All the other objects which refer to the same memory address make changes for all of the objects referring that address.

11. How to make more than one object refer to the same object?
a) Initialize it to null
b) Initialize the object with another at declaration
c) Use constructor to create new object
d) Assign the address directly
Answer: b
Clarification: The object must get initialized with another object at time of declaration only. We don’t have to create a new object we just have to get name of new object because there after same address will be referred.

12. We can assign ______________________
a) Value of one reference variable to another
b) Value of any object to another
c) Value of any type to any object
d) Value of non – reference to another reference
Answer: a
Clarification: Only the reference value can be assigned to another reference value. This is because both deal with the address. There is no type mismatch hence we can assign them.

13. Assigning reference to an object _________________
a) Will create another copy of the object
b) Will create two different copies of the object
c) Will not create any other copy of the object
d) Will not refer to the object
Answer: c
Clarification: When an object is assigned with another object. Same memory location is used. There is no other copy of the object created.

14. Which among the following is true?
a) We can use direct assignment for any object
b) We can use direct assignment only for different class objects
c) We must not use direct assignment
d) We can use direct assignment to same class objects
Answer: d
Clarification: The direct assignment can be used with the same class objects. There is no restriction on them. But better if the program have a predefined copy assignment operator.

15. Assigning objects takes place while passing the arguments.
a) True
b) False
Answer: b
Clarification: The actual assignment doesn’t take place as the object might have got passed by reference. Also even if not by reference, the copy constructor is called to copy the values into the new object and not exactly the assignment operator.

250+ TOP MCQs on Abstraction and Answers

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

1. Which among the following best defines abstraction?
a) Hiding the implementation
b) Showing the important data
c) Hiding the important data
d) Hiding the implementation and showing only the features
Answer: d
Clarification: It includes hiding the implementation part and showing only the required data and features to the user. It is done to hide the implementation complexity and details from the user. And to provide a good interface in programming.

2. Hiding the implementation complexity can ____________
a) Make the programming easy
b) Make the programming complex
c) Provide more number of features
d) Provide better features
Answer: a
Clarification: It can make programming easy. The programming need not know how the inbuilt functions are working but can use those complex functions directly in the program. It doesn’t provide more number of features or better features.

3. Class is _________ abstraction.
a) Object
b) Logical
c) Real
d) Hypothetical
Answer: b
Clarification: Class is logical abstraction because it provides a logical structure for all of its objects. It gives an overview of the features of an object.

4. Object is ________ abstraction.
a) Object
b) Logical
c) Real
d) Hypothetical
Answer: c
Clarification: Object is real abstraction because it actually contains those features of class. It is the implementation of overview given by class. Hence the class is logical abstraction and its object is real.

5. Abstraction gives higher degree of ________
a) Class usage
b) Program complexity
c) Idealized interface
d) Unstable interface
Answer: c
Clarification: It is to idealize the interface. In this way the programmer can use the programming features more efficiently and can code better. It can’t increase the program complexity, as the feature itself is made to hide it.

6. Abstraction can apply to ____________
a) Control and data
b) Only data
c) Only control
d) Classes
Answer: a
Clarification: Abstraction applies to both. Control abstraction involves use of subroutines and control flow abstraction. Data abstraction involves handling pieces of data in meaningful ways.

7. Which among the following can be viewed as combination of abstraction of data and code.
a) Class
b) Object
c) Inheritance
d) Interfaces
Answer: b
Clarification: Object can be viewed as abstraction of data and code. It uses data members and their functioning as data abstraction. Code abstraction as use of object of inbuilt class.

8. Abstraction principle includes___________
a) Use abstraction at its minimum
b) Use abstraction to avoid longer codes
c) Use abstraction whenever possible to avoid duplication
d) Use abstraction whenever possible to achieve OOP
Answer: c
Clarification: Abstraction principle includes use of abstraction to avoid duplication (usually of code). It this way the program doesn’t contain any redundant functions and make the program efficient.

9. Higher the level of abstraction, higher are the details.
a) True
b) False
Answer: b
Clarification: Higher the level of abstraction, lower are the details. The best way to understand this is to consider a whole system that is highest level of abstraction as it hides everything inside. And next lower level would contain few of the computer components and so on.

10. Encapsulation and abstraction differ as ____________
a) Binding and Hiding respectively
b) Hiding and Binding respectively
c) Can be used any way
d) Hiding and hiding respectively
Answer: a
Clarification: Abstraction is hiding the complex code. For example, we directly use cout object in C++ but we don’t know how is it actually implemented. Encapsulation is data binding, as in, we try to combine a similar type of data and functions together.

11. In terms of stream and files ____________
a) Abstraction is called a stream and device is called a file
b) Abstraction is called a file and device is called a stream
c) Abstraction can be called both file and stream
d) Abstraction can’t be defined in terms of files and stream
Answer: a
Clarification: Abstraction is called stream to provide a level of complexity hiding, for how the files operations are actually done. Actual devices are called file because in one way or another, those can be considered as single entity and there is nothing hidden.

12. If two classes combine some private data members and provides public member functions to access and manipulate those data members. Where is abstraction used?
a) Using private access specifier for data members
b) Using class concept with both data members and member functions
c) Using public member functions to access and manipulate the data members
d) Data is not sufficient to decide what is being used
Answer: c
Clarification: It is the concept of hiding program complexity and actual working in background. Hence use of public member functions illustrates abstraction here.

13. A phone is made up of many components like motherboard, camera, sensors and etc. If the processor represents all the functioning of phone, display shows the display only, and the phone is represented as a whole. Which among the following have highest level of abstraction?
a) Motherboard
b) Display
c) Camera
d) Phone
Answer: d
Clarification: Phone as a whole have the highest level of abstraction. This is because the phone being a single unit represents the whole system. Whereas motherboard, display and camera are its components.

14. Which among the following is not a level of abstraction?
a) Logical level
b) Physical level
c) View level
d) External level
Answer: d
Clarification: Abstraction is generally divided into 3 different levels, namely, logical, physical and view level. External level is not defined in terms of abstraction.

15. Using higher degree of abstraction __________
a) May get unsafe
b) May reduce readability
c) Can be safer
d) Can increase vulnerability
Answer: c
Clarification: It will make the code safer. One may think it reduces the readability, but the fact is, it actually helps us understand the code better. We don’t have to read the complex code which is of no use in understanding the program.

250+ TOP MCQs on Object Reference and Answers

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

1. What is reference to an object?
a) It is address of an object
b) It is address of where the variables and methods of object are stored
c) It is pointer having address of an object
d) It is address of only variables and not the methods of an object
Answer: b
Clarification: Reference indicates the address where the object’s variables and methods are stored. It is not actual address of the object. This is done to directly use the variables and methods whenever required.

2. Whenever an object is assigned to a variable or passed to a method ________________
a) Actually the objects aren’t used
b) Actually only the objects are used
c) Actually a pointer to an object is used
d) Actually copy of object is used
Answer: a
Clarification: Whenever an object is assigned to a variable or passed to a method, we aren’t actually using objects there. Actually the reference to the objects is used. The reference makes a lot of difference as the main object may or may not get affected depending on the code.

3. Does use of object reference in assignment or passing means copy of the object is being used?
a) No, because the copy would create a new temporary variable
b) No, because the copy would not help to make changes to main object
c) Yes, because the reference directly means using address
d) Yes, because the reference directly means the constructors are involved
Answer: c
Clarification: We can’t say that the reference involves constructors in passing the objects to some method. The reference is used to denote the addresses and hence to point to the main object itself. There is no copy involved.

4. What will be the output of the following code?

import java.awt.Point;
class Testing
{
	public static void main(String[] args)
	{
		Point p1,p2;
		p1=new Point(100,100);
		p2=p1;
		p1.x=200;
		p1.y=200;
		System.out.println(“Point 1:+ p1.x + ”, “ + p1.y);
		System.out.println(“Point 2:+ p2.x + ”, “ + p2.y);
	}
}

a)

Point 1: 100, 100
Point 2: 200, 200

b)

Point 1: 200, 200
Point 2: 100, 100

c)

Point 1: 100, 100
Point 2: 100, 100

d)

Point 1: 200, 200
Point 2: 200, 200

View Answer

Answer: d
Clarification: The expected output would be like p2 with values 100, 100. But this is not the case. The tricky part is assignment used (p2=p1;). Here a reference is created from object p1 to p2, and not any new object that would copy p1’s values. Hence when we change the values of p1 object members. There changes are reflected to the object p2 also.

 
 

5. Is there any explicit use of pointers in java that would be applicable to objects?
a) Yes, we use reference for this purpose
b) Yes, we use java arrays for this purpose
c) No, implicit pointing is possible
d) No, direct class names should be used
Answer: c
Clarification: The question clearly asks if there is any explicit use of pointers related to objects. Pointers are not applicable in java first of all. Secondly, the pointing in java is achieved implicitly using the references and object arrays.

6. Can a super class object give reference to a subclass method?
a) No, it is not possible
b) Maybe, it is possible
c) No, it’s not possible
d) No, It’s not possible in few cases only
Answer: c
Clarification: The object of a super class can never refer to methods of a subclass. Whereas vice versa is possible. If there is an overridden function in subclass, then the object of super class will execute the method of itself and not from the subclass.

7. What will be the output of the following code?

import java.awt.Point;
class Testing
{
	public static void main(String[] args)
	{
		Point t1,t2,t3;
		t1=new Point(100,100);
		t2=t1;
		t3=t1;
		t1.x=200;
		t1.y=200;
		t2.x=300;
		t3.y=500;
		System.out.println(“Point 1:+ p1.x + ”, “ + p1.y);
	}
}

a) Point 1: 200, 200
b) Point 1: 100,100
c) Point 1: 300, 300
d) Point 1: 300, 500
Answer: d
Clarification: When references are used, all the variables point to the same object. Whenever any of the variable changes any values, it will be reflected to all the variables pointing to the same object.

8. If a reference variable is declared final then _________________
a) It can never be reassigned to refer to a different object
b) It can be assigned to refer to any object anytime
c) It can never be assigned with any object
d) It can be assigned with 2 or more objects simultaneously
Answer: a
Clarification: Since the variable is declared final. It will have a constant value throughout the program. It can refer to only one object at a time. And if it was made to refer to none of the object, it would have got no use.

9. Which of the members are referred by this pointer usually (Java)?
a) Members of class where this is used
b) Member of the parent class where this is used
c) Members that are passed as argument to the object
d) Pointers are not applicable in java
Answer: a
Clarification: We use this pointer to differentiate the members of the class where this is used to the other inherited or passed variables. The local variables are denoted with this. Or specifically the members of class only.

10. How to refer to method of nested class?
a) enclosingClassObject.innerClassObject.method();
b) innerClassObject.method();
c) method();
d) depends on where the method is being called
Answer: d
Clarification: This depends on where the method is being called. If the method is called inside the enclosing class itself. Then we can’t use object of enclosing class. If the method is being called within the inner class itself, then its object will also be of no use.

11. How many objects can be referenced from the same variables?
a) One at a time
b) Many at a time
c) Many using array name
d) 7 at max at same time
Answer: a
Clarification: There should not be any confusion in how many references can be made from a single variable. A single variable can only point to one object at a time. Even if it’s an array, the name of the array is used and is considered one object name only (representing first array element).

12. Java handles memory dynamically and references are deleted as soon as they are out of scope.
a) True
b) False
Answer: a
Clarification: In Java, it is inbuilt feature that handles all the memory dynamically. It is not necessary to free or destroy all the references made from a function which is going out of scope. You can call destroy or free methods explicitly but there is no mandatory rule.

13. Which among the following is true?
a) Object referencing refers to methods address
b) Object referencing refers to variable of object
c) Object referencing points to same address, if assigned by variables
d) Object referencing is used to point methods
Answer: c
Clarification: The object referencing will point to the same address if variables are assigned. All the variables might have a different name but they will point to the same memory location. This is most basic concept of references.

14. Invoking a method on a particular object is ____________ sending a message to that object.
a) Different from
b) Same as
c) Somewhat similar
d) Part of
Answer: b
Clarification: The methods invoked on a particular object is same as sending a message with same values to that object. Message would contain values in a particular format that can be used by the object. And calling a method would be just another way to do the same task.

15. Can reference to an object be returned from a method?
a) Yes, always possible
b) Yes, but not always
c) No, never possible
d) No, Not possible because referred element would be destroyed
Answer: b
Clarification: This is possible but not always, since the reference being returned may get destroyed with the return of method. This is an undesirable condition, hence it is not always possible to return references. But it is always possible if the referred element is not local to the method.

250+ TOP MCQs on Virtual Functions and Answers

Object Oriented Programming Questions Entrance exams 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
Answer: a
Clarification: 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
Answer: c
Clarification: 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
Answer: c
Clarification: 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
Answer: a
Clarification: 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
Answer: d
Clarification: 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
Answer: c
Clarification: 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
Answer: b
Clarification: 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
Answer: a
Clarification: 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
Answer: a
Clarification: 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
Answer: c
Clarification: 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
Answer: b
Clarification: 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
Answer: d
Clarification: 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
Answer: b
Clarification: 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
Answer: a
Clarification: 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
Answer: b
Clarification: 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.

Object Oriented Programming for Entrance exams,

250+ TOP MCQs on Pointer to Objects and Answers

Object Oriented Programming Assessment Questions and Answers on “Pointer to Objects”.

1. Which language among the following doesn’t allow pointers?
a) C++
b) Java
c) Pascal
d) C
Answer: b
Clarification: The concept of pointers is not supported in Java. The feature is not given in the language but can be used in some ways explicitly. Though this pointer is supported by java too.

2. Which is correct syntax for declaring pointer to object?
a) className* objectName;
b) className objectName;
c) *className objectName;
d) className objectName();
Answer: a
Clarification: The syntax must contain * symbol after the className as the type of object. This declares an object pointer. This can store address of any object of the specified class.

3. Which operator should be used to access the members of the class using object pointer?
a) Dot operator
b) Colon to the member
c) Scope resolution operator
d) Arrow operator
Answer: d
Clarification: The members can be accessed from the object pointer by using arrow operator. The arrow operator can be used only with the pointer of class type. If simple object is declared, it must use dot operator to access the members.

4. How does compiler decide the intended object to be used, if more than one object are used?
a) Using object name
b) Using an integer pointer
c) Using this pointer
d) Using void pointer
Answer: c
Clarification: This pointer denotes the object, in which it is being used. If member function is called with respect to one object then this pointer refers to the same object members. It can be used when members with same name are involved.

5. If pointer to an object is declared __________
a) It can store any type of address
b) It can store only void addresses
c) It can only store address of integer type
d) It can only store object address of class type specified
Answer: d
Clarification: The address of only the specified class type can get their address stored in the object pointer. The addresses doesn’t differ but they do differ for the amount and type of memory required for objects of different classes. Hence same class object pointer should be used.

6. What is the size of an object pointer?
a) Equal to size of any usual pointer
b) Equal to size of sum of all the members of object
c) Equal to size of maximum sized member of object
d) Equal to size of void
Answer: a
Clarification: The size of object pointer is same as that of any usual pointer. This is because only the address have to be stored. There are no values to be stored in the pointer.

7. A pointer _________________
a) Can point to only one object at a time
b) Can point to more than one objects at a time
c) Can point to only 2 objects at a time
d) Can point to whole class objects at a time
Answer: a
Clarification: The object pointer can point to only one object at a time. The pointer will be able to store only one address at a time. Hence only one object can be referred.

8. Pointer to a base class can be initialized with the address of derived class, because of _________
a) derived-to-base implicit conversion for pointers
b) base-to-derived implicit conversion for pointers
c) base-to-base implicit conversion for pointers
d) derived-to-derived implicit conversion for pointers
Answer: a
Clarification: It is an implicit rule defined in most of the programming languages. It permits the programmer to declare a pointer to the derived class from a base class pointer. In this way the programmer doesn’t have to declare object for derived class each time it is required.

9. Can pointers to object access the private members of the class?
a) Yes, always
b) Yes, only if it is only pointer to object
c) No, because objects can be referenced from another objects too
d) No, never
Answer: d
Clarification: The pointers to an object can never access the private members of the class outside the class. The object can indirectly use those private members using member functions which are public in the class.

10. Is name of an array of objects is also a pointer to object?
a) Yes, always
b) Yes, in few cases
c) No, because it represents more than one object
d) No, never
Answer: a
Clarification: The array name represents a pointer to the object. The name alone can represent the starting address of the array. But that also represents an array which is in turn stored in a pointer.

11. Which among the following is true?
a) The pointer to object can hold address only
b) The pointer can hold value of any type
c) The pointer can hold only void reference
d) The pointer can’t hold any value
Answer: a
Clarification: The pointer to an object can hold only the addresses. Address of any other object of same class. This allows the programmer to link more than one objects if required.

12. Which is the correct syntax to call a member function using pointer?
a) pointer->function()
b) pointer.function()
c) pointer::function()
d) pointer:function()
Answer: a
Clarification: The pointer should be mentioned followed by the arrow operator. Arrow operator is applicable only with the pointers. Then the function name should be mentioned that is to be called.

13. If a pointer to an object is created and the object gets deleted without using the pointer then __________
a) It becomes void pointer
b) It becomes dangling pointer
c) It becomes null pointer
d) It becomes zero pointer
Answer: b
Clarification: When the address pointed by the object pointer gets deleted, the pointer now points to an invalid address. Hence it becomes a dangling pointer. It can’t be null or void pointer since it doesn’t point to any specific location.

14. How can the address stored in the pointer be retrieved?
a) Using * symbol
b) Using $ symbol
c) Using & symbol
d) Using @ symbol
Answer: c
Clarification: The & symbol must be used. This should be done such that the object should be preceded by & symbol and then the address should be stored in another variable. This is done to get the address where the object is stored.

15. What should be done to prevent changes that may be made to the values pointed by the pointer?
a) Usual pointer can’t change the values pointed
b) Pointer should be made virtual
c) Pointer should be made anonymous
d) Pointer should be made const
Answer: d
Clarification: The pointer should be declared as a const type. This prevents the pointer to change any value that is being pointed from it. This is a feature that is made to access the values using pointer but to make sure that pointer doesn’t change those values accidently.

16. References to object are same as pointers of object.
a) True
b) False
Answer: b
Clarification: The references are made to object when the object is created and initialized with another object without calling any constructor. But the object pointer must be declared explicitly using * symbol that will be capable of storing some address. Hence both are different.

Object Oriented Programming Assessment Questions,

250+ TOP MCQs on Constructors and Answers

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

1. Which among the following is called first, automatically, whenever an object is created?
a) Class
b) Constructor
c) New
d) Trigger
Answer: b
Clarification: 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
Answer: c
Clarification: 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(){} };
Answer: d
Clarification: 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
Answer: a
Clarification: 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
Answer: a
Clarification: 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)
Answer: b
Clarification: 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
Answer: a
Clarification: 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
Answer: d
Clarification: 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
Answer: a
Clarification: 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
Answer: b
Clarification: 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
Answer: c
Clarification: 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
Answer: a
Clarification: 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
Answer: d
Clarification: 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
Answer: c
Clarification: 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
Answer: a
Clarification: 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.