Java Questions & Answers – String Class

This section of our 1000+ Java MCQs focuses on String class of Java Programming Language.

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
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:

advertisement
$ javac string_demo.java
$ java string_demo
IlikeJava

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

advertisement
  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
I

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:

$ javac string_class.java
$ java string_class
false true

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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.