Java Questions and Answers Part-17

1. What will be the output of the following Java code?
class output
{
public static void main(String args[])
{
String a = "hello i love java";
System.out.println(a.indexOf('i')+" "+a.indexOf('o') +" "+a.lastIndexOf('i')+" "+a.lastIndexOf('o'));
}
}
a) 6 4 6 9
b) 5 4 5 9
c) 7 8 8 9
d) 4 3 6 9

Answer: a
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:
6 4 6 9

2. What will be the output of the following Java code?
class output
{
public static void main(String args[])
{
char ch;
ch = "hello".charAt(1);
System.out.println(ch);
}
}
a) h
b) e
c) i
d) o

Answer: b
Explanation: “hello” is a String literal, method charAt() returns the character specified at the index position. Character at index position 1 is e of hello, hence ch contains e.
output:
e

3. Which of these method of class String is used to compare two String objects for their equality?
a) equals()
b) Equals()
c) isequal()
d) Isequal()

Answer: a
Explanation: equals()

4. Which of these methods is used to compare a specific region inside a string with another specific region in another string?
a) regionMatch()
b) match()
c) RegionMatches()
d) regionMatches()

Answer: d
Explanation: regionMatches()

5. Which of these methods of class String is used to check whether a given object starts with a particular string literal?
a) startsWith()
b) endsWith()
c) Starts()
d) ends()

Answer: a
Explanation: Method startsWith() of string class is used to check whether the String in question starts with a specified string. It is a specialized form of method regionMatches().

6. What is the value returned by function compareTo() if the invoking string is less than the string compared?
a) zero
b) value less than zero
c) value greater than zero
d) none of the mentioned

Answer: b
Explanation: compareTo() function returns zero when both the strings are equal, it returns a value less than zero if the invoking string is less than the other string being compared and value greater than zero when invoking string is greater than the string compared to.

7. Which of these data type value is returned by equals() method of String class?
a) char
b) int
c) boolean
d) all of the mentioned

Answer: c
Explanation: equals() method of string class returns boolean value true if both the string are equal and false if they are unequal.

8. What will be the output of the following Java code?
class output
{
public static void main(String args[])
{
String c = "Hello i love java";
boolean var;
var = c.startsWith("hello");
System.out.println(var);
}
}
a) True
b) false
c) 0
d) 1

Answer: b
Explanation: startsWith() method is case sensitive “hello” and “Hello” are treated differently, hence false is stored in var.
Output:
false

9. What will be the output of the following Java code?
class output
{
public static void main(String args[])
{
String s1 = "Hello i love java";
String s2 = new String(s1);
System.out.println((s1 == s2) + " " + s1.equals(s2));
}
}
a) true true
b) false fals
c) true false
d) false true

Answer: d
Explanation: The == operator compares two object references to see whether they refer to the same instance, where as equals() compares the content of the two objects.
Output:
false true

10. What will be the output of the following Java code?
class output
{
public static void main(String args[])
{
String s1 = "Hello";
String s2 = new String(s1);
String s3 = "HELLO";
System.out.println(s1.equals(s2) + " " + s2.equals(s3));
}
}
a) true true
b) false false
c) true false
d) false true

Answer: c
Explanation:
Output:
true false