Classes and objects interview questions, with answers
Classes and objects are where OOP interviews start, and the questions look too easy to prepare for — until the interviewer writes Car myCar; on the board and asks what exists now, or copies one reference to another and asks what a change through the second does to the first. The topic is really about memory: what a class is, what an object is, and what a variable holds.
Here are the questions with the model that answers all of them. Then take the free OOP diagnostic — ten questions across all fifteen OOP topics, and a readiness figure with the arithmetic shown.
The questions, with answers
1.What is the difference between a class and an object?
A class is a definition: the fields an instance will have and the methods it can run — a blueprint, written once, existing in code. An object is an instance of that class created at run time with new: a block of memory holding its own values for those fields. One class, any number of objects. The follow-up that matters: fields belong to each object separately, but methods are shared — two Player objects have two scores and use the single addPoints() defined in the class, which updates whichever object's score it was called on. A class also exists as a runtime entity of its own (Class objects, static members), which is the answer to "can a class exist without objects": yes, and static members live there.
2.What does a declaration like Bike myBike; do, and what does new actually create?
Bike myBike; declares a reference variable of type Bike and nothing else — no Bike object exists, and using myBike before assigning it is a compile error for a local variable or a NullPointerException for a field, since fields default to null. new Bike() allocates an object on the heap, runs the constructor to initialise it, and returns a reference to it; myBike = new Bike() stores that reference in the variable. So a variable is a name for a slot that holds an address, and the object is the thing at that address. Drawing this as two boxes with an arrow between them — the variable and the object — is the single most useful habit for the rest of the questions on this page.
Bike myBike; // a reference, pointing nowhere myBike = new Bike(); // an object on the heap; myBike now refers to it Bike other = myBike; // a second reference to the SAME object
3.What happens when you assign one object variable to another?
You copy the reference, not the object. After Wallet w2 = w1, both variables point at the same Wallet, so w2.coins = 80 changes the one object and w1.coins reads 80 too. There is still exactly one Wallet. To get an independent copy you have to create one — a copy constructor, clone(), or a copy method — and even then a shallow copy shares any objects the fields refer to. This is the difference between reference types and primitives: int b = a copies the value, and changing b leaves a alone. Every "what is printed" question about aliasing is this rule; draw the boxes and arrows and the answer falls out.
Wallet w1 = new Wallet(50); Wallet w2 = w1; // same object, two names w2.coins = 80; System.out.println(w1.coins); // 80
4.Is Java pass-by-value or pass-by-reference?
Pass-by-value, always — but the value of an object variable is a reference, which is what confuses everyone. When you pass an object to a method, the method receives a copy of the reference pointing at the same object, so changes to the object's fields inside the method are visible to the caller, while reassigning the parameter to a new object changes only the method's copy and the caller's variable still points where it did. So void grow(List<Integer> l) { l.add(1); } affects the caller's list, and void replace(List<Integer> l) { l = new ArrayList<>(); } affects nothing outside. C++ offers genuine pass-by-reference with &; Python behaves like Java and calls it pass-by-assignment.
5.What is the lifecycle of an object, and when does it get collected?
An object is created when new allocates it and its constructor runs, it is alive as long as something can reach it — a local variable on some thread's stack, a static field, or a field of another reachable object — and it becomes eligible for garbage collection the moment no chain of references leads to it. Setting a variable to null, letting it go out of scope, or overwriting the last reference all do that. The collector reclaims it at a time of its own choosing; you cannot force it, and System.gc() is only a hint. Two consequences interviewers probe: objects that only reference each other (an island of isolation) are still collected, because reachability is measured from the roots; and a static collection that keeps adding objects is a memory leak, because everything in it stays reachable forever.
6.What is the difference between == and equals(), and how does hashCode fit in?
For objects, == asks whether two references point at the same object; equals() asks whether two objects are meaningfully equal, and by default it does the same as == until a class overrides it. So two Point objects built with the same coordinates are == false and, with a sensible equals override, equals true. The contract that comes with it: if you override equals you must override hashCode consistently, so that equal objects hash equally, or the objects misbehave as keys in a HashMap or members of a HashSet. For primitives, == compares values and is the only option. The interview version usually uses strings, where literals happen to be == because they are interned, so the honest rule is equals for content, always.
7.What is an anonymous object, and when is it useful?
An object created and used without being stored in a variable — new Greeter().greet(), or passing new Comparator<...>() { ... } straight into a sort call. It is useful when the object is needed exactly once and holding a name for it would be noise; it also avoids the temptation to reuse it later. Because nothing references it after the expression, it is eligible for collection immediately. Do not confuse it with an anonymous class, which is a class without a name — an anonymous object can be an instance of a perfectly ordinary named class. Method chaining on a fresh object, new StringBuilder().append("a").append("b").toString(), is the everyday form.
8.What is the difference between a class, a struct and a record?
In C++ a struct and a class are the same thing except for the default access level — public for struct, private for class — and convention uses struct for plain data bundles. C# structs are genuinely different: value types copied on assignment and stored inline, versus classes, which are reference types on the heap. Java has no struct; its record (Java 16) is a class whose fields are declared once in the header, with a constructor, accessors, equals, hashCode and toString generated, and whose instances are immutable — the right tool for a plain data carrier, and the answer to "how would you write a value object with less boilerplate". Python's dataclass and Kotlin's data class are the same idea. Naming which language you mean before answering is most of the mark.
How the diagnostic asks it
One question from the OOP bank, exactly as a sitting would show it. The bank has 4 on classes & objects and 60 across OOP.
Which statement best describes the relationship between a class and an object in OOP?
- 1A class can only be used to create one object during a program's lifetime.
- 2A class and an object are always the same thing with different names.
- 3An object is a blueprint/template, and a class is a specific instance created from that blueprint.
- 4A class is a blueprint/template, and an object is a specific instance created from that blueprint.correct
A class defines the structure (fields/methods) shared by all instances, while an object is a concrete instance in memory with its own state. Saying the object is the blueprint and the class the instance reverses the relationship; a class and an object are distinct concepts, not the same thing under two names; and a class can be used to instantiate any number of objects, not just one.
Measure it
Reading answers tells you what’s true. A diagnostic tells you what you get wrong.
10 OOP questions across its topics, easy to hard, about fifteen minutes. You get a readiness figure with the arithmetic shown, the topics you missed named, and a practice set sized for today. Free: 1 diagnostic a month and 15 problems a day. No card.