Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new String object from a character array.
DSA Java
char[] chars = {'H', 'e', 'l', 'l', 'o'};
String str = new String([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using the String variable 'str' inside its own constructor.
Trying to pass 'new char[]' without specifying the array.
✗ Incorrect
The String constructor takes a character array to create a new String object. Here, 'chars' is the character array.
2fill in blank
mediumComplete the code to compare two strings for content equality.
DSA Java
String a = "hello"; String b = new String("hello"); boolean result = a.[1](b);
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' which compares references, not content.
Using 'compareTo' which returns an int, not a boolean.
✗ Incorrect
The 'equals' method compares the content of two strings for equality.
3fill in blank
hardFix the error in the code to get the length of a string.
DSA Java
String s = "example"; int len = s.[1];
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'length' without parentheses which is for arrays, not strings.
Using 'size()' or 'count()' which are not String methods.
✗ Incorrect
In Java, the length of a String is obtained by calling the method length() with parentheses.
4fill in blank
hardFill both blanks to create a substring from index 2 to 5 (exclusive).
DSA Java
String s = "programming"; String sub = s.substring([1], [2]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same value for start and end index.
Confusing inclusive and exclusive indices.
✗ Incorrect
The substring method takes a start index (inclusive) and an end index (exclusive). To get characters at positions 2,3,4, use substring(2,5).
5fill in blank
hardFill all three blanks to create a map of words to their lengths for words longer than 3 characters.
DSA Java
Map<String, Integer> wordLengths = words.stream()
.filter(word -> word.length() [1] 3)
.collect(Collectors.toMap([2], [3]));Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in filter.
Using wrong lambda expressions in toMap.
✗ Incorrect
The filter keeps words with length greater than 3. The toMap uses the word as key and its length as value.
