Strings(Java Program ) Questions and Answers

Question 1. What will be the output of the following program?
public class Test{
public static void main(String args[]){
String s1 = "java";
String s2 = "java";
System.out.println(s1.equals(s2));
System.out.println(s1 == s2);
}
}
  1.    false true
  2.    false false
  3.    true false
  4.    true true
Explanation:-
Answer: Option D. -> true true

Question 2. The output of the following fraction of code is
public class Test{
public static void main(String args[]){
String s1 = new String("Hello");
String s2 = new String("Hellow");
System.out.println(s1 = s2);
}
}
  1.    Hello
  2.    Hellow
  3.    Compilation error
  4.    Throws an exception
  5.    None of these
Explanation:-
Answer: Option B. -> Hellow

Question 3. What will be the output of the following program code?
class LogicalCompare{
public static void main(String args[]){
String str1 = new String("OKAY");
String str2 = new String(str1);
System.out.println(str1 == str2);
}
}
  1.    true
  2.    false
  3.    0
  4.    1
  5.    Displays error message
Explanation:-
Answer: Option B. -> false

Question 4. Determine output:
public class Test{
public static void main(String args[]){
String s1 = "SITHA";
String s2 = "RAMA";
System.out.println(s1.charAt(0) > s2.charAt(0));
}
}
  1.    true
  2.    false
  3.    0
  4.    Compilation error
  5.    Throws Exception
Explanation:-
Answer: Option A. -> true
Output will be True. Since when s1.charAt(0) ascii value is greater then s2.charAt(0). So it will return True.

Question 5. What could be output of the following fragment of code?
public class Test{
public static void main(String args[]){
String x = "hellow";
int y = 9;
System.out.println(x += y);
}
}
  1.    Throws an exception as string and int are not compatible for addition
  2.    hellow9
  3.    9hellow
  4.    Compilation error
  5.    None of these
Explanation:-
Answer: Option B. -> hellow9

Question 6. ToString() method is defined in
  1.    java.lang.String
  2.    java.lang.Object
  3.    java.lang.util
  4.    None of these
Explanation:-
Answer: Option B. -> java.lang.Object

Question 7. What will be the output?
String str1 = "abcde";
System.out.println(str1.substring(1, 3));
  1.    abc
  2.    bc
  3.    bcd
  4.    abcd
  5.    None of these
Explanation:-
Answer: Option B. -> bc
substring() method has two variants and returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string or upto endIndex - 1 if second argument is given.
Syntax: Here is the syntax of this method:
public String substring(int beginIndex)
or
public String substring(int beginIndex, int endIndex)
Parameters: Here is the detail of parameters:
beginIndex -- the begin index, inclusive.
endIndex -- the end index, exclus

Question 8. What is the output of the following println statement?
String str1 = "Hellow";
System.out.println(str1.indexOf('t'));
  1.    true
  2.    false
  3.    1
  4.    -1
  5.    0
Explanation:-
Answer: Option D. -> -1

Question 9. What will be the output of the following program?
public class Test{
public static void main(String args[]){
String str1 = "one";
String str2 = "two";
System.out.println(str1.concat(str2));
}
}
  1.    one
  2.    two
  3.    onetwo
  4.    twoone
  5.    None of these
Explanation:-
Answer: Option C. -> onetwo
Concat() method appends one String to the end of another. The method returns a String with the value of the String passed into the method appended to the end of the String which is used to invoke this method.

Question 10. The String method compareTo() returns
  1.    true
  2.    false
  3.    an int value
  4.    1
  5.    -1
Explanation:-
Answer: Option C. -> an int value