25 MCQ Questions on Java Strings for ICSE 10th Computer Applications Subject

25 MCQ Questions on Java Strings for ICSE 10th Computer Applications Subject


1. What is the correct way to declare a String variable in Java?

   a. String s = "Hello";

   b.  s = new String("Hello"); 

   c.  String s; s = "Hello"; 

   d. All of the above


2.  Which method is used to find the length of a String in Java? 

   a.  length() 

   b.  size() 

   c.  len() 

   d.  count() 


3.  In Java, Strings are immutable. What does this mean? 

   a. Strings cannot be created.

   b. Strings cannot be modified once created.

   c. Strings cannot be used in loops.

   d. Strings cannot be printed.


4.  What is the output of the following code snippet? 

   

   String str = "Java";

   System.out.println(str.charAt(2));

      

   a. `J`

   b. `v`

   c. `a`

   d. `v` (index out of bounds error)


5.  Which method is used to concatenate two strings in Java? 

   a.  concat() 

   b.  append() 

   c.  add() 

   d.  merge() 


6.  What does the  trim()  method do in Java? 

   a. Removes leading and trailing spaces from a string.

   b. Converts the string to uppercase.

   c. Reverses the characters in the string.

   d. Returns the length of the string.


7.  What is the result of the following expression? 


   "Hello".substring(1, 4);

      

   a.  Hell 

   b.  ell 

   c.  llo 

   d.  ello 


8.  Which of the following methods is used to compare two strings for equality in Java? 

   a.  compare() 

   b.  equals() 

   c.  isEqual() 

   d.  compareTo() 


9.  What is the correct way to convert a string to lowercase in Java? 

   a.  toLowerCase() 

   b.  toLower() 

   c.  lowerCase() 

   d.  caseLower() 


10.  Which method is used to replace a character in a string in Java? 

    a.  replace() 

    b.  replaceChar() 

    c.  change() 

    d.  substitute() 


11.  What is the output of the following code snippet? 


    String str1 = "Hello";

    String str2 = new String("Hello");

    System.out.println(str1 == str2);

       

    a.  true 

    b.  false 

    c. Compiler Error

    d. Runtime Error


12.  How do you check if a string starts with a specific prefix in Java? 

    a.  startsWith() 

    b.  startsWithString() 

    c.  beginWith() 

    d.  startWith() 


13.  Which method is used to convert a numeric value to a string in Java? 

    a.  toString() 

    b.  convertToString() 

    c.  stringify() 

    d.  valueOf() 


14.  What is the purpose of the `indexOf()` method in Java? 

    a. Returns the first index of a specified character or substring.

    b. Returns the last index of a specified character or substring.

    c. Returns the length of the string.

    d. Converts the string to uppercase.


15.  Which of the following methods is used to extract a portion of a string in Java? 

    a.  slice() 

    b.  substring() 

    c.  cut() 

    d.  clip() 


16.  What is the output of the following code snippet? 


    String str = "Hello World";

    System.out.println(str.contains("World"));

       

    a.  true 

    b.  false 

    c. Compiler Error

    d. Runtime Error


17.  How is a string object created in Java without using the  new  keyword? 

    a.  String s = "Hello"; 

    b.  String s = new String("Hello"); 

    c.  String s; s = "Hello"; 

    d.  String s = createString("Hello"); 


18.  What does the  toUpperCase()  method do in Java? 

    a. Converts the string to lowercase.

    b. Reverses the characters in the string.

    c. Converts the string to uppercase.

    d. Returns the length of the string.


19.  Which method is used to compare two strings lexicographically in Java? 

    a.  compareTo() 

    b.  compare() 

    c.  equals() 

    d.  isEqual() 


20.  What is the output of the following code snippet? 


    String str = "Java Programming";

    System.out.println(str.indexOf("a"));

       

    a.  1 

    b.  5 

    c.  11 

    d.  -1 


21.  What is the purpose of the `endsWith()` method in Java? 

    a. Checks if a string ends with a specific character.

    b. Checks if a string ends with a specific substring.

    c. Checks if a string starts with a specific character.

    d. Checks if a string starts with a specific substring.


22.  Which of the following is the correct way to convert an integer to a string in Java? 

    a.  String.valueOf(42) 

    b.  String.toString(42) 

    c.  (String) 42 

    d.  42.toString() 


23.  What does the `charAt()` method return if the index is out of bounds? 

    a.  null 

    b.  -1 

    c.  0 

    d.  IndexOutOfBoundsException 


24.  What is the output of the following code snippet? 


    String str = "Hello World";

    System.out.println(str.replace('l', 'x'));

       

    a.  Hexxo Worxd 

    b.  Hxxlo Worxd 

    c.  Hello World 

    d.  Hexxo Worlx 


25.  Which method is used to convert a string to a character array in Java? 

    a.  toCharArray() 

    b.  charArray() 

    c.  toArray() 

    d.  stringArray() 


Answers:

1. a, 2. a, 3. b, 4. b, 5. a, 6. a, 7. b, 8. b, 9. a, 10. a,

11. b, 12. a, 13. a, 14. a, 15. b, 16. a, 17. a, 18. c, 19. a, 20. c,

21. b, 


Follow our blog for more updates and share this post in your contacts

Comments