Site icon Narayana Tutorial

Java keywords interview questions and answers

Classes and Objects interview questions

 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?

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 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?

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?

What is static class ?

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?

What is use of throws keyword?

What is a strictfp modifier?

What is a transient variable?

What is volatile keyword?

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. 🙂