Home > Java > javaTutorial > Java Interview Questions

Java Interview Questions

WBOY
Release: 2024-08-30 16:28:56
Original
579 people have browsed it

If you are looking for a Java-related job, you must prepare for the Java Interview Questions For Fresher Applicants. Though every interview is different and the job scope is also different, we can help you with the top 2023 Java Interview Questions and Answers, which will help you take the leap and succeed in your Java Interview.

Part 1 – Java Interview Questions (Basic)

This first part covers basic interview questions and answers.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

1. Difference between String Buffer and String Builder in Java

Answer:

This is one of the favorite questions of interviewers; however, frequently answered miserably by the candidates.

Here are a few notable differences between String Buffer and String Builder for a better understanding.

2. StringBuilder is a non-synchronized version of the StringBuffer class. Methods in StringBuilder

E.g., all overloaded version of the append () method is not synchronized.

  1. StringBuilder works faster than StringBuffer because of the no overhead of acquiring and releasing locks associated with synchronized methods.
  2. StringBuffer is considered thread-safe, whereas StringBuilder is not. It is recommended to use the StringBuffer class when synchronization is required. Instances of StringBuilder should not be shared between multiple threads.
  3. StringBuffer is the old class; it’s incorporated in JDK from its very first release, while StringBuilder is a relatively newer class.

3. Java program to find the most prominent and smallest number in an integer array

Answer:

A simple 5-pointer to answer this question

  • Create a Java source file named MaximumMinimumArrayDemo.java and copy the code to compile and execute in your favorite IDE.
  • Create a method called largest and smallest (int [] numbers) to print the most prominent and smallest number of the int array passed to the program.
  • Use the largest and smallest variables to store the maximum and minimum values from the array. Initially, the largest is initialized with Integer.MIN_VALUE and smallest are initialized with Integer.MAX_VALUE.
  • To all the iterations of the loop, compare the current number with largest to smallest and update them accordingly.
  • If a number is more significant than the largest, it can’t be smaller than the smallest, which means you don’t need to check if the first condition is accurate; that’s why we have used an if-else code block, where the else part will only execute if the first condition is not valid.

The logic to find the largest element from an array in Java is given below.

Java Interview Questions

Instead of assigning a variable with Integer.MAX_VALUE, we have assigned the first element from the array.

3. What is the difference between C++ & Java?

Answer:
C++ and Java are only similar in syntactical comparisons. Stated below are the differences:

  • Java is multithreaded
  • Java has no pointers
  • Java has automatic memory management (garbage collection)
  • Java is platform-independent
  • Java has built-in support for comment documentation
  • Java has no operator overloading

Java doesn’t provide multiple inheritances

  • There are no destructors in Java

Part 2 – Java Interview Questions (Advanced)

Let us now have a look at the advanced Interview Questions.

4. What is the Difference between Abstract class and Interface?

Answer:

  • Multiple Inheritance: An abstract class can inherit only one abstract class; however, a class may implement several interfaces in the interface. This proves Interface supports Multiple Inheritance, whereas the Abstract class does not.
  • Implementation: An abstract class can provide default code as it contains incomplete and complete members, whereas an interface cannot provide any code as it contains an incomplete member.
  • Fields: We can define fields and constraints in an Abstract class, whereas no fields can be defined in an interface.
  • Speed: An abstract class is fast when compared to an interface, as the latter requires more time to find the method to its corresponding class.
  • Usage: An abstract class comes into the picture when we want to share a common functionality in a parent-child relationship, whereas Interface is used to define and enforce polymorphism, decoupling, and standardization

5. Difference between private, protected, public, and package modifiers or keywords in Java

Answer:
private vs public vs protected vs package in Java

Java has four access modifiers, namely, private, protected, public, and package level.

These access modifiers function to restrict the accessibility of a class, method, or variable to which it applies. Described below are the functionalities of each access modifier and how different they are from one another

Private keyword in Java

  • The private modifier in Java can be applied to the member field, method, or nested class in Java.
  • One cannot use the private modifier on a top-level class.
  • Private variables, methods, and class are only accessible on the class on which they are declared.
  • Privates the highest form of Encapsulation Java API provides and should be used as much as possible.

It’s the best coding practice in Java to declare a variable private by default. A private method can only be called from the class where it has been declared.

Package or default access level in Java

  • The package is a keyword that is used to declare a package in Java; a package is a directory to which a class in Java belongs.
  • Package or default access level is the second-highest restrictive access modifier after private, and any variable, method, or class declared as package-private is only accessible on the package it belongs to.

The good thing about the default modifier is that the top-level class can also be package-private if there is no class-level access modifier.

Protected keyword in Java

If you declare a variable protected means, anyone can use it if they extend your class. The top-level class cannot be made protected as well.

Public keyword in Java

The public is the least restrictive access modifier in the Java programming language, and its bad practice to declare a field, method, or class by default public because once you make it public, it’s very difficult to make any change on the internal structure of the class as it affects all clients using it.

Making class or instance variables, the public also violated the principle of Encapsulation, which is not good at all and affects maintenance badly.

Recommended Article

We hope that this EDUCBA information on “Java Interview Questions” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. Java Developer Interview Questions
  2. Java Multithreading Interview Questions
  3. J2EE Interview Questions
  4. Java String interview question

The above is the detailed content of Java Interview Questions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template