Reference data types store the address (reference) of objects in memory, not the actual data value itself. Examples include classes, arrays, and interfaces.
Click to reveal answer
beginner
How do reference types differ from primitive types in Java?
Primitive types store actual values (like int, double), while reference types store memory addresses pointing to objects. Reference types can be null; primitives cannot.
Click to reveal answer
intermediate
What happens when you assign one reference variable to another?
Both variables point to the same object in memory. Changes through one reference affect the same object seen by the other.
Click to reveal answer
beginner
Can reference variables be null? What does that mean?
Yes, reference variables can be null, meaning they do not point to any object in memory. Accessing methods or fields on a null reference causes a NullPointerException.
Click to reveal answer
beginner
Give an example of a reference data type in Java.
Example:
String name = "Alice";
Here, name is a reference variable pointing to a String object containing "Alice".
Click to reveal answer
Which of the following is a reference data type in Java?
Aint
Bdouble
CString
Dboolean
✗ Incorrect
String is a class in Java, so it is a reference data type. int, double, and boolean are primitive types.
What does a reference variable store?
AThe actual data value
BThe size of the object
CThe data type of the object
DThe memory address of an object
✗ Incorrect
Reference variables store the memory address (reference) of the object they point to.
What happens if you try to use a method on a null reference variable?
AThe program crashes with NullPointerException
BThe method returns null
CThe variable automatically gets initialized
DThe method runs normally
✗ Incorrect
Using a method on a null reference causes a NullPointerException at runtime.
If you assign one reference variable to another, what happens?
ABoth variables point to the same object
BA new object is created
CThe original object is deleted
DThe variables become primitive types
✗ Incorrect
Both reference variables will point to the same object in memory.
Which of these is NOT a reference data type?
AInterface
Bchar
CArray
DClass
✗ Incorrect
char is a primitive data type, not a reference type.
Explain what reference data types are in Java and how they differ from primitive types.
Think about how variables hold data versus how they hold addresses.
You got /4 concepts.
Describe what happens when you assign one reference variable to another in Java.
Consider what happens to the memory address stored.
You got /3 concepts.
Practice
(1/5)
1.
Which of the following is a reference data type in Java?
int, double, String, boolean
easy
A. String
B. int
C. double
D. boolean
Solution
Step 1: Identify primitive vs reference types
int, double, and boolean are primitive types storing actual values.
Step 2: Recognize String as a reference type
String stores a reference to an object containing text data.
Final Answer:
String -> Option A
Quick Check:
Reference type = String [OK]
Hint: Remember: String is an object, others are primitives [OK]
Common Mistakes:
Confusing String as primitive
Thinking int is a reference type
Assuming all data types are reference types
2.
Which of the following is the correct way to create a new String object in Java?
?
easy
A. String s = new string("hello");
B. String s = new String("hello");
C. String s = String("hello");
D. String s = new String["hello"];
Solution
Step 1: Check correct constructor syntax
Use 'new' keyword with class name and parentheses: new String("hello")
Step 2: Verify case sensitivity and brackets
Class names are case-sensitive (String, not string), and arrays use [] differently.
Final Answer:
String s = new String("hello"); -> Option B
Quick Check:
Correct object creation syntax = String s = new String("hello"); [OK]
Hint: Use 'new ClassName()' with exact case for objects [OK]
Common Mistakes:
Using lowercase 'string' instead of 'String'
Missing 'new' keyword
Using array brackets [] incorrectly
3.
What will be the output of the following code?
String a = "hello";
String b = a;
b = b + " world";
System.out.println(a);
medium
A. world
B. hello world
C. hello
D. Compilation error
Solution
Step 1: Understand reference assignment
Variable b initially points to the same String object as a ("hello").
Step 2: String concatenation creates new object
b = b + " world" creates a new String object; a still points to original "hello".
Final Answer:
hello -> Option C
Quick Check:
Strings are immutable; original stays same [OK]
Hint: String concatenation creates new object, original unchanged [OK]