Sail E0 Webinar

MCQs

Total Questions : 29 | Page 2 of 3 pages
Question 11.


What is the output of this program?


class output {
public static void main(String args[])
{
String s1 = "Hello";
String s2 = s1.replace('l','w');
System.out.println(s2);
}
}
  1.    hello
  2.    helwo
  3.    hewlo
  4.    hewwo
 Discuss Question
Answer: Option D. -> hewwo

replace() method replaces all occurrences of one character in invoking string with another character. s1.replace('l','w') replaces every occurrence of 'l' in hello by 'w', giving hewwo.
Output:
$ javac output.java
$ java output
hewwo


Question 12.

Which of these constructors is used to create an empty String object?


  1.    String()
  2.    String(void)
  3.    String(0)
  4.    None of the mentioned
 Discuss Question
Answer: Option A. -> String()

None .


Question 13.


What is the output of this program?


class output {
public static void main(String args[])
{
String s1 = "one";
String s2 = s1 + " two";
System.out.println(s2);
}
}
  1.    one
  2.    two
  3.    one two
  4.    compilation error
 Discuss Question
Answer: Option C. -> one two

None.
Output:
$ javac output.java
$ java output
one two



Question 14.


What is the output of this program?


class output {
public static void main(String args[])
{
String c = "Hello i love java";
boolean var;
var = c.startsWith("hello");
System.out.println(var);
}
}
  1.    true
  2.    false
  3.    0
  4.    1
 Discuss Question
Answer: Option B. -> false

startsWith() method is case sensitive "hello" and "Hello" are treated differently, hence false 

is stored in var.
Output:
$ javac output.java
$ java output
false


Question 15.

Which of these is an oncorrect statement?


  1.    String objects are immutable, they cannot be changed.
  2.    String object can point to some other reference of String variable.
  3.    StringBuffer class is used to store string in a buffer for later use.
  4.    None of the mentioned
 Discuss Question
Answer: Option C. -> StringBuffer class is used to store string in a buffer for later use.

StringBuffer class is used to create strings that can be modified after they are created.


Question 16.

Which of these data type value is returned by equals() method of String class?


  1.    char
  2.    int
  3.    boolean
  4.    All of the mentioned
 Discuss Question
Answer: Option C. -> boolean

equals() method of string class returns boolean value true if both the string are equal and false 

if they are unequal.


Question 17.

Which of the following statement is correct?


  1.    replace() method replaces all occurrences of one character in invoking string with another character.
  2.    replace() method replaces only first occurances of a character in invoking string with another character.
  3.    replace() method replaces all the characters in invoking string with another character.
  4.    replace() replace() method replaces last occurrence of a character in invoking string with another character.
 Discuss Question
Answer: Option A. -> replace() method replaces all occurrences of one character in invoking string with another character.

replace() method replaces all occurrences of one character in invoking string with another character.


Question 18.

Which of these method of class String is used to extract a single character from a String object?


  1.    CHARAT()
  2.    chatat()
  3.    charAt()
  4.    ChatAt()
 Discuss Question
Answer: Option C. -> charAt()

None


Question 19.

What is the value returned by function compareTo() if the invoking string is less than the 

string compared?


  1.    zero
  2.    value less than zero
  3.    value greater than zero
  4.    None of the mentioned
 Discuss Question
Answer: Option B. -> value less than zero

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.


Question 20.

What is the value returned by function compareTo() if the invoking string is less than the 

string compared?


  1.    zero
  2.    value less than zero
  3.    value greater than zero
  4.    None of the mentioned
 Discuss Question
Answer: Option B. -> value less than zero

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.


Latest Videos

Latest Test Papers