You can check all 147 Java interview questions here π https://devinterview.io/dev/java-interview-questions
Java has two types of exceptions: checked exceptions and unchecked exceptions.
Unchecked exceptions do not need to be declared in a method or a constructorβs throws clause, if they can be thrown by the execution of the method or the constructor, and propagate outside the method or constructor boundary.
On the other hand, checked exceptions must be declared in a method or a constructorβs throws clause.
A Java virtual machine (JVM) is a process virtual machine that can execute Java bytecode. Each Java source file is compiled into a bytecode file, which is executed by the JVM. Java was designed to allow application programs to be built that could be run on any platform, without having to be rewritten or recompiled by the programmer for each separate platform. A Java virtual machine makes this possible, because it is aware of the specific instruction lengths and other particularities of the underlying hardware platform.
The Java Runtime Environment (JRE) is basically the Java Virtual Machine (JVM) where your Java programs are being executed. It also includes browser plugins for applet execution.
The Java Development Kit (JDK) is the full featured Software Development Kit for Java, including the JRE, the compilers and tools (like JavaDoc, and Java Debugger), in order for a user to develop, compile and execute Java applications.
These methods can be used as a hint to the JVM, in order to start a garbage collection. However, this it is up to the Java Virtual Machine (JVM) to start the garbage collection immediately or later in time.
There are several differences between HashMap
and Hashtable
in Java:
Hashtable
is synchronized, whereasHashMap
is not. This makesHashMap
better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.Hashtable
does not allownull
keys or values.HashMap
allows onenull
key and any number ofnull
values.One of HashMap's subclasses is
LinkedHashMap
, so in the event that you'd want predictable iteration order (which is insertion order by default), you could easily swap out theHashMap
for aLinkedHashMap
. This wouldn't be as easy if you were usingHashtable
.
JDBC is an abstraction layer that allows users to choose between databases. JDBC enables developers to write database applications in Java, without having to concern themselves with the underlying details of a particular database.
The static
keyword denotes that a member variable or method can be accessed, without requiring an instantiation of the class to which it belongs.
A user cannot override static methods in Java, because method overriding is based upon dynamic binding at runtime and static methods are statically binded at compile time. A static method is not associated with any instance of a class so the concept is not applicable.
A finally block will always be executed, whether or not an exception is actually thrown. Even in the case where the catch statement is missing and an exception is thrown, the finally block will still be executed. Last thing to mention is that the finally block is used to release resources like I/O buffers, database connections, etc.
- An Error "indicates serious problems that a reasonable application should not try to catch."
- An Exception "indicates conditions that a reasonable application might want to catch."
A Java object is subject to garbage collection when it becomes unreachable to the program in which it is currently used.
The Iterator interface provides a number of methods that are able to iterate over any Collection. Each Java Collection contains the Iterator method that returns an Iterator instance. Iterators are capable of removing elements from the underlying collection during the iteration.
- When an object is passed by value, this means that a copy of the object is passed. Thus, even if changes are made to that object, it doesnβt affect the original value.
- When an object is passed by reference, this means that the actual object is not passed, rather a reference of the object is passed. Thus, any changes made by the external method, are also reflected in all places.
A Java Applet is program that can be included in a HTML page and be executed in a java enabled client browser. Applets are used for creating dynamic and interactive web applications.
A HashMap in Java stores key-value pairs. The HashMap requires a hash function and uses hashCode and equals methods, in order to put and retrieve elements to and from the collection respectively. When the put method is invoked, the HashMap calculates the hash value of the key and stores the pair in the appropriate index inside the collection. If the key exists, its value is updated with the new value. Some important characteristics of a HashMap are its capacity, its load factor and the threshold resizing.
Java Collections Framework provides a well designed set of interfaces and classes that support operations on a collections of objects. The most basic interfaces that reside in the Java Collections Framework are:
- Collection, which represents a group of objects known as its elements.
- Set, which is a collection that cannot contain duplicate elements.
- List, which is an ordered collection and can contain duplicate elements.
- Map, which is an object that maps keys to values and cannot contain duplicate keys.
The eight primitive data types supported by the Java programming language are:
- byte
- short
- int
- long
- float
- double
- boolean
- char
Autoboxing is the automatic conversion made by the Java compiler between the primitive types and their corresponding object wrapper classes. If the conversion goes the other way, this operation is called unboxing.
The main difference between them is that
- a Process is a program which is executing some code and
- a Thread is an independent path of execution in the process.
A process can have more than one thread for doing independent task e.g. a thread for reading data from disk, a thread for processing that data and another thread for sending that data over the network.
The Exception object will be garbage collected in the next garbage collection.
Java provides and supports the creation both of abstract classes and interfaces. Both implementations share some common characteristics, but they differ in the following features:
- All methods in an interface are implicitly abstract. On the other hand, an abstract class may contain both abstract and non-abstract methods.
- A class may implement a number of Interfaces, but can extend only one abstract class.
- In order for a class to implement an interface, it must implement all its declared methods. However, a class may not implement all declared methods of an abstract class. Though, in this case, the sub-class must also be declared as abstract.
- Abstract classes can implement interfaces without even providing the implementation of interface methods.
- Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.
- Members of a Java interface are public by default. A member of an abstract class can either be private, protected or public.
- An interface is absolutely abstract and cannot be instantiated. An abstract class also cannot be instantiated, but can be invoked if it contains a main method.
A JSP expression is used to insert the value of a scripting language expression, converted into a string, into the data stream returned to the client, by the web server. Expressions are defined between <% = and %> tags.
The Big-O notation simply describes how well an algorithm scales or performs in the worst case scenario as the number of elements in a data structure increases. The Big-O notation can also be used to describe other behavior such as memory consumption. Since the collection classes are actually data structures, we usually use the Big-O notation to chose the best implementation to use, based on time, memory and performance. Big-O notation can give a good indication about performance for large amounts of data.
- Method overloading in Java occurs when two or more methods in the same class have the exact same name, but different parameters.
class Dog{ public void bark(){ System.out.println("woof "); }
<span class="token cComment">//overloading method</span> <span class="token cVar">public</span> <span class="token cVar">void</span> <span class="token cMod">bark</span><span class="token cBase">(</span><span class="token cVar">int</span> num<span class="token cBase">)</span><span class="token cBase">{</span> <span class="token cVar">for</span><span class="token cBase">(</span><span class="token cVar">int</span> i<span class="token cBase">=</span><span class="token cNum">0</span><span class="token cBase">;</span> i<span class="token cBase"><</span>num<span class="token cBase">;</span> i<span class="token cBase">++</span><span class="token cBase">)</span> <span class="token class-name">System</span><span class="token cBase">.</span>out<span class="token cBase">.</span><span class="token cMod">println</span><span class="token cBase">(</span><span class="token cString">"woof "</span><span class="token cBase">)</span><span class="token cBase">;</span> <span class="token cBase">}</span>
}
- On the other hand, method overriding is defined as the case when a child class redefines the same method as a parent class. Overridden methods must have the same name, argument list, and return type. The overriding method may not limit the access of the method it overrides.
class Dog{ public void bark(){ System.out.println("woof "); } } class Hound extends Dog{ public void sniff(){ System.out.println("sniff "); }
<span class="token cVar">public</span> <span class="token cVar">void</span> <span class="token cMod">bark</span><span class="token cBase">(</span><span class="token cBase">)</span><span class="token cBase">{</span> <span class="token class-name">System</span><span class="token cBase">.</span>out<span class="token cBase">.</span><span class="token cMod">println</span><span class="token cBase">(</span><span class="token cString">"bowl"</span><span class="token cBase">)</span><span class="token cBase">;</span> <span class="token cBase">}</span>
}
public class OverridingTest{ public static void main(String [] args){ Dog dog = new Hound(); dog.bark(); } }
On the arrival of a JSP request, the browser first requests a page with a .jsp extension. Then, the Web server reads the request and using the JSP compiler, the Web server converts the JSP page into a servlet class. Notice that the JSP file is compiled only on the first request of the page, or if the JSP file has changed.The generated servlet class is invoked, in order to handle the browserβs request. Once the execution of the request is over, the servlet sends a response back to the client. See how to get Request parameters in a JSP.
The design pattern used by Java for all Swing components is the Model View Controller (MVC) pattern.
This method is used to method is used to load the driver that will establish a connection to the database.
The purpose of garbage collection is to identify and discard those objects that are no longer needed by the application, in order for the resources to be reclaimed and reused.
Thanks π for reading and good luck on your next tech interview!
Explore 3800+ dev interview question here π Devinterview.io