0
0
Javaprogramming~10 mins

Reference data types 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 declare a String variable named greeting.

Java
String greeting = [1];
Drag options to blanks, or click blank then click option'
A"Hello"
BHello
Cchar[]
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the quotes around the text.
Using single quotes instead of double quotes.
2fill in blank
medium

Complete the code to create a new Integer object with value 10.

Java
Integer number = [1];
Drag options to blanks, or click blank then click option'
Anew Integer(10)
BInteger(10)
C10
Dint(10)
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call the constructor without new.
Assigning a primitive int directly to an Integer without autoboxing.
3fill in blank
hard

Fix the error in the code to compare two String objects correctly.

Java
if (str1.[1](str2)) {
    System.out.println("Strings are equal");
}
Drag options to blanks, or click blank then click option'
A==
Bequals
Cequals()
DcompareTo()
Attempts:
3 left
💡 Hint
Common Mistakes
Using == to compare Strings, which compares references, not content.
Using equals without parentheses, which is a syntax error.
4fill in blank
hard

Fill both blanks to create an array of Strings and assign a value to the first element.

Java
String[] names = new String[[1]];
names[[2]] = "Alice";
Drag options to blanks, or click blank then click option'
A3
B0
C1
Dnew String[3]
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 as the first index instead of 0.
Confusing the array size with the index.
5fill in blank
hard

Fill all three blanks to create a HashMap, add a key-value pair, and retrieve a value.

Java
Map<String, Integer> map = new [1]<>();
map.[2]("age", 25);
int age = map.[3]("age");
Drag options to blanks, or click blank then click option'
AHashMap
Bput
Cget
DTreeMap
Attempts:
3 left
💡 Hint
Common Mistakes
Using TreeMap instead of HashMap (both work but only one is correct here).
Using get to add entries or put to retrieve values.