Java Interview Questions And Answers

Q: – Can I call a abstract method from a non abstract method ? 

Yes, We can call a abstract method from a Non abstract method in a Java abstract class

Q: – What is a collection ? 

Collection is a group of objects. java.util package provides important types of collections. There are two fundamental types of collections they are Collection and Map. Collection types hold a group of objects, Eg. Lists and Sets where as Map types hold group of objects as key, value pairs Eg. HashMap and Hashtable.

Q: – What methods java providing for Thread communications ? 

Java provides three methods that threads can use to communicate with each other: wait, notify, and notifyAll. These methods are defined for all Objects (not just Threads). The idea is that a method called by a thread may need to wait for some condition to be satisfied by another thread; in that case, it can call the wait method, which causes its thread to wait until another thread calls notify or notifyAll.

Q: – Can you write a Java class that could be used both as an applet as well as an application?

Yes, Add a main() method to the applet.

Q: – What’s the difference between J2SDK 1.5 and J2SDK 5.0?

There’s no difference, Sun Microsystems just re-branded this version.

Q: – You are planning to do an indexed search in a list of objects. Which of the two Java collections should you use: ArrayList or LinkedList?

ArrayList

Q: – Why would you use a synchronized block vs. synchronized method?

Synchronized blocks place locks for shorter periods than synchronized methods.

Q: – Explain the usage of the keyword transient?

This keyword indicates that the value of this member variable does not have to be serialized with the object. When the class will be de-serialized, this variable will be initialized with a default value of its data type (i.e. zero for integers).

Q: – How can you force garbage collection?

You can’t force GC, but could request it by calling System.gc(). JVM does not guarantee that GC will be started immediately.

Q: – What is Abstraction?

Abstraction refers to the act of representing essential features without including the background details or explanations.

Q: – What is Encapsulation?

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

Q: – What is Dynamic Binding?

Dynamic binding (also known as late binding) means that the code associated with a given procedure call is not known until the time of the call at run-time

Q: – Is it possible to override the main method?

NO, because main is a static method. A static method can’t be overridden in Java.

Q: – What is super?

Super is a keyword which is used to access the method or member variables from the superclass.

Q: – Can we instantiate an interface?

You can’t instantiate an interface directly, but you can instantiate a class that implements an interface.

Q: – Can we create an object for an interface?

Yes

Q: – What modifiers are allowed for methods in an Interface?

public
abstract

Q: – What is an abstract class?

Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation.

Q: – When you declare a method as abstract, can other nonabstract methods access it?

Yes

Q: – Can there be an abstract class with no abstract methods in it?

Yes

Q: – What is Constructor?

A constructor is a special method whose task is to initialize the object of its class.

Q: – Can constructor be inherited?

No

Q: – What is an EntrySet View?

Entry Set view is a set that is returned by the entrySet() method in the map and contains Objects of type Map.

Q: – What is a KeySet View ?

KeySet is a set returned by the keySet() method of the Map Interface, It is a set that contains all the keys present in the Map.

Q: – What are the main Implementations of the Map interface ?

HashMap
HashTable
TreeMap
EnumMap

Q: – What is a Map ?

A map is an object that stores associations between keys and values (key/value pairs).

Q: – What is an EnumSet ?

An EnumSet is a specialized set for use with enum types, all of the elements in the EnumSet type that is specified, explicitly or implicitly, when the set is created.

Q: – What are the advantages of ArrayList over arrays ?

It can grow dynamically
It provides more powerful insertion and search mechanisms than arrays.

Q: – What is the List interface?

The List interface provides support for ordered collections of objects. Lists may contain duplicate elements.

Q: – Can null value be added to a List?

Yes

Q: – What is Dictionary class?

The Dictionary class is the abstarct super class of Hashtable and Properties class.Dictionary provides the abstarct functions used to store and retrieve objects by key value. This class allows any object to be used as a key or value

Q: – What is the Hashtable class?

The Hashtable class implements a hash table data structure. A hash table indexes and stores objects in a dictionary using hash codes as the objects’ keys

Q: – Garbage collector thread belongs to which priority?

low-priority

Q: – Which method waits for the thread to die ? 

join() method

Q: – What are all the four states associated in the thread?

new
runnable
blocked
dead

Q: – What is the default thread at the time of starting the program?

main thread

Q: – What is the method available for setting the priority?

setPriority()

Q: – What are all the methods available in the Runnable Interface?

run()

Q: – What are the two types of multitasking?

process-based
Thread-based

Q: – Can we have catch block with out try block?

No

Q: – How do we change the values of the elements of the array?

The array subscript expression can be used to change the values of the elements of the array.

Q: – Is it possible to use few methods of an interface in a class ? If so, how?

Yes. Declare the class as abstract.

Q: – What are the possible access modifiers when implementing interface methods?

public

Q: – How do you achieve multiple inheritances in Java? 

We can use interfaces.

Q: – Is it is necessary to implement all methods in an interface?

Yes

Q: – How this() is used with constructors?

 this() is used to invoke a constructor of the same class.

Q: – How super() used with constructors?

super() is used to invoke a super class constructor

Q: – Which methods in the Object class are declared as final?

getClass()
notify()
notifyAll()
wait()

Q: – Which Java operator is right associatively?

Java support = operator

Q: – Can a byte object be cast to a double value ?

No

Q: – What is the % operator?

The % operator is the modulo operator or reminder operator. It returns the reminder of dividing the first operand by second operand.

Q: – What is the use of bin and lib in JDK?

bin contains all tools such as javac, applet viewer, awt tool etc., whereas lib contains all packages and variables

Q: – What are the three OOPs principles and define them?

Encapsulation, Inheritance and Polymorphism are the three OOPs Principles.

Q: – What declarations are required for every Java application?

A class and the main( ) method declarations.

Q: – What modifiers may be used with top-level class?

public
Abstract
final.

Q: – Is a class a subclass of itself?

Yes

Q: – Which method is used to garbage collect an object?

finalize ()

Q: – What is casting?

Casting is process of convert the value of one type to another.

Q: – What is phantom memory?

Phantom memory is false memory. Memory that does not exist in reality.

Q: – What is MVC?

Model-View-Controller (MVC) is a design pattern put together to help control change.

Q: – What is a framework?

A framework is made up of the set of classes which allow us to use a library in a best possible way for a specific requirement.

Q: – What are the components of Struts?

Model
View
Controller

Q: – What is the ActionForm?

ActionForm is javabean which represents the form inputs containing the request parameters from the View referencing the Action bean.

Q: – What are the important methods of ActionForm?

validate()

reset()

Q: – What is ActionMapping?

Action mapping contains all the deployment information for a particular Action bean. This class is to determine where the results of the Action will be sent once its processing is complete.

Q: – What is role of Action Class?

An Action Class performs a role of an adapter between the contents of an incoming HTTP request and the corresponding business logic that should be executed to process this request.

Q: – Can we have more than one struts-config.xml file for a single Struts application?

Yes

Q: – What are the different kinds of actions in Struts?

ForwardAction
IncludeAction
DispatchAction
LookupDispatchAction
SwitchAction

Q: – What is IncludeAction?

The IncludeAction class is useful when you want to integrate Struts into an application that uses Servlets.

Q: – What is SwitchAction?

The SwitchAction class provides a means to switch from a resource in one module to another resource in a different module. SwitchAction is useful only if you have multiple modules in your Struts application.

Q: – How to display validation errors on jsp page?

<html:errors/> tag displays all the errors. <html:errors/> iterates over ActionErrors request attribute.

Q: – What are the uses of Servlet?

Processing and/or storing data submitted by an HTML form.
Providing dynamic content, e.g. returning the results of a database query to the client.
A Servlet can handle multiple requests concurrently and be used to develop high performance system
Managing state information on top of the stateless HTTP, e.g. for an online shopping cart system which manages shopping carts for many concurrent customers and maps every request to the right customer.

Q: – What is the GenericServlet class?

GenericServlet is an abstract class that implements the Servlet interface and the ServletConfig interface.

Q: – Why is HttpServlet declared abstract?

The HttpServlet class is declared abstract because the default implementations of the main service methods do nothing and must be overridden.