Top 20 Java interview Questions for Fresher in 2022

Top 20 Java interview Questions for Fresher in 2022

Q1- What is Java?

Java is a high-level, object-oriented, reliable, secured, slightly elevated, multiprocessing, and adaptable programming language. It is also platform-independent. In June 1991, James Gosling created it. Because it offers its very own JRE & API, it may also be referred to as the platforms.

Q2- Which one is superclass of all classes?

Java.lang. There is no need to upgrade the object because it serves as the base class including all Java classes. The objects is the base class for all other Java classes. Arrays are one of the several non-primitive classes that are either directly or indirectly derived from this class.

Q3- What is the static keyword?

A class level variables is made global by using the static keyword, allowing all entities to utilize the same value. It may also be used to techniques. Only static classes elements and static class methods may be accessed or called by a static method.

Note:

This topic is frequently asked in Java interview questions for beginners. Even as a beginner, you must have a strong understanding of Java’s keywords.

Q4- What is the use of Classloader in Java?

A Java program is made up of a different number of custom classes and pre-defined classes. When a program is executed, JVM is used to load all the content of that needed class and through the use of Classloader JVM, it finds that class.

There are three types of Classloaders:

  • System Class Loader

    It loads all the classes from the classpath.

  • Extension ClassLoader

    It loads all the classes from the extension directory.

  • Bootstrap Class Loader

    It loads all the pre-defined java classes.

Q5- What are finally and finalize in Java?

Finally:

Finally, a try-catch block is applied to code that should always be run even if the try-catch block throws an exception. Finally is only used to release the resources produced by the try block.

Finalize():

Finalize() is an Object class function which we can override in our classes. The garbage collector calls Finalize() to gather the trash value whenever the object receives it. Whenever a garbage value is gathered from an object, this function is usually overridden to free system resources.

Q6- What is Type casting in Java?

Casting in Java is one of the most popular subjects for interview questions. Whenever we allocate a value from one dataset to a distinct data type, the two data kinds may be incompatible and require transformation. When data types are appropriate, for as when converting an int result to a long value, Java does automated transformation and does not need typecasting. However, if the data types are incompatible, they must be transferred for conversions.

Syntax

dataTypevariablename = (dataType) variableToConvert;

Q7- What is break and continue statement?

To end a while or do-while loop, we apply the break statement. To escape the switch case, we employ a break statement within a switch statement. We could also employ the break statement to end the stacked loop.

The continue statement is utilized to terminate a for, while, or do-while loop execution. To ignore the current version of the topmost loop, we may employ the break statement with such a label.

NOTE:

The most basic programming question, not only related to the Java. If you have some knowledge of programming languages, you should know the answer to this question as it is among frequently asked Java interview questions for freshers.

Q8- What is the inner and anonymous inner class?

In Java, we may define a class within a class, which is known as nested classes. Inner classes are any non-statically nested classes. Inner classes are connected with class functions and have access to each of the outer class’s attributes and functions.

The anonymous inner class is any localized inner class that does not have a name. In such a single sentence, it is declared and implemented. Anonymous inner classes always extended or execute a class or interface. Because an unnamed inner class has no name, its function Object() cannot be created.

Q9- What is an interface?

Interfaces are a fundamental component of the Java programming language and are widely utilized in JDK, java application frameworks, as well as the majority of platforms and tools. The interfaces is a mechanism to accomplish abstractions in Java and is used to describe the contract that subclasses must follow.

The interface is a useful place to start when defining the type and establishing a top-level organization in the code. In Java, a class can implement numerous interfaces; however, in most circumstances, it is preferable to employ interfaces as a superclass.

Q10- What is aggregation in Java?

The easiest way to describe aggregated is as an asset referenced that reflects the connection between two classes and includes a connection to the class that it contains. Aggregation indicates a whole/part and has-a connection.

Example of an aggregate class, for instance. Address class data includes city, state, and pin code, whereas Employee class data includes name, age, and income. Now, it will be claimed that the Employee object contains an Address object if the Employee class is declared to hold one. Since every employee needs a place to reside, the Address object is also a component of the Employee object. As a result, the Address object is owned by the Employee object.

Q11- What is an Iterator?

Any collection can be iterated through using the iterator interface’s methods. The iterator() function allows us to retrieve an iterator object from a collection. In the Java Collection Framework, Iterator replaces Enumeration. During the repetition, the iterator lets the user delete elements from the surrounding collections. The Java Collection Iterator implements the Iterator Design Pattern and offers a general method for a collection’s cross-sectional members.

Q12- What is the Java Collections Framework?

Every programming language uses collections, and the first version of Java included just a handful classes for them: Vectors, Stacking, Array, and Hashtable. However, Java 1.2 introduced the Collection Framework, which gathered all of the collections standards, representations, and techniques for wider application and usage.

With the introduction of Generalized and Asynchronous Collections classes for thread-safe activities, Java Collection has advanced significantly. Blocking interfaces and associated Java simultaneous package representations are supplied.

Q13- What do you understand about Thread Priority?

Every thread that is created is given a priority value, and in general, the processes with higher priorities are executed first. However, this relies on how the OS’s version of the Thread Scheduling is implemented. Although we may set a thread’s priority, there is no assurance that higher importance threads will be processed before lower priority threads. The integer value of thread priority ranges from 1 to 10, with 1 denoting the lowest priority thread and 10 the highest.

Q14- How to create daemon thread in Java?

Thread class setDaemon(true) is used for creating daemon thread in Java. By invoking start(), they used to invoke that method to avoid receiving an IllegalThreadStateException.

Q15- What is ThreadLocal?

For generating thread-local variables in Java, use ThreadLocal. We are aware that even an object’s properties are shared by all threads. So, we may implement synchronization if the variable is not thread-safe. But we may utilize ThreadLocal variables if we wish to prevent synchronization.

Q16- Which is more preferred – Synchronized method or Synchronized block?

The synchronized block is recommended because it doesn’t hold the component; synchronized methods lock the object. When a class contains multiple synchronization blocks, sometimes when they are unrelated, the execution of all of them will be halted and placed in a standby mode until the lock on the object has been obtained.

Q17- What is an instanceof keyword?

We can use instanceof keyword in java to check whether an object belongs to a class or not. We should avoid much usage of it. Sample usage:

public static void main(String[] args) {

Object str = new String(“abc”);

If(strinstanceof String) {

System.out.println(“String value:” +str);

}

If(strinstanceof Integer) {

   System.out.println(“Integer value:” +str);

}

}

Every thread has its own ThreadLocal variable and they can use get() and set() methods to get the default value or change its local thread value. ThreadLocal instances are typically private static fields in classes that wish to associate the state with a thread.

Q18- What is the use of System class in Java?

This question is among the most common Java interview questions for freshers.

Java System class is one of the core classes. One of the easiest ways to log information for debugging is System.out.print() method. System class is final so we can’t subclass and override its behavior through inheritance.

System class doesn’t provide any public constructors, so we can’t instantiate this class and that’s why all of its methods are static. Some of the utility methods of System class are for array copy, get the current time, and reading environment variables.

Q19- What are the key differences between C++ and Java?

C++ and Java both are object-oriented programming languages with some differences. The interviewer may ask the difference between the two and include this in the top Java interview questions for freshers to check your basic knowledge. The differences between C++ and Java are as follows –

C++ Java
C++ is platform dependent. Java is platform-independent.
C++ writes structural programs without using classes and objects. Java is a pure object-oriented language except for the primitive variables.
C++ doesn’t support documentation comments. Java supports documentation comment (/**…*/) to create documentation for java code.
C++ fully supports pointers. In Java, there is no concept of pointers.
C++ supports multiple inheritance. Java doesn’t support multiple inheritance.

Q20- What happens when an exception is thrown by the main method?

When an exception is thrown by the main() method, Java Runtime terminates the program and print the exception message and stack trace in system console.

Copyright 1999- Ducat Creative, All rights reserved.

Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.