Browsed by
Tag: core java in java questions asked in interview

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.

Under what conditions is an object’s finalize() method invoked by the garbage collector

Under what conditions is an object’s finalize() method invoked by the garbage collector

The garbage collector invokes an object’s finalize() method when it detects that the object has become unreachable. In Java, the finalize() method is a method of the Object class, and it is called by the garbage collector before an object is reclaimed (garbage collected). However, there are no guarantees about when the finalize() method will be called or whether it will be called at all. The Java documentation explicitly states that it is typically not recommended to rely on finalize() for resource…

Read More Read More

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

What an I/O Filter

What an I/O Filter

An I/O filter is an object that reads from one stream and writes to another, usually altering the data in some way as it is passed from one stream to another. In the context of Core Java, an I/O filter typically refers to an object that is used to perform filtering on data as it is read from or written to a stream. Input/Output (I/O) streams in Java are used for reading from or writing to various sources, such as…

Read More Read More

Are there any global variables in Java, which can be accessed by other part of your program?-

Are there any global variables in Java, which can be accessed by other part of your program?-

No, it is not the main method in which you define variables. Global variables is not possible because concept of encapsulation is eliminated here. In Java, the concept of global variables, as traditionally understood in some other programming languages like C, doesn’t directly apply. In Java, variables are typically declared within a class, method, or block, and their scope is limited to that particular context. However, if you want to create variables that can be accessed globally across different parts…

Read More Read More

Can Abstract Class Be Instantiated

Can Abstract Class Be Instantiated

No abstract class cannot be instantiated i.e you cannot create a new object of this class No, an abstract class cannot be instantiated in Java. An abstract class is a class that is declared with the abstract keyword. It may have abstract methods (methods without a body) that must be implemented by its subclasses. Abstract classes cannot be instantiated on their own because they may contain incomplete or abstract methods, and it doesn’t make sense to create objects of an…

Read More Read More

What is session tracking and how do you track a user session in servlets?-

What is session tracking and how do you track a user session in servlets?-

  Session tracking is a mechanism that servlets use to maintain state about a series requests from the same user across some period of time. The methods used for session tracking are: a) User Authentication – occurs when a web server restricts access to some of its resources to only those clients that log in using a recognized username and password. b) Hidden form fields – fields are added to an HTML form that are not displayed in the client’s…

Read More Read More

What is Static Initializer Code

What is Static Initializer Code

: A class can have a block of initializer code that is simply surrounded by curly braces and labeled as static e.g. Code: public class Demo{ static int =10; static{ System.out.println(�Hello world�); } } And this code is executed exactly once at the time of class load In Java, a static initializer block is a block of code within a class that is executed only once when the class is loaded into the Java Virtual Machine (JVM). It is used…

Read More Read More

What is Inet address

What is Inet address

?– Every computer connected to a network has an IP address. An IP address is a number that uniquely identifies each computer on the Net. An IP address is a 32-bit number. In Java, InetAddress is a class that represents an Internet Protocol (IP) address. It is part of the java.net package and provides methods for working with IP addresses. The InetAddress class can be used to create instances representing both IPv4 and IPv6 addresses. Here’s a brief overview of…

Read More Read More

How to Read a File from CLASSPATH in Java

How to Read a File from CLASSPATH in Java

This can be done in two simple ways   Using ClassLoader.getResourceAsStream This method can be used to load any file from CLASSPATH InputStream in =this.getClass().getClassLoader().getResourceAsStream(“MyFile.txt”);   Using Class.getResourceAsStream This method can be used to load files using relative path to the package of the class InputStream in =this.getClass().getResourceAsStream(“SomeTextFile.txt”);   This method can also be used to load any files from CLASSPATH by prefixing a “/”