Core Language
Java features, the main method, primitives vs wrappers, static vs instance, and final/finally/finalize.
9 questions
JuniorTheoryVery commonIs Java pass-by-value or pass-by-reference when you call a method?
Is Java pass-by-value or pass-by-reference when you call a method?
Java is always pass-by-value — there is no pass-by-reference. For a primitive, the value itself is copied into the parameter. For an object, the reference is copied, so the parameter points at the very same object and the method can mutate its state through it. But reassigning the parameter rebinds only that local copy, so the caller's variable still refers to the original object.
Common mistakes
- ✗Calling Java pass-by-reference for objects because the callee can mutate them through the copied reference
- ✗Expecting a method to be able to swap the caller's two object variables by reassigning its parameters
- ✗Thinking the object itself is copied into the method, rather than the reference to it
Follow-up questions
- →Why can a method mutate the fields of an object it receives but not swap two objects for its caller?
- →How does passing an array differ from passing an
intto the same method?
JuniorTheoryVery commonWhat is the difference between a static member and an instance member?
What is the difference between a static member and an instance member?
A static member belongs to the class itself: there is one shared copy for all objects, and you access it through the class without any instance. An instance member belongs to each object, so every object gets its own separate copy. A static method cannot use instance fields directly because no particular object is associated with the call.
Common mistakes
- ✗Confusing static (shared one copy) with final (cannot change) — they are independent concepts
- ✗Thinking each instance gets its own copy of a static field instead of sharing one
- ✗Trying to read instance fields directly from a static method without an object reference
Follow-up questions
- →Why can a static method not reference this, while an instance method can?
- →When is a static field initialized relative to the first instance being created?
MiddleTheoryVery commonWhat is the difference between final, finally, and finalize?
What is the difference between final, finally, and finalize?
They are three unrelated things. final is a modifier: a final variable is a constant, a final method cannot be overridden, and a final class cannot be subclassed. finally is a block after try/catch that always runs, even if an exception is thrown, so it is used for cleanup. finalize() is a deprecated Object method the GC could call before reclaiming an object.
Common mistakes
- ✗Treating the three as related when they govern modifiers, control flow, and GC separately
- ✗Believing finally is skipped when an exception is thrown, when it runs regardless
- ✗Relying on finalize for cleanup, though it is deprecated and may never be called
Follow-up questions
- →Is the finally block guaranteed to run if the try block calls System.exit?
- →Why was finalize deprecated, and what replaces it for releasing resources?
JuniorTheoryCommonWhat is an enum in Java, and can it carry fields and methods?
What is an enum in Java, and can it carry fields and methods?
An enum is a class whose instances are a fixed set of named constants that the JVM creates once, which makes it type-safe and usable in a switch. Because each constant is a real object, an enum can declare fields, a private constructor and methods, and a single constant may even override a method with its own body. It implicitly extends java.lang.Enum, so it may implement interfaces but cannot extend a class.
Common mistakes
- ✗Treating an
enumas a bare list ofintconstants with no state or behaviour of its own - ✗Trying to make an
enumextend a class, which is impossible because it already extendsjava.lang.Enum - ✗Instantiating a constant with
newinstead of relying on the fixed instances the JVM creates once
Follow-up questions
- →Why is comparing two enum constants with
==safe, unlike comparing twoIntegervalues? - →How does a single-constant
enumgive you a singleton for free?
MiddleTheoryCommonWhat are varargs, and what does the compiler turn them into?
What are varargs, and what does the compiler turn them into?
A varargs parameter, written T... xs, lets a caller pass any number of arguments. It is compiler sugar: the parameter becomes a plain array T[], and at each call site the loose arguments are wrapped into a freshly allocated array whose reference is passed by value. Inside the method xs is just an array — never null, and empty for a zero-argument call. It must be the last parameter, and a method may declare only one.
Common mistakes
- ✗Thinking varargs deliver a
Listrather than a plain array created by the compiler - ✗Expecting a zero-argument call to pass
nullinstead of an empty array - ✗Believing a method can declare more than one varargs parameter, or place it before another parameter
Follow-up questions
- →What exactly does a varargs method receive when it is called with no arguments at all?
- →Why does passing an existing
T[]to a varargs method not allocate a second array?
JuniorTheoryOccasionalWhat is the default value of a field versus a local variable?
What is the default value of a field versus a local variable?
Fields — both instance and static — are automatically initialized to a type-specific default: 0 for numeric types, false for boolean, '\u0000' for char, and null for references. A local variable gets no default at all; the compiler enforces definite assignment and rejects any read of it before it has been assigned a value.
Common mistakes
- ✗Believing a local variable is silently zero-initialized like a field, rather than rejected by the compiler
- ✗Thinking a field must be explicitly initialized before use, when it defaults automatically
- ✗Assuming a numeric field defaults to
nullinstead of0
Follow-up questions
- →Why does the compiler allow an uninitialized field but not an uninitialized local?
- →What default does an array's elements get right after
new int[3]?
JuniorTheoryOccasionalWhat are the core traits that define Java as a language?
What are the core traits that define Java as a language?
Java is object-oriented, statically and strongly typed. Source compiles to bytecode that any JVM runs, making it platform-independent (write once, run anywhere). The JVM manages memory automatically via garbage collection, so there are no raw pointers, which keeps it robust. It also has built-in multithreading support.
Common mistakes
- ✗Saying Java compiles to native machine code rather than to portable bytecode run by the JVM
- ✗Calling Java dynamically typed when it is statically typed and checked at compile time
- ✗Claiming Java requires manual memory freeing instead of relying on the garbage collector
Follow-up questions
- →How does the JVM make the same compiled bytecode run on different operating systems?
- →What does garbage collection free the programmer from compared with manual memory management?
JuniorTheoryOccasionalWhat order does Java initialize fields and blocks in?
What order does Java initialize fields and blocks in?
Static parts run once, when the class is first loaded: the parent's static fields and static blocks first, then the child's, in source order. Then per instance: the parent runs its instance-field initializers and instance-initializer blocks and its constructor, then the child does the same. So all of the parent's static, then the child's static; then the parent's instance and constructor, then the child's.
Common mistakes
- ✗Believing static blocks re-run on every instantiation rather than once at class load
- ✗Thinking the child initializes before the parent
- ✗Assuming the constructor runs before field initializers and instance blocks
Follow-up questions
- →When exactly is a class loaded and its static block triggered?
- →Why can reading a child field from a parent constructor see the default value?
JuniorTheoryOccasionalWhy is the main method declared public static void main(String[] args)?
Why is the main method declared public static void main(String[] args)?
It is the JVM's entry point, so the JVM must be able to call it before any object exists. public lets the JVM call it from outside the class. static means it runs without creating an instance. void says it returns nothing to the JVM. String[] args receives the command-line arguments as an array of strings.
Common mistakes
- ✗Thinking the JVM creates an instance first, instead of calling the static main directly
- ✗Believing main must return an int exit code rather than void
- ✗Assuming args holds environment variables instead of the command-line arguments
Follow-up questions
- →What happens at runtime if a class has no method matching the exact main signature?
- →Can main be overloaded, and which overload does the JVM actually invoke?