Top 150+ Java Interview Questions and Answers

    58 Votes

47. What is access modifier and what are it's types?

An attribute that determines whether a class member can be accessed or not. Different types of access modifiers are:
  • Public - Members are accessible to the class and to other classes.
  • Private - Members are accessible only to the class.
  • Protected - Members are accessible only to the class, it's subclass(es), and other classes in the package.
  • Default - Members are accessible only to the class and other classes within that package. This is the default access specifier.

 

48. What is final keyword?
If a variable is declared as final, it's value can't be modified. If a method is declared as final, it can't be overridden in the subclass. A class declared as final cannot be inherited.
49. What is static keyword?

We can define variables and methods of a class as "Static". If we declare a method or a variable as static, it means that the data member belongs to the class rather than any specific instance of a class. Static Methods and data members can be called without instantiating the class. A static member exists even if no objects of that class exist.

50. What is abstract keyword?

Abstract keyword is used to declare a class, when we want to expose certain functionality to the users and hide the underlying implementation. We can't create an instance of the abstract class. An abstract class can contain abstract and non-abstract methods.

51. What is synchronized keyword?

It controls the access to a block in a multithreaded environment.

52. What is native keyword?

Native keyword is used only with methods. It signals the compiler that the method has been coded in a language other than Java. It indicates that method lies outside the Java Runtime Environment.

53. What is a transient variable?

A transient variable is a variable that won't be serialized.i.e JVM understands that the transient variable is not part of the persistent state of the object. For example

class A implements Serializable {
transient int i; // will not persist
int j; // will persist
}

Here, if an object of type A is written to a persistent storage area, the contents of "i" would not be saved, but the contents of "j" would.

54. What is sctrictfp keyword?

The floating point precisions vary based on the platform on which Java program is run. In order to make sure that constant results are obtained while doing operations with floating point variables, Java provides "strictfp" keyword. If not at class class level , strictfp behavior can be applied at method level, by declaring a method as strictfp.

55. Explain method overriding with an example?

Method overriding is defined as creating a non-static method in the subclass that has the same return type and signature as a method defined in the superclass. The signature of a method includes the name of the method and number, sequence, type of arguments.

public class Person {
private String name;
private String ssn;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name; }
public String getId() {
return ssn;}

getId method is overridden in subclass:

public class Employee extends Person
{
private String empId;
public void setId(String empId)
{
this.empId=empId;
}
public String getId() {
return empId;
}
}

56. What is the use of super keyword. Give an example?
It allows you to access methods and properties of the parent class.

public class Person {
private String name;
public Person(String name) {
this.name = name; }
public String getName() {
return name;
} }
public class Employee extends Person {
private String empID;
public Employee(String name) {
super(name); // Calls Person constructor
}
public void setID(String empID){
this.empID=empID;
}
public String getID(){
return empID;
}
}

57. What is an interface?

If we want enforce certain behaviour on objects implementing an interface, so that all the object provides certain functionalities irrespective of it's underlying implementation, we use Interface Class. The Interface is a conceptual entity. It can contain only final variables and abstract methods.

For example, you might specify Driver interface as part of a Vehicle object hierarchy. A human can be a Driver, but so can a Robot.

Page 5 of 14

Popular Videos

communication

How to speak so that people want to listen.

Got a tip or Question?
Let us know

Related Articles

Java OOPS Interview Questions and Answers
Top Advanced Java Interview Questions and Answers
Java Collections Interview Questions and Answers
Java Thread Interview Questions and Answers