Top 25 Java Interview Questions and Answers in 2020

Introduction to Java

 

Java is the most widely used and deployed language in the world. Created by James Gosling, Patrick Naughton and Mike Sheridan in 1991 while they were working with Netscape Communications, Java is now one of the three core technologies in the world of web. Being the high-level language, Java has made its mark in the IT industry with its first-class functions and holds a bright future.

 

Why is Java important?

 

In Java, the primary advantage is that it eliminates the need for recompilation. It enables developers to write code that runs everywhere making it easier and feasible for them to perform their tasks.

Regardless of the architecture of a system, Java compiled code can run on any JVM (Java virtual machine). This makes it one of the foremost widely-used programming languages. Java is a class-based and object-oriented programming language.

If you are aspiring to acquire this skill and make a career as a Java developer, you must consider taking up the Java course training from a reputed training provider and get certified.

Here, in this Java Interview Questions article, we have listed out the most important Java Interview Questions and Answers which will prepare you for any Java interview process.

For professionals with good knowledge of Java programming, looking for job opportunities and interviews, we have something interesting for you.

Here’s an updated list of top Java interview questions and answers that’ll help you in Java interviews.

 

Java Interview Questions and Answers – Set 1:

 

  1. Define class in Java?

 

In Java, a class is a template used to create objects and define the data type of them. It acts as a building block for Java language-oriented system.

 

  1. Explain the difference between dynamic and static loading?

 

Static class loading involves the creation of objects and instances using new keywords and dynamic class loading is done when the name of the class is not known at compile time.

 

  1. What is multi-threading?

 

It is a programming concept that allows to run multiple tasks within one single program in a concurrent manner.

 

  1. When and by whom was Java developed?

 

Java was developed in 1995 by James Gosling.

 

Java Interview Questions and Answers – Set 2:

 

  1. What do JVM, JRE, and JDK stand for?

 

  • JVM stands for Java Virtual Machine.
  • JRE stands for Java Runtime Environment.
  • JDK stands for Java Development Kit

 

  1. Does Java use pointers?

 

No, Java doesn’t use pointers. It has tough security. Instead of pointers, references are used in Java as they are safer and secure when compared to a pointer.

 

  1. Explain the steps involved in connecting to a database in java?

 

Below are the steps to connect to a database in Java,

  • Registering the driver class
  • Creating connection
  • Creating statement
  • Executing queries
  • Closing connection

 

  1. Explain the functions of JVM and JRE?

 

The function of JVM is to offer a runtime environment for Java Byte Codes to be executed. And JRE includes the sets of files required by JVM during its runtime.

 

  1. Explain the difference between overloading and overriding?

 

Overloading occurs when there are two methods of the same name but different properties.

When there are two methods of the same name and properties, one is in the child class and the other is in the parent class, overriding occurs.

 

Java Interview Questions and Answers – Set 3:

 

  1. What is the default size of load factor in hashing based collection?

 

The default size is 0.75 and the default capacity is computed as

 

Initial capacity * Load factor.

 

 

  1. What is a package?

 

A Package is a collection of related classes and interfaces.

 

  1. What is the superclass of all exception classes?

Java.lang.Throwable is the superclass of all exception classes. In Java, all exception classes are derived from this base class.

 

  1. Explain the difference between equals() and = =?

Equals() method is used for checking equality of two objects defined by business logic.

“==” aka the equality operator is used to compare objects and primitives.

 

 

  1. State two differences between inner class and subclass

Inner classes must be in the same file whereas the subclasses can be in different files.

And subclasses have the methods of their parent class, but the inner classes can get the methods they want.

 

Java Interview Questions and Answers – Set 4:

 

  1. How are destructors defined in Java?

Java has its own garbage collection. This means that no destructors are required to be defined in Java. Destruction of objects is an automated process that is performed by the garbage collection mechanism.

 

  1. Define JSON

JSON is an acronym for JavaScript Object Notation. It uses the JavaScript syntax. The format in JSON is text only.

 

  1. What is the most important feature of Java?

The most important feature of Java is that it is a platform-independent language.

 

  1. What is an anonymous class?

The class that is defined in a single line of code using new keyword and without a name is an anonymous class.

 

  1. What is a JVM?

JVM is a Java Virtual Machine which is a runtime environment for the compiled java class files.

 

Java Interview Questions and Answers – Set 5:

 

  1. Can a dead thread be started again?

No, a thread that is in the dead state can’t be started again.

 

  1. Are arrays of primitive data types?

No. In Java, Arrays are objects.

 

  1. What are constructors in Java?

In Java, the constructor is a block of code which is used to initialize an object.

 

  1. What are the types of constructors?

There are two types of constructors:

  1. Default constructor
  2. Parameterized constructor

 

  1. Explain garbage collection in Java

In Java, when an object is longer used or referenced, garbage collection is called and the object is destroyed automatically.

 

  1. What’s the difference between Stack and Queue?

The difference between a stack and a queue is that stack is based on Last in First out (LIFO) principle and a queue is based on FIFO (First In First Out) principle.

 

Java Interview tips

 

Having discussed the list of important Java Interview Questions and Answers, here we have listed some quick basic interview tips to help you ace the Java interview,

  • Be punctual and show up on time.
  • Be confident and communicate effectively
  • Avoid using slang words and fillers.
  • Read the job description and research about the company.
  • Listen carefully and take time before answering. Make sure your answers are relevant to the questions asked.
  • Dress and behave professionally.
  • Avoid interrupting the interviewer.
  • Carry all the essential documents.
  • Understand the job requirements and explain how you can fulfill them.
  • Thank the interviewer for his/her time. Ask for feedback and be open to constructive criticism too.

 

If you want to learn Java online, you can sign up for the course on Sprintzeal’s E-learning platform and take up their Java online course to get certified.

 

Conclusion

 

JavaScript is a vast topic. The list of Java interview questions and answers could be endless. But we hope you’ve found this comprehensive list of Java interview questions and answers we made for you informative and helpful.

To ensure you are well versed with all the Java programming related concepts, you can take up Java training and add the certification to your resume. Sprintzeal is a global training provider offering a wide range of training and certifications for professionals. You can get an overview and practical knowledge of Java concepts with our Java certification training. You get trained by our industry expert and accredited trainers. And the skills acquired from our Java training will give you an edge over other candidates when you attend interviews.

For more Java training and certification related details, you can reach us at support@sprintzeal or chat with our course expert. If you’re looking for other training and certifications to enhance your career, you can explore all courses offered by us and request for a trial.

If you are encountering any difficulties with these java interview questions, feel free to reach us. Besides this Java Interview Questions Blog, if you want to gain more knowledge about the programming language, you can get in touch with our trainers and opt for the Java training program.

So this leads us to the end of the blog for Java interview questions. The questions you were exposed to in this Java Interview Questions blog remain the most commonly asked questions by the recruiters in the industry. We are certain that these sets of Java Interview Questions will be of great help to you if you are preparing for interviews.

 

/* https://www.sprintzeal.com/blog/top-25-java-interview-questions-and-answers-in-2020?fbclid=IwAR3oMU0b1SEJLrLAlj1zN4duUfJ2hOI1svjVewFqyXqME2yO-QfhOQLt944 */

How To Install Java with `apt` on Ubuntu 18.04

Introduction

Java and the JVM (Java’s virtual machine) are required for many kinds of software, including Tomcat, Jetty, Glassfish, Cassandra and Jenkins.

In this guide, you will install various versions of the Java Runtime Environment (JRE) and the Java Developer Kit (JDK) using apt . You’ll install OpenJDK as well as official packages from Oracle. You’ll then select the version you wish to use for your projects. When you’re finished, you’ll be able to use the JDK to develop software or use the Java Runtime to run software.

Prerequisites

To follow this tutorial, you will need:

Installing the Default JRE/JDK

The easiest option for installing Java is to use the version packaged with Ubuntu. By default, Ubuntu 18.04 includes Open JDK, which is an open-source variant of the JRE and JDK.

This package will install either OpenJDK 10 or 11.

  • Prior to September 2018, this will install OpenJDK 10.
  • After September 2018, this will install OpenJDK 11.

To install this version, first update the package index:

  • sudo apt update

Next, check if Java is already installed:

  • java -version

If Java is not currently installed, you’ll see the following output:

Output
Command 'java' not found, but can be installed with:

apt install default-jre
apt install openjdk-11-jre-headless
apt install openjdk-8-jre-headless
apt install openjdk-9-jre-headless

Execute the following command to install OpenJDK:

  • sudo apt install default-jre

This command will install the Java Runtime Environment (JRE). This will allow you to run almost all Java software.

Verify the installation with:

  • java -version

You’ll see the following output:

Output
openjdk version "10.0.1" 2018-04-17
OpenJDK Runtime Environment (build 10.0.1+10-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 10.0.1+10-Ubuntu-3ubuntu1, mixed mode)

You may need the Java Development Kit (JDK) in addition to the JRE in order to compile and run some specific Java-based software. To install the JDK, execute the following command, which will also install the JRE:

  • sudo apt install default-jdk

Verify that the JDK is installed by checking the version of javac, the Java compiler:

  • javac -version

You’ll see the following output:

Output
javac 10.0.1

Next, let’s look at specifying which OpenJDK version we want to install.

Installing Specific Versions of OpenJDK

While you can install the default OpenJDK package, you can also install different versions of OpenJDK.

OpenJDK 8

Java 8 is the current Long Term Support version and is still widely supported, though public maintenance ends in January 2019. To install OpenJDK 8, execute the following command:

  • sudo apt install openjdk-8-jdk

Verify that this is installed with

  • java -version

You’ll see output like this:

Output
openjdk version "1.8.0_162"
OpenJDK Runtime Environment (build 1.8.0_162-8u162-b12-1-b12)
OpenJDK 64-Bit Server VM (build 25.162-b12, mixed mode)

It is also possible to install only the JRE, which you can do by executing sudo apt install openjdk-8-jre.

OpenJDK 10/11

Ubuntu’s repositories contain a package that will install either Java 10 or 11. Prior to September 2018, this package will install OpenJDK 10. Once Java 11 is released, this package will install Java 11.

To install OpenJDK 10/11, execute the following command:

  • sudo apt install openjdk-11-jdk

To install the JRE only, use the following command:

  • sudo apt install openjdk-11-jre

Next, let’s look at how to install Oracle’s official JDK and JRE.

Installing the Oracle JDK

If you want to install the Oracle JDK, which is the official version distributed by Oracle, you’ll need to add a new package repository for the version you’d like to use.

To install Java 8, which is the latest LTS version, first add its package repository:

  • sudo add-apt-repository ppa:webupd8team/java

When you add the repository, you’ll see a message like this:

output
 Oracle Java (JDK) Installer (automatically downloads and installs Oracle JDK8). There are no actual Jav
a files in this PPA.

Important -> Why Oracle Java 7 And 6 Installers No Longer Work: http://www.webupd8.org/2017/06/why-oracl
e-java-7-and-6-installers-no.html

Update: Oracle Java 9 has reached end of life: http://www.oracle.com/technetwork/java/javase/downloads/j
dk9-downloads-3848520.html

The PPA supports Ubuntu 18.04, 17.10, 16.04, 14.04 and 12.04.

More info (and Ubuntu installation instructions):
- for Oracle Java 8: http://www.webupd8.org/2012/09/install-oracle-java-8-in-ubuntu-via-ppa.html

Debian installation instructions:
- Oracle Java 8: http://www.webupd8.org/2014/03/how-to-install-oracle-java-8-in-debian.html

For Oracle Java 10, see a different PPA: https://www.linuxuprising.com/2018/04/install-oracle-java-10-in-ubuntu-or.html

More info: https://launchpad.net/~webupd8team/+archive/ubuntu/java
Press [ENTER] to continue or Ctrl-c to cancel adding it.

Press ENTER to continue. Then update your package list:

  • sudo apt update

Once the package list updates, install Java 8:

  • sudo apt install oracle-java8-installer

Your system will download the JDK from Oracle and ask you to accept the license agreement. Accept the agreement and the JDK will install.

Now let’s look at how to select which version of Java you want to use.

Managing Java

You can have multiple Java installations on one server. You can configure which version is the default for use on the command line by using the update-alternatives command.

  • sudo update-alternatives –config java

This is what the output would look like if you’ve installed all versions of Java in this tutorial:

Output
There are 3 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1101      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1101      manual mode
  2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode
  3            /usr/lib/jvm/java-8-oracle/jre/bin/java          1081      manual mode

Choose the number associated with the Java version to use it as the default, or press ENTER to leave the current settings in place.

You can do this for other Java commands, such as the compiler (javac):

  • sudo update-alternatives –config javac

Other commands for which this command can be run include, but are not limited to: keytool, javadoc and jarsigner.

Setting the JAVA_HOME Environment Variable

Many programs written using Java use the JAVA_HOME environment variable to determine the Java installation location.

To set this environment variable, first determine where Java is installed. Use the update-alternatives command:

  • sudo update-alternatives –config java

This command shows each installation of Java along with its installation path:

Output
There are 3 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1101      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1101      manual mode
  2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode
  3            /usr/lib/jvm/java-8-oracle/jre/bin/java          1081      manual mode

Press <enter> to keep the current choice[*], or type selection number:

In this case the installation paths are as follows:

  1. OpenJDK 11 is located at /usr/lib/jvm/java-11-openjdk-amd64/bin/java.
  2. OpenJDK 8 is located at /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java.
  3. Oracle Java 8 is located at /usr/lib/jvm/java-8-oracle/jre/bin/java.

Copy the path from your preferred installation. Then open /etc/environment using nano or your favorite text editor:

  • sudo nano /etc/environment

At the end of this file, add the following line, making sure to replace the highlighted path with your own copied path:

/etc/environment
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64/bin/"

Modifying this file will set the JAVA_HOME path for all users on your system.

Save the file and exit the editor.

Now reload this file to apply the changes to your current session:

  • source /etc/environment

Verify that the environment variable is set:

  • echo $JAVA_HOME

You’ll see the path you just set:

Output
/usr/lib/jvm/java-11-openjdk-amd64/bin/

Other users will need to execute the command source /etc/environment or log out and log back in to apply this setting.

Conclusion

In this tutorial you installed multiple versions of Java and learned how to manage them. You can now install software which runs on Java, such as Tomcat, Jetty, Glassfish, Cassandra or Jenkins.

 

How to Install Grub Customizer in Ubuntu 18.04 LTS

For those who need to modify the default Grub boot-loader settings, Grub Customizer is a useful tool with a graphical user interface.

With grub customizer, you can:

  • Rearrange, add, remove, rename boot menu entries.
  • Hide or show boot menu while starting up.
  • Change the default boot entry.
  • Edit kernel parameters.
  • Change the time delay to boot the default entry.
  • Change text colors and screen background image.
  • Reinstall Grub boot-loader into Windows MBR.

grub-menu-ordergrub-generalgrub-appearance

To install Grub Customizer in Ubuntu:

The software has an official PPA repository contains the packages for all current Ubuntu releases.

1. Open terminal either via Ctrl+Alt+T or by searching for ‘terminal’ from app launcher. When it opens, run command to add the PPA:

sudo add-apt-repository ppa:danielrichter2007/grub-customizer

Input your password (no visual feedback while typing) when it prompts and hit Enter to continue.

grub-customizer-ppa

2. After added the PPA, run commands one by one to refresh package cache and install Grub Customizer:

sudo apt-get update

sudo apt-get install grub-customizer

Once installed, launch the software from your application launcher and enjoy!

Uninstall:

To remove the software, run command in terminal:

sudo apt-get remove --autoremove grub-customizer

And you can remove PPAs via Software & Updates utility under ‘Other Software’ tab.

 

/* https://tipsonubuntu.com/2018/03/11/install-grub-customizer-ubuntu-18-04-lts/ */

Windows 10 deleted GRUB [FIXED BY EXPERTS]

To fix various PC problems, we recommend this tool: This software will repair common computer errors, protect you from file loss, malware, hardware failure and optimize your PC for maximum performance. Fix PC issues and remove viruses now in 3 easy steps:
  1. Download this PC Repair Tool rated Great on TrustPilot.com.
  2. Click Start Scan to find Windows issues that could be causing PC problems.
  3. Click Repair All to fix issues with Patented Technologies (Exclusive Discount for our readers).
GRUB is a multiboot loader and an essential part of the Linux operating system, but some users on the Microsoft community and Linux forums have reported that Windows 10 has deleted their grub, thus leaving their Linux distribution unable to boot. If you are also troubled this issue, here are a couple of solutions that can help you resolve this problem.

How do I recover GRUB after installing Windows?

1. Run Boot Repair

Boot Repair Disk to install GRUB windows 10 deleted grub
  1. There are two ways to get boot-repair. First is to create a disk containing the tool like Boot-Repair-Disk. This will help you to create a disk starting Boot-Repair automatically and boot on it.
  2. The second option is to Install Boot-Repair in Ubuntu. To do this, create Ubuntu Live-session on your USB and then choose “Try Ubuntu“.
  3. Connect to the Internet.
  4. Open a new terminal, and type the following command. Press enter after running each command. sudo add-apt-repository ppa:yannubuntu/boot-repair sudo apt-get update sudo apt-get install -y boot-repair && boot-repair
  5. Now you need to launch Boot-Repair by typing boot-repair in a terminal.  You can also launch it from the Dash (Ubuntu logo at the top).
  6. Next, click the “Recommended Repair” button.
  7. Wait for the repair to finish. Note the URL that appeared on the screen.
  8. Now reboot the system and check if you can access the OS and GRUB is reinstalled.

Want to learn how to dual boot Windows 10? You won’t believe how easy it is!



2. Reinstall GRUB

grub menu windows 10 deleted grub
  1. First, boot into your system using any Live Linux Distribution.
  2. Open Gnome Disks or GParted, the partition editor.
  3. Now find the partition where your Linux system is on. The Linux system is usually in /dev/sdax path (here X means number).
  4. Now close the Disk partition tool that is running (Gnome Disks or GParted).
  5. Open a terminal and run the following command and press Enter to execute it. sudo mount /dev/sdaX /mnt && sudo mount –bind /dev /mnt/dev && sudo mount –bind /dev/pts /mnt/dev/pts && sudo mount –bind /proc /mnt/proc && sudo mount –bind /sys /mnt/sys && sudo chroot /mnt
  6. In the above command replace X with the disk number that you discovered in the step 3.
  7. Next, enter the following command and hit enter as well. grub-install /dev/sda && update-grub && exit udo umount /mnt/sys && sudo umount /mnt/proc && sudo umount /mnt/dev/pts && sudo umount /mnt/dev && sudo umount /mnt && reboot
  8. If everything goes right, this should reinstall GRUB. Reboot the computer and check if GRUB is re-installed successfully
By following any one of the methods above you should be able to fix the deleted GRUB issue. One thing to keep in mind is that if you want to dual boot on your system with Linux and Windows 10, always install Windows 10 first and then install Linux. This way Windows 10 won’t delete the GRUB by mistaking the EFI partition for its own.

// https://windowsreport.com/windows-10-deleted-grub/