Search Tutorials


Java String MCQ - Multiple Choice Questions And Answers | JavaInUse

Java String MCQ - Multiple Choice Questions And Answers

Q. What is the length() method used for in Java String?

A. To find the last index of a character
B. To concatenate two strings
C. To convert a string to lowercase
D. To find the number of characters in a string

Q. Which method is used to compare two strings for equality in Java String?

A. compare()
B. equals()
C. compareTo()
D. hashCode()

Q. Which method is used to convert a string to uppercase in Java String?

A. toUppercase()
B. toUpperCase()
C. upperCase()
D. convertCase()

Q. Which method is used to find the index of a specified character in Java String?

A. indexOf()
B. findIndex()
C. searchIndex()
D. locateIndex()

Q. Which method is used to replace characters in a string with another character in Java String?

A. replace()
B. replaceChar()
C. replaceAll()
D. replaceCharacter()

Q. Which method is used to extract a substring from a string in Java String?

A. substring()
B. extractString()
C. getSubstring()
D. subString()

Q. Which method is used to remove leading and trailing whitespace from a string in Java String?

A. strip()
B. trim()
C. removeWhitespace()
D. removeSpaces()

Q. Which method is used to split a string into an array of substrings based on a specified delimiter in Java String?

A. split()
B. divide()
C. separate()
D. break()

Q. Which method is used to check if a string starts with a specified prefix in Java String?

A. startsWith()
B. hasPrefix()
C. checkPrefix()
D. beginWith()

Q. Which method is used to convert a string to lowercase in Java String?

A. toLower()
B. toLowerCase()
C. lowerCase()
D. convertToLower()

Q. Which of the following code snippets correctly initializes a String variable in Java?

A.
String name = "John";
B.
String name = new String("John");
C.
String name;
name = "John";
D.
String name;
name = new String("John");





Q. Which of the following methods can be used to find the length of a String in Java?

A.
string.length()
B.
string.length
C.
string.size()
D.
string.size

Q. Which of the following methods can be used to convert a String to uppercase in Java?

A.
string.toUpperCase()
B.
string.toUpper()
C.
string.upperCase()
D.
string.upper()

Q. Which of the following methods can be used to check if a String contains a specific substring in Java?

A.
string.contains(substring)
B.
string.includes(substring)
C.
string.hasSubstring(substring)
D.
string.has(substring)

Q. Which of the following methods can be used to concatenate two Strings in Java?

A.
string.concat(otherString)
B.
string.join(otherString)
C.
string.append(otherString)
D.
string.add(otherString)

Q. What will be the output of the following code snippet?

String name = "John";
System.out.println(name.charAt(2));
A.
J
B.
o
C.
h
D.
n

Q. Which of the following methods can be used to check if two Strings are equal in Java?

A.
string.equals(otherString)
B.
string.equalTo(otherString)
C.
string.compare(otherString)
D.
string.compareString(otherString)

Q. Which of the following methods can be used to replace a specific character in a String with another character in Java?

A.
string.replace(character, newCharacter)
B.
string.replaceAll(character, newCharacter)
C.
string.replaceChar(character, newCharacter)
D.
string.replaceString(character, newCharacter)

Q. Which of the following methods can be used to split a String into an array of substrings in Java?

A.
string.split(separator)
B.
string.splitString(separator)
C.
string.split(separator, limit)
D.
string.splitString(separator, limit)