[go: up one dir, main page]

0% found this document useful (0 votes)
3 views50 pages

Strings MCQs

The document contains a series of Java code snippets along with multiple-choice questions predicting their outputs. Each question tests knowledge of string manipulation, object comparison, and string classes in Java. The questions cover various aspects of string handling, such as immutability, equality checks, and methods of string classes.

Uploaded by

ishow2002speed
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)
3 views50 pages

Strings MCQs

The document contains a series of Java code snippets along with multiple-choice questions predicting their outputs. Each question tests knowledge of string manipulation, object comparison, and string classes in Java. The questions cover various aspects of string handling, such as immutability, equality checks, and methods of string classes.

Uploaded by

ishow2002speed
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/ 50

1 // Predict the output

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
)

D None of the mentioned


)
1 // Predict the output
2
3 public class Main{
4 public static void main(String[] args)
5 {
6 StringBuffer sb1 = new StringBuffer("Spider man");
7 StringBuffer sb2 = new StringBuffer("Spider man");
8 System.out.println(sb1.equals(sb2));
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
Question 3
A true
)

B false
)

C Error
)

D None of the mentioned


)
1 // Predict the output
2
3 public class Main{
4 public static void main(String[] args)
5 {
6 String s1 = "Force";
7 String s2 = "Force";
8 System.out.println(s1.equals(s2));
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
Question 4
A true
)

B false
)

C Error
)

D None of the mentioned


)
1 // Predict the output
2
3 public class Main{
4 public static void main(String args[])
5 {
6 String str = "Fire";
7 StringBuilder sb = new StringBuilder("Fire");
8 System.out.print(str.equals(sb) + " " + sb.equals(str));
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
Question 5
A true false
)

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
)

D None of the mentioned


)
1 // Predict the output
2
3 public class Main{
4 public static void main(String args[])
5 {
6 String s1 = "I want to become a full stack developer";
7 System.out.println(s1.replace('a','e'));
8 System.out.println(s1.charAt(7));
9 System.out.println(s1.startsWith("i"));
10 }
11 }
12
13
14
15
16
17
18
19
20
21
22
Question 7
A I went to become a full stack developer
) t
false
B I went to become a full stack developer
) t
true
C I went to become e full steck developer
) t
true
D I went to become e full steck developer
) t
false
1 // Predict the output
2
3 public class Main{
4 public static void main (String[] args)
5 {
6 StringBuilder sb = new StringBuilder("Welcome");
7 sb.append(" to programming..");
8 sb.setCharAt(6, 'Z');
9 System.out.print(sb);
10 }
11 }
12
13
14
15
16
17
18
19
20
21
22
Question 8
A Welcome to programming..
)

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

C String() & StringBuffer()


)

D None of the mentioned


)
Question 11
A String()
)

B StringBuffer()
)

C String() & StringBuffer()


)

D None of the mentioned


)
1 //Predict the output
2
3 class output
4 {
5 public static void main(String args[])
6 {
7 String a = "hello i love java";
8 System.out.println(a.indexOf('e')+" "+a.indexOf('a')+“
9 "+a.lastIndexOf('l'));
10 }
11 }
12
13
14
15

Text Here
Question 12
A 1 13 7
)

B 1 14 8
)

C Compilation Error
)

D None of the mentioned


)
Question 12
A 1 13 7
)

B 1 14 8
)

C Compilation Error
)

D None of the mentioned


)
1 //Predict the output
2
3 class output
4 {
5 public static void main(String args[])
6 {
7 String s1 = "Cat";
8 String s2 = "Cat";
9 String s3 = new String("Cat");
10 System.out.println(s1==s2);
11 System.out.println(s1==s3);
12 }
13 }
14
15

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
)

D None of the mentioned


)
1 //Predict the output
2
3 class Test
4 {
5 public static void main(String args[])
6 {
7 StringBuffer c = new StringBuffer("Hello");
8 StringBuffer c1 = new StringBuffer(" World");
9 c.append(c1);
10 System.out.println(c);
11 }
12 }
13
14
15

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
)

C Run time error


)

D Compilation Error
)
Question 18
A true
)

B false
)

C Run time error


)

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

You might also like