Java Interview Preparation Part3

Below is a list of the 15 most common Java interview preparation part3. This blog will help to clear doubts, build logic and revise concepts around Java.

Below are the interview questions which gets asked frequently.

Part1part2 and current part3 together will help us to cover most of the interview question.

What is Singleton design pattern ?

Singleton is a design pattern where a class can have only one instance per application. Logging, driver objects, caching, etc. are some of the examples of Singleton design pattern.

Follow below steps to create singleton class.

  1. Create static object of Connection class.
  2. Declare default constructor as private which will help us for not to create an instance outside of Singleton class.
  3. Now, create static method to create and return of an instance of class.
  4. Return private instance variable created at line number 6 as part of static method.

OUTPUT
true

Output is true as this class is singleton and will always return same instance.

What is this keyword?

this keyword is used to access instance attributes and methods of current class and it doesn’t work inside static method and block. This keyword can be pass as a parameter in the method and constructor.

As this keyword is final, we cannot assign any value to this keyword.

What is super keyword ?

super keyword use to access attributes, methods and constructor of super class. super keyword must be the first statement in block of code.

What is ByteCode?

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.

Difference between Break and Continue

What are main pillars of Java ?

There are four main pillars of Java as mentioned below:

  • Abstraction
  • Polymorphism
  • Inheritance
  • Encapsulation

What is Cloneable interface ?

Cloning is similar to making a copy of an object. Below is the Employee class example where it has two fields with name and age stored in emp1 object. The clone() method will help us to make a copy of emp1 to emp2 with all field values.

Example :

OUTPUT:

Note: Accessing clone() method without implementing cloneable interface will throw CloneNotSupportedException exception

HashMap vs Hashtable

What is Lombok

Lombok is a Java library which helps us to minimize boilerplate code.

Java compiler helps us to generate or include boilerplate code at compile time based on annotations we include. Lombok will not generate boilerplate code at run time if we have already written code manually in class. e.g. @ToString Lombok annotation will not create boilerplate code if toString() method is already written in java class.

Below is an example of Student.java class having Lombok annotations such as @ToString, @Getter, @Setter, @NoArgsConstructor, @AllArgsConstructor.

Follow link to read more about Lombok.

What is reflection in Java?

Java Reflection helps us to change class behavior at run time. java.lang.Class methods allow us to get information or metadata related to class.

java.lang.reflect package provides us the capability to work with java reflection.

Below is reflection example to access private field name from Student class.

OUTPUT
Name =John

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