Java methods and objects quiz

  • Admin
  • 1 min read

Nine questions on methods, parameters, classes, constructors and encapsulation.

Java

Java methods and objects quiz

9 questions, instant feedback

Question 1 of 9
Question 1 of 9 What does a method's return type void mean?
Why this is the answer

void declares that the method produces no value, so it cannot be used in an expression.

Question 2 of 9
Question 2 of 9 What is method overloading?
Why this is the answer

Overloading distinguishes methods by parameter list. Overriding replaces an inherited method with the same signature.

Question 3 of 9
Question 3 of 9 What does a constructor do?
Why this is the answer

A constructor runs when an object is created and sets up its initial state. It has no return type.

Question 4 of 9
Question 4 of 9 What happens if a class declares no constructor?
Why this is the answer

The compiler adds a default constructor that takes no arguments. Declaring any constructor removes it.

Question 5 of 9
Question 5 of 9 Why make a field private?
Why this is the answer

Private fields can only be changed by the class itself, so invariants can be enforced in one place.

Question 6 of 9
Question 6 of 9 What does the this keyword refer to?
Why this is the answer

this is a reference to the object the method was called on, which disambiguates fields from parameters.

Question 7 of 9
Question 7 of 9 What does static mean on a method?
Why this is the answer

A static method is called on the class, such as Math.max, and has no access to instance fields.

Question 8 of 9
Question 8 of 9 Which pair of methods should usually be overridden together?
Why this is the answer

Equal objects must have equal hash codes. Overriding one without the other breaks hash-based collections.

Question 9 of 9
Question 9 of 9 What is a getter for?
Why this is the answer

A getter returns the value and can apply logic, such as returning a copy or a computed value.

Your answers stay in this browser. Nothing is sent to a server.