What does it mean to traverse a string in programming?
Traversing a string means going through each character one by one, usually from the first character to the last, to read or process them.
Click to reveal answer
beginner
How do you access the character at a specific position in a string in Java?
You use the method charAt(index), where index is the position starting from 0 for the first character.
Click to reveal answer
beginner
What is the index of the first character in a Java string?
The first character of a Java string is at index 0 because Java uses zero-based indexing.
Click to reveal answer
intermediate
Why is zero-based indexing used in string traversal?
Zero-based indexing starts counting from 0, which matches how memory addresses work in computers and makes calculations simpler.
Click to reveal answer
intermediate
What happens if you try to access a character at an index outside the string length in Java?
Java throws a StringIndexOutOfBoundsException error because the index is invalid and does not point to any character.
Click to reveal answer
In Java, what does str.charAt(3) return?
AAn error because index 3 is invalid
BThe third character of the string <code>str</code>
CThe first character of the string <code>str</code>
DThe fourth character of the string <code>str</code>
✗ Incorrect
Java strings use zero-based indexing, so index 3 is the fourth character.
What is the index of the last character in a string of length 10?
A9
B10
C0
D11
✗ Incorrect
The last character is at index length - 1, so 10 - 1 = 9.
Which method is used to find the length of a string in Java?
Alength
Bsize()
Clength()
Dcount()
✗ Incorrect
Java strings use the length() method to get the number of characters.
What will happen if you try to access str.charAt(-1) in Java?
AReturns the first character
BThrows StringIndexOutOfBoundsException
CReturns null
DReturns the last character
✗ Incorrect
Negative indexes are invalid and cause an exception in Java.
Which loop is commonly used to traverse all characters in a string?
Afor loop with index from 0 to length-1
Bwhile loop with index starting at length
Cdo-while loop starting at length
Dfor loop with index from 1 to length
✗ Incorrect
A for loop from 0 to length-1 covers all characters in order.
Explain how to traverse a string and access each character in Java.
Think about how to visit every character one by one.
You got /3 concepts.
What errors can occur when accessing characters in a string and how to avoid them?
Consider what happens if you ask for a character outside the string.
You got /3 concepts.
Practice
(1/5)
1. What does the charAt() method do when used on a Java String?
easy
A. Returns the character at a specified index in the string
B. Returns the length of the string
C. Converts the string to uppercase
D. Splits the string into an array of substrings
Solution
Step 1: Understand the purpose of charAt()
The charAt() method is designed to access a single character at a given index in a string.
Step 2: Compare with other string methods
Methods like length() return the string length, toUpperCase() changes case, and split() divides the string. These are different from charAt().
Final Answer:
Returns the character at a specified index in the string -> Option A
Quick Check:
charAt() = character access [OK]
Hint: Remember: charAt gets one character by index [OK]
Common Mistakes:
Confusing charAt() with length()
Thinking charAt() splits the string
Assuming charAt() changes string case
2. Which of the following is the correct way to loop through each character of a string str in Java?
easy
A. for (int i = 1; i < str.length(); i++) { char c = str.charAt(i); }
B. for (int i = 0; i <= str.length(); i++) { char c = str.charAt(i); }
C. for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); }
D. for (int i = 1; i <= str.length(); i++) { char c = str.charAt(i); }
Solution
Step 1: Understand string indexing in Java
String indices start at 0 and go up to length - 1. So loop must run from 0 to less than length.
Step 2: Check loop conditions
for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); } uses i < str.length(), which correctly stops before index equals length. Options B and D use <= which causes out-of-bounds error. Options C and D start at 1, skipping first character.
Final Answer:
for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); } -> Option C
Quick Check:
Loop from 0 to length-1 [OK]
Hint: Loop from 0 to length-1 for string indices [OK]
Common Mistakes:
Using <= instead of < causing index error
Starting loop at 1 skipping first char
Confusing string length with last index
3. What is the output of the following Java code?
String s = "hello";
for (int i = 0; i < s.length(); i++) {
if (i % 2 == 0) {
System.out.print(s.charAt(i));
}
}
medium
A. hello
B. hlo
C. el
D. heo
Solution
Step 1: Identify loop and condition
The loop runs from i=0 to 4 (length 5). It prints characters only when i is even (0,2,4).
Step 2: Find characters at even indices
Index 0: 'h', Index 2: 'l', Index 4: 'o'. These characters are printed in order.
Final Answer:
hlo -> Option B
Quick Check:
Even indices chars = hlo [OK]
Hint: Print chars at even indices only [OK]
Common Mistakes:
Printing all characters ignoring condition
Confusing even and odd indices
Off-by-one errors in loop
4. Consider this Java code snippet:
String text = "abc";
for (int i = 0; i <= text.length(); i++) {
System.out.print(text.charAt(i));
}
What error will occur when running this code?
medium
A. StringIndexOutOfBoundsException
B. ArrayIndexOutOfBoundsException
C. NullPointerException
D. No error, prints 'abc'
Solution
Step 1: Analyze loop bounds
The loop runs from i=0 to i=3 (because of <= and length=3). Valid indices are 0,1,2 only.
Step 2: Identify error cause
At i=3, charAt(3) is called, which is out of string bounds, causing StringIndexOutOfBoundsException.
Final Answer:
StringIndexOutOfBoundsException -> Option A
Quick Check:
charAt index out of range causes StringIndexOutOfBoundsException [OK]
Hint: Loop must use < length, not <= length [OK]
Common Mistakes:
Confusing StringIndexOutOfBoundsException with ArrayIndexOutOfBoundsException
Assuming no error occurs
Thinking NullPointerException will happen
5. Given the string input = "a1b2c3d4", which code snippet correctly extracts and prints only the digits in order?
hard
A. for (int i = 0; i < input.length(); i++) { if (Character.isLetter(input.charAt(i))) System.out.print(input.charAt(i)); }
B. for (int i = 0; i < input.length(); i++) { if (input.charAt(i) >= 'a' && input.charAt(i) <= 'z') System.out.print(input.charAt(i)); }
C. for (int i = 0; i < input.length(); i++) { System.out.print(input.charAt(i)); }
D. for (int i = 0; i < input.length(); i++) { if (input.charAt(i) >= '0' && input.charAt(i) <= '9') System.out.print(input.charAt(i)); }
Solution
Step 1: Understand the goal
We want to print only digits from the string, ignoring letters.
Step 2: Check each option's condition
for (int i = 0; i < input.length(); i++) { if (Character.isLetter(input.charAt(i))) System.out.print(input.charAt(i)); } uses Character.isLetter(), prints letters. for (int i = 0; i < input.length(); i++) { if (input.charAt(i) >= 'a' && input.charAt(i) <= 'z') System.out.print(input.charAt(i)); } checks lowercase letters range, prints letters. for (int i = 0; i < input.length(); i++) { System.out.print(input.charAt(i)); } prints all characters. for (int i = 0; i < input.length(); i++) { if (input.charAt(i) >= '0' && input.charAt(i) <= '9') System.out.print(input.charAt(i)); } checks if char between '0' and '9', prints "1234" correctly.
Final Answer:
for (int i = 0; i < input.length(); i++) { if (input.charAt(i) >= '0' && input.charAt(i) <= '9') System.out.print(input.charAt(i)); } -> Option D
Quick Check:
Check char between '0' and '9' to find digits [OK]
Hint: Check if char is between '0' and '9' for digits [OK]
Common Mistakes:
Printing letters instead of digits
Printing all characters without filtering
Using wrong character range for digits
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.