Pandiyan Mani
3 min readAug 4, 2021

Java Interview Question

1.A single try block and multiple catch blocks can co-exist in a Java Program

Yes the first catch block matching the condition will be executed and remain will be stopped if the exception is already catched.

We cannt place catch(Exception exception) before any exception because this will say the exception is already catched on compile time itself.

try
{
int op = 9/0;
}
catch (ArrayIndexOutOfBoundsException exception) {
System.out.println("1st block = ArrayIndexOutOfBoundsException");
}
catch (ArithmeticException exception) {
System.out.println("2nd block = ArithmeticException");
}
catch (Exception exception) {
System.out.println("3rd block = Exception");
}

it will throw ArithmeticException since divisible by zero.

2.Explain the use of final keyword in variable, method and class

final keyword in variable mean once we declared a variable as final mean it cannt be modified once it has been assigned it will compile time error itself during reassigning.

If any value has not been assigned to that variable, then it can be assigned only by the constructor of the class

final keyword in method if declare a method as final that method cannot be accessed by its child class.

final keyword in class that can be used by other class by this final class can extend other class based on the need.

3. Do final, finally and finalize keywords have the same function?

Final: If any restriction is required for classes, variables, or methods, the final keyword comes in handy. Inheritance of a final class and overriding of a final method is restricted by the use of the final keyword. The variable value becomes fixed after incorporating the final keyword. Example:

final int a=100;
a = 0; // error

The second statement will throw an error.

Finally: It is the block present in a program where all the codes written inside it get executed irrespective of handling of exceptions. Example:

try {
int variable = 5;
}
catch (Exception exception) {
System.out.println("Exception occurred");
}
finally {
System.out.println("Execution of finally block");
}

Finalize: Prior to the garbage collection of an object, the finalize method is called so that the clean-up activity is implemented. Example:

public static void main(String[] args) {
String example = new String("InterviewBit");
example = null;
System.gc(); // Garbage collector called
}
public void finalize() {
// Finalize called
}

4.How would you differentiate between a String, StringBuffer, and a StringBuilder?

Strings in Java are objects used to represent a sequence of character. They can be either created using the String Literal or by using the NEW keyword. Strings are immutable in Java . When a new String is created, it looks for the String with the same value in the JVM string pool. If it finds a same value, then it returns the reference else it will create a String object and places that object in the String pool.

StringBuffer vs StringBuilder

StringBuffer operations are thread-safe and synchronized and StringBuilder operations are not thread-safe are not-synchronized.

StringBuffer is to used when multiple threads are working on the same String StringBuilder is used in a single-threaded environment.

StringBuffer performance is slower when compared to StringBuilderStringBuilder performance is faster when compared to StringBuffer

Syntax: StringBuffer var = new StringBuffer(str);

Syntax: StringBuilder var = new StringBuilder(str);

5.Using relevant properties highlight the differences between interfaces and abstract classes.

Availability of methods: Only abstract methods are available in interfaces, whereas non-abstract methods can be present along with abstract methods in abstract classes.

Variable types: Static and final variables can only be declared in the case of interfaces, whereas abstract classes can also have non-static and non-final variables.

Inheritance: Multiple inheritances are facilitated by interfaces, whereas abstract classes do not promote multiple inheritances.

Data member accessibility: By default, the class data members of interfaces are of the public- type. Conversely, the class members for an abstract class can be protected or private also.

Implementation: With the help of an abstract class, the implementation of an interface is easily possible. However, the converse is not true.

No responses yet