Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a simple class named User.
Java
public class [1] { // class body }
🎯 Drag options to blanks, or click blank then click option
Attempts:
3 left
2fill in blank
mediumComplete the code to create a method named greet that returns a String.
Java
public class Greeter { public [1] greet() { return "Hello!"; } }
🎯 Drag options to blanks, or click blank then click option
Attempts:
3 left
3fill in blank
hardFix the error in the code to correctly instantiate an object of class Car.
Java
public class Car { String model; public Car(String model) { this.model = model; } } Car myCar = new [1]("Tesla");
🎯 Drag options to blanks, or click blank then click option
Attempts:
3 left
4fill in blank
hardFill both blanks to create a method that checks if a user is adult based on age.
Java
public class User { int age; public boolean isAdult() { return age [1] [2]; } }
🎯 Drag options to blanks, or click blank then click option
Attempts:
3 left
5fill in blank
hardFill all three blanks to create a method that returns a map of user names and their ages if age is above 20.
Java
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; } }
🎯 Drag options to blanks, or click blank then click option
Attempts:
3 left
