Java Interview Questions

Java Interview Questions can be intimidating if you are not prepared to answer them. Don’t worry though, because now you can start here. I will be adding more questions and answers as time goes by so you can keep your eye open for more goodies. I will also be adding interview questions for other areas including jQuery, Python and general questions that you might run into while being interviewed for a job. Let us get to it, shall we?

Commonly Asked Java Interview Questions

1) What is Object-Oriented Programming?

It is a programming methodology to design computer programs using classes and objects. There are several Object-oriented programming concepts and they include: Object, Class, Abstraction, encapsulation, Inheritance, Polymorphism, Composition. Always remember to give an example while answering this question.

2) What is an Object and how do you create it using Java?

An Object is a software bundle of related state and behavior. Software objects are often used to model the real-world objects that you run into in everyday life. You can alternatively say an object is an instance of a class. Here is how you create an object:

1

2

3

4

5

6

7

8

9

10

11

12

13

/*

* Java Objects - Java Interview Questions and Answers

*/

publicclassDemo{

publicstaticvoidmain(String[]args){

Demo demoObject=newDemo();

}

}

// demoObject is our object example. Demo is our class.

3) What is a Class?

A class is a blueprint or a prototype from which objects are created (instantiated). Here is an example of a class:

1

2

3

4

5

6

7

8

9

10

/*

* Class Example - java interview questions

*/

publicclassVehicle{

//your method definitions and variables go here

}

4) What is Abstraction?

Abstraction is a way to remove the association of the behavior of an object with the actual details behind the scenes which implement that object’s behavior. It is worth mentioning the idea that Abstraction talks about what it does, it never talks about how it does something. Consider giving an example.

5) What is Encapsulation?

Encapsulation is a technique used for hiding the properties and behavior of an object and allowing outside access only as appropriate. This prevents other objects from directly altering or accessing the properties or methods of the encapsulated object.

6) What is Inheritance?

As the name itself suggests, an object is able to inherit characteristics from another object. In more concrete terms, an object is able to pass on its state and behavior to its children. For inheritance to work, the objects need to have common characteristics between each other.

7) What is Polymorphism?

It is briefly described as “one interface, many implementations.” Polymorphism is a characteristic of being able to assign a different meaning or usage to something in different contexts – specifically, to allow an entity such as a variable, a function or an object to have more than one form. You can also mention the fact that there are two types of Polymorphism: Compile time and Run time polymorphism.

8) Explain the different forms of polymorphism.

Compile time polymorphism: this is done using method overloading.Run time polymorphism: this is done using inheritance and interfaces.From a practical view, polymorphism manifests itself in three forms in Java:

Method Overloading

Method Overriding

Method Overriding through the Java Interface

9) What is the difference between a constructor and a method?

A constructor gets invoked automatically every time an object is created or instantiated whereas a method has to be called explicitly to do its designated work.

10) So what is Java?

Java is a high level programming language that has the following features:

Simple

Architecture Neutral

Object-oriented

Portable

Distributed

High Performance

Multithreaded

Robust

Dynamic

Secure

11) What can you tell me about a JVM?

First, it stands for “Java Virtual Machine”. Every time you compile a Java file, the output of the process is not an exe. Instead, it is a .class file which consists of Java byte codes which the JVM understands. The JVM interprets the byte code into the machine code depending upon the underlying operating system and hardware combination.It is also responsible for all the things including garbage collection, array bounds checking and others.

12) What is a JRE?

First, it stands for Java Runtime Environment. JRE contains JVM, class libraries and other supporting files. It does not contain any development tools such as compiler, debugger among others. You must have JRE installed on your computer to run any java program successfully.

13) Can you differentiate between JRE and JVM?

JRE equals JVM plus Java Packages, Classes (for example util, math, lang, awt, swing etc) plus runtime libraries. In order to run applets like online games, JRE must be installed on the machine you want to run them on.

14) Is String a class or a data type in Java?

String is a class in java.lang package. But in Java, all classes are constructed as data types. That implies that we can take String as a data type as well.

15) What are the primitive data types in Java?

There are 8 available primitive data types in Java:

byte

short

int

long

float

double

char

boolean

16) What are the access specifiers in Java?

There are four access specifiers in Java:public – public classes, methods and fields can be accessed from everywhere.protected – protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes of the same package.default (no specifier) – if you don’t set access to specific level, then such a class, method or field will be accessible from inside the same packege to which the class, method, or field belongs, but not from outside this package.private – private methods and fields can only be accessed within the same class to which the methods and fields belong. Private methods and fields are not visible within subclasses and are not inherited by subclasses.

17) What is a package?

A package is a namespace that organizes a set of related classes and interfaces. Conceptually, you can think of a package as being similar to different folders on your computer.

18) Can you differentiate between String and StringBuffer Classes?

String class objects are immutable and therefore their contents cannot be modified.StringBuffer class objects are mutable and that means they can be modified.

19) Tell me the difference between StringBuffer and StringBuilder classes:

StringBuffer class is synchronized while StringBuilder class is not.

20) What is the difference between == and equals() while comparing Strings?

== operator compares the references of the string objects. It does not compare the contents of the objects.equals() method compares the contents.It is recommended to use equals() method while comparing strings because it yields the correct results.

Name:

Email:

Meanwhile, you can check out my post on jquery interviews if you love web development.

More Java Interview Questions Coming Soon…

Please subscribe for updates and consider sharing this post with your friends if you found it useful. Thank you so much for stopping by. You can read more about Java by clicking here.