0% found this document useful (0 votes)
31 views20 pages

Strings MCQ's

The document contains multiple choice questions about Java String, StringBuffer and StringBuilder classes. It asks about output of code snippets involving these classes and methods. It also asks about memory allocation, immutable nature and thread safety related to these classes.

Uploaded by

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

Strings MCQ's

The document contains multiple choice questions about Java String, StringBuffer and StringBuilder classes. It asks about output of code snippets involving these classes and methods. It also asks about memory allocation, immutable nature and thread safety related to these classes.

Uploaded by

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

STRINGS

1. What is the output of the following snippet?


a) Compilation Error
class Strings
{ b) FACE
public static void main(String[] args){ c) Focus
String str1 = new String("FACE");
String str2 = new String("Focus"); d) Throws exception
System.out.print(str1 = str2);
}
}
STRINGS
2. What is the output of the following snippet?
a) True
class Strings
{ b) False
public static void main(String[] args){ c) Compilation error
String str1 = "FACE";
StringBuffer str2 = new StringBuffer(str1); d) ClassCast Exception at Runtime
System.out.print(str1.equals(str2);
}
}
STRINGS
3. What is the output of the following snippet?
a) Java3Quiz34
class Strings
{ b) Java12Quiz34
public static void main(String[] args){ c) Java3Quiz7
String s = "Java"+1+2+"Quiz"+""+(3+4);
System.out.println(s); d) Java12Quiz7
}
}
STRINGS
4. What is the output of the following snippet?
a) -3
class Strings
{ b) 0
public static void main(String[] args){ c) true
String s1 = "abc";
String s2 = "def"; d) false
System.out.println(s1.compareTo(s2));
}
}
STRINGS
5. What is the output of the following snippet?
a) Null
class Strings
{ b) abcabc
public static void main(String[] args){ c) abc
String x = "abc";
String y = "abc"; d) Compilation error
x.concat(y);
System.out.print(x);
}
}
STRINGS
7. What is the output of the following snippet? a) true
class Strings
{ b) str1 == str2 is : true
public static void main(String[] args){ c) str1 == str2 is : false
String str1 = “FACE";
String str2 = “FACE"; d) false
System.out.println("str1 == str2 is:"+str1==str2);
}
}
STRINGS
9. What is the output of the following snippet? a) true
class Strings
b) null
{
public static void main(String[] args){ c) false
String str1 = “FACE";
d) Compilation error
String str2 = new String(“FACE");
str2.intern();
System.out.println(str1==str2);
}
}
STRINGS
10. What is the output of the following snippet? a) true false
class Strings
b) true true
{
public static void main(String[] args){ c) false true
String str2 = "FACE";
d) false false
String str3 = new String("FACE");
String str1 = "FACE";
System.out.print(str1==str2);
System.out.print(str1==str3);
}
}
STRINGS
11. What is the output of the following snippet? a) Compile Time Error
class Strings
{ b) Runtime Exception
public static void main(String[] args){ StringIndexOutOfBoundsException
String str = "Focus Academy For Career
Enhancement private limited"; c) Prints "Str"
System.out.println(str.substring(5,3)); d) Runtime Exception
}
IndexOutOfBoundsException
}
STRINGS
12. What is the output of the following snippet? a) Prints “e”
class Strings
{ b) Runtime Exception
public static void main(String[] args){
c) Prints “E"
String str = “FACE Academy face";
d) Converts “E” to 69 and prints 69
System.out.println(str.charAt(str.toUpperCase().
length()));
}
}
STRINGS
13. What is the output of the following snippet? a) NullPointerException
class Strings
{
NullPointerException
public static void main(String[] args){ b) null NullPointerException
String str1 = null;
System.out.println(str1); c) null null
System.out.println(str1.toString());
d) Compilation error
}
}
STRINGS
14. What is the output of the following snippet? a) truetrue
class Strings
{ b) falsefalse
public static void main(String[] args){
c) truefalse
String str1 = “FACE";
String str2 = new String(“FACE"); d) falsetrue
System.out.print(str1==str2);
System.out.println(str1==str2.intern());
}
}
STRINGS
15. Where is toString method defined?

a) String class
b) Object class
c) StringBuffered class
d) None of these
STRINGS
16. Which of these methods is used to compare a specific region inside a string with another
specific region in another string?

a) regionMatch()
b) match()
c) regionMatches()
d) RegionMatches()
STRINGS
17. Which of these method of class String is used to check weather a given object starts with a
particular string literal?

a) startsWith()
b) Starts()
c) endsWith()
d) ends()
STRINGS
18. What is the string contained in str after following lines of code? a) Hell
class Strings
{ b) ello
public static void main(String[] args){
c) Hel
StringBuffer str = new StringBuffer(“Hello”);
str.deleteCharAt(0); d) llo
}
}
STRINGS
19. 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)
STRINGS
20. String constant pool allocates its memory in

a) Stack
b) Heap
c) Data Segment
d) Register
STRINGS
21. To overcome immutable nature of string class we use

a) StringBuffer
b) StringBuilder
c) Both a and b
d) None of these
STRINGS
22. Which class will you recommend among String, StringBuffer and StringBuilder classes if I want
mutable and thread safe objects?

a) String
b) StringBuffer
c) StringBuilder
d) None of these

You might also like