Bird
Raised Fist0
DSA Javaprogramming~20 mins

String Traversal and Character Access in DSA Java - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
String Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of character traversal with index increment
What is the output of the following Java code snippet?
DSA Java
public class Main {
    public static void main(String[] args) {
        String s = "hello";
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < s.length(); i += 2) {
            result.append(s.charAt(i));
        }
        System.out.println(result.toString());
    }
}
Ahlo
Bhelo
Cel
Dheo
Attempts:
2 left
💡 Hint
Look at how the loop increments the index by 2 each time.
❓ Predict Output
intermediate
2:00remaining
Output of reverse string traversal
What will be printed by this Java code?
DSA Java
public class Main {
    public static void main(String[] args) {
        String s = "world";
        StringBuilder reversed = new StringBuilder();
        for (int i = s.length() - 1; i >= 0; i--) {
            reversed.append(s.charAt(i));
        }
        System.out.println(reversed.toString());
    }
}
Adlorw
Bworld
Cdlrow
Dwrold
Attempts:
2 left
💡 Hint
The loop goes from the last character to the first.
🔧 Debug
advanced
2:00remaining
Identify the error in string character access
What error will this Java code produce when run?
DSA Java
public class Main {
    public static void main(String[] args) {
        String s = "abc";
        char c = s.charAt(3);
        System.out.println(c);
    }
}
ANullPointerException
BStringIndexOutOfBoundsException
CNo error, prints 'c'
DArrayIndexOutOfBoundsException
Attempts:
2 left
💡 Hint
Check the valid index range for the string.
❓ Predict Output
advanced
2:00remaining
Output of nested loops with string character access
What is the output of this Java code?
DSA Java
public class Main {
    public static void main(String[] args) {
        String s = "abc";
        StringBuilder output = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            for (int j = 0; j <= i; j++) {
                output.append(s.charAt(j));
            }
            output.append(",");
        }
        System.out.println(output.toString());
    }
}
Aa,ab,abc,
Ba,ab,ac,
Ca,aab,abc,
Da,b,abc,
Attempts:
2 left
💡 Hint
The inner loop prints characters from start up to current outer index.
❓ Predict Output
expert
3:00remaining
Output after filtering vowels from string
What is the output of this Java code that filters vowels from a string?
DSA Java
public class Main {
    public static void main(String[] args) {
        String s = "education";
        StringBuilder vowels = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
                vowels.append(c);
            }
        }
        System.out.println(vowels.toString());
    }
}
Auioa
Beducation
Cedctn
Deuaio
Attempts:
2 left
💡 Hint
Only vowels are appended to the result string.

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

  1. Step 1: Understand the purpose of charAt()

    The charAt() method is designed to access a single character at a given index in a string.
  2. 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().
  3. Final Answer:

    Returns the character at a specified index in the string -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); } -> Option C
  4. 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

  1. 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).
  2. Step 2: Find characters at even indices

    Index 0: 'h', Index 2: 'l', Index 4: 'o'. These characters are printed in order.
  3. Final Answer:

    hlo -> Option B
  4. 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

  1. 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.
  2. Step 2: Identify error cause

    At i=3, charAt(3) is called, which is out of string bounds, causing StringIndexOutOfBoundsException.
  3. Final Answer:

    StringIndexOutOfBoundsException -> Option A
  4. 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

  1. Step 1: Understand the goal

    We want to print only digits from the string, ignoring letters.
  2. 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.
  3. 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
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard