Java Multiple Choice Questions & Answers on Searching & Modifying a String for Freshers

1. Which of these method of class String is used to extract a substring from a String object?

a) substring()
b) Substring()
c) SubString()
d) None of the mentioned
Answer: a
2. What will s2 contain after following lines of code?
String s1 = “one”;
String s2 = s1.concat(“two”)

a) one
b) two
c) onetwo
d) twoone
Answer: c

Explanation: Two strings can be concatenated by using concat() method.
3. Which of these method of class String is used to remove leading and trailing whitespaces?

a) startsWith()
b) trim()
c) Trim()
d) doTrim()
Answer: b
4. What is the value returned by function compareTo() if the invoking string is greater than the string compared?

a) zero
b) value less than zero
c) value greater than zero
d) None of the mentioned
Answer: c

Explanation: if (s1 == s2) then 0, if(s1 > s2) > 0, if (s1 < s2) then < 0.
5. Which of the following statement is correct?

a) replace() method replaces all occurrences of one character in invoking string with another character.
b) replace() method replaces only first occurances of a character in invoking string with another character.
c) replace() method replaces all the characters in invoking string with another character.
d) replace() replace() method replaces last occurrence of a character in invoking string with another character.
Answer: a

Explanation: replace() method replaces all occurrences of one character in invoking string with another character.
6. What is the output of this program?
  1.     class output {
  2.         public static void main(String args[])
  3.         { 
  4.            String c = "  Hello World  ";
  5.            String s = c.trim();
  6.            System.out.println("\""+s+"\"");
  7.         }
  8.     }
a) “”Hello World””
b) “”Hello World”
c) “Hello World”
d) Hello world
Answer: c

Explanation: trim() method is used to remove leading and trailing whitespaces in a string.
7. What is the output of this program?
  1.     class output {
  2.         public static void main(String args[])
  3.         { 
  4.            String s1 = "one";
  5.            String s2 = s1 + " two";
  6.            System.out.println(s2);
  7.         }
  8.     }
a) one
b) two
c) one two
d) compilation error
Answer: c
8. What is the output of this program?
  1.     class output {
  2.         public static void main(String args[])
  3.         { 
  4.            String s1 = "Hello";
  5.            String s2 = s1.replace('l','w');
  6.            System.out.println(s2);
  7.         }
  8.     }
a) hello
b) helwo
c) hewlo
d) hewwo
Answer: d

Explanation: 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.
9. What is the output of this program?
  1.     class output {
  2.         public static void main(String args[])
  3.         {
  4.            String s1 = "Hello World";
  5.            String s2 = s1.substring(0 , 4);
  6.            System.out.println(s2);
  7.         }
  8.    }
a) Hell
b) Hello
c) Worl
d) World
Answer: a

Explanation: substring(0,4) returns the character from 0 th position to 3 rd position.
10. What is the output of this program?
  1.     class output {
  2.         public static void main(String args[])
  3.         {             String s = "Hello World";
  4.              int i = s.indexOf('o');
  5.              int j = s.lastIndexOf('l');
  6.              System.out.print(i + " " + j);
  7.  
  8.         }
  9.    }
a) 4 8
b) 5 9
c) 4 9
d) 5 8
Answer: c

Explanation: indexOf() method returns the index of first occurrence of the character where as lastIndexOf() returns the index of last occurrence of the character.

Related

Multiple Choice Questions 7589494571666446869

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item