Common Jobs

Common Jobs Logo

Basic Core Java Interview Questions and Answers

Basic Core java interview quentions and answer: The term “Core Java” describes the basic and necessary elements of the Java programming language. It includes all of the fundamental tools, frameworks, and ideas that underpin Java development. Any Java developer must comprehend Core Java since it offers the foundation for developing reliable and effective Java applications. Here are a few essential interview quentions and answerof Core Java:

1)Explain about instance of operator in java?

To determine whether an object is an instance of a specific class, a subclass, or a class that implements a specific interface, use Java’s instanceof operator. This operator is binary and yields a boolean value that indicates if the item on the left is an instance of the class or interface on the right.

  • To determine the type of object, use the instanceof operator.
  • Syntax: instance of with
  • If the reference expression is a subtype of the destination type, then instanceof returns true.
  • In case the reference expression is null, Instanceof returns false.

Example : public classInstanceOfExample {public static voidmain(String[] args) {Integer a =
newInteger(5);if (a instanceof java.lang.Integer) {
} else {

  • Since a is integer object it returns true.
  • There will be a compile time check whether reference expression is subtype of destination type. If it is not
  • a subtype then compile time error will be shown as Incompatible types
02) Can we have multiple classes in single file ?
  • Yes, it is possible to have numerous classes in a single file, however doing so is uncommon and not advised. Although we can have several classes in a file, we can only make one public. Attempting to make two classes in File public results in the compilation error that follows.
  • “The public type must be defined in its own file”.
03) What are identifiers in java?

A programming element, such as a variable, method, class, or package, is called an identifier in Java. In a Java program, identifiers are used to distinguish and uniquely identify certain elements. The following are some essential guidelines and traits of Java identifiers:

Most tricky Java Interview Questions?

Rules for defining identifiers in java:

  • Identifiers have to begin with a letter, dollar ($) sign, or Underscore.
  • Numbers cannot be used as identifiers.
  • The identification can include as many characters as you like, although more than 15 is not advised.
  • Case matters when using Java IDs.
  • The initial character may be an alphabet, an underscore, or a dollar sign. Numbers can be obtained from the second letter.
  • Reserve words for IDs in Java should not be used.
04) What are access modifiers in java?
  • Access control is one of encapsulation’s key components. We can misuse classes, methods, and members if access control is blocked.
  • Only public, abstract, and final modifications that are illegal are allowed for the class.”The public type needs to have its definition in a separate file.”
  • The access modifier specifies whether a class, method, or variable can be accessed. Java has three different kinds of access modifiers. protected, private, and public. In case an access modifier is not supplied, the default access is applied.
Basic Core java interview quentions and answer
Basic Core java interview quentions and answer
05) Explain what access modifiers can be used for variables ?

Access modifiers in Java are keywords that regulate a program’s classes, methods, and fields’ visibility and accessibility. One of the main tenets of object-oriented programming, encapsulation, is enforced in part via these modifiers. Four primary access modifiers are offered by Java:

We can use all access modifiers public, private,protected and default for variables.

Public (public) :

  • A variable can be accessed if it is declared as public.
  • In the same class
  • Within the same subclass package
  • Within the same nonsubclass package
  • Within an alternative package subtype
  • In a separate non-subclass package

Private (private):

  • When a variables is declared as default, we can access that method in
  • within the same course
  • Within the same package subclass
  • Within the same non-subclass package In a different package subclass, we are unable to access the
  • default access variables.
  • Not a subclass of the same package.

Protected (protected):

  • A variable that has been marked as protected can be accessed.
  •  In the same class
  •  Inside the identical package subtype
  •  With non-subclass within the same package
  •  Inside distinct package subclasses
  • It is not accessible to non-subclasses in other packages.

Default (Package-Private):

  • A variable that has been designated as private can only be accessed within that class.
  • It is not accessible within the same package subclass.
  • identical package without subclass
  • distinct package subclass
  • Not a subclass of the same package.
06) What is final access modifier in java?

The final keyword in Java is used as an access modifier to prevent additional changes to variables, methods, and classes. Depending on the elements it is applied to, final has distinct functions.

  • final class: It is not possible to expand or subclass a final class. We are designating a class as final in order to avoid inheritance. However, we are still able to use this class’s compositional methods. For example, the String class
  • final methods: Java has several significant features, one of which is method overriding. However, there are circumstances in which we might want to avoid using this functionality. Next, we announced the method as final, causing it to print overriding. For methods, we employ the final access modifier to prevent overriding.
  • final variables : A variable acts like a constant if it is declared as final. The last variable’s value cannot be changed. Compilation errors occur when the last variable is altered. Here’s the error:
  • “final variable cannot be assigned.”
07) Explain about abstract classes in java?
  • We could occasionally run into a scenario where we are unable to implement every method in a class. We would prefer that a class that extends it handle the implementation.
  • We designate a class as abstract in such a situation.We use key word abstract to create an abstract for the class. A class is considered abstract if it has one or more abstract methods.
  • Compile time errors occur when we fail to declare a class that has abstract methods as abstract. The error that appears is as follows.
  • “The type must be an abstract class to define abstract methods.” Signature ; abstract class { }
Basic Core java interview quentions and answer
Basic Core java interview quentions and answer
08) What are abstract methods in java?
  • A method without a body is called an abstract method. Method body is replaced with a semicolon when declaring an abstract method, along with the keyword abstract.
  • Public abstract void () is the signature.
  • Public abstract void getDetails(), for example
  • It is the duty of the subclass to implement the abstract method that the abstract class defines.
09) What is an exception in java?
  • An exception in Java is a program execution event that interferes with the regular flow of instructions. An object that represents the exception is made and thrown whenever an extraordinary circumstance occurs. The Java runtime system searches for an exception handler that can handle the thrown exception when this disrupts the program’s regular flow.
  • Try-catch blocks are commonly used in Java to handle exceptions. The code that could raise an exception is contained in the try block, and the code to manage the exception is contained in the catch block. In addition, code that is to be executed whether or not an exception is thrown can be specified using the finally block.
10) State some situations where exceptions may arise in java?
  • Accessing a non-existent element within the array.
  • Invalid string to number and number to string conversion.
  • (Exception for Number Format)
  • Class cast exception: incorrect casting of the class
  • Attempting to construct an instance exception for an interface or abstract class
11) What is Exception handling in java?

Exception handling is a program’s mechanism for dealing with unusual situations. If an exception is raised within the program and it is not handled appropriately, the program will terminate. Here is when exception handling becomes important so that the program can continue as intended and not end suddenly. With the aid of exception handling, this is possible.

13) What are advantages of Exception handling in java?
  • separating the exception-handling code from the regular code to prevent unexpected program termination.
  • dividing up into several kinds of exceptions so that we can handle certain exceptions instead of using the Exception root class to handle all errors. Instead of using the Exception root class to handle exceptions, it is advised to use a particular Exception.
  • Call stack mechanism: An exception is propagated, or thrown to the method’s caller, if it is not handled right away by the handling method. This propagation continues until it locates a suitable exception handler; if one is found, the exception is handled; if not, the program ends abruptly.
Basic Core java interview quentions and answer
Basic Core java interview quentions and answer
13) What is an eror in Java?
  • Java’s Error class is a subclass of the Throwable class. We refer to errors produced by our application as exceptions, but occasionally environmental problems like memory exhaustion result in exceptions. We are unable to handle exceptions in these situations. In Java, unrecoverable exceptions are referred to as errors.
  • For example: Memory problems
14) What is an eror in Java?
  • Java’s Error class is a subclass of the Throwable class. We refer to errors produced by our application as exceptions, but occasionally environmental problems like memory exhaustion result in exceptions. We are unable to handle exceptions in these situations. In Java, unrecoverable exceptions are referred to as errors.
  • For example: Memory problems
16) 72) Explain importance of throws keyword in java?
  • To signal that an exception of a specific type may be thrown from the method, the throws statement is used at the end of the method signature.
  • When an exception is checked, the throws keyword’s primary function is to assign exception handling responsibilities to the caller methods.
  • The inclusion of the throws keyword is not necessary when dealing with unchecked exceptions.
  • Only throwable types may utilize the throws keyword; other kinds will result in a compile-time error stating they are incompatible.

An error is unchecked , it is not required to handle by try catch or by throws.
Syntax : Class Test{
Public static void main(String args[]) throws IE

  • Note : The method should throw only checked exceptions and subclasses of checked exceptions.
  • It is not recommended to specify exception superclasses in the throws class when the actual exceptions
  • thrown in the method are instances of their subclass.
17) Explain when ClassNotFoundException will be raised ?
  • JVM throws a classNotFoundException when it tries to load a class by its string name and is unable to locate the class. One instance of this exception is when the class name is spelled incorrectly and we attempt to load the class using the string name; as a result, the class cannot be found error is raised.
  • Exception: ClassNotFound.
18) Explain when NoClassDefFoundError will be raised ?
  • When the JVM tries to load the class but cannot find the definition for that class, this error is produced.
  • There will be a NoClassDefFoundError. The class might not be found at runtime, even though it exists at build time. This could be the result of a typo in the classname on the command line, improper classpath specification, or the
  • The byte code class file is no longer accessible.
19) What is multitasking ?
  • Multitasking refers to using a computer to do multiple tasks at once. Example: Using a calculator and a spreadsheet simultaneously.

Types of multitasking

Process based multitasking :

  • It permits the simultaneous operation of two or more programs. A process is the smallest unit of code in process-based multitasking.
    Using PowerPoint and Word at the same time is an example.

Thread based multitasking :

  • It enables the parallel operation of certain program components.
  • Example: Formatting text and simultaneously printing a Word document.
  • Java has built-in support for multithreading and thread-based multitasking.
20) Explain thread in java?
  • In Java, a thread is the smallest unit of execution within a program. It represents an independent path of execution in a Java program, allowing concurrent execution of tasks. Java provides built-in support for multithreading, allowing developers to create and manage threads easily. Threads are particularly useful for executing multiple tasks concurrently, improving the performance and responsiveness of applications.
  • A thread is a program’s independent path of execution.
  • Virtual CPU, code, and data are the three components that make up a thread.
  • Threads share code and data at runtime, utilizing the same address space.
  • In Java, each thread is an object of the java.lang module.class for threads.