MCQ

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

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

1. String class belongs to __________ package

A java.awt

B java.lang

C java.applet

D java.string

B
String class is located in java.lang package.

 

 

2. How many constructors in the String class?

A 1

B 2

C 13

D 11

C
String class has over 60 methods and 13 constructors. Here is the list of String constructors in Java:

  1. String()
  2. String(String original)
  3. String(byte[] bytes)
  4. String(byte bytes[], String charsetName)
  5. String(byte bytes[], Charset charset)
  6. String(byte bytes[], int offset, int length)
  7. String(byte bytes[], int offset, int length, Charset charset)
  8. String(byte bytes[], int offset, int length, String charsetName)
  9. String(char value[])
  10. String(char value[], int offset, int count)
  11. String(int[] codePoints, int offset, int count)
  12. String(StringBuffer buffer)
  13. String(StringBuilder buffer)

 

 

3. Which of the following methods of the “StringBuffer” class is used to find the length of a String?

A length()

B Length()

C Capacity()

D capacity()

A
length() method is used to find the length of a String. Example:

String str = new String("Hello" );
System.out.println(str.length());    //5

 

 

4. What is the output of the following code?
public class Main{
	public static void main(String args[]){ 
		String str = "Bob";
		System.out.println(str.indexOf('s'));
	} 
}

A true

B false

C 0

D 1

E -1

E
The indexOf() method returns the value of the index if it is present. Otherwise, it returns -1;

 

 

5. What is the output of the following code?
public class Main{
	public static void main(String args[]){ 
		StringBuffer s = new StringBuffer("Bob");
		s.deleteCharAt(0);
		System.out.println(s);
	} 
}

A Bo

B ob

C Bob

D B

B
deleteCharAt() method delete the character at the given index.

 

 

6. Which of the following statements is correct?

A reverse() method reverses all characters.

B reverseall() method reverses all characters.

C replace() method replaces the first occurrence of a character in a string with another character.

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

A
The reverse() method reverses all characters. It returns the reversed object on which it was called. Example:

StringBuilder reversedStr = new StringBuilder("Hello").reverse();
System.out.println(reversedStr);   //olleH

 

 

7. Which of the following is an incorrect form of StringBuffer constructor?

A StringBuffer()

B StringBuffer(int size)

C StringBuffer(String str)

D StringBuffer(int size , String str)

D
StringBuffer(int size , String str)

 

 

8. What is the output of the following code?
public class Main{
	public static void main(String args[]){ 
		String str = "toto".replace('t', 's');
		System.out.println(str);
	} 
}

A The first occurrence of ‘t’ is replaced by ‘s’.

B All characters ‘t’ are replaced by ‘s’.

C All characters ‘s’ are replaced by ‘t’.

D None of the above

B
The replace() method returns a string replacing all old characters with a new character.

 

 

9. What is the output of the following code?
public class Main{ 
	public static void main (String[] args){ 
		String str = "x1y2z3"; 
		String[] arr = str.split("\\d"); 
		for(String s: arr) 
			System.out.print(s); 
	} 
}

A x1y2z3

B 123

C xyz

D None of the above

C
The split method belongs to the String class, uses regular expressions to split a string. \d splits a string based on numbers.

 

 

10. What is the output of the following code?
public class Main{
      public static void main(String args[]){
            String s = null;
            if(s == null){
                  System.out.print("A");
            }
            else if(s.length() == 0){
                  System.out.print("B");
            }
            else{
                  System.out.print("C");
            }
      }
}

A A

B B

C C

D ABC

A

 

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 *