Edit
Reply




Edit

  1. Home
  2. Articles

Most Asked Java Interview Questions - Set 1

BOOKMARK

In today’s world, Java is used everywhere. Over 15 billion devices, starting from the household devices like DTH and Mobile Phones to technologies like Big Data, support Java. So, it can be rightly said that we use Java daily. But, how many of us know this programming language and can answer questions from it confidently? The number of people would be less. Sometimes, out of nervousness, many of us stammer and blabber in Technical Interviews even if we know the correct answer.  Hence, in this article, we will discuss the most asked Java interview questions and answers that will set you apart from the other candidates.

Most Asked Java Interview Questions

1. What are the similarities and difference between C++ and Java?

Java, unlike C++, doesn’t have pointers, operator overloading, typedef keyword, define keyword, structures, unions, enumeration, functions, goto statements, templates, and default parameters. Java doesn’t have functions as it is Object Oriented. It has methods instead.

Most of you are confused between methods and functions. Functions, used in C++, and methods, used in Java, have the same functionality with a minor difference. Functions are a set of codes that are called in any part of the program by referring to its name. Methods are a set of codes that are called in any part of the program by referring to its name and these are associated with an object.   

There is no automatic coercion in Java, unlike C++. Coercion is to convert one data type to another. In Java, coercion is explicitly made and is performed using code instructions.

In Java, variables are not declared separately. They are the part of a class.

 Java supports interface inheritance and not implementation inheritance. i.e, it doesn’t support private, public and protected inheritance but supports multiple inheritance.

There’s no scope resolution operator (::) in Java.  Java uses the dot for everything but can get away with it since you can define elements only within a class.

Unlike C++, Java has garbage collection and multithread support.

Even though Java doesn’t support operator overloading, it supports function overloading. Like C++, it also supports internationalization and exception handling. Exception handling in Java is different because there are no destructors.

2. Explain JVM, JRE, and JDK.

JVM (Java Virtual Machine): During the development of Java, the developers faced many problems because as the OS gets updated, the system was not able to run the previously written codes. To rectify this problem, JVM came into existence. It is an abstract machine that is present in the user’s computer and converts the bytecode into machine code. Thus, it enables a computer to run a java program.   It is a specification that provides a run-time environment in which Java bytecode can be executed. It follows three notations:

Specification: It is a document that describes the implementation of the Java virtual machine. It is provided by Sun and other companies.

Implementation: It is a program that meets the requirements of JVM specification.

Runtime Instance: An instance of JVM is created whenever you write a java command on the command prompt and run the class.

JRE (Java Runtime Environment): JRE refers to a software package in which java bytecode can be executed. It implements the JVM (Java Virtual Machine) and provides all the class libraries and other support files that JVM uses at runtime.

JDK (Java Development Kit): This tool is necessary to compile, document and package Java programs. The JDK includes JRE which contains tools for Java programmers. Along with JRE, it includes an interpreter/loader, a documentation generator (javadoc), an archiver (jar), a compiler (javac), and other tools needed in Java development. To put it simply, it encloses the JRE and the development tools.

3. Explain public static void main(String args[]).

public: Public is an access modifier. Access modifiers are used to specify who can access this method and hence, public specifies that this method will be accessible to any class.

static: Static is a keyword in java to identify if the program is class-based or not because, in Java, a program cannot be accessed without creating the instance of a Class.

void: It is the return type of the method. Void defines the method that doesn’t return any value.

main: Main is the method that is searched by the JVM as it is considered as the starting point of the program.

String args[]: It is the parameter passed to the main method.

 In this, only the name of the string can be changed and the rest remains constant.

4. Why is Java platform independent?

Java is created keeping in mind the concept of “write once run anywhere”. A code once compiled, can run on any operating system as it uses the JVM. The source code is converted into a bytecode by the Java compiler and then, the JVM on the user’s system converts it into machine codes. As these machine codes can run on any system irrespective of its underlying operating system, it is platform independent.

 5. Explain Final keyword in java.

The final keyword is used in variables, classes, and methods to restrict the usage.

In variables, the value of the final variable is constant and cannot be changed.

In classes, final classes can’t be inherited.

In methods, a final method can never be overridden.

6. When is the super keyword used?

A super keyword is used to refer the following:

Immediate parent class constructor
Immediate parent class variable
Immediate parent class method.

super() is used to invoke the immediate parent class constructor.

An example for how the super keyword is used to access the data members of a parent class is given below:

class Animal{ 
String color="white"; 

class Dog extends Animal{ 
String color="black"; 
void printColor(){ 
System.out.println(color);//prints color of Dog class 
System.out.println(super.color);//prints color of Animal class 


class TestSuper1{ 
public static void main(String args[]){ 
Dog d=new Dog(); 
d.printColor(); 
}} 
 
An example for how the super keyword is used to invoke a method of a parent class is given below:

class Animal{ 
void eat(){System.out.println("eating...");} 

class Dog extends Animal{ 
void eat(){System.out.println("eating bread...");} 
void bark(){System.out.println("barking...");} 
void work(){ 
super.eat(); 
bark(); 


class TestSuper2{ 
public static void main(String args[]){ 
Dog d=new Dog(); 
d.work(); 
}} 

An example for how the super keyword is used to invoke a constructor of a parent class is given below:

class Animal

Animal(){System.out.println("animal is created");} 

class Dog extends Animal{ 
Dog(){ 
super(); 
System.out.println("dog is created"); 


class TestSuper3{ 
public static void main(String args[]){ 
Dog d=new Dog(); 
}} 

7. Compare SWING components to standard AWT.

Swing is an extension and not a replacement for the AWT (Abstract Window Toolkit). There are few overlaps between AWT and Swing. For example, a Swing JButton component might be viewed as an improved functional replacement for an AWT Button component.

One of the advantages of Swing components is that because the components are not rendered on the screen by the operating system, the look, and feel of a component does not change as the application or applet is executed on different platforms running under different operating systems.

Furthermore, it is possible to cause Swing components to mimic the look and feel of a specific platform no matter what platform the program is running on. This is known as pluggable look and feel. Swing components support the JDK 1.1 Delegation Event Model.

From an event handling point of view, Swing components operate the same as AWT components except that Swing provides a number of new event types. Many Swing components don't have an AWT counterpart. A number of new and exciting components are included in the Swing library that doesn't exist in the AWT like tooltips, progress bars, trees, etc.

8. What is the difference between ‘throw’ and ‘throws’ in Java Exception Handling?

The throw keyword is used to throw Exception from any method or static block whereas the throws keyword is used to indicate the method in which the Exception can possibly be thrown.

If any method throws checked Exception, then the user can either handle this exception by using the try-catch block or they can re-throw it by declaring another ‘throws’ clause during the declaration of methods.

The throw clause can be used in any part of the program like

throw
throw new Exception(“You have some exception”)
throw new IOException(“Connection failed!!”)
throws
throws IOException, NullPointerException, ArithmeticException

The above mentioned are some of the most asked Java interview questions.

For more, stay in touch with FACE Prep.


5 Comments
×

Boyina Sreeja
It's very easy to understand and we can get the information at a glance .its help full .

Thank (1)
Reply
1 month ago


ramunaik banavath
easily understandable. Very esay. I want more sets...

Thank ()
Reply
1 year ago


Sharmila Balasubramani
easily understandable

Thank ()
Reply
1 year ago


Vijay Raman
Good one

Thank ()
Reply
1 year ago


Rohit Bhavsar
Nice one. Can you give us more such questions please.

Thank ()
Reply
1 year ago


Krishna Pathak
If this is set-1 they will probably come up with more set of questions. Hoping so.

Thank ()
1 year ago

Open in App