MCQ based on String
MCQ based on String
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
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