Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views6 pages

Strings MCQ

Uploaded by

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

Strings MCQ

Uploaded by

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

1) Which of these class is superclass of String and StringBuffer class?

a) java.util
b) java.lang
c) ArrayList
d) None of the mentioned

2)Which of these operators can be used to concatenate two or more String objects?
a) +
b) +=
c) &
d) ||

3) Which of these method of class String is used to obtain length of String object?
a) get()
b) Sizeof()
c) lengthof()
d) length()

4) Which of these method of class String is used to extract a single character from a String
object?
a) CHARAT()
b) charat()
c) charAt()
d) ChatAt()

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


a) String()
b) String(void)
c) String(0)
d) None of the mentioned

6) 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()

7) Which of these method of class String is used to check weather a given object starts with
a particular string literal?
a) startsWith()
b) endsWith()
c) Starts()
d) ends()
8) 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

9) Which of these data type value is returned by equals() method of String class?
a) char
b) int
c) boolean
d) All of the mentioned

10)Which of these method of class String is used to remove leading and trailing
whitespaces?
a) startsWith()
b) trim()
c) Trim()
d) doTrim()

11)Which of these class is used to create an object whose character sequence is mutable?
a) String()
b) StringBuffer()
c) Both of the mentioned
d) None of the mentioned

12) Which of these method of class StringBuffer is used to concatenate the string
representation to the end of invoking string?
a) concat()
b) append()
c) join()
d) concatenate()

13) Which of the following statements are incorrect?


a) String is a class
b) Strings in java are mutable
c) Every string is an object of class String
d) Java defines a peer class of String, called StringBuffer, which allows string to be altered

14) String in java implements ____________ interface(s).


a) Serializable
b) CharSequence
c) Comparable
d) All of the above
15) Java String object cannot be changed after creation as it is marked __________
a) Constant
b) transient
c) final
d) volatile

16) String can be created in java using:


a) String literal
b) new keyword
c) both of the above
d) by extending CharSequence

17)String s=new String("TIH");


How many objects are created for the above statement in java?
a) 0
b) 1
c) 2
d) 3

18) What will be the output of below statements?


String s1 = "abc";
String s2 = "def";
System.out.println(s1.compareTo(s2));
a) 0
b) true
c) -3
d) false

19) What will be the output of below program?


public class Test {
public static void main(String[] args) {
String x = "abc";
String y = "abc";
x.concat(y);
System.out.print(x);
}
}
A. abc
B. abcabc
C. null
20) What will be the output of below program?
public class Test {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";
System.out.println("s1 == s2 is:" + s1 == s2);
}
}
a) false
b) s1 == s2 is:true
c) s1 == s2 is:false
d) true

Explanation: The given statements output will be “false” because in java + operator
precedence is more than == operator. So the given expression will be evaluated to “s1 == s2
is:abc” == “abc” i.e false.

21) What will be the output of below statements?


String s = "Java"+1+2+"Quiz"+""+(3+4);
System.out.println(s);
a) Java3Quiz7
b) Java12Quiz7
c) Java12Quiz34
d) Java3Quiz34

22) How many String objects created in below statements?


String s = "abc"; // statement 1
String s1 = new String("abcd"); // statement 2
a) 1
b) 2
c) 3
d) 4

23) What will be the output of below statements?


String s1 = "abc";
String s2 = new String("abc");
System.out.print(s1==s2);
System.out.println(s1==s2.intern());
a) falsetrue
b) falsefalse
c) truetrue
d) truefalse
24) Select all the reasons that make String perfect candidate for Map key?
a) String is immutable
b) String is final
c) String properly implements hashCode() and equals() method
d) All of the above

25) What will be the output of below code snippet?


String s1 = new String("java");
String s2 = new String("JAVA");
System.out.println(s1 = s2);
a) JAVA
b) java
c) true
d) false

26) What will be the output of below statements?


String s1 = "abc";
StringBuffer s2 = new StringBuffer(s1);
System.out.println(s1.equals(s2));
a) false
b) true
c) ClassCastException at runtime
d) Compile-time error

Explanation: It will print false because s2 is not of type String. If you will look at the String
equals() method implementation, you will find a check using instanceof operator to check if
the type of passed object is String? If not, then return false.

27) What will be the output of below statements?


String s1 = null;
System.out.print(s1); // line 2
System.out.print(s1.toString()); // line 3
a) nullnull
b) null followed by NullPointerException
c) NullPointerException

28) 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

29) Which of the following are incorrect form of StringBuffer class constructor?
a) StringBuffer()
b) StringBuffer(int size)
c) StringBuffer(String str)
d) StringBuffer(int size , String str)

30) 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

You might also like