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.

Leave a Reply

Your email address will not be published. Required fields are marked *