Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to call the instance method greet on the object person.
Java
Person person = new Person();
person.[1](); Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using incorrect method names or wrong capitalization.
Trying to call the method without the object.
β Incorrect
The instance method is named
greet, so calling person.greet() is correct.2fill in blank
mediumComplete the code to define an instance method named sayAge that prints the age.
Java
public class Person { int age; public void [1]() { System.out.println("Age: " + age); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using a different method name than requested.
Forgetting to include parentheses after the method name.
β Incorrect
The method name must be
sayAge as requested to match the task.3fill in blank
hardFix the error in the instance method call to correctly print the name.
Java
public class Person { String name; public void printName() { System.out.println(name); } } Person p = new Person(); p.name = "Alice"; p.[1]();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using wrong capitalization in method names.
Adding underscores or other characters not in the method name.
β Incorrect
Method names are case-sensitive in Java. The correct method name is
printName.4fill in blank
hardFill both blanks to create a method that returns the full name by combining first and last names.
Java
public class Person { String firstName; String lastName; public String [1]() { return firstName + [2] + lastName; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using a minus sign instead of a space string.
Forgetting to add a space between names.
β Incorrect
The method name should be
getFullName. To combine names with a space, use " " between them.5fill in blank
hardFill both blanks to define an instance method isAdult that returns true if age is 18 or more.
Java
public class Person { int age; public boolean [1]() { return age [2] 18; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using > instead of >=, which excludes age 18.
Forgetting the semicolon at the end of the return statement.
β Incorrect
The method name is
isAdult. The condition uses >= to check if age is 18 or more. The statement ends with a semicolon.