Which statement best describes the relationship between a class and an object?
One class can produce any number of objects.
Classes and Objects · Exercise 1 of 4
Twenty multiple-choice questions on Java classes and objects: classes vs objects, new, references, null, == vs equals, toString, object arrays, garbage collection and nested classes. Pick an option to check your answer instantly.
Which statement best describes the relationship between a class and an object?
One class can produce any number of objects.
What does new Car() do?
It also initialises the fields and runs a constructor.
How many Car objects does Car c1, c2, c3; create?
It only declares three reference variables.
After Car b = a;, how many Car objects exist (assuming a referred to one)?
Only the reference is copied.
What does it mean when a reference variable is null?
Calling a method through it throws NullPointerException.
Which line throws NullPointerException if s is null?
Comparing, assigning or printing null is fine; dereferencing it is not.
A class has fields int age; String name; boolean active;. What are they right after new (no constructor code)?
Fields always get default values.
If a class does not override equals, what does a.equals(b) compare?
Object.equals is identity comparison.
Why must hashCode be overridden whenever equals is?
This is the equals/hashCode contract from java.lang.Object.
What does System.out.println(obj) print for a class that does not override toString?
For example Product@1b6d3586.
Timed online tests with instant scores and explanations.