Java Interview Questions and Answers.
Hi Guys. You can see the list of important interview questions asked in Java. Please have a look. This might be useful for you for preparing your interviews. You can get a clear view on definitions and concepts from this page.
What is Java? Where it is used?
Java is a programming language and computing platform. It is released by Sun Microsystems in 1995. It is used in Gaming, Web building & Business Applications.
What are the Basic concepts of OOPS?
Dynamic Binding & Message Parsing
What is an Object?
An object is an instance of class. It can be a name, color, etc....
What is a Class?
A class is a blue print from which individual objects are created.
What is Data Abstraction?
Data Abstraction refers to the act of representing essential features without considering the background details or explanations.
What is Data Encapsulation?
It is used for hiding the properties of an object. It prevents other objects from accessing the properties of the encapsulate object.
What is Polymorphism?
Polymorphism is the ability to have more than one form.
Explain the different forms of Polymorphism?
There are two types of polymorphism one is Compile time polymorphism and the other one is run time polymorphism.
Compile time polymorphism is method overloading.
Runtime time polymorphism is done using inheritance and interface.
How does Java implement polymorphism?
Java implements polymorphism by using overloading and overriding concepts.
What is Overloading?
Methods with same name, but with different arguments.
What is overriding?
When a method in a subclass has the same name and type signature as a method in its super class, then the method in the subclass is said to override the method in the super class.
A method named final or a method named static cannot be overridden.
Can a main method be overridden?
Since the main method is static it cannot be overridden.
What is super keyword?
Super keyword is used to access the methods or member variables from the super class. If a method hides one of the member variables in its super class, the method can refer to the hidden variable through the use of the super keyword.
If a method overrides one of the methods in its super class, the method can invoke the overridden method through the use of the super keyword.
What is Inheritance? What are its types?
Inheritance is the process by which objects of one class acquiring the properties of objects of another class. The types are single inheritance, multiple inheritance, multilevel inheritance and hierarchical inheritance.
Does Java support multiple inheritances? If yes/no why?
No. Java does not support multiple inheritances. Because when a class inherits from more than one class, it will lead to inheritance path ambiguity problem which is normally called as diamond problem.
Name the keyword used for inheritance?
Extends keyword is used for inheritance.
What is Dynamic Binding?
Dynamic Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding means that the code associated with a given procedure call is not known until the time of the call at run-time.
It is associated with polymorphism and inheritance.
What is an Interface?
An interface is a description of a set of methods that conforming implementing classes must have.
You cannot mark an interface as final. Interface variables must be static. An Interface cannot extend anything but it can extend with another interfaces.
Is it possible to instantiate an interface?
It is not possible to instantiate an interface directly, but you can instantiate a class that implements an interface.
Do interfaces have member variables?
Interfaces may have member variables, but these are implicitly public, static, and final. In other words, interfaces can declare only constants, not instance variables that are available to all implementations and may be used as key references for method arguments for example.
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.
If even a single method is abstract, the whole class must be declared abstract.
Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods.
An abstract class can never be instantiated. Its purpose is to be extended (sub classed).
You cannot mark a class as both abstract and final.
What are the differences between Interface and Abstract class?
An abstract class can provide complete, default code and just the details that have to be overridden.
An interface cannot provide any code at all, just the signature.
All the methods declared inside an interface are implicitly abstract whereas abstract class must have at least one abstract method and others may be concrete or abstract.
In abstract class, key word abstract must be used for the methods whereas interface we need not use that keyword for the methods.
Abstract class must have subclasses whereas interface cannot have subclasses.
When should I use abstract classes and when should I use interfaces?
Use an interface if various implementations only share method signatures.
You need some classes to use some methods which you don't want to be included in the class, then you go for the interface, which makes it easy to just implement and make use of the methods defined in the interface.
Use Abstract Class if various implementations are of the same kind and use common behavior or status then abstract class is better to use. Abstract classes are an excellent way to create planned inheritance hierarchies.
When you declare a method as abstract, can other non abstract methods access it?
Yes, other non abstract methods can access a method that you declare as abstract.
Can there be an abstract class with no abstract methods in it?
Yes, there can be an abstract class without abstract methods.
What is Constructor?
A constructor is a special method whose task is to initialize the object of its class.
It is a special method because its name is the same as the class name.
They do not have any return types. They not even have void, they cannot be inherited. A constructor is called when instantiating a object with new (). Constructors can be overloaded.
Can constructor be inherited?
No, constructor cannot be inherited, though a derived class can call the base class constructor.
How are this () and super () used with constructors?
Constructors use this to refer to another constructor in the same class with a different parameter list.
Constructors use super to invoke the super class’s constructor. If a constructor uses super it must use it in the first line. Otherwise the compiler will throw.
What are the differences between Class Methods and Instance Methods?
Class methods are methods which are declared as static. The method can be called without creating an instance of the class.
Instance methods on the other hand require an instance of the class to exist before they can be called, so an instance of a class needs to be created by using the new keyword.
What are Access Specifies? What are its types?
One of the techniques in object-oriented programming is encapsulation. It concerns the hiding of data in a class and making this class available only through methods. Java allows you to control access to classes, methods, and fields via so-called access specifies.
Public- public classes, methods, and fields can be accessed from everywhere.
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.
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- If you do not set access to specific level, then such a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package.
Name some modifiers in java?
Final, Transient, abstract, public, private, etc...
What is final modifier?
A class declared as final cannot be sub classed.
A method declared as final cannot be overridden and dynamically looked up.
A field declared as final cannot change its value. Static final fields are compile-time constants.
A variable declared as final cannot change its value.
What is static block?
Static block executes exactly once when the class is first loaded into JVM. Before going to the main method the static block will execute.
What is the difference between static and non-static variables?
A static variable is associated with the class as a whole rather than with specific instances of a class. Non-static variables take on unique values with each object instance.
What is an Iterator?
The Iterator interface is used to step through the elements of a Collection.
Iterators let you process each element of a Collection.
Iterators are a generic way to go through all the elements of a Collection no matter how it is organized.
Iterator is an Interface implemented a different way for every Collection.
What are the advantages of Array List over arrays ?
It can grow dynamically
It provides more powerful insertion and search mechanisms than arrays.
What are the main Implementations of the Set interface?
Linked Hash Set
Difference between Hash Map and Hash table ?
Hash Map lets you have null values as well as one null key.
Hash Table does not allows null values as key and value.
The iterator in the Hash Map is fail-safe.
The enumerator for the Hash table is not fail-safe.
Hash Map is unsynchronized.
Hash table is synchronized.
Explain different way of using thread?
The thread could be implemented by using runnable interface or by inheriting from the Thread class.
What are pass by reference and passby value?
Pass By Reference means the passing the address itself rather than passing the value. Passby Value means passing a copy of the value to be passed.
What is Hash Map and Map?
Map is Interface and Hash map is class that implements that.
Difference between Vector and Array List?
Vector is synchronized whereas array list is not synchronized.
Difference between Swing and Awt?
AWT are heavy-weight components. Swings are light-weight components. Swing works faster than AWT.
What is the difference between a constructor and a method?
A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator.
A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.
What is Garbage Collection and how to call it explicitly?
The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used. When an object is no longer referred to by any variable, java automatically reclaims memory used by that object. This is known as garbage collection. System. gc () method may be used to call it explicitly.
What is finalize () method?
Finalize () method is used just before an object is destroyed and can be called just prior to garbage collection.
What is daemon thread and which method is used to create the daemon thread?
Daemon thread is a low priority thread which runs intermittently in the back ground doing the garbage collection operation for the java runtime system. setDaemon method is used to create a daemon thread.
What are applets?
Applets are java programs used for internet computing. Applet is a dynamic and interactive program that runs inside a web page displayed by a java capable browser.
What is serialization and deserialization?
Serialization is the process of writing the state of an object to a byte stream. Deserialization is the reverse process of restoring these objects.
What is JDBC?
JDBC stands for Java Data Base Connectivity which is a set of Java API for executing SQL statements.
What are drivers available in Java?
JDBC-ODBC Bridge driver
Native API Partly-Java driver
JDBC-Net Pure Java driver
Native-Protocol Pure Java driver
What is a subclass?
Subclass is a class that inherits from one or more classes.
What is a super class or base class?
Super class or a base class is a class from which another class inherits.
Explain Destructors in java?
Java does not have any destructors because java has garbage collection.
What is JVM?
JVM is Java Virtual Machine which is a run time environment for the compiled java class files.
What are Instance variables?
Instance variables are variables which are defined inside the class but outside the method. They will be automatically initialized.
What is a package?
Package is a collection of related classes and interfaces.
Can a Class extend more than one Class?
No. A Class can extend only one class but it can implements any number of Interfaces.
What is a Marker Interface?
Interfaces with no methods are known as marker interfaces. Marker interfaces are Serializable, Clonable, Single Thread Model, Event listener. Marker Interfaces are implemented by the classes or their super classes in order to add some functionality.
Explain about transient keyword?
Transient keyword indicates that the member variable should not be serialized.
What is Synchronization?
Synchronization is the mechanism that ensures that only one thread is accessed the resources at a time.
Synchronization is a process of controlling the access of shared resources by the multiple threads in such a manner that only one thread can access one resource at a time.
With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchonization, it is possible for one thread to modify a shared variable while another thread is in the process of using or updating same shared variable. This usually leads to significant errors.
All the best...