1. What will be the output of the following Java program?
class output
{
public static void main(String args[])
{ String s = "Hello World";
int i = s.indexOf('o');
int j = s.lastIndexOf('l');
System.out.print(i + " " + j);
}
}
a) 4 8
b) 5 9
c) 4 9
d) 5 8
Explanation: indexOf() method returns the index of first occurrence of the character where as lastIndexOf() returns the index of last occurrence of the character.
output:
4 9
2. Which of these class is used to create an object whose character sequence is mutable??
a) String()
b) StringBuffer()
c) String() & StringBuffer()
d) None of the mentioned
Explanation: StringBuffer represents growable and writable character sequence.
3. Which of this method of class StringBuffer is used to concatenate the string representation to the end of invoking string?
a) concat()
b) append()
c) join()
d) concatenate()
Explanation: append()
4. Which of these method of class StringBuffer is used to find the length of current character sequence?
a) length()
b) Length()
c) capacity()
d) Capacity()
Explanation: length()
5. What is the string contained in s after following lines of Java code?
StringBuffer s new StringBuffer("Hello");
s.deleteCharAt(0);?
a) Hell
b) ello
c) Hel
d) llo
Explanation: deleteCharAt() method deletes the character at the specified index location and returns the resulting StringBuffer object.
6. Which of the following statement is correct?
a) reverse() method reverses all characters
b) reverseall() method reverses all characters
c) replace() method replaces first occurrence of a character in invoking string with another character
d) replace() method replaces last occurrence of a character in invoking string with another character
Explanation: reverse() method reverses all characters. It returns the reversed object on which it was called.
7. What will be the output of the following Java program?
class output
{
public static void main(String args[])
{
String a = "hello i love java";
System.out.println(a.indexOf('e')+" "+a.indexOf('a')+" "+a.lastIndexOf('l')+" "+a.lastIndexOf('v'));
}
}
a) 6 4 6 9
b) 5 4 5 9
c) 7 8 8 9
d) 1 14 8 15
Explanation: indexof(‘c’) and lastIndexof(‘c’) are pre defined function which are used to get the index of first and last occurrence of the character pointed by c in the given array.
Output:
1 14 8 15
8. What will be the output of the following Java program?
class output
{
public static void main(String args[])
{
StringBuffer c = new StringBuffer("Hello");
c.delete(0,2);
System.out.println(c);
}
}
a) He
b) Hel
c) lo
d) llo
Explanation: delete(0,2) is used to delete the characters from 0 th position to 1 st position.
Output:
llo
9. What will be the output of the following Java program?
class output
{
public static void main(String args[])
{
StringBuffer c = new StringBuffer("Hello");
StringBuffer c1 = new StringBuffer(" World");
c.append(c1);
System.out.println(c);
}
}
a) Hello
b) World
c) Helloworld
d) Hello World
Explanation: append() method of class StringBuffer is used to concatenate the string representation to the end of invoking string.
Output:
Hello World
10. What will be the output of the following Java program?
class output
{
public static void main(String args[])
{
StringBuffer s1 = new StringBuffer("Hello");
StringBuffer s2 = s1.reverse();
System.out.println(s2);
}
}
a) Hello
b) olleH
c) HelloolleH
d) olleHHello
Explanation:
output:
olleH