Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a String variable named greeting.
Java
String greeting = [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the quotes around the text.
Using single quotes instead of double quotes.
✗ Incorrect
In Java, String values must be enclosed in double quotes.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call the constructor without
new.Assigning a primitive int directly to an Integer without autoboxing.
✗ Incorrect
To create an Integer object, use the new keyword with the constructor.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
== to compare Strings, which compares references, not content.Using
equals without parentheses, which is a syntax error.✗ Incorrect
Use the equals() method to compare the contents of two String objects.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 as the first index instead of 0.
Confusing the array size with the index.
✗ Incorrect
The array size is 3, and the first element index is 0.
5fill in blank
hardFill 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'
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.✗ Incorrect
Use HashMap to create the map, put to add entries, and get to retrieve values.