Java Interview Questions with Answers

 
♦ What is Java?

Java is a general-purpose computer programming language originally developed by Sun Microsystems and released in 1995. It runs on any computer system including Windows, Macintosh Operating System and UNIX.

On what platforms can Java run?

Java is an amalgamation of OS and underlying hardware. It runs on diverse platform such as Microsoft Windows, Mac OS, and various versions of UNIX/Linux like HP-Unix, Sun Solaris, Redhat Linux, Ubuntu, CentOS, etc.

What are the key features of Java language?

Java language is

Simple & Reliable

Object Oriented programming

Distributed & multithreaded

Interpreted & Secure,

Architecture neutral & portable,

Enable High performance with dynamic language.

Why Java is Architectural Neutral?

Java helps to generate architecture-neutral object file design which allows the complied code to be used on many processors, within a definite runtime system. It is useful for both networks and single system software distribution.

♦ How Java facilitate High Performance?

It supports “just-in-time” compilers to enable high performance. It kind of compiler turns Java byte code that contains instructions which needs to be interpreted into commands to be send directly to the processor.

Is Java dynamic?

Yes, this computer-programming language can cope with the changing website requirement. The Java programs have the ability to carry huge amount of run-time information which can be used to verify & resolve entry to objects on run-time.

Why is Java Virtual Machine considered as Java’s platform independent?

Java Virtual Machine (JVM) provides a platform to execute the java file. It translates the bytecode into the machine language for a particular workstation. Without JVM, Java application cannot run.

List few best Java IDE’s of all era?

Netbeans, Android Studio, Eclipse, IntelliJ IDEA, BlueJ , jGrasp, Jdeveloper etc.

List few keywords of JAVA (other than C, C++ keywords)?

Abstract, assert, byte, final, implement, import, instanceof, interface, native, package, strictfp, super, synchronized, transient etc.

What is Object in JAVA?

It’s a runtime entity which is stored in fields. It is created using the keyword “new” in Java language. The actions of ‘Object’ are generally shown via specific methods.

What is class?

It’s a blueprint/template which is used to create single object in Java language. Both ‘Class’ and ‘Objects’ are basic concepts of Object Oriented Programming. It must be noted that all class objects should have basic class properties.

What type of variables does class comprise of?

Class in JAVA comprise of Local variable, instance variables and static/class variables.

Define Local Variable?

Local variables are confirmed within a function, and its scope is limited to that function. The variable is only accessible inside the method, block or constructor that declared it.  Local variables are not given initial default values.

Define Instance Variable?

Instance variables are declared in class level and not within methods. With keyword ‘new’ when an object is created, instance variables is created and when the same object is destroyed, instance variable is also destroyed.

♦ Define Class Variable?

Class Variable or Static variables are initialized only one time when class is loaded prior to any instance variables. It fulfills the common properties of all objects. As the term suggest, Static variable are stored in static memory.

Define Singleton class?

It’s a class that enables to create only one single instance at a time. Its purpose is to control object creation, limiting the number of objects. A technique known as lazy instantiation ensures that singleton instances are created only when needed.

Define Constructor?

In object-oriented programming constructor is a special method of a class or structure that initializes an object of that type. It is called mechanically when a new instance of an object is created.

What are the three steps for creating an Object for a class?

The vital three steps for creating ‘Object’ includes Declaration, next instantiated and after that Initialization.

Mention the default value of byte data type in Java?

Well, 0 is the default value of byte data type in Java

Mention the default value of float and double data type in Java?

The Default value of float is 0.0f while for double data, its 0.0d.

♦ What are the uses of byte datatype?

The byte data is mainly used in place of integers. It is also used for saving memory in large arrangement.

What does static variable mean?

Static variables also defined as class variables, common to all the instances (or objects) of the class. Static keyword is mainly used in Java for memory management.

♦ What is Access Modifier?

Access modifiers are a specific part of programming language used to set the accessibility of classes, methods, Variable and constructors.

What do you mean by protected access modifier?

Protected access modifier specifies those variables, methods and constructors that are only accessible by the classes of the same package and the sub-classes present in any package.

What is synchronized Non Access Modifier?

This JAVA Modifier performs much functionality other than Access Modifiers. It specifies that a method can be accessed by only one thread at a time.

♦ Which operator is given highest priority in Java?

Postfix operators (expr++, expr–) is known to have highest preference in Java.

In which datatypes Variables used in a switch statement can work?

Variables used in switch statement can work with byte, char , short , int primitive data types ,String class and a few  special classes.

When parseInt() method is  used?

ParseInt() method is used to convert ‘String to Integer’ in Java , It is a technique of finding the primitive data type of a certain String.

Why String class is called immutable?

String class provides no methods to modify String object, once created. It only allows creating new String objects based on content of existing ones, therefore, it’s termed as immutable.

Why StringBuffer is called mutable?

Unlike String Class, StringBuffer can change the value of the object, so it is called mutable.

♦ What is the distinction between StringBuilder and StringBuffer class?

StringBuilder is faster than StringBuffer class. Both follow same methods but the difference is that StringBuffer is thread safe and synchronized.

Mention the package used for pattern matching with regular expressions?

java.util.regex package is considered in this case

Name the classes java.util.regex consist of?

Java.util.regex package offers three classes – Matcher class, Pattern class and PatternSyntaxException class.

Define finalize() method?

Finalize() method  is used to carry out some final operations or clean up operations on an object prior to final destruction from the memory.

Define Exception?

An Exception is an event that occurs throughout the execution of program which disrupts the normal flow of the program’s command. It is managed by exception handler.

What is Checked Exceptions?

It is a kind of Exceptions which occur frequently in a program and thus compiler mandate that such exception be detected at compiled time, so that it can be handled or declared as thrown using throws keyword.

What is Runtime Exceptions?

Also known as Unchecked exceptions, this event are anticipated to crash or break down the program or application when they arise. It is generally overlooked at the time of compilation.

Mention two subclasses under Exception class?

RuntimeException class and IOException class.

When is throws keyword used?

Such keywords are used to provide information to caller about the exception of the method. It is required only for checked exception, convince compiler about the usage of keyword to prevent abnormal termination of program.

When throw keyword is used?

When a method cannot handle the exception using catch block, it declares the exception as thrown. Hence, throw keyword is used openly to throw an exception from any block of code or method.

♦ Use of ‘Finally’ keyword in exception handling

It is used to create a block of code whether an exception has occurred or not, It always appears at the end of try or catch block.

♦ Things to keep in mind while creating ‘Exceptions’ in Java?

During the creation of ‘Exceptions’ in Java

ο The entire exceptions must be a child of Throwable.

ο We have to extend the Runtime Exception class in case of writing a runtime exception.

♦ Explain Inheritance?

It is a method in which a new class is derived from an existing class by extending its common data members and methods. It allows reuse of code which improves reusing the fields and methods of existing class.

♦ Use of ‘Super keyword’

It is used to invoke a method from the parent class when it gets overridden in the subclass.

♦ Define  Polymorphism?

Polymorphism is the ability to permit an entity such as a variable, a function, or an object to have more than one form. The most frequent use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.

♦ Define Abstraction?

Abstraction is the process to show the essential features of the object, it deals with the outside view of an object (interface) and hide the implementation details. It improves the maintainability of the system.

♦ Define Abstract class?

Abstract class is one of the fundamental concepts in OOP declared with an abstract keyword, It cannot be instantiated instead it can have final methods which force the subclass not to change the body of the method.

♦ When is Abstract methods used?

Abstract methods simply describe a contract that derived classes must implement. We can declare the method in the parent class as abstract and use the concept of code reusability if we want a class to contain a particular method.

♦ Define Encapsulation?

Encapsulation is the technique of wrapping code and data together into a single unit to provide the code flexibility, security, and its easy maintainability. It is a combination of both Abstraction and data hiding.

♦ Benefit of Encapsulation

In OOP language, Encapsulation provides easy maintainability, flexibility and security to our code. It also has the facility to modify the implemented code without breaking the code of others who use our code.

♦ Define Interface?

Interface is a process to support the functionality of multiple inheritances. It is a technique to achieve abstraction.  Interfaces can have abstract methods and variables but cannot have a method body.

♦ Mention various features of Interface?

Various features of Interface are

ο It allow several inheritance

ο It is used to implement abstraction.

ο It is more flexible as it can extend multiple interfaces.

ο It cannot be instantiated.

♦ What do you mean by Packages in Java?

A Package in Java is a method to sum up a group of classes, interfaces, and sub packages. It has two types: Built-in packages and User defined package.

♦ What are the uses of Packages in Java?

Package in Java are used for managing name conflicts, data-hiding or data encapsulation, It provide restricted access and make  smooth functioning of usage of classes, interfaces, searching, locating enumerations and annotations.

♦ Define Multithreaded program?

Multithreaded program is concurrent implementation of two or more parts of a program for maximum utilization of CPU .All part of such program is called a thread that shares the process resources but can execute independently.

♦ Mention two ways in which Thread can be created?

Two ways in which Thread can be created are

ο Extending the Thread class itself, then override its run()method.

ο Implementing Runnable interface to the constructor of Thread, then call start().

♦ Define an applet?

An applet is a programming language for the web browser; which can be downloaded by any computer. It is used to make the web site more dynamic and entertaining.

♦ Which classes does an applet extend?

An applet can extends java.applet.Applet class or the java.swing.JApplet class.

♦ What is ‘garbage collection’ in Java?

Garbage collection in Java is a form of automatic memory management. It runs on the Java Virtual Machine which cleans those objects which are not being used by a Java application anymore.

♦ What is immutable object?

Immutable object are those object which cannot be changed after it is constructed. It is mainly useful in concurrent applications.

♦ Explicate the usage of this () with constructors?

It is used initialize a newly created object which is called just after the memory is allocated for the object. It constructs the values i.e. provides data for the object.

♦ What is Set Interface?

Set Interface is a collection of unordered objects which cannot store duplicate values. It has various methods to add, remove clear, size, etc to improve the usage of the interface.

♦ What is TreeSet?

TreeSet is a part of sorted collections of Java where the elements are arranged according to their natural ordering. It implements java.util. Set interface which doesn’t allow any duplicates.

♦ Define Comparable Interface?

Comparable Interface is mostly used to sort the arrays (or lists) of custom objects using Collections.sort (and Arrays.sort). It is used to order the objects of user-defined class.

♦ Distinguish between throw and throws?

The difference between throw and throws are

ο Throw is used to generate an exception whereas throws is used to declare an exception.

ο We can throw either checked or unchecked whereas throws keyword is necessary only for checked exception.