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: 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 these method of class String is used to obtain 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().

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 .

6. Which of these is an oncorrect 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 is the output of this program?

  1.     class String_demo {
  2.         public static void main(String args[])
  3.         {
  4.             char chars[] = {'a', 'b', 'c'};
  5.             String s = new String(chars);
  6.             System.out.println(s);
  7.         }
  8.    }

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

  1.     class String_demo {
  2.         public static void main(String args[])
  3.         {
  4.             int ascii[] = { 65, 66, 67, 68};
  5.             String s = new String(ascii, 1, 3);
  6.             System.out.println(s);
  7.         }
  8.    }

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

  1.     class String_demo {
  2.         public static void main(String args[])
  3.         {
  4.             char chars[] = {'a', 'b', 'c'};
  5.             String s = new String(chars);
  6.             String s1 = "abcd";
  7.             int len1 = s1.length();
  8.             int len2 = s.length();
  9.             System.out.println(len1 + " " + len2);
  10.         }
  11.    }

a) 3 0
b) 0 3
c) 3 4
d) 4 3
View Answer

Answer: d
Explanation: None.
output:
$ javac String_demo.java
$ java String_demo
4 3

10. What is the output of this program?

  1.    class A {
  2.         int i;
  3. 	int j;
  4.         A() {
  5.             i = 1;
  6.             j = 2;
  7.         }
  8.    }
  9.    class Output {
  10.         public static void main(String args[])
  11.         {
  12.              A obj1 = new A();
  13. 	     System.out.print(obj1.toString());
  14.         }
  15.    }

a) true
b) false
c) String associated with obj1
d) Compilation Error
View Answer

Answer: c
Explanation: toString() is method of class Object, since it is superclass of every class, every object has this method. toString() returns the string associated with the calling object.
output:
$ javac Output.java
$ java Output
A@1cd2e5f

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
Follow Manish & Sanfoundry