Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a string variable named greeting with the value "Hello".
DSA Java
String greeting = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes instead of double quotes for strings.
Not using quotes at all, which causes a syntax error.
✗ Incorrect
In Java, strings must be enclosed in double quotes to be valid string literals.
2fill in blank
mediumComplete the code to find the length of the string stored in variable word.
DSA Java
int length = word.[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using size() which is for collections, not strings.
Using count() or getLength() which are not valid string methods.
✗ Incorrect
The length() method returns the number of characters in a Java string.
3fill in blank
hardFix the error in the code to get the character at index 2 from the string text.
DSA Java
char ch = text.[1](2);
Attempts:
3 left
💡 Hint
Common Mistakes
Using getChar or at which are not valid string methods.
Trying to access characters like an array without a method.
✗ Incorrect
The correct method to get a character at a specific index in a string is charAt(index).
4fill in blank
hardFill both blanks to create a substring from index 1 to 4 of the string s.
DSA Java
String sub = s.[1]([2], 5);
Attempts:
3 left
💡 Hint
Common Mistakes
Using substr which is not a Java String method.
Using wrong start index like 0 instead of 1.
✗ Incorrect
The substring method takes a start index and an end index (exclusive). Here, start is 1 and end is 5.
5fill in blank
hardFill all three blanks to check if the string str contains the word "data" ignoring case.
DSA Java
boolean containsData = str.[1]().[2]("data"); if (containsData [3] true) { System.out.println("Found data!"); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of == in the if condition.
Not converting string to lower case before contains check.
✗ Incorrect
Convert string to lower case, then check if it contains "data". Use == to compare boolean true.
