Java Interview Question
Categories: Java 8(JDK1.8)
What is the difference between throw and throws?
throw keyword - throws keyword
1) The throw keyword is used to throw an exception explicitly. - The throws keyword is used to declare an exception.
2) The checked exceptions cannot be propagated with throw only. - The checked exception can be propagated with throws
3) The throw keyword is followed by an instance. - The throws keyword is followed by class.
4) The throw keyword is used within the method. - The throws keyword is used with the method signature.
5) You cannot throw multiple exceptions. - You can declare multiple exceptions, e.g., public void method()throws IOException, SQLException.
What is the output of the following Java program?
class Calculation extends Exception
{
public Calculation()
{
System.out.println("Calculation class is instantiated");
}
public void add(int a, int b)
{
System.out.println("The sum is "+(a+b));
}
}
public class Main{
public static void main(String []args){
try
{
throw new Calculation();
}
catch(Calculation c){
c.add(10,20);
}
}
}
Output
Calculation class is instantiated
The sum is 30
Can subclass overriding method declare an exception if parent class method doesn't throw an exception?
Yes but only unchecked exception not checked.
What is exception propagation?
An exception is first thrown from the top of the stack and if it is not caught, it drops down the call stack to the previous method, If not caught there, the exception again drops down to the previous method, and so on until they are caught or until they reach the very bottom of the call stack. This procedure is called exception propagation. By default, checked exceptions are not propagated.
What is String Pool?
String pool is the space reserved in the heap memory that can be used to store the strings. The main advantage of using the String pool is whenever we create a string literal; the JVM checks the "string constant pool" first. If the string already exists in the pool, a reference to the pooled instance is returned. If the string doesn't exist in the pool, a new string instance is created and placed in the pool. Therefore, it saves the memory by avoiding the duplicacy.
What is the meaning of immutable regarding String?
The simple meaning of immutable is unmodifiable or unchangeable. In Java, String is immutable, i.e., once string object has been created, its value can't be changed. Consider the following example for better understanding.
class Testimmutablestring{
public static void main(String args[]){
String s="Sachin";
s.concat(" Tendulkar");//concat() method appends the string at the end
System.out.println(s);//will print Sachin because strings are immutable objects
}
}
Why are the objects immutable in java?
Because Java uses the concept of the string literal. Suppose there are five reference variables, all refer to one object "sachin". If one reference variable changes the value of the object, it will be affected by all the reference variables. That is why string objects are immutable in java.
How many ways can we create the string object?
1) String Literal
Java String literal is created by using double quotes. For Example:
String s="welcome";
Each time you create a string literal, the JVM checks the "string constant pool" first. If the string already exists in the pool, a reference to the pooled instance is returned. If the string doesn't exist in the pool, a new string instance is created and placed in the pool. String objects are stored in a special memory area known as the string constant pool For example:
String s1="Welcome";
String s2="Welcome";//It doesn't create a new instance
2) By new keyword
String s=new String("Welcome");//creates two objects and one reference variable
In such case, JVM will create a new string object in normal (non-pool) heap memory, and the literal "Welcome" will be placed in the constant string pool. The variable s will refer to the object in a heap (non-pool).
How many objects will be created in the following code?
String s1="Welcome";
String s2="Welcome";
String s3="Welcome";
Only one object will be created using the above code because strings in Java are immutable.