0% found this document useful (0 votes)
13 views

mcqstring

mcq withs tings

Uploaded by

peloric692
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

mcqstring

mcq withs tings

Uploaded by

peloric692
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

1. String in Java is a?

a) class
b) object
c) variable
d) character array
View Answer

Answer: a
Explanation: None.

2. Which of these method of String class is used to obtain character at specified index?
a) char()
b) Charat()
c) charat()
d) charAt()
View Answer

Answer: d
Explanation: None.

3. Which of these keywords is used to refer to member of base class from a subclass?
a) upper
b) super
c) this
d) none of the mentioned
View Answer

Answer: b
Explanation: Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the
keyword super.

advertisement

4. Which of these method of String class can be used to test to strings for equality?
a) isequal()
b) isequals()
c) equal()
d) equals()
View Answer

Answer: d
Explanation: None.

5. 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
View Answer

Answer: b
Explanation: Strings in Java are immutable that is they can not be modified.

Note: Join free Sanfoundry classes at Telegram or Youtube

6. What will be the output of the following Java program?

1. class string_demo

2. {

3. public static void main(String args[])

4. {

5. String obj = "I" + "like" + "Java";

6. System.out.println(obj);

7. }

8. }

a) I
b) like
c) Java
d) IlikeJava
View Answer

Answer: d
Explanation: Java defines an operator +, it is used to concatenate strings.
output:

$ javac string_demo.java

$ java string_demo

IlikeJava

7. What will be the output of the following Java program?

1. class string_class
2. {

3. public static void main(String args[])

4. {

5. String obj = "I LIKE JAVA";

6. System.out.println(obj.charAt(3));

7. }

8. }

a) I
b) L
c) K
d) E
View Answer

Answer: a
Explanation: charAt() is a method of class String which gives the character specified by the index.
obj.charAt(3) gives 4th character i:e I.
output:

$ javac string_class.java

$ java string_class

8. What will be the output of the following Java program?

1. class string_class

2. {

3. public static void main(String args[])

4. {

5. String obj = "I LIKE JAVA";

6. System.out.println(obj.length());

7. }

8. }
a) 9
b) 10
c) 11
d) 12
View Answer

Answer: c
Explanation: None.
output:

$ javac string_class.java

$ java string_class

11

9. What will be the output of the following Java program?

1. class string_class

2. {

3. public static void main(String args[])

4. {

5. String obj = "hello";

6. String obj1 = "world";

7. String obj2 = obj;

8. obj2 = " world";

9. System.out.println(obj + " " + obj2);

10. }

11. }

a) hello hello
b) world world
c) hello world
d) world hello
View Answer
Answer: c
Explanation: None.
output:

$ javac string_class.java

$ java string_class

hello world

10. What will be the output of the following Java program?

1. class string_class

2. {

3. public static void main(String args[])

4. {

5. String obj = "hello";

6. String obj1 = "world";

7. String obj2 = "hello";

8. System.out.println(obj.equals(obj1) + " " + obj.equals(obj2));

9. }

10. }

a) false false
b) true true
c) true false
d) false true
View Answer

Answer: d
Explanation: equals() is method of class String, it is used to check equality of two String objects, if they
are equal, true is retuned else false.
output:
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
View Answer
Answer: b
Explanation: None.

2. Which of these operators can be used to concatenate two or more String objects?
a) +
b) +=
c) &
d) ||
View Answer
Answer: a
Explanation: Operator + is used to concatenate strings, Example String s = “i ” + “like ” +
“java”; String s contains “I like java”.

3. Which of this method of class String is used to obtain a length of String object?
a) get()
b) Sizeof()
c) lengthof()
d) length()
View Answer
Answer: d
Explanation: Method length() of string class is used to get the length of the object which
invoked method length().

advertisement

4. Which of these method of class String is used to extract a single character from a String
object?
a) CHARAT()
b) chatat()
c) charAt()
d) ChatAt()
View Answer
Answer: c
Explanation: None.

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
View Answer
Answer: a
Explanation: None.

Note: Join free Sanfoundry classes at Telegram or Youtube

6. Which of these is an incorrect statement?


a) String objects are immutable, they cannot be changed
b) String object can point to some other reference of String variable
c) StringBuffer class is used to store string in a buffer for later use
d) None of the mentioned
View Answer
Answer: c
Explanation: StringBuffer class is used to create strings that can be modified after they are
created.

7. What will be the output of the following Java program?

1. class String_demo
2. {
3. public static void main(String args[])
4. {
5. char chars[] = {'a', 'b', 'c'};
6. String s = new String(chars);
7. System.out.println(s);
8. }
9. }
a) a
b) b
c) c
d) abc
View Answer
Answer: d
Explanation: String(chars) is a constructor of class string, it initializes string s with the
values stored in character array chars, therefore s contains “abc”.
output:
$ javac String_demo.java
$ java String_demo
abc
8. What will be the output of the following Java program?

1. class String_demo
2. {
3. public static void main(String args[])
4. {
5. int ascii[] = { 65, 66, 67, 68};
6. String s = new String(ascii, 1, 3);
7. System.out.println(s);
8. }
9. }
a) ABC
b) BCD
c) CDA
d) ABCD
View Answer
Answer: b
Explanation: ascii is an array of integers which contains ascii codes of Characters A, B, C,
D. String(ascii, 1, 3) is an constructor which initializes s with Characters corresponding to
ascii codes stored in array ascii, starting position being given by 1 & ending position by 3,
Thus s stores BCD.
output:
$ javac String_demo.java
$ java String_demo
BCD
9. What will be the output of the following Java program?

1. class String_demo
2. {
3. public static void main(String args[])
4. {
5. char chars[] = {'a', 'b', 'c'};
6. String s = new String(chars);
7. String s1 = "abcd";
8. int len1 = s1.length();
9. int len2 = s.length();
10. System.out.println(len1 + " " + len2);
11. }
12. }
a) 3 0
b) 0 3
c) 3 4
d) 4 3
View Answer
Answer: d
Explanation: None.
output:
Q1. Consider the following program:

public class StrEqual {

public static void main(String[] args) {

String s1 = "hello";

String s2 = new String("hello");

String s3 = "hello";

if (s1 == s2) {

System.out.println("s1 and s2 equal");

} else {

System.out.println("s1 and s2 not equal");

if (s1 == s3) {

System.out.println("s1 and s3 equal");

} else {

System.out.println("s1 and s3 not equal");

Which one of the following options provides the output of this program when executed?
a)

s1 and s2 equal

s1 and s3 equal

b)

s1 and s2 equal

s1 and s3 not equal

c)
s1 and s2 not equal

s1 and s3 equal

d)

s1 and s2 not equal

s1 and s3 not equal

Click to View Answer and Explanation

Answer:

c)

s1 and s2 not equal

s1 and s3 equal

Explanation:

JVM sets a constant pool in which it stores all the string constants used in the type. If two references are
declared with a constant, then both refer to the same constant object. The == operator checks the
similarity of the objects themselves (and not the values in it). Here, the first comparison is between two
distinct objects, so we get s1 and s2 not equal. On the other hand, since references to s1 and s3 refer to
the same object, we get s1 and s3 equal.

Q2. Consider the following program:

public class Test {

public static void main(String[] args) {

String str = null;

System.out.println(str.valueOf(10));

Which of the following statements correctly describes the behavior of this program?
a) This program will result in a compiler error.
b) This program will throw a NullPointerException.
c) This program will print 10 in the console.
d) This program will print null in the console.

Click to View Answer and Explanation


Answer:

c) This program will print 10 in the console.

Explanation:

The valueOf(int) method is a static method in String that returns the String representation of the integer
value that is passed as its argument. Since calling a static method does not require dereferencing the
reference variable on which it is called, this program does not throw a NullPointerException.

Q3. Consider the following program and predict the output:

public class Test {

public static void main(String[] args) {

String s = new String("5");

System.out.println(1 + 10 + s + 1 + 10);

a) 11511
b) 1105110
c) 115110
d) 27

Click to View Answer and Explanation

Answer:

c) 115110

Explanation:

The string concatenation operator works as follows: if both the operands are numbers, it performs the
addition; otherwise, it concatenates the arguments by calling the toString() method if needed. It
evaluates from left to right. Hence, the expression in the program results in the string 115110.

Q4. Consider the following program and predict its output:

public class Test {

public static void main(String[] args) {

String str = null;


switch (str) { // #1

case "null":

System.out.println("null string"); // #2

break;

a) This program results in a compiler error in statement #1.


b) This program results in a compiler error in statement #2.
c) This program results in throwing a NullPointerException.
d) This program prints the following: null string.

Click to View Answer and Explanation

Answer:

c) This program results in throwing a NullPointerException.

Explanation:

If a null value is passed to a switch statement, it results in a NullPointerException.

Q5. What will be the output of the below statements?

public class Test {

public static void main(String[] args) {

String s1 = null;

System.out.println(s1); //line 2

System.out.println(s1.toString()); //line 3

a) null null
b) null NullPointerException
c) NullPointerException NullPointerException
d) None

Click to View Answer and Explanation

Answer:

b) null NullPointerException

Explanation:

Line 2 will print null because the println() method has a null check like below.

if (s == null) {

s = "null";

Q6. Select all the classes that extend the String class

a) StringBuffer
b) StringBuilder
c) StringWriter
d) None
Click to View Answer and Explanation

Answer:

d) None

Explanation:

The String is a final class, so you can't extend it.

Q7. What is the output of the following program?

public class Test {

public static void main(String[] args) {

String s1 = "hello";

String s2 = new String("hello");


s2 = s2.intern();

System.out.println(s1 == s2);

a) false
b) true
c) none
Click to View Answer and Explanation

Answer:

b) true

Explanation:

We know that the intern() method will return the String object reference from the string pool since we
assign it back to s2 and now both s1 and s2 are having the same reference. It means
that s1 and s2 references point to the same object.

Q8. What will be the output of the below statements?

public class Test {

public static void main(String[] args) {

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
Click to View Answer and Explanation

Answer:
a) false

Explanation:

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

Q9. What is the result of the following code?

String s1 = "Java";

String s2 = "Java";

StringBuilder sb1 = new StringBuilder();

sb1.append("Ja").append("va");

System.out.println(s1 == s2);

System.out.println(s1.equals(s2));

System.out.println(sb1.toString() == s1);

System.out.println(sb1.toString().equals(s1));

A. true is printed out exactly once.


B. true is printed out exactly twice.
C. true is printed out exactly three times.
D. true is printed out exactly four times.
E. The code does not compile.
Click to View Answer and Explanation

Answer:

C. true is printed out exactly three times.

Explanation:

String literals are used from the string pool. This means that s1 and s2 refer to the same object and are
equal. Therefore, the first two print statements print true. The third print statement prints false
because toString() uses a method to compute the value and it is not from the string pool. The final print
statement again prints true because equals() looks at the values of String objects.

Q10. What is the difference between StringBuilder and StringBuffer in Java?

a) StringBuilder is mutable, while StringBuffer is immutable.


b) StringBuilder is not a thread-safe, while StringBuffer is a thread-safe.
c) StringBuilder is synchronized, while StringBuffer is not synchronized.
d) There is no difference; they can be used interchangeably.
Click to View Answer and Explanation

Answer:

b) StringBuilder is not a thread-safe, while StringBuffer is a thread-safe.

Q11. What happens when two String objects are concatenated using the + operator in Java?

a) A new String object is created.


b) The first String object is modified.
c) The second String object is modified.
d) The original String objects remain unchanged.
Click to View Answer and Explanation

Answer:

a) A new String object is created.

Q12. What is the output of the following code snippet?

String str = "Hello";

str += " World!";

System.out.println(str.length());

a) 5
b) 11
c) 12
d) Compile-time error
Click to View Answer and Explanation

Answer:

c) 12

Explanation:

When "Hello" is concatenated with " World!", it forms a new string "Hello World!". The length of this
string is 12 characters, so the output is 12.

Q13. What is the output of the following code snippet?

public class Main {

public static void main(String[] args) {


String str = "Java";

str.concat(" Programming");

System.out.println(str);

a) Java
b) Java Programming
c) Programming
d) Compile-time error
Click to View Answer and Explanation

Answer:

a) Java

Explanation:

The concat() method returns a new string resulting from concatenating the specified string to the
original string. However, in this code, the result of concat() is not assigned back to the str variable.
Therefore, the original string "Java" remains unchanged, and the output is "Java".

Q14. Which of the following methods is used to compare two strings for equality in Java?

a) equals()
b) compareTo()
c) contains()
d) concat()
Click to View Answer and Explanation

Answer:

a) equals()

Explanation:

The equals() method is used to compare two strings for equality in Java. It returns true if the two strings
have the same characters in the same order, and false otherwise.

Q15. Which method is used to convert a string to uppercase in Java?

a) toLowerCase()
b) toUpperCase()
c) trim()
d) replace()
Click to View Answer and Explanation

Answer:

b) toUpperCase()

Explanation:

The toUpperCase() method is used to convert a string to uppercase in Java. It returns a new string with
all characters converted to uppercase letters.

Q 16. What will be the output of the following code?

public class Main{

public static void main(String []args){

String str1 = "Java";

String str2 = "Java";

String str3 = new String("Java");

System.out.println(str1.equals(str2) && str1.equals(str3));

A) true
B) false
C) Compile error
D) Runtime error
Click to View Answer and Explanation

Answer:

The correct answer is A) true.

Explanation:

In this code, str1 and str2 both refer to the same string literal "Java." Therefore, str1.equals(str2) returns
true. The equals() method compares the actual contents of the strings.

You might also like