Java Questions & Answers – String Comparison

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

1. Which of these method of class String is used to compare two String objects for their equality?
a) equals()
b) Equals()
c) isequal()
d) Isequal()
View Answer

Answer: a
Explanation: None.

2. Which of these methods is used to compare a specific region inside a string with another specific region in another string?
a) regionMatch()
b) match()
c) RegionMatches()
d) regionMatches()
View Answer

Answer: d
Explanation: None.

3. Which of these method of class String is used to check weather a given object starts with a particular string literal?
a) startsWith()
b) endsWith()
c) Starts()
d) ends()
View Answer

Answer: a
Explanation: Method startsWith() of string class is used to check whether the String in question starts with a specified string. It is specialized form of method regionMatches()

4. What is the value returned by unction compareTo() if the invoking string is less than the string compared?
a) zero
b) value less than zero
c) value greater than zero
d) None of the mentioned
View Answer

Answer: b
Explanation: compareTo() function returns zero when both the strings are equal, it returns a value less than zero if the invoking string is less than the other string being compared and value greater than zero when invoking string is greater than the string compared to.

5. Which of these data type value is returned by equals() method of String class?
a) char
b) int
c) boolean
d) All of the mentioned
View Answer

Answer: c
Explanation: equals() method of string class returns boolean value true if both the string are equal and false if they are unequal.

6. What is the output of this program?

  1.     class output {
  2.         public static void main(String args[])
  3.         { 
  4.            String c = "Hello i love java";
  5.            boolean var;
  6.            var = c.startsWith("hello");
  7.            System.out.println(var);
  8.         }
  9.     }

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

Answer: b
Explanation: startsWith() method is case sensitive “hello” and “Hello” are treated differently, hence false is stored in var.
Output:
$ javac output.java
$ java output
false

7. What is the output of this program?

  1.     class output {
  2.         public static void main(String args[])
  3.         { 
  4.            String s1 = "Hello i love java";
  5.            String s2 = new String(s1);
  6.            System.out.println((s1 == s2) + " " + s1.equals(s2));
  7.         }
  8.     }

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

Answer: d
Explanation: The == operator compares two object references to see whether they refer to the same instance, where as equals() compares the content of the two objects.
Output:
$ javac output.java
$ java output
false true

8. What is the output of this program?

  1.     class output {
  2.         public static void main(String args[])
  3.         { 
  4.            String s1 = "Hello";
  5.            String s2 = new String(s1);
  6.            String s3 = "HELLO";
  7.            System.out.println(s1.equals(s2) + " " + s2.equals(s3));
  8.         }
  9.     }

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

Answer: c
Explanation: None.
Output:
$ javac output.java
$ java output
true false

9. What is the output of this program?

  1.     class output {
  2.         public static void main(String args[])
  3.         {
  4.             char ch;
  5.             ch = "hello".charAt(1);
  6.             System.out.println(ch);
  7.         }
  8.    }

a) h
b) e
c) l
d) o
View Answer

Answer: b
Explanation: “hello” is a String literal, method charAt() returns the character specified at the index position. Character at index position 1 is e of hello, hence ch contains e.
output:
$ javac output.java
$ java output
e

10. What is the output of this program?

  1.     class output {
  2.         public static void main(String args[])
  3.         {
  4.              String chars[] = {"a", "b", "c", "a", "c"};
  5.              for (int i = 0; i < chars.length; ++i)
  6.                  for (int j = i + 1; j < chars.length; ++j)
  7.                      if(chars[i].compareTo(chars[j]) == 0)
  8.                          System.out.print(chars[j]); 
  9.         }
  10.    }

a) ab
b) bc
c) ca
d) ac
View Answer

Answer: d
Explanation: compareTo() function returns zero when both the strings are equal, it returns a value less than zero if the invoking string is less than the other string being compared and value greater than zero when invoking string is greater than the string compared to.
output:
$ javac output.java
$ java output
ac

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