Java Questions & Answers – String Handling Basics

This section of our 1000+ Java MCQs focuses on string handling in Java Programming Language.

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: String and StringBuffer are part of the java.lang package, which means they inherit from the Object class, the root of the Java class hierarchy.

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: charAt() is a predefined method of String class which returns the character at the given index.

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: The String() invokes the default constructor of String class and returns the object of NULL String.
Free 30-Day Python Certification Bootcamp is Live. Join Now!

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:

advertisement
$ 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: The String s is created by passing the array of characters, while String s1 is created using auto unboxing with 4 characters, Because the length() method returns the no. of character in the string. Hence output is 4 3.

output:

$ javac String_demo.java
$ java String_demo 
4 3

Sanfoundry Global Education & Learning Series – Java Programming Language.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.