Java Basics (GATE - CS)
Java String and StringBuffer operations including substring, comparison, modification methods, and Character class utilities for GATE CS preparation
Questions
String s = “Welcome To Java” s.substring(4, 3 );
What will be the result of the above code?
- ome
- - 1
- com
- Runtime error
- Compile time error
Which of the following functions cannot be invoked using the object of String class?
- toLower()
- trim()
- split()
- Both (1) and (3)
- Both (2) and (3)
String s1 = “abc”; String s2 = “abg”; S1.compareTo(s2);
What will be the result of the above code?
- - 4
- 4
- - 1
- 1
- 0
Which of the following methods is/are provided by the character class?
- isDigit()
- isLetter()
- isLetterOrDigit()
- All of the above
- None of these
String s = “Welcome To Java” s.lastIndexOf(“Java”,5);
What is the result of the given statements?
- - 1
- 11
- 6
- Compiler error : method lastIndexOf() not declared
- Compiler error : parameters to method not sufficient
String s = “Welcome To Java”; String s1 = “Java”; s.match(s1);
What will be the output of the given piece of code?
- True
- False
- - 1
- Compile time error
- Runtime error
Which of the following functions is/are valid in Java for Strings?
- valueOf()
- toCharArray()
- toIntArray()
- Both (1) and (2)
- Both (1) and (3)
StringBuffer s = new StringBuffer(“Welcome To Java”); s.delete(8,12);
What will be the output of the given code segment?
- Welcome ava
- Welcome va
- Welcomeava
- Compiler error : delete cannot be used with StringBuffer
- Runtime error
String s = “Welcome To Java” s.substring(4, 4 );
What will be the answer of the given code?
- o
- c
- Empty string
- Compile time error
- Runtime error
Which of the following data types can be passed as the first parameter to the lastIndexOf() method in String class?
- Char
- String
- Int
- Both (1) and (2)
- All of the above
String s = “Welcome To Java”; s.length(); s.charAt(9);
What will be the output of the given code?
- s.length() = 14s.charAt(9) = T
- s.length() = 15s.charAt(9) = o
- s.length() = 15s.charAt(9) = T
- s.length() = 14s.charAt(9) = o
- Compiler error
String s = “Welcome To Java”; s.charAt(s.length());
What will be the result of the above code?
- a
- v
- Compiler Error : cannot pass a function as a parameter
- Compiler Error : parameters are not sufficient
- Runtime error
Which of the following methods cannot be used with the object of String class?
- replace(char, char)
- replaceAll(String, String)
- append(String)
- Both (1) and (2)
- All of these
StringBuffer s = new StringBuffer(“Hello”); s.capacity();
What will be returned by the function capacity()?
- 16
- 4
- 18
- 3
- 15
Which of the following methods is/are defined in the String class for comparing the Strings?
- equals(String s)
- compareToIgnoreCase(String s1)
- compareTo(String s3)
- Both (2) and (3)
- All of these