Java Interview Questions Part 1

Below is a list of the 15 most common Java interview questions part 1. Depending on experience, all of the questions below will get asked in 99% of cases.

Java is an object-oriented language because everything depends on objects.

Java helps us create robust, large, high-performance, and scalable applications.

What is final, finally and finalize

Final is a java keyword used as a non-access modifier with Classes, attributes and methods. After making class as final will not allow us to inherit the class. Method as final will not allow us override in child class. Making any variable as final will not allow us to override the value.

Finally block used with try block to execute statement. It is mainly use for closing an open connections. Finally will always get call either exception is thrown or not. finally block can be used with or without catch block.

finalize() method used to perform cleanup activity and destroy objects. It will totally depend on the garbage collector when to run. An object will be ready for garbage collection if it is no longer in use. Anonymous objects are always ready for garbage collection.

difference between throw and throws

Garbage Collection

When the JVM starts up, it creates a heap memory area, which is also called a dynamic and runtime data area. All the objects and instances will be stored in the heap area.

Garbage collection works well for managing heap memory effectively. The garbage collector collects all the unreferenced data, destroys it and frees the memory.

system.gc() can be use to request the garbage collector to run.

What is Marker interface

It is an empty interface having no fields and methods inside it and give a special definition to a class. Serializable and Cloneable interface are the example of marker interface. Cloneable

interface belongs to java.lang.Cloneable and and serializable interface belongs to java.io.Serializable.

Syntax:

Public interface  {
// having no fields and methods
}

Lambda Expression

Lambda expression introduced in java 8 and as an anonymous function it helps us to write short and clean code. Functional interface having single abstract method helps us to implement lambda expression.

Characteristics of lambda expression:

  • It is option to declare parameter type declaration while writing lambda expression.
  • No need to write circular brackets if we have single parameter.
  • No need to write circular brackets if we have single parameter.
  • return key word is optional if we don’t have curly braces or it is a single line expression.

Lambda Expression with no function argument and no return type:

Below is the example of Lambda expression having no parameters and function return type.

OUTPUT:

Name Rida

Iterator vs ListIterator

Equals and Hashcode contract

This is an important question ask in most of the Java interview. Follow blog to read in detail.

Abstract Class

A class contains abstract keyword in declaration known as Abstract class.

// abstract class
abstract class TestClass {

}

Rule:

  1. Abstract class may or may not have abstract functions.
  2. If any method defined in the class as abstract than the class must have abstract keyword.
  3. Abstract class cannot be instantiated.
  4. Abstract class need to inherit in other class for usage.
  5. The child class which is implementing Abstract class need to declare itself as abstract or implement all abstract methods to instantiate that class.
  6. An abstract class can have data member, abstract method, define method(non abstract method) and constructor.

Default Method

Java 8 introduces a new technique where it allows us to write implementation of default method in interfaces.

Single interface can have any number of default method implementation.

It provides us the backward compatibility to add some functionality for existing interfaces without changing existing logic of classes implementation the same interface.

For example, if we have an interface and same is

implementing by multiple classes. After adding a default method or methods will not force us to update the existing classes implementing the same interface.

Syntax:

equals() and ==

What is ByteCode and why Java is platform independent ?

Java also called as platform independent because of Bytecode.

The Bytecode gets generate in the form of .class file as soon as the Java program gets compiled. Bytecode is an instruction for JVM to covert it in to machine language. The

Bytecode makes java to write once, run anywhere (WORA).

Java is platform independent. Because the Java compiler converts the source code to bytecode, which is Intermediate Language called as bytecode which can be executed on any platform or operating system.

Bytecode alone is of no use and non-runnable as it requires an interpreter (JVM) to run on any machine.

Runtime polymorphism (function overriding):

It is also called as dynamic polymorphism or function overriding.

Function having same name with exactly same number and sequence of parameters defined in both parent and child class is called as function overriding.

Process where call to overridden method will get resolves only at dynamic or run time.

Function overriding Rules:

  1. Method name must be same in both parent and child class.
  2. Method should have same number and sequence of parameter.
  3. There must be an IS-A relationship (inheritance).

Example:

OUTPUT:

This is Audi car engine

Difference between Java method and constructor?

What is List, Map and Set

List is an interface belongs to java.util package and it is a child interface of Collection interface. Elements maintain insertion order and can be access by using index. List items can be accessed in both forward and backward direction.

Map is an interface that belongs to java.util package and stores data as key value pair. It allows single null key and multiple null values. It doesn’t maintain any insertion order.

Set is an interface that belongs to java.util package. Set allows only single null value as well as unique value to store. It doesn’t maintain any insertion order.

Why Java is not 100% Object Oriented ?

Java is not 100% object-oriented because of primitive data types int, char, float, etc. as they are not object-oriented. The primitive data types are faster than objects, and to achieve faster execution of Java programs, they kept the primitive data types as it is !

Imran Khan

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

📝 Blogs

📧 Imran Khan