Code Snippets-based MCQs
Loops
Table of contents
Easy Level MCQs
The questions are based on frequently used Java 8 String methods:
-
What will the following code print?
String text = "Java Programming"; System.out.println(text.substring(5));
Answer
Programming
(Explanation:substring(5)
starts from index 5, returning the substring starting from ‘P’.) -
What is the return type of the method
toUpperCase()
in the following code?String str = "hello"; String result = str.toUpperCase();
Answer
String
(Explanation:toUpperCase()
returns a new string where all characters are converted to uppercase.) -
Which method would you use to check if a string “Java” contains the substring “av”?
String str = "Java";
Answer
str.contains(“av”)
(Explanation:contains()
checks if the substring exists within the string.) -
What does the following code output?
String str = "Java Programming"; System.out.println(str.replace('a', 'o'));
Answer
Jovo Progromming
(Explanation:replace('a', 'o')
replaces all occurrences of ‘a’ with ‘o’.) -
What is the return value of
str.trim()
when the string is “ Hello “?String str = " Hello "; System.out.println(str.trim());
Answer
“Hello”
(Explanation:trim()
removes leading and trailing whitespace from the string.) -
What will the following code output?
String str = "Java"; System.out.println(str.charAt(2));
Answer
v
(Explanation:charAt(2)
returns the character at index 2, which is ‘v’.) -
Which method should you use to split a string “Java Programming” into words?
String str = "Java Programming";
Answer
str.split(“ “)
(Explanation:split(" ")
splits the string at spaces, resulting in words.) -
What is the return type of the
toLowerCase()
method?String str = "HELLO"; String result = str.toLowerCase();
Answer
String
(Explanation:toLowerCase()
returns a new string where all characters are converted to lowercase.) -
What is the output of the following code?
String str = "Java"; System.out.println(str.indexOf('a'));
Answer
1
(Explanation:indexOf('a')
returns the index of the first occurrence of ‘a’, which is at index 1.) -
What does the method
String.format()
do?String result = String.format("The number is: %d", 10); System.out.println(result);
Answer
Formats the string to include variables
(Explanation:String.format()
allows formatting strings by inserting variables like numbers into placeholders.) -
What will the following code print?
String str = " Hello World "; System.out.println(str.trim().toUpperCase());
Answer
HELLO WORLD
(Explanation:trim()
removes leading and trailing spaces, andtoUpperCase()
converts the string to uppercase.) -
What will the following code output?
String str = "Java Programming"; System.out.println(str.contains("Pro"));
Answer
true
(Explanation:contains("Pro")
checks if “Pro” is a part of the string and returns true.) -
What does the following code do?
String str = "Java"; System.out.println(str.substring(1, 3));
Answer
av
(Explanation:substring(1, 3)
extracts the substring starting from index 1 and ending at index 3, which is “av”.) -
What is the result of the following expression?
String str = "Java"; boolean result = str.startsWith("J"); System.out.println(result);
Answer
true
(Explanation:startsWith("J")
checks if the string starts with the specified character ‘J’ and returns true.) -
What is the return value of the following method call?
String str = "Java"; System.out.println(str.length());
Answer
4
(Explanation:length()
returns the number of characters in the string “Java”, which is 4.) -
What will the following code print?
String str = "Hello"; System.out.println(str.equals("hello"));
Answer
false
(Explanation:equals()
is case-sensitive, so “Hello” is not equal to “hello”.) -
What will the following code output?
String str = "Java Programming"; System.out.println(str.indexOf("Prog"));
Answer
5
(Explanation:indexOf("Prog")
returns the index where the substring “Prog” begins, which is 5.) -
What will this code print?
String str = "Java"; System.out.println(str.replace('a', 'o'));
Answer
Jovo
(Explanation:replace('a', 'o')
replaces all occurrences of ‘a’ with ‘o’, resulting in “Jovo”.) -
What is the return type of the method
toCharArray()
?String str = "Java"; char[] result = str.toCharArray();
Answer
char[]
(Explanation:toCharArray()
converts the string into an array of characters and returns it.) -
What does the method
str.equalsIgnoreCase("java")
return when the string is “JAVA”?String str = "JAVA"; System.out.println(str.equalsIgnoreCase("java"));
Answer
true
(Explanation:equalsIgnoreCase()
compares two strings ignoring case, so “JAVA” equals “java”.)
Slightly harder MCQs
Here are 20 more MCQs with code snippets that involve more than one method call, where the result of one method call is supplied to another:
-
What will the following code print?
String str = " Java Programming "; System.out.println(str.trim().toUpperCase());
Answer
JAVA PROGRAMMING
(Explanation:trim()
removes leading and trailing spaces, andtoUpperCase()
converts the string to uppercase.) -
What will the following code output?
String str = "Java Programming"; System.out.println(str.substring(5).toLowerCase());
Answer
programming
(Explanation:substring(5)
extracts the substring starting at index 5, andtoLowerCase()
converts it to lowercase.) -
What is the result of the following code?
String str = "Java Programming"; System.out.println(str.replace("Java", "Python").toLowerCase());
Answer
python programming
(Explanation:replace("Java", "Python")
replaces “Java” with “Python” andtoLowerCase()
converts the string to lowercase.) -
What will the following code print?
String str = " Hello World "; System.out.println(str.trim().substring(0, 5));
Answer
Hello
(Explanation:trim()
removes leading and trailing spaces, andsubstring(0, 5)
extracts the first five characters.) -
What is the output of the following code?
String str = "Java Programming"; System.out.println(str.substring(0, 4).toUpperCase());
Answer
JAVA
(Explanation:substring(0, 4)
extracts “Java”, andtoUpperCase()
converts it to “JAVA”.) -
What will be the output of this code?
String str = "Hello"; System.out.println(str.toLowerCase().replace('h', 'j'));
Answer
jello
(Explanation:toLowerCase()
converts “Hello” to “hello”, andreplace('h', 'j')
replaces ‘h’ with ‘j’.) -
What does the following code print?
String str = "Java"; System.out.println(str.concat(" Programming").toUpperCase());
Answer
JAVA PROGRAMMING
(Explanation:concat(" Programming")
adds “ Programming” to “Java”, andtoUpperCase()
converts the result to uppercase.) -
What will the output of the following code be?
String str = "Java"; System.out.println(str.substring(1).replace('a', 'o'));
Answer
Jovo
(Explanation:substring(1)
removes the first character ‘J’, resulting in “ava”, andreplace('a', 'o')
changes ‘a’ to ‘o’, resulting in “Jovo”.) -
What does the following code print?
String str = " Hello World "; System.out.println(str.trim().substring(0, 4).toUpperCase());
Answer
HELL
(Explanation:trim()
removes the spaces,substring(0, 4)
extracts “Hell”, andtoUpperCase()
converts it to “HELL”.) -
What is the result of the following?
String str = "Java Programming"; System.out.println(str.substring(5, 15).toLowerCase().replace("programming", "rocks"));
Answer
rocks
(Explanation:substring(5, 15)
gives “Programming”,toLowerCase()
converts it to “programming”, andreplace("programming", "rocks")
changes it to “rocks”.) -
What will the following code output?
String str = "Java"; System.out.println(str.toUpperCase().substring(1, 3));
Answer
VA
(Explanation:toUpperCase()
converts “Java” to “JAVA”, andsubstring(1, 3)
extracts “VA”.) -
What does the following code print?
String str = " Hello World "; System.out.println(str.trim().replace("World", "Java").toLowerCase());
Answer
hello java
(Explanation:trim()
removes leading/trailing spaces,replace("World", "Java")
replaces “World” with “Java”, andtoLowerCase()
converts it to lowercase.) -
What is the result of this code?
String str = "Java Programming"; System.out.println(str.replace("a", "o").substring(5));
Answer
o Progrmming
(Explanation:replace("a", "o")
changes “Java Programming” to “Jovo Progrmming”, andsubstring(5)
removes the first 5 characters.) -
What will the output be of this code?
String str = "Java Programming"; System.out.println(str.substring(5).replace('a', 'o'));
Answer
Jovo Progrogramming
(Explanation:substring(5)
extracts “Programming”, andreplace('a', 'o')
changes ‘a’ to ‘o’.) -
What does the following code print?
String str = "Hello"; System.out.println(str.concat(" World").toUpperCase());
Answer
HELLO WORLD
(Explanation:concat(" World")
appends “ World” to “Hello”, andtoUpperCase()
converts it to “HELLO WORLD”.) -
What will the following code output?
String str = "Java"; System.out.println(str.toLowerCase().replace('a', 'o').substring(1, 3));
Answer
vo
(Explanation:toLowerCase()
converts “Java” to “java”,replace('a', 'o')
changes ‘a’ to ‘o’, andsubstring(1, 3)
extracts “vo”.) -
What will the following code print?
String str = "Java Programming"; System.out.println(str.substring(5, 10).toUpperCase().concat(" is fun"));
Answer
PROGRAM is fun
(Explanation:substring(5, 10)
extracts “Progr”,toUpperCase()
converts it to “PROGR”, andconcat(" is fun")
appends “ is fun”.) -
What does this code print?
String str = " Hello World "; System.out.println(str.trim().substring(6).toLowerCase());
Answer
world
(Explanation:trim()
removes spaces,substring(6)
extracts “World”, andtoLowerCase()
converts it to “world”.) -
What will the following code print?
String str = "Hello"; System.out.println(str.substring(1).toUpperCase().replace('E', 'O'));
Answer
Ollo
(Explanation:substring(1)
extracts “ello”,toUpperCase()
converts it to “ELLO”, andreplace('E', 'O')
changes ‘E’ to ‘O’, resulting in “Ollo”.) -
What is the result of the following code?
String str = "Java Programming"; System.out.println(str.substring(5, 10).toLowerCase().replace('r', 'l'));
Answer
proglamming
(Explanation:substring(5, 10)
extracts “Prog”,toLowerCase()
converts it to “prog”, andreplace('r', 'l')
changes ‘r’ to ‘l’, resulting in “proglamming”.)
Tricky MCQs
Here are 20 tricky and slightly confusing questions based on Java String methods:
-
What does the following code print?
String str = "Java"; System.out.println(str.substring(2, 2));
Answer
Empty string
(Explanation:substring(2, 2)
extracts from index 2 to index 2, so no characters are selected, resulting in an empty string.) -
What is the result of the following code?
String str = "Java Programming"; System.out.println(str.substring(5, 20));
Answer
Error
(Explanation:substring(5, 20)
will throw an error because the second index exceeds the length of the string.) -
What is the output of the following code?
String str = "Java Programming"; System.out.println(str.replace('a', 'o').substring(4, 10));
Answer
Prorg
(Explanation:replace('a', 'o')
replaces ‘a’ with ‘o’, so “Java Programming” becomes “Jovo Progromming”.substring(4, 10)
extracts “Prorg”.) -
What will the output be?
String str = "Java"; System.out.println(str.indexOf('a', 2));
Answer
3
(Explanation:indexOf('a', 2)
starts searching from index 2, and the next ‘a’ is at index 3.) -
What is the output of this code?
String str = "Java"; System.out.println(str.charAt(5));
Answer
Error
(Explanation:charAt(5)
is out of bounds since the string “Java” has a length of 4.) -
What will the output of this code be?
String str = "Java"; System.out.println(str.substring(4));
Answer
Empty string
(Explanation:substring(4)
starts at index 4, but the string has only 4 characters, so it returns an empty string.) -
What will the following code print?
String str = "Java"; System.out.println(str.replace("av", "o"));
Answer
Joo
(Explanation:replace("av", "o")
replaces “av” with “o”, resulting in “Joo”.) -
What will the following code output?
String str = "Java"; System.out.println(str.indexOf("va", 1));
Answer
2
(Explanation:indexOf("va", 1)
starts searching from index 1, and “va” is found starting at index 2.) -
What is the output of this code?
String str = "Java"; System.out.println(str.replace("a", "o").substring(1, 3));
Answer
vo
(Explanation:replace("a", "o")
changes “Java” to “Jovo”, andsubstring(1, 3)
extracts “vo”.) -
What will be the output of this code?
String str = "Java"; System.out.println(str.charAt(str.length() - 1));
Answer
a
(Explanation:str.length() - 1
calculates the index of the last character, which is ‘a’.) -
What is the output of this code?
String str = "Java Programming"; System.out.println(str.substring(5, 5));
Answer
Empty string
(Explanation:substring(5, 5)
extracts a substring starting and ending at index 5, so the result is an empty string.) -
What will be the output of this code?
String str = "Java"; System.out.println(str.toLowerCase().concat(" Programming").substring(0, 10));
Answer
java program
(Explanation:toLowerCase()
converts “Java” to “java”,concat(" Programming")
appends “ Programming”, andsubstring(0, 10)
extracts “java progra”.) -
What does this code output?
String str = "Hello World"; System.out.println(str.indexOf('o', 5));
Answer
7
(Explanation:indexOf('o', 5)
searches for ‘o’ starting from index 5, and the next ‘o’ is at index 7.) -
What will this code print?
String str = "Java Programming"; System.out.println(str.replace(" ", ""));
Answer
JavaProgramming
(Explanation:replace(" ", "")
removes all spaces from the string.) -
What is the output of this code?
String str = "Java Programming"; System.out.println(str.replace("a", "o").substring(5, 15).toUpperCase());
Answer
OPROGRAMMING
(Explanation:replace("a", "o")
replaces “a” with “o”, thensubstring(5, 15)
extracts “Programming”, andtoUpperCase()
converts it to uppercase.) -
What is the result of the following?
String str = "Hello"; System.out.println(str.substring(1, 3).toUpperCase());
Answer
EL
(Explanation:substring(1, 3)
extracts “el”, andtoUpperCase()
converts it to “EL”.) -
What will this code print?
String str = "Java Programming"; System.out.println(str.indexOf("Java") == 0);
Answer
true
(Explanation:indexOf("Java") == 0
checks if “Java” starts at index 0, which is true.) -
What does this code print?
String str = "Java"; System.out.println(str.charAt(str.length()));
Answer
Error
(Explanation:charAt(str.length())
causes an error because indexstr.length()
is out of bounds.) -
What will the following code print?
String str = " Hello "; System.out.println(str.trim().length());
Answer
5
(Explanation:trim()
removes the spaces, leaving “Hello”, and the length is 5.) -
What does this code print?
String str = "Java Programming"; System.out.println(str.substring(5, 20));
Answer
Error
(Explanation: The second argument insubstring(5, 20)
is greater than the length of the string, so it throws an error.)