Test Series - oops

Test Number 26/78

Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q:  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
Solution: 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).
Q: 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
Solution: 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.

You Have Score    /10