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 sub class?
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.

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.

6. What is the output of this program?

  1.     class string_demo {
  2.         public static void main(String args[])
  3.         {
  4.             String obj = "I" + "like" + "Java";   
  5.             System.out.println(obj);     
  6.         }
  7.    }

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 is the output of this program?

  1.     class string_class {
  2.         public static void main(String args[])
  3.         {
  4.             String obj = "I LIKE JAVA";   
  5.             System.out.println(obj.charAt(3));
  6.         } 
  7.     }

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 is the output of this program?

  1.     class string_class {
  2.         public static void main(String args[])
  3.         {
  4.             String obj = "I LIKE JAVA";   
  5.             System.out.println(obj.length());
  6.         }
  7.     }

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 is the output of this program?

  1.     class string_class {
  2.         public static void main(String args[])
  3.         {
  4.             String obj = "hello";
  5.             String obj1 = "world";   
  6.             String obj2 = obj;
  7.             obj2 = " world";
  8.             System.out.println(obj + " " + obj2);
  9.         }
  10.     }

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 is the output of this program?

  1.     class string_class {
  2.         public static void main(String args[])
  3.         {
  4.             String obj = "hello";
  5.             String obj1 = "world";   
  6.             String obj2 = "hello";
  7.             System.out.println(obj.equals(obj1) + " " + obj.equals(obj2));
  8.         }
  9.     }

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.

Subscribe Newsletter & Posts
About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.

1000 Java MCQs | Quiz

Integer and Floating Data Types
Character and Boolean Data Types
Literals & Variables
Type Conversions, Promotions and Castings
Arrays
Arithmetic Operators
Bitwise Operators
Relational Operators and Boolean Logic Operators
Assignment Operators and Operator Precedence
Control Statements
Class Fundamentals & Declaring objects
Introduction to Methods
Constructors & Garbage Collection
Overloading Methods & Argument Passing
Access Control
Arrays Revisited & Keyword static
String Class
Inheritance
Method overriding
The Object Class
String Handling Basics
Character Extraction
String Comparison
Searching & Modifying a String
StringBuffer class
StringBuffer Methods
Packages
Interfaces
Java.lang Introduction
Java.lang - Integer, Long And Character Wrappers
Java.lang - Void, Process and System Class
Java.lang - Object & Math Class
Java.io Introduction
Java.io Byte Streams
Java.io Character Streams
Serialization
Serialization & Deserialization
Networking Basics
URL class
HttpResponse & URLConnection Class
networking - Server, Sockets & httpd Class
networking - httpd.java Class
networking - Datagrams
Java.util - ArrayList Class
Java.util - LinkedList, HashSet & TreeSet Class
Java.util - Maps
Java.util - Array Class
Java.util - Vectors & Stack
Java.util - Dictionary, Hashtable & Properties
Java.util - BitSet & Date class
Methods Taking Parameters
Exceptional Handling Basics
Exceptions Types
Throw, Throws & Nested Try
isAlive(), Join() & Thread Synchronization
Implementing Runnable interface for Threads
Thread class
Multithreading Basics
Networking Basics
Finally & Built in Exceptions
Creating Threads
Input & Output Basics
Reading Console Input
Writing Console Output
Reading & Writing Files
Applets Fundamentals
Core Java API Packages
Remote Method Invocation (RMI)
Text Formatting
Event Handling Basics
ActionEvent & AdjustmentEvent Class
ComponentEvent, ContainerEvent & FocusEvent Class
MouseEvent, TextEvent & WindowEvent Class
Event Listeners Interfaces
Java.lang - Rounding Functions
Java.lang - Miscellaneous Math Methods & StrictMath Class
Java.lang - ThreadGroup class & Runnable Interface
Java.lang - System Class Advance
Java.lang - Double & Float Wrappers
Java.lang - Byte & Short Wrappers
Java.lang - Character Wrapper Advance
Java.lang - Boolean Wrapper Advance
Java.lang - Runtime & ClassLoader Classes
java.lang - Class
Collection Framework Overview
Collections Interface
Collection Algorithms
Iterators
Locale & Random Classes
Observable & Timer Class
Restrictions On Generics
Wildcards
Recursion
Command Line Arguments
Inheritance - Abstract Class and Super
Try & Catch
Creating Exceptions
Generics
Generic Methods
Type Interface
Subscribe Newsletter & Posts
Follow Manish & Sanfoundry