0% found this document useful (0 votes)
46 views

MCQ based on String

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

MCQ based on String

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

String Handling In Java

1. String in Java is a _________?


A. class B. object C. reference D. array of character
2. Which of the following class is the superclass of String and StringBuffer class?
A. java.util B. java.lang C. ArrayList D. java.string
3. Which of these operators can be used to concatenate two or more String objects?
A. + B. += C. & D. | |
4. Which of these constructors is used to create an empty String object?
A. String(void) B. String(0) C. String() D. None of the mentioned
5. String is ___________ in java.
A. interface B. mutable C. method D. immutable
6. Which of these method of class String is used to extract more than one character at a time a String
object?
A. Getchars() B. getChars() C. getchars() D. GetChars()
7. 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()
8. Which of this method of class String is used to obtain a length of String object?
A. get() B. Sizeof() C. lengthof() D. length()
10. What will s2 contain after following lines of Java code?
String s1 = "one";
String s2 = s1.concat("two")
A. one B. two C. onetwo D. twoone
11. Which of these method of class String is used to extract a single character from a String object?
A. ChatAt() B. charAt() C. chatat() D. CHARAT()
12. Which of these methods of class String is used to check whether a given object starts with a
particular string literal?
A. ends() B. Starts() C. endsWith() D. startsWith()
13. Which of the following affirmations are incorrect?
A. String is a class B. Strings in java are changeable
C. Each string is an object of class String
D. Java defines a fellow class of String, called StringBuffer, which enables string to be modified
14. What is the value returned by unction 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
15. Java String object cannot be changed after creation as it is marked __________
A. final B. Constant C. transient D. volatile
16. Which of these data type value is returned by equals() method of String class?
A. char B. int C. Boolean D. all the above
17. Which of these method of class String is used to remove leading and trailing whitespaces?
A. startsWith() B. trim() C. Trim() D. doTrim()
18. What will be the output of the following Java program?
class String_demo
{
public static void main(String args[])
{
char chars[] = {'a', 'b', 'c'};
String s = new String(chars);
System.out.println(s);
} }
A. a B. b C. c D. abc
19. Which of the following statement is correct?
A. replace() method replaces all the characters in invoking string with another character.
B. replace() method replaces all occurrences of one character in invoking string with another character.
C. replace() method replaces only first occurances of a character in invoking string with another character.
D. replace() method replaces last occurrence of a character in invoking string with another character.
20. String can be created in java using:
A. new keyword B. String literalC. both of the above D. by extending CharSequence
21. String s=new String("TIH");
How many objects are created for the above statement in java?
A. 0 B. 1 C. 2 D. 3
22. What will be the output of the following Java program?
class String_demo
{
public static void main(String args[])
{
int ascii[] = { 65, 66, 67, 68};
String s = new String(ascii, 1, 3);
System.out.println(s);
}
}
A. CDA B. ABC C. BCD D. ABCD
23. What will be the output of the following Java program?

class String_demo
{
public static void main(String args[])
{
char chars[] = {'a', 'b', 'c'};
String s = new String(chars);
String s1 = "abcd";
int len1 = s1.length();
int len2 = s.length();
System.out.println(len1 + " " + len2);
}
}
A. 3 0 B. 0 3 C. 3 4 D. 4 3
24. 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
25. 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. 0 B. 1 C. True D. False
26. String str1 = "Kolkata".replace('k', 'a');
In the above statement, the effect on string Kolkata is
A. Displays error message B. All characters a are replaced by k.
C. All characters k are replaced by a. D. The first occurrence of k is replaced by a.
27. What will be the output?
public class Test
{
public static void main (String[] args)
{
String test = "a1b2c3";
String[] tokens = test.split("\\d");
for(String s: tokens)
System.out.print(s);
}
}
A. 123 B. abc C. Compilation error D. Runtime exception thrown
28. How many Constructor String class have?
A. 2 B. 7 C. 11 D. 13
29. What will be the output of the following Java program?
class output
{
public static void main(String args[])
{
String c = " Hello World ";
String s = c.trim();
System.out.println("\""+s+"\"");
}
}
A. ”Hello World” B. ”Hello World” C. “Hello World” D. Hello world
30. 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
31. 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);
}
}
A. Hello B. Hellow C. Compilation error D. Throws an exception
32. 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));
}
}
A. 0 B. True C. False D. Compilation error
33. 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);
}
}
A. hellow9 B. 9hellow C. Compilation error D. None of these
34. What will be the output of the following Java code?

class output
{
public static void main(String args[])
{
String chars[] = {"a", "b", "c", "a", "c"};
for (int i = 0; i < chars.length; ++i)
for (int j = i + 1; j < chars.length; ++j)
if(chars[i].compareTo(chars[j]) == 0)
System.out.print(chars[j]);
}
}
A. ab B. bc C. ca D. ac
35. 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

You might also like