Loading…
Java interview questions

An Overview of Java Programming Language

Java is a computing platform as well as a programming language. You can download it for free. An object-oriented language, Java is a fast, secure and easily extendible language. Java is said to be more dynamic than C or C++ language because of its adaptable design.

Unlike many other programming languages, Java is a platform independent and Architecture-neutral language. Our massive collection of core java interview questions will definitely help you find a great job.

Development History of Java Programming Language

Sun Microsystems released its first version in 1995 as a core component of its platform. Majority of candidates are asked this information in advanced java interview questions.

Latest Version: The latest version is Java SE 11 (18.9 LTS), which was released in September 2018. This information is critical if you are preparing for Java interview questions.

Advantages of Java

  • Simple and easy to learn
  • Robust and multithreaded
  • Enables high performance
  • Follows WORA (write once, run anywhere) approach

Java interview questions and answers

Looking for a new job? Do not miss to read our Java interview questions and answers. Whether you are a fresher or an experienced, these questions and answers that can help you to crack your interview.

It is one endpoint of two-way communication between programs that are running on the same network.

A socket is tied to a port number in such a manner that the TCP layer can recognize the application where the data needs to be sent. If you use Socket instead of native code, your programs will communicate platform-independently.

It refers to writing programs or applications that run across multiple devices when the devices are connected on a network.

The java.net package contains a collection of interfaces and classes that communicate and allow programmers to write programs that focus on solving the current problem.

In Java, there are three ways to generate random numbers:

  • java.util.Random class
  • Math.random method
  • ThreadLocalRandom class

Example

                                                

This is done Using java.util.Random class:
import java.util.Random;
Random rand = new Random();
int n = rand.nextInt(100) + 1;
//100 is the maximum and the 1 is our minimum.

For copying an object in Java, there are two ways- shallow copy and deep copy.

  Shallow Copy Deep Copy
1. When you want to copy only field values All the objects are deeply copied
2. Copy is dependant on original object Copy is not dependent on earlier objects
  Stack Queue
1. Objects accessed on Last In First Out (LIFO). Objects accessed on First In First Out (FIFO)
2. Object pushed on top of collection Object inserted at the end
3. Object removed from the top Object removed from the beginning.
4. Two operations are called push and pop Two operations are called enqueue and dequeue

In Java, there are various states of a thread. But at any point of time, it can exist in any one of these states:

  • New
  • Runnable
  • Blocked
  • Waiting
  • Timed Waiting
  • Terminated

An anonymous class is like a local class or an inner class, but without a name. You can use an anonymous class for declaring and instantiating a class simultaneously.

Java is a computing platform as well as a programming language. An object-oriented language, Java is a fast, secure and easily extendible language. Java is a platform independent and Architecture-neutral language.

It is used to create complete & powerful applications that may run on a single computer or be distributed among clients and servers in a network. I also can be used on the intranet applications.

  • Simple and easy to learn
  • Platform Independent
  • Supports is Object Oriented Programming language
  • Robust and multithreaded
  • Enables high performance
  • Java Interpreted
  • Secure and its interpreter
  • Follows WORA (write once, run anywhere) approach
  C++ Java
1. Follows WOCA approach (Write once, compile anywhere). Follows WORA approach (Write once, run anywhere).
2. Runs as code on native machine. Runs on virtual machines.
3. Multiple binary compatibility standards are included. Has single binary compatibility standard.
4. All types of Pointers and pass-by-value are supported. Primitive and reference types are only passed by value.

Java Programming language was created by James Gosling at sun micro systems in 1991.

It is a virtual machine that enables computers to run Java programs in addition to programs written in other languages but are compiled to Java bytecode. OpenJDK project has developed the Java virtual machine (JVM) reference as open source code. The reference includes a JIT compiler which is known as HotSpot.

  Java Development Kit (JDK) JVM JRE
1. Core component of Java Environment Converts Byte code to machine-specific code Implementation of JVM.
2. Provides tools and executables required for a Java Program Platform dependent Provides a platform for executing Java programs.
3. It is platform specific Provides core functions like garbage collection, memory management and security etc. Consists of java binaries and classes.

Class loaders are responsible for dynamically loading the classes during the runtime to the JVM. They are a part of the JRE.

Three types of Class Loaders are built-in - Bootstrap, Extensions and System.

Following are the advantages:

  • Allow you to define packages for bundling a group of classes or interfaces.
  • No name conflicts with other package names.
  • Packages make access control easier
  • Packages make access control easier
  Object-Based Languages Object-Oriented Languages
1. Does not support inheritance or polymorphism. Supports polymorphism and inheritance
2. Does not support built-in objects. Support built-in objects.
3. Examples - Javascript and VB Examples - C# and Java
  Local Variables Instance Variables
1. Declared in constructor, method or block. Declared in a class
2. Not possible to use access modifiers Access modifiers can be given
3. Visible within the declared method Visible for all methods
4. No default value Have default values

It is a prototype or user-defined blueprint that is used to create objects or data types. A class represents properties or methods that are common to objects of one type. All class objects possess the class properties.

It is the core unit of Object-Oriented Programming and represents real entities. As we know, a Java program creates objects that interact by invoking various methods.

An object consists of state, behavior, and identity.

The constructor is a method used to initialize an object. A normal java method has return type but the constructor does not have any explicit return type. It is called during the object creation time.

There is two type of Constructors - Default or no-arg Constructor, Parameterized Constructor.

It is a reference variable that refers to the current object. It can be used as follows:

  • To refer instance variable of class
  • To invoke class constructor
  • Passed as an argument in method call
  • Passed as an argument in the constructor call
  • Used to return a class instance

It is the process when one class acquires the properties or methods or fields of another class. This process is used to arrange and manage information in hierarchical order. The class that inherits the properties is called subclass. The class whose properties get inherited is called superclass.

It is one of the features of object-oriented programming that allows the developers to perform one action in different ways. Developers often use Polymorphism in referencing a parent class to a child class object.

Method overriding is a feature in object-oriented programming that allows a child class to give a specific implementation method that is already given by its parent classes.

Method Overloading allows a class to have more than one methods of having the same name, but only if the argument lists are varying. This feature is similar to constructor overloading feature that allows more than one constructor to a class only if their argument lists are different.

Similar to class, an interface in Java is a reference type. An interface contains abstract methods, constants, static methods, default methods and nested types.

A class implements an interface and inherits the abstract methods of the same interface. Writing an interface is like writing a class.

It is a class that is declared with an abstract keyword. An abstract class can have both abstract and non-abstract methods and needs to be extended. In this class, the method must be implemented and cannot be instantiated.

  Array ArrayList
1. Basic functionality Part of collection framework.
2. Fixed sized Dynamic sized
3. Contain primitive data types as well as objects of a class Only supports object entries
  Hashmap Hashtable
1. Nonsynchronized Synchronized
2. Not thread safe Thread-safe
3. Cannot be shared between threads without synchronization code Can be shared with many threads
4. Allows null key and multiple null values Does not allow null key or value
  HashSet TreeSet
1. Offers constant time cost Offers log time cost
2. Does not maintain the order of elements By default sorts elements in ascending order.
3. Implemented using a hash table Implemented using Binary Search Tree

A Collection in Java is a group of individual objects that are represented as a single unit. The Collection Framework in Java defines classes and interfaces that represent a group of objects as a single unit.

The two main root interfaces are Collection interface and Map interface.

By being platform independent, we mean that the java source code can be run on all OS. The presence of a machine independent code called Byte Code, which once compiled, can be run on any platform, makes Java platform independent.

The compiler feature in Java converts or translates the high-level language into a common format understood by all the machines. The only requirement is the availability of JVM.

  Heap space Stack Memory
1. Used to allocate memory Used for execution of the thread
2. Instance variables are created Local variables are created
3. Contains Objects and reference variables Contains methods and local variables
  Interface Abstract class
1. Can only have abstract methods Can have abstract, non-abstract methods
2. Variables are by default final. May contain non-final variables.
3. Only has static and final variables Can have non-final, final, non-static and static variables

This is a Java programming class that runs on Java-enabled server or application server and extends the capabilities of servers that host applications.

A servlet possesses all the features of Java such as platform independence, portability, security and database connectivity.

JIT compiler is a component of JRE that improves the performance of applications.

JIT compiler improve the performance of JVM by compiling bytecode into machine code at runtime. Choosing the right compiler is one of the first and biggest decisions to be made when you are running a Java application.

Applet is a Java program that gets embedded into web pages using the APPLET or OBJECT tag and runs inside the browser.

It works at the client side, and is hosted on a web server. Applets make websites dynamic and entertaining.

This is a good question concerning core java interview questions for experienced professionals. finalize method is called by the Garbage Collector before performing the clean-up activity. Clean-up activity refers to the process of de-allocating all the resources, such as database connection and network connection, associated with an object.

instanceof operator is used in object reference variables. In this, the operator will check the class type or interface type of an object.

It is written as follows:

( Object reference variable ) instanceof (class/interface type)

By Read-only class we are referring to the "IMMUTABLE" concept. You can begin by defining a class so that no methods can cause changes to the internal state.

In such classes, aliasing will have no impact because its internal state is read-only.

Here are the steps for creating a package:

  • Choose a name for the package
  • On top of every source file, put the package statement with the package name. Source file must contain classes and types that you want in the package.
  • Package statement is the first line of source file.
  • You can only have one package statement in each file
  • It applies to all file types.

Exception handling allows developers to handle runtime errors caused by exceptions. One way to handle exceptions is to print a simple message for users that help them correct the error that occurs due to bad data sent by the user.

Exception handling ensures the flow of the application does not break.

  Throw Throws
1. Used to throw an exception. Used to declare an exception
2. Followed by Exception class instance Followed by exception class names
3. Used in the method body Used in method signature

System class has useful fields, such as static members, that are related to the environment. This class is provided with standard streams like input, output and error.

These streams can be used to access the properties and environment variables that are externally defined.

Ask Question