Java keywords interview questions and answers


 Java keywords interview questions and answers

Here I am going to sharing some of the important and frequently asking Java keywords interview questions and answers. So before going to java interview, refresh your mind go through the following Java keywords interview questions and answers.

What is use of synchronized keyword?

  • This keyword is used to prevent concurrency. Synchronized keyword can be applied to static/non-static methods or a block of code. Only one thread at a time can access synchronized methods and if there are multiple threads trying to access the same method then other threads have to wait for the execution of method by one thread. Synchronized keyword provides a lock on the object and thus prevents race condition.

Example

 public void synchronized method(){
          // TO DO Something
       } 
    public void synchronized static method(){
         // TO DO Something
    } 
    public void myMethod(){ 
       synchronized (this){ // synchronized keyword on block of code 
       } 
    }

What is a static variable?

  • Static keyword can be used with the variables and methods but not with the class. Anything declared as static is related to class and not objects.

Static variable :Multiples objects of a class shares the same instance of a static variable.

Example

public class Counter{ 

private static int count=0; 

private int nonStaticcount=0;
 
public void incrementCounter(){ 

count++; 

nonStaticcount++; 

} 
public int getCount(){ 

return count; 

} 

public int getNonStaticcount(){ 

return nonStaticcount; 

} 
 
public static void main(String args[]){ 
 
 Counter countObj1 = new Counter(); 

 Counter countObj2 = new Counter(); 

 countObj1.incrementCounter(); 

 countObj1.incrementCounter(); 

 System.out.println("Static count for Obj1: "+countObj1.getCount()); 

 System.out.println("NonStatic count for Obj1: "+countObj1.getNonStaticcount()); 

 System.out.println("Static count for Obj2: "+countObj2.getCount()) 

 System.out.println("NonStatic count for Obj2: "+countObj2.getNonStaticcount()) 

}

Output

Static count for Obj1: 2

NonStatic count for Obj1: 1

Static count for Obj2: 2

NonStatic count for Obj2: 1

In the above program obj1 and obj2 share the same instance of static variable count hence if the value is incremented by one object , the incremented value will be reflected across the other objects.

What is a static method?

  • A method defined as static is called static method. A static method can be accessed without creating the objects. Just by using the Class name the method can be accessed.
  • Static method can only access static variables and not local or global non-static variables.
public class Test{ 

public static void printMe(){ 

System.out.println("Hello World"); 

  } 
} 

public class MainClass{ 

public static void main(String args[]){ 

Test.printMe() 

 } 
} 

Output
Hello World

Also static method can call only static methods and not non static methods. But non-static methods can call static mehtods.

Why static methods cannot access non static variables or methods?

  • A static method cannot access non static variables or methods because static methods can be accessed without instantiating the class, so if the class is not instantiated the variables are not intialized and thus cannot be accessed from a static method.

What is static class ?

  • A class cannot be declared static. But a class can be said a static class if all the variables and methods of the class are static and the constructor is private. Making the constructor private will prevent the class to be instantiated. So the only possibility to access is using Class name only

What is the use of final keyword?

The final keyword can be assigned to

  1. Class level variable
  2. method
  3. Class
  4. Objects

If a final is assigned to a variable, the variable behaves as a constant. It means that the value of variable once set cannot be changed.

final int i = 1;
i = 5; // error  

If a final is assigned to a method then it cannot be overridden in its child class.

Example

public class Parent { 
final void print(){ 
System.out.println("Inside"); 
} 
} 
 
public class Child extends Parent{ 
public final void print(){ 
// error cannot override final method 
System.out.println("Inside"); 
} 
}

If a class is made as final, then no other class can extend it and make it as parent class.

Ex. String Class.

Final objects are instantiated only once. i.e

final Map map = new HashMap();
map.put(“key”;,”value”);
map = new HashMap(); // error  

What is throw keyword?

  • Throw keyword is used to throw the exception manually. It is mainly used when the program fails to satisfy the given condition and it wants to warn the application.The exception thrown should be subclass of Throwable.

What is use of throws keyword?

  • Throws clause is used to throw the exception from a method to the calling method which could decide to handle exception or throw to its calling method in a class.

What is a strictfp modifier?

  • Strictfp is used with variable only .
  • It is used to restrict floating point calculations ( fp ) to ensure portability ( platform Independent ). When this modifier is specified, the JVM adheres to the Java specifications ( IEEE-754 floating-point specification ) and returns the consistent value independent of the platform. That is, if you want the answers from your code (which uses floating point values) to be consistent in all platforms, then you need to specify the strictfp modifier.

What is a transient variable?

  • If some of the properties of a class are not required to be serialized then the varaibles are marked as transient. When an object is deserialized the transient variables retains the default value depending on the type of variable declared and hence lost its original value.

What is volatile keyword?

  • In general each thread has its own copy of variable, such that one thread is not concerned with the value of same variable in the other thread. But sometime this may not be the case. Consider a scenario in which the count variable is holding the number of times a method is called for a given class irrespective of any thread calling, in this case irrespective of thread access the count has to be increased. In this case the count variable is declared as volatile. The copy of volatile variable is stored in the main memory, so every time a thread access the variable even for reading purpose the local copy is updated each time from the main memory.

The volatile variable also have performance issues.

I hope this article is helped for you. If you have any suggestions or having interview questions, please comment below. We will get back you with solution. 🙂

Leave a Reply