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

What’s the main difference between ArrayList / HashMap and Vector / Hashtable?

What’s the main difference between ArrayList / HashMap and Vector / Hashtable?

Vector / HashTable are synchronized which means they are thread safe. Cost of thread safe is performance degradation. So if you are sure that you are not dealing with huge number of threads then you should use ArrayList / HashMap.But yes you can stillsynchronize List and Map’s using Collections provided methods :- List OurList = Collections.synchronizedList (OurList); Map OurMap = Collections.synchronizedMap (OurMap);  

Which containers use a Border layout as their default layout

Which containers use a Border layout as their default layout

Window, Frame and Dialog classes use a BorderLayout as their layout. In Core Java, the containers that use a BorderLayout as their default layout are Frame, Window, and Dialog. The BorderLayout divides the container into five areas: North, South, East, West, and Center. When you create a Frame, Window, or Dialog without specifying a layout manager, it defaults to BorderLayout. Here’s an example: java import java.awt.*; public class BorderLayoutExample { public static void main(String[] args) { Frame frame = new…

Read More Read More

How are Java source code files named

How are Java source code files named

within the file. A source code file may contain at most one public class or interface. If a public class or interface is defined A Java source code file takes the name of a public class or interface that is defined within a source code file, then the source code file must take the name of the public class or interface. If no public class or interface is defined within a source code file, then the file must take on…

Read More Read More

How much subclasses you can maximum in Inheritance?

How much subclasses you can maximum in Inheritance?

In one of our old JAVA projects we had an inheritance depth of five. Believe us we never liked that code. It’s bad to have a huge inheritance depth. A maintainable inheritance depth should be maximum 5. Anything above that is horrible. There is no limit as such specified anywhere that there is some limit on the inheritance sub classing but depending on environments you will get stack over flow error.

Who is loading the init() method of servlet

Who is loading the init() method of servlet

?– Web server. In Java servlets, the init() method is automatically called by the servlet container (e.g., Tomcat, Jetty) when the servlet is first loaded into memory. It is part of the servlet lifecycle and is invoked only once during the servlet’s initialization. So, the correct answer to the question “Who is loading the init() method of servlet?” would be: The servlet container (or servlet engine) is responsible for loading and invoking the init() method of a servlet.

What value does read() return when it has reached the end of a file

What value does read() return when it has reached the end of a file

The read() method returns -1 when it has reached the end of a file. In Core Java, the read() method of the InputStream class returns an integer value. When it reaches the end of a file, it typically returns -1 to indicate the end of the stream. Here’s a simple example: java import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class ReadFileExample { public static void main(String[] args) { try (InputStream inputStream = new FileInputStream(“example.txt”)) { int data; while ((data = inputStream.read()) != -1) {…

Read More Read More

Why main() in java is declared as public static void main? What if the main method is declared as private?

Why main() in java is declared as public static void main? What if the main method is declared as private?

Public – main method is called by JVM to run the method which is outside the scope of project therefore the access specifier has to be public to permit call from anywhere outside the application static – When the JVM makes are call to the main method there is not object existing for the class being called therefore it has to have static method to allow invocation from class. void – Java is platform independent language therefore if it will…

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 the difference between the String and StringBuffer classes

What is the difference between the String and StringBuffer classes

String objects are constants. StringBuffer objects are not constants. In Core Java, the main difference between the String and StringBuffer classes lies in their mutability. Immutability (String): Strings in Java are immutable, meaning their values cannot be changed once they are assigned. Any operation that appears to modify a String actually creates a new String object. Example: java String str1 = “Hello”; str1 = str1 + ” World”; // This creates a new String object Mutability (StringBuffer): StringBuffer is mutable, allowing the content…

Read More Read More

What are Encapsulation, Inheritance and Polymorphism?

What are Encapsulation, Inheritance and Polymorphism?

Encapsulation is the mechanism that binds together code and data it manipulates and keeps both safe from outside interference and misuse. Inheritance is the process by which one object acquires the properties of another object. Polymorphism is the feature that allows one interface to be used for general class actions. In Java, Encapsulation, Inheritance, and Polymorphism are three fundamental concepts of object-oriented programming (OOP). Here’s a brief explanation of each: Encapsulation: Definition: Encapsulation is the bundling of data (attributes) and…

Read More Read More