Strings MCQs
Strings MCQs
2
3 public class Main{
4 public static void main (String[] args) {
5 String str1 = "Great";
6 String str2 = " job";
7 String str = "joy";
8 if(str1.equals(str2))
9 {
10 str1.concat(str2);
11 }
12 else{
13 str = str2.trim();
14 }
15 System.out.println(str1.toUpperCase());
16 System.out.println(str);
17 }
18 }
19
20
21
22
Question 1
A GREAT
) joy
B GREAT joy
)
C GREAT job
)
D GREAT
) job
1 // Predict the output
2
3 public class Main{
4 public static void main(String args[])
5 {
6 String str1 = "Great things never come from comfort zones";
7 if (str1.contains("Comfort"))
8 {
9 System.out.println("mountain");
10 }
11 else
12 {
13 System.out.println("peak");
14 }
15 }
16 }
17
18
19
20
21
22
Question 2
A mountain
)
B peak
)
C Error
)
B false
)
C Error
)
B false
)
C Error
)
B false true
)
C true true
)
D false false
)
1 // Predict the output
2
3 public class Main{
4 public static void main(String[] args) {
5 String s1 = "Rare";
6 StringBuffer sb1 = new StringBuffer("Rare");
7 String s2 = sb1.toString();
8 System.out.println(s1.equals(s2));
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
Question 6
A true
)
B false
)
C Error
)
B WelcomZ to programming..
)
C WelcomZ
)
D Error
)
1 // Predict the output
2
3 public class output{
4 public static void main(String args[])
5 {
6 StringBuffer s1 = new StringBuffer("Microsoft");
7 s1.insert(9 , " Employee");
8 System.out.println(s1);
9 s1.delete(0, 10);
10 System.out.println(s1);
11 }
12 }
13
14
15
16
17
18
19
20
21
22
Question 9
A Microsoft Employee
) Employee
B Microsoft Employee
) Employee
C MicrosEmployee ft
) Employee
D Error
)
1 // Predict the output
2
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 StringBuilder str = new StringBuilder("Keep Going");
8 String str2 = str.toString();
9 StringBuilder s = str2.deleteCharAt(8);
10 System.out.println(s);
11 }
12 }
13
14
15
16
17
18
19
20
21
22
Question 10
A Keep Goig
)
B Keep Goin
)
C Keep Gong
)
D Error
)
Question 11
Which of these class is used to create an object whose
character sequence is mutable?
Question 11
A String()
)
B StringBuffer()
)
B StringBuffer()
)
Text Here
Question 12
A 1 13 7
)
B 1 14 8
)
C Compilation Error
)
B 1 14 8
)
C Compilation Error
)
Text Here
Question 13
A true false
)
B true true
)
C false true
)
D false false
)
Question 13
A true false
)
B true true
)
C false true
)
D false false
)
1 //Predict the output
2
3 public class Test
4 {
5 public static void main(String[] args)
6 {
7 String x = "abc";
8 String y = "abc";
9 x.concat(y);
10 System.out.print(x);
11 }
12 }
13
14
15
Text Here
Question 14
A abc
)
B abcabc
)
C No output
)
D Compilation Error
)
Question 14
A abc
)
B abcabc
)
C No output
)
D Compilation Error
)
Question 15
Select all the classes that extend String class.
A StringBuffer
)
B StringBuilder
)
C StringWriter
)
D None of the mentioned
)
Question 15
A StringBuffer
)
B StringBuilde
) r
C StringWriter
)
Text Here
Question 16
A Hello World
)
B HelloWorld
)
C Hello
)
D World
)
Question 16
A Hello World
)
B HelloWorld
)
C Hello
)
D World
)
1 //Predict the output
2
3 import java.util.*;
4 public class ILike
5 {
6 public static void main(String[] args)
7 {
8 String like;
9 Formatter convert = new Formatter();
10 convert.format("pen");
11 convert.format("~pencil");
12 like = convert.toString();
13 System.out.println(like);
14 }
15 }
16
17
Text Here
Question 17
A pen~pencil
)
B pen
)
C ~pencil
)
D Compilation Error
)
Question 17
A pen~pencil
)
B pen
)
C ~pencil
)
D Compilation Error
)
1 //Predict the output
2
3 public class MyClass
4 {
5 public static void main(String args[])
6 {
7 String s1 = new String("Batman");
8 String s2 = new String("Batman");
9 String s3 = s1;
10 System.out.println(s1.equals(s2));
11 }
12 }
13
14
15
16
17
Text Here
Question 18
A true
)
B false
)
D Compilation Error
)
Question 18
A true
)
B false
)
D Compilation Error
)
Question 19
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
Question 19
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
1 //Predict the output
2
3 public class MyClass
4 {
5 public static void main(String args[])
6 {
7 String obj = "I" + "like" + "Coding";
8 System.out.println(obj);
9 }
10 }
11
12
13
14
15
16
17
Text Here
Question 20
A I like Coding
)
B No output
)
C IlikeCoding
)
D Compilation Error
)
Question 20
A I like Coding
)
B No output
)
C IlikeCoding
)
D Compilation Error
)
1 //Predict the output
2
3 public class MyClass
4 {
5 public static void main(String args[])
6 {
7 String str = "Java Programming";
8 char arr[] = new char[6];
9 str.getChars(2,8,arr,0);
10 System.out.println(arr);
11 }
12 }
13
14
15
16
17
Text Here