MCQ

Java MCQ – Multiple Choice Questions and Answers – Strings – Part 3

This collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Java Strings”.
 

1. What is the output of the following code?
public class Main{
	public static void main(String args[]){ 
		String str = "Welcome to StackHowTo";
		System.out.println(str.startsWith("welcome"));
	} 
}

A true

B false

C 0

D 1

B
startsWith() method is case sensitive “Welcome” and “welcome” are treated differently, so it displays “false”.

 

 

2. Which of the following classes can be used to create a mutable String?

A String()

B StringBuffer()

C Both A and B are true.

D None of the above

B
StringBuffer is mutable which means that we can change the value of the object.
 

3. Which of these methods of StringBuffer class is used to concatenate a String at the end of another String?

A concat()

B append()

C join()

D concatenate()

B
The StringBuffer class provides append() method to concatenate a string at the end of another string. Example:

StringBuffer sb = new StringBuffer("Welcome to ");
sb.append("StackHowTo!");
System.out.println(sb);

Output:

Welcome to StackHowTo!
 

5. Which of these methods of the String class is used to remove leading and trailing spaces?

A startsWith()

B empty()

C Trim()

D trim()

D
trim() method is used to remove leading and trailing spaces. Example:

String str = "    StackHowTo    "; 
System.out.println(str.trim());

Output:

StackHowTo

 

 

6. What is the output of the following code?
public class Main{
	public static void main(String args[]){ 
		String s1 = "s1";
		String s2 = s1.concat("s2");
		System.out.println(s2);
	} 
}

A s1

B s2

C s1s2

D s1s1

C
Two strings can be concatenated using the concat() method.
 

7. Which of these methods is used to extract a substring from a string?

A substring()

B Substring()

C SubString()

D None of the above

A
substring() method is used to extract a substring from a string. Example:

String str = "welcome";  
System.out.println(str.substring(0,2));

Output:

we

 

 

8. What is the output of the following code?
public class Main{
	public static void main(String args[]){ 
		String str = "StackHowTo";
		System.out.println(str.substring(3, 5)); 
	} 
}

A Sta

B ckH

C ck

D HowTo

C
substring() method is used to extract a substring from a string.

 

 

9. Which of the following statements is correct?

A replace() method replaces all occurrences of a character in a string with another character.

B replace() method replaces only the first occurrences of a character in a string with another character.

C replace() method replaces all characters in a string with another character.

D replace() method replaces the last occurrence of a character in a string with another character.

A
replace() method replaces all occurrences of a character in a string with another character.

 

10. The method compareTo() returns _______

A 1

B -1

C true

D false

E An integer value

E
The method compareTo() returns an integer value. Example:

String s1 = "bob";
String s2 = "bob";
System.out.println(s1.compareTo(s2)); // Returns 0 because they are equal

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *