Java Strings

Questions on Strings in Java and Stringbuffer

15 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

Which of the following methods is/are defined in the String class for comparing the Strings?

  1. equals(String s)
  2. compareToIgnoreCase(String s1)
  3. compareTo(String s3)
  4. both 2 and 3
  5. all of these
Question 2 Multiple Choice (Single Answer)

String s = “Welcome To Java” s.substring(4 , 3 );

What will be the result of the above code?

  1. ome
  2. - 1
  3. com
  4. run time error
  5. compile time error
Question 3 Multiple Choice (Single Answer)

String s = “Welcome To Java”; String s1 = “Java”; s.match(s1);

What will be the output of the given code?

  1. true
  2. false
  3. - 1
  4. compile time error
  5. run time error
Question 4 Multiple Choice (Single Answer)

Which of the following functions cannot be invoked using the object of String class?

  1. toLower()
  2. trim()
  3. split()
  4. both 1 and 3
  5. both 2 and 3
Question 5 Multiple Choice (Single Answer)

String s1 = “abc”; String s2= “abg”; S1.compareTo(s2);

What will be result of the above code?

  1. - 4
  2. 4
  3. - 1
  4. 1
  5. 0
Question 6 Multiple Choice (Single Answer)

Which of the following methods is/are provided by the Character class?

  1. isDigit()
  2. isLetter()
  3. isLetterOrDigit()
  4. all of these
  5. none of these
Question 7 Multiple Choice (Single Answer)

String s = “Welcome To Java” s.lastIndexOf(“Java”,5);

What is the result of the given code?

  1. - 1
  2. 11
  3. 6
  4. Compiler error : method lastIndexOf() not declared
  5. Compiler error : parameters to method not sufficient
Question 8 Multiple Choice (Single Answer)

String s = “Welcome To Java”; s.charAt(s.length());

What will be the result of the above code?

  1. a
  2. v
  3. compiler error : cannot pass a function as parameter
  4. compiler error : parameters not sufficient
  5. runtime error
Question 9 Multiple Choice (Single Answer)

StringBuffer s = new StringBuffer(“Hello”); s.capacity();

What will be returned by the function capacity()?

  1. 16
  2. 4
  3. 18
  4. 3
  5. 15
Question 10 Multiple Choice (Single Answer)

Which of the following functions is/are valid in Java for Strings?

  1. valueOf()
  2. toCharArray()
  3. toIntArray()
  4. both 1 and 2
  5. both 1 and 3
Question 11 Multiple Choice (Single Answer)

Which of the following methods cannot be used with the object of String class?

  1. replace(char , char)
  2. replaceAll(String , String)
  3. append(String)
  4. both 1 and 2
  5. all of these
Question 12 Multiple Choice (Single Answer)

Which of the following data types can be passed as the first parameter to the lastIndexOf() method in the String class?

  1. Char
  2. String
  3. Int
  4. Both 1 and 2
  5. All of the above
Question 13 Multiple Choice (Single Answer)

StringBuffer s = new StringBuffer(“Welcome To Java”); s.delete(8,12);

What will be the output of the given code segment?

  1. Welcome ava
  2. Welcome va
  3. Welcomeava
  4. Compiler error : delete cannot be used with StrinBuffer
  5. Run time error
Question 14 Multiple Choice (Single Answer)

String s = “Welcome To Java”; s.length(); s.charAt(9);

What will be the output of the given code?

  1. s.length() = 14s.charAt(9) = T
  2. s.length() = 15s.charAt(9) = o
  3. s.length() = 15s.charAt(9) = T
  4. s.length() = 14s.charAt(9) = o
  5. compiler error
Question 15 Multiple Choice (Single Answer)

String s = “Welcome To Java” s.substring(4 , 4 );

What will be the outcome of the given code?

  1. o
  2. c
  3. empty string
  4. compile time error
  5. run time error