#20 Top Interview Questions in Java | java technical interview questions and Answers

Java is a programming language that is used to create applications. It is one of the most popular programming languages in the world. Java has been around for a long time and it has been updated many times over the years.

The interview questions in this section are designed to test your knowledge of Java and its features.

Q1. What are the differences between C++ and Java?

C++ Java
C++ is not platform-independent; the principle behind C++ programming is “write once, compile anywhere.” In contrast, because the byte code generated by the Java compiler is platform-independent, it can run on any machine, Java programs are written once and run everywhere.
C++ is a programming language that is based on the C programming language. Most other high-level languages are compatible with C++. Most of the languages of Java are incompatible. Java is comparable to those of C and C++.
Java refers to a compiled and interpreted language. In contrast, C++ is only a compiled language. In Java, the source code is the compiled output is a platform-independent byte code.

Q2. Explain JDK, JRE, and JVM?

JDK: It stands for Java Development Kit. and is the tool necessary to compile, document, and package Java programs

JRE: It stands for Java Runtime Environment. and It refers to a runtime environment in which Java bytecode can be executed.

JVM: It stands for Java Runtime Environment. an abstract machine. It is a specification that provides a run-time environment in which java bytecode can be executed.

Q3.Why is Java a platform-independent language?

Java language was developed in such a way that it does not depend on any hardware or software because the compiler compiles the code and then converts it to platform-independent byte code which can be run on multiple systems.

Q4. What is a ClassLoader?

Class loaders are a mechanism for loading classes from a particular location. There are several class loaders available, including the Java ClassLoader, the native C++ ClassLoader, and the .NET ClassLoader.

A class loader can be used to load classes from a local filesystem location, a network location, or a remote server. A class loader can also be used to load classes from a pack file.

Q5. Which class is the superclass for every class?

When you create a class, you are creating a new type of object. Classes are the superclass for all other types of objects in Java. This means that any object you create can be considered a descendant of a class.

Q6. What is the “this” keyword in java?

The this keyword refers to the current object in a method or constructor. The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter).

Q7. Pointers are used in C/ C++. Why does Java not make use of pointers?

Java does not use pointers because using pointers the memory area can be directly accessed, which is a security issue. pointers need so memory spaces at the runtime. to reduce the usage of memory spaces java does not support pointers
Q8. What are the Memory Allocations available in Java?

The Memory allocation in java is divided into parts, namely Heap, Stack, Code, and Static.
Q9. Why Java is not 100% Object-oriented?
Java is not fully object-oriented because it supports primitive data types like it, byte, long, etc., which are not objects. Because in JAVA we use data types like int, float, double, etc which are not object-oriented, and of course is what the opposite of OOP is. That is why JAVA is not 100% objected oriented.

Q10. What do you mean by data encapsulation?

By definition, encapsulation describes the idea of bundling data and methods that work on that data within one unit, like a class in Java. This concept is also often used to hide the internal representation or state of an object from the outside. This is called information hiding.
Q11. How is an infinite loop declared in Java?

infinite loop in java example.
Some of the common codes that will result in an infinite loop are:
  1. use of for(;;){//your code} in the code.
  2. use of while(true){//your code} in the code.
  3. use of do-while(true){//your code} in the code

Q12. Which class is the superclass for every class?

The class named Object is the superclass of every class in Java.

 Q13. Can we override private methods in Java? 

In Java, methods declared as private can never be overridden, they are bounded during compile time.

Q14. What are constructors in Java?

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created.

Q15.What is Object Cloning?

Object cloning refers to the creation of an exact copy of an object. It creates a new instance of the class of the current object and initializes all its fields with exactly the contents of the corresponding fields of this object. Using Assignment Operator to create a copy of the reference variable.

Q16. What are the differences between HashMap and Hashtable in Java?

Hashmap:1.No method is synchronized.

2. Threads are not required to wait and hence relatively performance is high.

3. It is introduced in the 1.2 version.

HashTable:1.Every method is synchronized.

2. It increases the waiting time of the thread and hence performance is low.

3. It is introduced in the 1.0 version.

Q17. How is the creation of a String using new() different from that of a literal?

When we create a String object using the new() operator, it always creates a new object in heap memory. On the other hand, if we create an object using String literal syntax e.g. “Baeldung”, it may return an existing object from the String pool, if it already exists.

Q18. What is a “super” keyword in java?

The super keyword refers to superclass (parent) objects. It is used to call superclass methods, and to access the superclass constructor. The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name.

Q19. Can the static methods be overloaded?

Can we overload static methods? The answer is ‘Yes‘. We can have two or more static methods with the same name, but differences in input parameters.

Q20.What is the main objective of garbage collection?

The basic principles of garbage collection are to find data objects in a program that cannot be accessed in the future, and to reclaim the resources used by those objects.

Leave a Reply

Your email address will not be published.