Browsed by
Tag: java 15 interview questions and answers

What is the difference between exception and error?-

What is the difference between exception and error?-

?– The exception class defines mild error conditions that your program encounters. Exceptions can occur when trying to open the file, which does not exist, the network connection is disrupted, operands being manipulated are out of prescribed ranges, the class file you are interested in loading is missing. The error class defines serious error conditions that you should not attempt to recover from. In most cases it is advisable to let the program terminate when such an error is encountered.

Can an object’s finalize() method be invoked while it is reachable

Can an object’s finalize() method be invoked while it is reachable

An object’s finalize() method cannot be invoked by the garbage collector while the object is still reachable. However, an object’s finalize() method may be invoked by other objects. No, an object’s finalize() method cannot be invoked while the object is reachable. The finalize() method in Java is called by the garbage collector before reclaiming the memory occupied by an object that is no longer reachable (i.e., there are no references to the object). The purpose of the finalize() method is to allow an object…

Read More Read More

In which package is the applet class located?

In which package is the applet class located?

Applet classes are located in ” java.applet “package. In Core Java, the Applet class is located in the java.applet package. The full name of the Applet class is java.applet.Applet. Applets are Java programs that are designed to be embedded within HTML pages and run on the client side, typically in a web browser.

What is the class and interface in java to create thread and which is the most advantageous method?

What is the class and interface in java to create thread and which is the most advantageous method?

Thread class and Runnable interface can be used to create threads and using Runnable interface is the most advantageous method to create threads because we need not extend thread class here. In Core Java, the primary classes and interfaces used to create threads are: Thread class: The Thread class itself is used to create and manage threads. You can extend the Thread class and override its run() method to define the code that will be executed by the thread. Example:…

Read More Read More

Can an anonymous class be declared as implementing an interface and extending a class

Can an anonymous class be declared as implementing an interface and extending a class

An anonymous class may implement an interface or extend a superclass, but may not be declared to do both. Yes, in Java, an anonymous class can be declared as implementing an interface and extending a class simultaneously. This allows you to create a class on the fly that provides a specific implementation for an interface while also inheriting from a class. Here is an example: java interface MyInterface { void interfaceMethod(); } class MyBaseClass { void baseClassMethod() { System.out.println(“Base class…

Read More Read More

How to create a immutable object in Java? Does all property of immutable object needs to be final?

How to create a immutable object in Java? Does all property of immutable object needs to be final?

  To create a object immutable You need to make the class final and all its member final so that once objects gets crated no one can modify its state. You can achieve same functionality by making member as non final but private and not modifying them except in constructor. Also its NOT necessary to have all the properties final since you can achieve same functionality by making member as non final but private and not modifying them except in…

Read More Read More

What are the differences between checked and unchecked exceptions?

What are the differences between checked and unchecked exceptions?

A checked exception is any subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses. Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown. eg, IOException thrown by java.io.FileInputStream’s read() method Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses also are unchecked. With an unchecked exception, however, the compiler doesn’t force client programmers either to catch the exception or declare it in a…

Read More Read More

What are the states associated in the thread

What are the states associated in the thread

Thread contains ready, running, waiting and dead states. In Java, a thread can be in one of the following states: New: When a thread is created but not yet started, it is in the new state. Runnable: A thread that is ready to run is moved to the runnable state. It can be waiting for its turn to be picked for execution by the thread scheduler. Blocked: A thread that is blocked is waiting for a monitor lock to enter…

Read More Read More

What is the difference between the Boolean & operator and the && operator

What is the difference between the Boolean & operator and the && operator

If an expression involving the Boolean & operator is evaluated, both operands are evaluated. Then the &operator is applied to the operand. When an expression involving the && operator is evaluated, the first operand is evaluated. If the first operand returns a value of true then the second operand is evaluated. The && operator is then applied to the first and second operands. If the first operand evaluates to false, the evaluation of the second operand is skipped.

What is difference between String, StringBuffer and StringBuilder? When to use them?

What is difference between String, StringBuffer and StringBuilder? When to use them?

The main difference between the three most commonly used String classes as follows. StringBuffer and StringBuilder objects are mutable whereas String class objects are immutable. StringBuffer class implementation is synchronized while StringBuilder class is not synchronized. Concatenation operator “+” is internally implemented by Java using either StringBuffer or StringBuilder. Criteria to choose among String, StringBuffer and StringBuilder If the Object value will not change in a scenario use String Class because a String object is immutable. If the Object value…

Read More Read More