- Executors
-
Learn
Learn Python Runnable examples, NumPy, pandas, and more Learn AI / Machine LearningNeural networks, ML and LLMs from scratch Learn C & C++ Pointers, memory, STL, and modern C++ Learn C#C# basics, OOP, LINQ, async, and .NET exercises Learn MatplotlibLine, bar, scatter and pie charts with live visualizers Learn RustOwnership, traits, error handling, and threads Learn GoSlices, interfaces, goroutines, and channels Python Algorithms Search, sorting, recursion, and visualizers O(n) C & C++ Algorithms Compare algorithms implemented in C and C++ Coding for Kids Learn to code by playing!
- QuizzesNew
- ChallengesNew
- For TeachersNew
- Updates
- Buy me a coffee
Is Java Pass by Value?
"Is Java pass by value or pass by reference?" is one of the most asked Java interview questions. This quiz answers it with real programs: you will see when a method's changes reach the caller and when they silently disappear.
Sample question
Question 1 of 10What does this print?
public class Main {
static void swap(String a, String b) {
String temp = a;
a = b;
b = temp;
}
public static void main(String[] args) {
String x = "left";
String y = "right";
swap(x, y);
System.out.println(x + " " + y);
}
}
Show the answer
B. left right a and b are copies of the references in x and y. Swapping the copies inside the method does not touch the caller's variables, which is why a classic swap method is impossible in Java.
What this quiz covers
- Java always passes a copy of the value
- For objects, the copied value is the reference
- Mutating an object vs reassigning a parameter
- Why a swap method does not work
- Immutable types like
StringandIntegeras parameters
Questions people ask
Do I need an account?
No. Anyone can play. Sign in to keep your XP, streak, badges and history.
Can I retake it?
Yes. Your best score is kept, and the questions and answers are shuffled each time.