0
0
Javaprogramming~10 mins

Instance methods in Java - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AsayHello
BGreet
Cgreet
DgreetPerson
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using incorrect method names or wrong capitalization.
Trying to call the method without the object.
2fill in blank
medium

Complete 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'
AsayAge
BshowAge
CprintAge
DgetAge
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a different method name than requested.
Forgetting to include parentheses after the method name.
3fill in blank
hard

Fix 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'
Aprint_Name
BPrintName
Cprintname
DprintName
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using wrong capitalization in method names.
Adding underscores or other characters not in the method name.
4fill in blank
hard

Fill 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'
AgetFullName
B-
C " "
D+
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a minus sign instead of a space string.
Forgetting to add a space between names.
5fill in blank
hard

Fill 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'
AisAdult
B>=
C;
D>
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using > instead of >=, which excludes age 18.
Forgetting the semicolon at the end of the return statement.