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
- Which of the following data types is a reference type in Java? a) int b) double c) String d) char
Answer: c) String
- 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
- 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
- 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.
- In Java, can a reference variable be null? a) Yes b) No
Answer: a) Yes
- 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
- 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";
- 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
- 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
- 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
What is a reference variable in Java? Answer: A reference variable in Java is a variable that holds the memory address of an object.
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.
What is the default value of a reference variable in Java? Answer: The default value of a reference variable in Java is null.
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.
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.
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.
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.
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.
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.
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 (==).