Python Java C C++ HTML CSS JS

Java Practice Questions

Topic-based coding problems with sample test cases — build confidence step by step.

Java Strings Practice

String methods, manipulation, palindromes, anagrams and character processing.

1
String Length
Easy
Read a string and print its length.
A single line containing a string S (no spaces).
A single integer: the length of S.
Sample Input 1
hello
Sample Output 1
5
Sample Input 2
java
Sample Output 2
4
Sample Input 3
a
Sample Output 3
1
Sample Input 4
TechBridge24
Sample Output 4
12
Sample Input 5
abcdefghijklmnopqrstuvwxyz
Sample Output 5
26
1
Use s.length().
2
Reverse a String
Easy
Read a string and print it reversed.
A single line containing a string S (no spaces).
The reversed string.
Sample Input 1
hello
Sample Output 1
olleh
Sample Input 2
abc
Sample Output 2
cba
Sample Input 3
a
Sample Output 3
a
Sample Input 4
racecar
Sample Output 4
racecar
Sample Input 5
TechBridge24
Sample Output 5
42egdirBhceT
1
Iterate from the end or use StringBuilder.reverse().
3
Count Vowels
Easy
Read a string of lowercase letters and print the number of vowels (a, e, i, o, u).
A single line containing a string S of lowercase letters.
A single integer: the vowel count.
Sample Input 1
hello
Sample Output 1
2
Sample Input 2
programming
Sample Output 2
3
Sample Input 3
aeiou
Sample Output 3
5
Sample Input 4
bcdfg
Sample Output 4
0
Sample Input 5
java
Sample Output 5
2
1
Loop over chars and check against "aeiou".
4
First and Last Character
Easy
Read a string and print its first and last characters on one line, separated by a space.
A single line containing a string S (no spaces).
Two characters: first and last, space-separated.
Sample Input 1
hello
Sample Output 1
h o
Sample Input 2
java
Sample Output 2
j a
Sample Input 3
ab
Sample Output 3
a b
Sample Input 4
TechBridge24
Sample Output 4
T 4
Sample Input 5
palindrome
Sample Output 5
p e
2
s.charAt(0) and s.charAt(s.length() - 1).
5
String to Uppercase
Easy
Read a string of lowercase letters and print it in uppercase.
A single line containing a string S of lowercase letters.
The uppercase version of S.
Sample Input 1
hello
Sample Output 1
HELLO
Sample Input 2
java
Sample Output 2
JAVA
Sample Input 3
a
Sample Output 3
A
Sample Input 4
techbridge24
Sample Output 4
TECHBRIDGE24
Sample Input 5
xyz
Sample Output 5
XYZ
1
s.toUpperCase() or subtract 32 from each char.
1
Check Palindrome String
Medium
Read a string and print "Palindrome" if it reads the same forwards and backwards, otherwise "Not Palindrome".
A single line containing a string S (no spaces).
"Palindrome" or "Not Palindrome".
Sample Input 1
racecar
Sample Output 1
Palindrome
Sample Input 2
hello
Sample Output 2
Not Palindrome
Sample Input 3
a
Sample Output 3
Palindrome
Sample Input 4
abba
Sample Output 4
Palindrome
Sample Input 5
12321
Sample Output 5
Palindrome
1
Compare s with its reverse, or two-pointer from both ends.
2
Count Words
Medium
Read a sentence and print the number of words (words are separated by single spaces).
A single line containing a sentence.
A single integer: the word count.
Sample Input 1
hello world
Sample Output 1
2
Sample Input 2
Java is fun
Sample Output 2
3
Sample Input 3
one
Sample Output 3
1
Sample Input 4
a b c d e
Sample Output 4
5
Sample Input 5
Tech Bridge 24
Sample Output 5
3
1
Split by space and count, or count spaces + 1.
3
First Letter of Each Word
Medium
Read a sentence and print the first letter of each word, concatenated (no separators).
A single line containing words separated by single spaces.
A string: the first letters concatenated.
Sample Input 1
hello world
Sample Output 1
hw
Sample Input 2
Tech Bridge 24
Sample Output 2
TB2
Sample Input 3
a b c
Sample Output 3
abc
Sample Input 4
Java Programming Practice
Sample Output 4
JPP
Sample Input 5
one
Sample Output 5
o
1
Split by space, take charAt(0) of each word.
4
Remove Vowels
Medium
Read a string of lowercase letters and print it with all vowels removed.
A single line containing a string S of lowercase letters.
S with a, e, i, o, u removed.
Sample Input 1
hello
Sample Output 1
hll
Sample Input 2
programming
Sample Output 2
prgrmmng
Sample Input 3
aeiou
Sample Output 3

                      
Sample Input 4
bcdfg
Sample Output 4
bcdfg
Sample Input 5
elephant
Sample Output 5
lphnt
1
Build a new string skipping vowels.
5
Replace Spaces with Hyphens
Medium
Read a sentence and print it with every space replaced by a hyphen (-).
A single line containing a sentence.
The sentence with spaces replaced by hyphens.
Sample Input 1
hello world
Sample Output 1
hello-world
Sample Input 2
Java is fun
Sample Output 2
Java-is-fun
Sample Input 3
a b
Sample Output 3
a-b
Sample Input 4
one
Sample Output 4
one
Sample Input 5
Tech Bridge 24
Sample Output 5
Tech-Bridge-24
1
s.replace(" ", "-") or a loop.
1
Longest Word
Hard
Read a sentence and print the longest word. If there is a tie, print the first one. Words are separated by single spaces.
A single line containing a sentence.
The longest word.
Sample Input 1
hello world
Sample Output 1
hello
Sample Input 2
Java programming is fun
Sample Output 2
programming
Sample Input 3
a bb ccc
Sample Output 3
ccc
Sample Input 4
one
Sample Output 4
one
Sample Input 5
The quick brown fox
Sample Output 5
quick
1
Split by space and track the longest so far.
2
String Compression
Hard
Read a string of lowercase letters and print its run-length compression: consecutive identical characters become the character followed by its count. For example "aaabbc" → "a3b2c1".
A single line containing a string S of lowercase letters.
The compressed string.
Sample Input 1
aaabbc
Sample Output 1
a3b2c1
Sample Input 2
abcd
Sample Output 2
a1b1c1d1
Sample Input 3
a
Sample Output 3
a1
Sample Input 4
aaaaaaaaaa
Sample Output 4
a10
Sample Input 5
aaabbbaaa
Sample Output 5
a3b3a3
1
Loop and count consecutive identical chars, append char + count.
3
Anagram Check
Hard
Read two strings (each on its own line) and print "Anagram" if one is a permutation of the other, otherwise "Not Anagram".
Two lines: string A, then string B.
"Anagram" or "Not Anagram".
Sample Input 1
listen
silent
Sample Output 1
Anagram
Sample Input 2
hello
world
Sample Output 2
Not Anagram
Sample Input 3
a
a
Sample Output 3
Anagram
Sample Input 4
abc
cba
Sample Output 4
Anagram
Sample Input 5
aaa
aa
Sample Output 5
Not Anagram
1
Sort both and compare, or count character frequencies.
4
First Non-Repeating Character
Hard
Read a string and print the first character that appears exactly once. If none exists, print "-1".
A single line containing a string S of lowercase letters.
The first non-repeating character, or "-1".
Sample Input 1
leetcode
Sample Output 1
l
Sample Input 2
loveleetcode
Sample Output 2
v
Sample Input 3
aabbcc
Sample Output 3
-1
Sample Input 4
a
Sample Output 4
a
Sample Input 5
abacaba
Sample Output 5
c
1
Count frequencies first, then scan left-to-right for the first char with count 1.
5
Toggle Case
Hard
Read a string and print it with uppercase letters made lowercase and lowercase letters made uppercase. Digits and other characters are unchanged.
A single line containing a string S.
The case-toggled string.
Sample Input 1
Hello World
Sample Output 1
hELLO wORLD
Sample Input 2
Java
Sample Output 2
jAVA
Sample Input 3
TechBridge24
Sample Output 3
tECHbRIDGE24
Sample Input 4
a
Sample Output 4
A
Sample Input 5
123
Sample Output 5
123
1
If char is uppercase add 32; if lowercase subtract 32; else keep.
Competitive MCQs — Java Strings
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score 0/ 34
Q1
Which class is immutable?
Correct!
Wrong — correct answer is .
String is immutable; StringBuilder/StringBuffer are mutable.
Q2
Which method returns the length of a String?
Correct!
Wrong — correct answer is .
Strings use length(); collections use size().
Q3
What is the output of this code?
java
1
2
String s = "Hello";
System.out.println(s.charAt(1));
Correct!
Wrong — correct answer is .
charAt is 0-based: index 1 is the second character, 'e'.
Q4
What is the output of this code?
String s = "Hello";
System.out.println(s.charAt(1));
Correct!
Wrong — correct answer is .
charAt is 0-based: index 1 is the second character, 'e'.
Q5
Which method compares two Strings ignoring case?
Correct!
Wrong — correct answer is .
equalsIgnoreCase compares content case-insensitively.
Q6
What is the output of this code?
java
1
2
3
String a = "Java";
String b = "Java";
System.out.println(a == b);
Correct!
Wrong — correct answer is .
String literals are interned, so a and b point to the same object.
Q7
What is the output of this code?
String a = "Java";
String b = "Java";
System.out.println(a == b);
Correct!
Wrong — correct answer is .
String literals are interned, so a and b point to the same object.
Q8
Which method extracts a substring from index 1 to 4 (excluding 4)?
Correct!
Wrong — correct answer is .
substring(begin, end) — end is exclusive.
Q9
What is the output of this code?
java
1
2
String s = "abc";
System.out.println(s.toUpperCase());
Correct!
Wrong — correct answer is .
toUpperCase() returns an uppercased copy.
Q10
What is the output of this code?
String s = "abc";
System.out.println(s.toUpperCase());
Correct!
Wrong — correct answer is .
toUpperCase() returns an uppercased copy.
Q11
Which class is best for repeated string concatenation in a loop?
Correct!
Wrong — correct answer is .
StringBuilder avoids creating many immutable String objects.
Q12
What is the output of this code?
java
1
2
String s = "Hello World";
System.out.println(s.split(" ").length);
Correct!
Wrong — correct answer is .
Splitting on space yields {"Hello","World"} — length 2.
Q13
What is the output of this code?
String s = "Hello World";
System.out.println(s.split(" ").length);
Correct!
Wrong — correct answer is .
Splitting on space yields {"Hello","World"} — length 2.
Q14
Which method finds the index of the first occurrence of a character?
Correct!
Wrong — correct answer is .
indexOf(char/string) returns the first index or -1.
Q15
What is the output of this code?
String s = "Hello";
System.out.println(s.length());
Correct!
Wrong — correct answer is .
length() counts the characters in "Hello": 5.
Q16
What is the output of this code?
String s = "abc";
System.out.println(s.charAt(0));
Correct!
Wrong — correct answer is .
charAt(0) returns the first character, 'a'.
Q17
Which method checks if a string contains a substring?
Correct!
Wrong — correct answer is .
contains(CharSequence) returns true when the substring is present.
Q18
What is the output of this code?
String s = "Java";
System.out.println(s.concat("!"));
Correct!
Wrong — correct answer is .
concat() appends the argument to produce a new string.
Q19
Which method removes leading and trailing whitespace?
Correct!
Wrong — correct answer is .
trim() removes whitespace; Java 11+ also has strip().
Q20
What is the output of this code?
String s = "Hello World";
System.out.println(s.indexOf("o"));
Correct!
Wrong — correct answer is .
The first 'o' appears at index 4.
Q21
Which method checks if a string is empty?
Correct!
Wrong — correct answer is .
isEmpty() returns true when length() is 0.
Q22
What is the output of this code?
String s = " hi ";
System.out.println(s.trim());
Correct!
Wrong — correct answer is .
trim() removes spaces from both ends.
Q23
Which method compares strings ignoring case?
Correct!
Wrong — correct answer is .
equalsIgnoreCase compares content without case sensitivity.
Q24
What is the output of this code?
String a = "java";
String b = "JAVA";
System.out.println(a.equalsIgnoreCase(b));
Correct!
Wrong — correct answer is .
The two strings match when case is ignored.
Q25
Which method converts a string to lowercase?
Correct!
Wrong — correct answer is .
toLowerCase() returns an all-lowercase copy.
Q26
What is the output of this code?
String s = "Java";
System.out.println(s.substring(1));
Correct!
Wrong — correct answer is .
substring(1) returns everything from index 1 onward.
Q27
Which class should you use to build strings in a loop?
Correct!
Wrong — correct answer is .
StringBuilder mutates without creating many String objects.
Q28
What is the output of this code?
StringBuilder sb = new StringBuilder("Hi");
sb.append("!");
System.out.println(sb);
Correct!
Wrong — correct answer is .
append() adds "!" to the mutable builder.
Q29
Which method returns the character at a given index?
Correct!
Wrong — correct answer is .
charAt(i) returns the char at position i (0-based).
Q30
What is the output of this code?
String s = "banana";
System.out.println(s.replace("a", "o"));
Correct!
Wrong — correct answer is .
replace swaps every "a" for "o".
Q31
What is string interning?
Correct!
Wrong — correct answer is .
The JVM interns literals so equal literals share the same object.
Q32
What is the output of this code?
String s = "abcdef";
System.out.println(s.substring(2, 4));
Correct!
Wrong — correct answer is .
substring(2, 4) includes indexes 2 and 3: "cd".
Q33
Which method reports whether a string ends with a suffix?
Correct!
Wrong — correct answer is .
endsWith(suffix) returns a boolean result.
Q34
What is the output of this code?
String s = "Hello";
System.out.println(s.toUpperCase());
Correct!
Wrong — correct answer is .
toUpperCase() returns an all-caps copy.