Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Java. 1 Comment on Java Interview Questions and Answers
4.5
(2)

Here is a collection of frequently asked java interview questions and answers for technical interviews. All Java questions are provided with appropriate answers. If you find any answer is not correct, do comment in the comments section (scroll down).

 

Note: Click the question or click on the arrow to see the answer for a question.

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


Question and Answers

 

Access specifiers applicable for a top level class are public and default. Look at the below image for more information.

access-specifiers-rules

image source: http://stackoverflow.com/questions/2516569/java-access-specifier

The implicit return type of a constructor is the class name itself. For example if we are creating a constructor for class Student, then the return type of constructor is Student.

If the array's type is a class, array can store objects. It will not exactly store the object itself but a reference to the object. This is similar to the concept of an array of pointers in C and C++.

If a class is created with public access specifier, then the java file must be saved with the name of that class. So, this also implies that a java file cannot contain more than one public class.

If the variable or method is declared as static, we can access them using the class name itself without creating any object. One example method called directly without creating any object is main() method. This is the reason why main() is always prefixed with the static keyword.

A static method can access only static variables (also called class variables).

Yes. A static method can be overloaded but cannot be overriden.

Static methods can access only other static methods.

For instance variables, default value is 0 for int, 0.0 for float and double. Local variables does not have any default values. They must be initialized before using them in the program.

The final keyword can be used to create constants, create methods which are non-overridable, and to create class which cannot be inherited.

Strings can be created using the classes: String, StringBuffer, or StringBuilder.

String class is for creating immutable strings. StringBuffer and StringBuilder classes are for creating mutable strings. StringBuffer methods are synchronized, whereas, StringBuilder methods are not synchronized.

In the case of strings, == operator checks whether two references are pointing to the same object (memory location) or not. For example, consider the following code:

String s1 = new String("hello");
String s2 = new String("hello");

If we compare s1 and s2 as s1==s2, it evaluates to false as s1 and s2 reference separate memory locations (objects). To compare the string content we have to write s1.equals(s2). It evaluates to true.

Main use of inheritance is reusability. General properties are moved to parent class and special properties and behavior is specified in the child classes.

In case of classes multiple inheritance is not supported. But in case of interfaces multiple inheritance is supported.

The super keyword is used to refer immediate super class (parent class) constructor and immediate super class (parent class) members.

If parent class and child class contains a method with same signature, and if child class method is called, then child class method executes hiding the parent class method. This process is known as method overriding.

When a parent class reference is used to invoke a child class method, the association between the method call and method definition takes place at runtime. This process is known as dynamic method dispatch or late binding.

The abstract keyword is used to create abstract classes and abstract methods.

No. As an abstract class is not complete, it cannot be instantiated.

When a class is declared as final it can no longer be extended (it cannot be a parent class).

When a method is declared as final it cannot be overriden.

Base class or super class for all classes in Java is Object.

Yes. Interfaces can be extended.

A package is a group of similar classes. A package can be created using the package keyword.

We can import a package using the import keyword. To import all the classes in a package named mypackage, we will write import mypackage.*.

We can declare a package using the keyword package. Consider this example:

package mypack;

In the above example mypack is the name of our package.

An exception is a run-time error. Handling exceptions is known as exception handling.

Run-time anomalies caused by instructions in the program are exceptions and run-time anomalies mostly caused due to operating system and hardware are termed as errors.

Java provides try, catch, throw, throws, and finally for handling exceptions.

Yes. A try block must be followed by a catch block or finally block.

In generally finally block is guaranteed to execute always. But if we use Jump statements like return etc., finally block might not get executed.

Checked exceptions must be handled by the programmers. Handling unchecked exceptions is not mandatory. If an unchecked exception is not handled by the programmers, Java run-time will handle it automatically.

The throws keyword is used to throw one or more exceptions to the calling method or to the Java's run-time system.

When there are multiple catch blocks, the exceptions are matched against the catch blocks in the order they are defined.

When there are nested try blocks, if the exception raised in the inner try block is not handled, it will be checked against the catch blocks of the outer try block. If still not handled, it is handled by the Java's run-time system.

To create own exceptions, a new class should be created and it should extend the pre-defined class Exception. The message to be displayed when the exception is handled can be provided by overriding the method getMessage().

Multithreading increases the efficiency of CPU. If one thread is suspended, another thread can be executed by the CPU. Also throughput might also increase if there are multiple processors.

The sates in the thread life cycle are: New, Runnable, Terminated, Waiting, and Blocked.

Threads can be created in two ways: 1) By extending Thread class or 2) By implementing Runnable interface.

Multiple threads accessing a shared resource can be synchronized to avoid race conditions by using either synchronized blocks or using synchronized methods.

Threads which run in the background are known as daemon threads. An example for daemon thread is Java's garbage collector. An user thread can be converted to a daemon thread by invoking setDaemon() method.

Multiple threads can be created by instantiating Thread class multiple times. Those multiple threads can be controlled by using cooperation synchronization and by using thread life cycle methods.

An applet is a small Java program which is embedded in a webpage and is automatically downloaded when the user requests the webpage.

There are many differences between a Java program and an applet. Some of them are: a Java program requires main(), whereas an applet doesn't require main(). Starting point of execution for a Java program is main(), whereas for an applet is init().

There are two types of applets: 1) Signed applets and 2) Unsigned applets. Signed applets are trusted applets which digitally signed by a third party. Unsigned applets are not signed by anyone. So they are unsecure.

Java provides security through applets. An applet runs inside a sandbox in the browser's memory and prevents from accessing the local resources like memory, files etc.

Applet can be created either by extending Applet class or by extending JApplet class.

Applet life cycle methods are init(), start(), stop() and destroy(). The init() and destroy() methods execute only once in the applet life cycle. Other methods can execute multiple times.

Event is an object which is generated by an event source. Examples of events are key press, mouse click etc.

Event source is an object which generates events. Examples of event sources are button, checkbox, textfield etc.

Event listener is an object which listens to events that are raised on event sources. An event listener should be attached to an event source to take appropriate action when an event occurs.

To eliminate the need to implement all the methods in a listener interface, Java developers developed a set of classes called adapter classes which provide default implementations for all the methods in a listener interface.

Non static nested classes are known as inner classes.

Nameless nested class is known as anonymous class. Main use of anonymous classes is to handle events in GUI programs.

Motto of Java is WORA (Write Once Run Anywhere).

AWT components are heavy weight, whereas, Swing components are light weight. AWT components have different look and feel in different platforms, whereas Swing components have same look and feel in all platforms.AWT is the base GUI package, whereas Swing is developed on to off AWT.

Some of the differences are: interface does not contain variables, whereas, an abstract class can contain variables. Interface contains only abstract methods, whereas, abstract class can contain both abstract and non-abstract methods. Interface does not have a constructor, whereas, an abstract class can have a constructor.

The program will compile but it will not run as JVM will not be able to access main method as it is declared as private.

Yes. We can have multiple main methods by overloading it. But, the entry point for any Java program is the main() method with String array as argument. Other versions of main cannot be called automatically.

Yes. Same package or class can be imported multiple times. But JVM loads a package or class only once at runtime.

No. Importing the parent package does not mean that sub packages are also imported.

In Java everything is passed by value. When an object is passed as a parameter, a copy of the object reference is passed which is pass by value.

There is no direct support in Java to find the size of an object.

Using multi level inheritance this can be done. The parent class can extend the Exception class and it can be extended by the sub class.

Yes. We can create an abstract class with no abstract methods. But, it is meaningless.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

1 Comment

You can follow any responses to this entry through the RSS 2.0 feed.

Well, your blog is quite interesting and helpful. I just loved reading your blog. Thanks a lot for sharing the top java interview question here, i have found your article very good and useful as i have an interview as java developer and was looking for some java questions to prepare for.

Leave a Reply

Your email address will not be published. Required fields are marked *