Java Interview Questions : Series 7
121. What is the meaning of
Immutable in the context of String
class in Java?
An Immutable object cannot be modified or changed in Java. String
is an Immutable class in Java.
Once a String object is created, it cannot be changed. When we
assign the String to a new value, a new object is created.
122. Why a String object is considered
immutable in java?
Java language uses String for a variety of purposes. For this it has
marked String Immutable.
There is a concept of String literal in Java.
Let say there are 2 String variables A and B that reference to a
String object “TestData”. All these variables refer to same String
literal. If one reference variable A changes the value of the String
literal from “TestData” to “RealData”, then it will affect the other
variable as well. Due to which String is considered Immutable. In
this case, if one variable A changes the value to “RealData”, then a
new String literal with “RealData” is created and A will point to
new String literal. While B will keep pointing to “TestData”
123. How many objects does following
code create?
Code:
String s1="HelloWorld";
String s2=" HelloWorld ";
String s3=" HelloWorld ";
The above code creates only one object. Since there is only one
String Literal “HelloWorld” created, all the references point to
same object.
124. How many ways are there in
Java to create a String object?
Java provides two ways to create a String object. One is by using
String Literal, the other is by using new operator.
125. How many objects does
following code create?
Code:
String s = new String("HelloWorld");
The above code creates two objects. One object is created in String
constant pool and the other is created on the heap in non-pool area.
126. What is String interning?
String interning refers to the concept of using only one copy of a
distinct String value that is Immutable.
It provides the advantage of making String processing efficient in
Time as well as Space complexity. But it introduces extra time in
creation of String.
127. Why Java uses String literal
concept?
Java uses String literal concept to make Java more efficient in
memory. If same String already exists in String constant pool, it can
be reused. This saves memory usage.
128. What is the basic difference
between a String and StringBuffer
object?
String is an immutable object. Its value cannot change after creation.
StringBuffer is a mutable object. We can keep appending or
modifying the contents of a StringBuffer in Java.
129. How will you create an immutable
class in Java?
In Java, we can declare a class final to make it immutable. There
are following detailed steps to make it Immutable:
1. Add final modifier to class to prevent it from getting
extended
2. Add private modifier to all the fields to prevent direct
access
3. Do not provide any setter methods for member variables
4. Add final modifier to all the mutable fields to assign value
only once
5. Use Deep Copy to initialize all the fields by a constructor
6. In clone method, return a copy of object instead of the
actual object reference
130. What is the use of toString()
method in java ?
In Java, Object class has toString() method. This method can be
used to return the String representation of an Object. When we print
an object, Java implicitly calls toString() method.
Java provides a default implementation for toString() method. But
we can override this method to return the format that we want to
print.
131. Arrange the three classes String,
StringBuffer and StringBuilder in the
order of efficiency for String
processing operations?
StringBuilder is the most efficient class. It does not have the
overhead of Synchronization. StringBuffer is a Synchronized class.
It has better performance than String but it is slower than
StringBuilder. String is the slowest for any String processing
operations, since it is leads to creation of new String literal with
each modification.
So the decreasing order of efficiency is: StringBuilder, StringBuffer,
String
132. What is Exception Handling in
Java?
Java provides Exception Handling mechanism to handle Runtime
errors that occur in JVM. There are checked exceptions in a
program that we expect to occur in certain situations.
Exception handling mechanism catches these checked exceptions
and takes relevant actions.
133. In Java, what are the differences
between a Checked and Unchecked?
Checked Exceptions extend Throwable class, but they do not extend
RuntimeException or Error classes. UncheckedException extend
RuntimeException class.
Checked Exceptions are checked at compile time in Java.
Unchecked Exceptions happen at Runtime, so they are not checked
at compile time.
IOException, SQLException etc. are examples of Checked
Exceptions. NullPointerException, ArithmeticException etc. are
examples of Unchecked Exceptions.
134. What is the base class for
Error and Exception classes in Java?
Error as well as Exception class is derived from Throwable class
in Java.
135. What is a finally block in Java?
Java provides a finally block with a try block. This is an optional
block. But finally block is always executed after the execution of try
block.
136. What is the use of finally block in
Java?
As per Java specification, a finally block is always executed,
whether an error occurs or not, whether an exception is handled or
not. It helps in doing the cleanup like- Rollback Transaction, Close
Connection, Close a file etc.
137. Can we create a finally block
without creating a catch block?
Yes. A finally block can follow a try block or catch block. So we
can defined a finally block just after a try block.
138. Do we have to always put a catch
block after a try block?
Java does not enforce the rule to put a catch block after try block.
We can write catch block or finally block after a try block.
Any exception that we want to catch is mentioned in catch block.
139. In what scenarios, a finally block
will not be executed?
There are two main scenarios in which finally block is not
executed:
1. Program exits by calling system.exit() call.
2. A fatal error causes JVM to crash.
140. Can we re-throw an Exception
in Java?
Yes, Java allows to re-throw an Exception.
141. What is the difference between
throw and throws in Java?
Java provides throw keyword to throw an exception from a method
or a static block. Java provides throws keyword to mention the
probable exception thrown by a method in its declaration.
We use throw to explicitly throw an exception. We used
throws to declare an exception in method definition.
We cannot propagate checked exceptions with throw only. But
checked exceptions can be propagated with throws keyword.
A throw call is followed by an instance. Class or Exception follows
a throws keyword.
Call to throw occurs within a method. throws is just used with
method signature.
We can throw only one exception at a time. But we can mention as
many exceptions in throws clause.
142. What is the concept of
Exception Propagation?
In Exception Propagation, uncaught exceptions are propagated in the
call stack until stack becomes empty. This propagation is called
Exception Propagation.
Let say an exception propagates from one method to another method.
A() calls B(), which calls C(), which calls D(). And if D() throws
an exception, the exception will propagate from D to C to B to A,
unless one of the methods catches the exception.
143. When we override a method in
a Child class, can we throw an
additional Exception that is not
thrown by the Parent class method?
Yes, Java allows us to throw additional Exception in a child class,
but the additional exception should be an unchecked exception
(RuntimeException).

Comments
Post a Comment