17 Lecture

CS301

Midterm & Final Term Short Notes

Reference Variables

In Java, a reference variable is a variable that holds the memory address of an object. Unlike primitive data types, reference variables do not hold the actual value of an object, but rather a reference to its location in memory. This allows obj


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. Which of the following data types is a reference type in Java? a) int b) double c) String d) char

Answer: c) String

  1. What does a reference variable in Java hold? a) The actual value of an object b) The memory address of an object c) The size of an object d) The type of an object

Answer: b) The memory address of an object

  1. When an object is assigned to a reference variable, what is stored in the variable? a) A copy of the object b) A reference to the object c) The value of the object d) None of the above

Answer: b) A reference to the object

  1. What is the difference between a reference variable and a primitive variable? a) A reference variable holds the actual value of an object, while a primitive variable holds a reference to an object. b) A reference variable holds a reference to an object, while a primitive variable holds the actual value of a data type. c) A reference variable holds a value of an object, while a primitive variable holds a reference to an object. d) There is no difference between the two.

Answer: b) A reference variable holds a reference to an object, while a primitive variable holds the actual value of a data type.

  1. In Java, can a reference variable be null? a) Yes b) No

Answer: a) Yes

  1. What happens when a reference variable is assigned to another reference variable? a) Both variables hold a reference to the same object b) Both variables hold a copy of the same object c) The original variable is deleted d) None of the above

Answer: a) Both variables hold a reference to the same object

  1. What is the syntax for creating a reference variable in Java? a) int x = 5; b) double y = 3.14; c) String s = "Hello"; d) char c = 'a';

Answer: c) String s = "Hello";

  1. What happens when a reference variable is passed as a parameter to a method in Java? a) The method receives a copy of the object b) The method receives a reference to the object c) The method receives the actual value of the object d) None of the above

Answer: b) The method receives a reference to the object

  1. What is the default value of a reference variable in Java? a) null b) 0 c) false d) None of the above

Answer: a) null

  1. Can a reference variable be used to access static methods in Java? a) Yes b) No

Answer: a) Yes



Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is a reference variable in Java? Answer: A reference variable in Java is a variable that holds the memory address of an object.

  2. How is a reference variable different from a primitive variable in Java? Answer: A reference variable holds a reference to an object, while a primitive variable holds the actual value of a data type.

  3. What is the default value of a reference variable in Java? Answer: The default value of a reference variable in Java is null.

  4. Can a reference variable be reassigned to a different object in Java? Answer: Yes, a reference variable can be reassigned to a different object in Java.

  5. How does Java handle passing a reference variable as a parameter to a method? Answer: Java passes the reference to the object held by the reference variable as a parameter to the method.

  6. Can a reference variable be used to access static methods in Java? Answer: Yes, a reference variable can be used to access static methods in Java.

  7. How does Java handle garbage collection for objects referenced by reference variables? Answer: Java's garbage collector automatically frees the memory allocated to objects that are no longer referenced by any reference variable.

  8. What is the difference between an instance variable and a reference variable in Java? Answer: An instance variable is a variable declared in a class, while a reference variable is a variable declared in a method or block that holds a reference to an object.

  9. Can a reference variable be used to access private members of a class in Java? Answer: No, a reference variable cannot be used to access private members of a class in Java.

  10. How can we check if a reference variable is referring to an object or is null in Java? Answer: We can check if a reference variable is referring to an object or is null in Java by using the null check operator (==).

In Java, reference variables are used to refer to objects. Unlike primitive variables, which store values, reference variables store the memory address of an object in the memory heap. This means that multiple reference variables can refer to the same object, allowing for shared access to the same data. Reference variables play an important role in Java's object-oriented programming model. They enable the creation of complex data structures and the sharing of objects between classes and methods. When a reference variable is assigned an object, it does not copy the object's contents, but rather creates a reference to the object's memory location. Reference variables can be null, meaning they do not reference any object. This can occur when a reference variable is created but has not been assigned an object, or when an object is deleted and the reference variable still holds the deleted object's memory address. Java's garbage collector automatically frees up memory allocated to objects that are no longer being referenced by any reference variable. It is important to ensure that all reference variables to an object are set to null when the object is no longer needed, to allow the garbage collector to reclaim the memory. In Java, reference variables can be used to access instance variables and methods of an object. They cannot be used to access private members of a class. When a reference variable is passed as a parameter to a method, the method receives a copy of the reference to the object held by the reference variable. In summary, reference variables are an essential concept in Java programming. They allow for the creation and manipulation of complex data structures, as well as shared access to objects between methods and classes.