Java Classes Quiz: Constructors, Static and Encapsulation Test
Test your Java class skills with 12 questions and two programs on new and this, constructors, initialisation order, static members, access and immutability.
- Course: Java study plan
- Module: OOP I — classes & objects
- Kind: Checkpoint — cleared at 70%
- Reading time: 22 min
- Runtime: OpenJDK 18
Checkpoint — Classes & objects is the checkpoint that closes the OOP I — classes & objects module: a graded quiz and whole-program exercises, passed at 70%.
Instructions
This checkpoint covers the object model, constructors and initialisation order, static members, access modifiers and immutability, and class design.
How it works. Twelve questions and two programs; 70% on the questions and both programs accepted clears the module.
Before you start, make sure you can answer:
- What
newdoes, step by step, and whatthisis. - What happens to the default constructor when you declare one.
- The order in which field initialisers and constructor bodies run.
- Why a static method cannot read an instance field.
- The four access levels and what
protectedincludes. - The recipe for an immutable class.
The programs ask you to write two small classes from scratch — an entity with invariants and a value type with toString — and drive them from main.
Common questions
What does new do step by step in Java?
It allocates memory for the object on the heap, sets every field to its default (0, false or null), runs the constructor — the super constructor first, then the field initialisers, then the constructor body — and returns a reference to the new object.
What happens to the default constructor when you declare a constructor in Java?
It disappears. The compiler generates a no-argument default constructor only when a class declares no constructors at all, so once you add any constructor, new X() compiles only if you also declare a no-argument one.
What is the recipe for an immutable class in Java?
No mutators, all fields private final, a final class, defensive copies of any mutable component on the way in and out, and methods that return a new instance instead of changing the current one.
Exercises
A stack class
Implement class IntStack backed by an int[] that grows (double the array with Arrays.copyOf when full), with push(int), pop() (throws IllegalStateException when empty), peek(), size() and isEmpty(). Read an integer n then n commands: push <v>, pop (print the popped value or empty), peek (print the top or empty), size (print the size).
Example: 6 then push 1, push 2, peek, pop, pop, pop →
2
2
1
emptyMoney that adds up
Write an immutable final class Money with private final long cents and private final String currency, a constructor that rejects a negative amount or a blank currency, plus(Money other) (throws IllegalArgumentException if the currencies differ), times(int factor), and toString() formatted as <currency> <whole>.<cc> (two-digit cents).
Read an integer n, then n lines <currency> <cents>. Sum all amounts of the first line's currency with plus, skipping lines whose currency differs (count them), then print the total and skipped=<count>.
Example: 3 then EUR 150, USD 100, EUR 5 →
EUR 1.55
skipped=1Library loans
Model a small library. class Book has a title and a loanedTo (member name or null). class Library holds books in insertion order and supports lend <title> <member> (lent <title> to <member>, or <title> is already out / no such book), return <title> (returned <title>, or <title> was not out), and report (one line per book: <title>: available or <title>: with <member>). Read an integer n and n titles, then m commands.
Example: 2 then Dune Emma, then 3 then lend Dune ann, lend Dune bob, report →
lent Dune to ann
Dune is already out
Dune: with ann
Emma: availableIn this module: OOP I — classes & objects
← Designing a class — cohesion, composition and toString · Extending classes — super, constructors and what is inherited →