Java Basics (GATE - CS)

Java String and StringBuffer operations including substring, comparison, modification methods, and Character class utilities for GATE CS preparation

15 Questions Published

Questions

Question 1 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. Runtime error
  5. Compile time error
Question 2 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 3 Multiple Choice (Single Answer)

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

What will be the result of the above code?

  1. - 4
  2. 4
  3. - 1
  4. 1
  5. 0
Question 4 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 the above
  5. None of these
Question 5 Multiple Choice (Single Answer)

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

What is the result of the given statements?

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

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

What will be the output of the given piece of code?

  1. True
  2. False
  3. - 1
  4. Compile time error
  5. Runtime error
Question 7 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 8 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 StringBuffer
  5. Runtime error
Question 9 Multiple Choice (Single Answer)

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

What will be the answer of the given code?

  1. o
  2. c
  3. Empty string
  4. Compile time error
  5. Runtime error
Question 10 Multiple Choice (Single Answer)

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

  1. Char
  2. String
  3. Int
  4. Both (1) and (2)
  5. All of the above
Question 11 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 12 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 a parameter
  4. Compiler Error : parameters are not sufficient
  5. Runtime error
Question 13 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 14 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 15 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