Complete the code to declare a simple class named User.
public class [1] { // class body }
The class name should start with an uppercase letter and match the intended name. Here, User is the correct class name.
Complete the code to create a method named greet that returns a String.
public class Greeter { public [1] greet() { return "Hello!"; } }
void which means no return valueThe method returns a text message, so its return type must be String.
Fix the error in the code to correctly instantiate an object of class Car.
public class Car { String model; public Car(String model) { this.model = model; } } Car myCar = new [1]("Tesla");
Class names are case-sensitive and must match exactly. The correct class name is Car with uppercase C.
Fill both blanks to create a method that checks if a user is adult based on age.
public class User { int age; public boolean isAdult() { return age [1] [2]; } }
The method returns true if age is greater than or equal to 18, which means the user is an adult.
Fill all three blanks to create a method that returns a map of user names and their ages if age is above 20.
import java.util.Map; import java.util.HashMap; public class UserData { Map<String, Integer> users = new HashMap<>(); public Map<String, Integer> getAdults() { Map<String, Integer> adults = new HashMap<>(); for (String [1] : users.keySet()) { int [2] = users.get([1]); if ([2] [3] 20) { adults.put([1], [2]); } } return adults; } }
The loop variable is name, the age is retrieved into age, and the condition checks if age is greater than 20.
