Java Interview Questions-Core Faq - 3
What's the base class of all exception classes?
The base class of all exception classes in Java is the java.lang.Throwable class.
Which are the two subclasses under the Exception class?
The two subclasses under the Exception class are RuntimeException (unchecked exceptions) and IOException (checked exceptions).
What is the difference between Checked Exception and Unchecked Exception?
Checked exceptions (e.g., IOException) are checked at compile time and must be either caught or declared in the method signature using the throws keyword. Unchecked exceptions (e.g., NullPointerException) are not checked at compile time and do not require explicit handling.
When ArithmeticException is thrown?
An ArithmeticException is thrown when an exceptional arithmetic condition occurs, such as dividing an integer by zero.
What is the difference between an error and an exception?
Errors (e.g., OutOfMemoryError) are severe problems that may occur in the application or the Java Virtual Machine, and they are not meant to be caught or handled by the application. Exceptions, on the other hand, are less severe and represent recoverable problems that can be caught and handled in the application.
Can the finally block be used without catch?
Yes, the finally block can be used without catch. It is used to ensure that code placed inside it gets executed regardless of whether an exception is thrown or not. However, if there is no catch block, the finally block will not handle any exceptions.
Which types of exceptions are caught at compile time?
Checked exceptions (e.g., IOException, SQLException) are caught at compile time. The compiler forces the programmer to either handle these exceptions or declare them in the method signature.
What happens if an exception is not handled in a program?
If an exception is not handled in a program, it will lead to termination of the program with a stack trace (exception details). The program will terminate abnormally, and the flow of execution will stop.
What is the difference between throw, throws, and Throwable?
throw is used to explicitly throw an exception from a method or a block of code. throws is used in a method declaration to indicate that the method might throw a specific type of exception, which needs to be handled by the caller. Throwable is the base class for all exceptions and errors in Java.
Does system.exit() in try block execute the finally block?
No, if System.exit() is called in the try block, the finally block will not execute. The program terminates immediately, skipping the finally block.
What causes a java.lang.ArrayIndexOutOfBoundsException, and how do I prevent it?
An ArrayIndexOutOfBoundsException is caused when you try to access an array element using an invalid index, i.e., an index that is outside the bounds of the array. To prevent it, make sure to check the array length and use valid indices when accessing array elements.
In case there is a return at the end of the try block, will it execute the finally block?
Yes, if there is a return statement at the end of the try block, the finally block will execute before the method returns. The finally block is guaranteed to execute regardless of whether there is a return in the try block or not.
What happens when you add a double value to a String?
When you add a double value to a String using the + operator, the double value will be converted to its string representation, and the concatenation will result in a new String.
When can the parseInt() method be used?
The parseInt() method in Java is used to parse (convert) a string representation of an integer into its numeric value. It can be used when you want to convert a String containing digits into an integer data type.
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. This value indicates that there are no more bytes to read from the file.
Can you serialize static fields of a class?
No, static fields are not serialized along with the object. Only the non-static (instance) fields of an object are serialized.
What is a serialVersionUID, and why should I use it?
serialVersionUID is a unique identifier associated with a serializable class in Java. It is used to ensure compatibility between serialized objects and their class versions, preventing potential deserialization errors when class structures change.
How do I convert a numeric IP address into a hostname?
You can use the InetAddress class in Java to convert a numeric IP address into a hostname. Use getHostName() method, passing the IP address as an argument, to obtain the corresponding hostname.
What's the simplest way to print a Java array?
The simplest way to print a Java array is by using the Arrays.toString() method from the java.util.Arrays class. It converts the array into a string representation, and you can directly print it to the console.
- Java Interview Questions-Core Faq - 1
- Java Interview Questions-Core Faq - 2
- Features of Java Programming Language (2024)
- Difference between Java and JavaScript?
- What is the difference between JDK and JRE?
- What gives Java its 'write once and run anywhere' nature?
- What is JVM and is it platform independent?
- What is Just-In-Time (JIT) compiler?
- What is the garbage collector in Java?
- What is NullPointerException in Java
- Difference between Stack and Heap memory in Java
- How to set the maximum memory usage for JVM?
- What is numeric promotion?
- Generics in Java
- Static keyword in Java
- What are final variables in Java?
- How Do Annotations Work in Java?
- How do I use the ternary operator in Java?
- What is instanceof keyword in Java?
- How ClassLoader Works in Java?
- What are fail-safe and fail-fast Iterators in Java
- What are method references in Java?
- "Cannot Find Symbol" compile error
- Difference between system.gc() and runtime.gc()
- How to convert TimeStamp to Date in Java?
- Does garbage collection guarantee that a program will not run out of memory?
- How setting an Object to null help Garbage Collection?
- How do objects become eligible for garbage collection?
- How to calculate date difference in Java
- Difference between Path and Classpath in Java
- Is Java "pass-by-reference" or "pass-by-value"?
- Difference between static and nonstatic methods java
- Why Java does not support pointers?
- What is a package in Java?
- What are wrapper classes in Java?
- What is singleton class in Java?
- Difference between Java Local Variable, Instance Variable and a Class Variable?
- Can a top level class be private or protected in Java
- Are Polymorphism , Overloading and Overriding similar concepts?
- Locking Mechanism in Java
- Why Multiple Inheritance is Not Supported in Java
- Why Java is not a pure Object Oriented language?
- Static class in Java
- Difference between Abstract class and Interface in Java
- Why do I need to override the equals and hashCode methods in Java?
- Why does Java not support operator overloading?
- Anonymous Classes in Java
- Static Vs Dynamic class loading in Java
- Why am I getting a NoClassDefFoundError in Java?
- How to Generate Random Number in Java
- What's the meaning of System.out.println in Java?
- What is the purpose of Runtime and System class in Java?
- The finally Block in Java
- Difference between final, finally and finalize
- What is try-with-resources in java?
- What is a stacktrace?
- Why String is immutable in Java ?
- What are different ways to create a string object in Java?
- Difference between String and StringBuffer/StringBuilder in Java
- Difference between creating String as new() and literal | Java
- How do I convert String to Date object in Java?
- How do I create a Java string from the contents of a file?
- What actually causes a StackOverflow error in Java?
- Why is char[] preferred over String for storage of password in Java
- What is I/O Filter and how do I use it in Java?
- Serialization and Deserialization in Java
- Understanding transient variables in Java
- What is Externalizable in Java?
- What is the purpose of serialization/deserialization in Java?
- What is the Difference between byte stream and Character streams
- How to append text to an existing file in Java
- How to convert InputStream object to a String in Java
- What is the difference between Reader and InputStream in Java
- Introduction to Java threads
- Synchronization in Java
- Static synchronization Vs non static synchronization in Java
- Deadlock in Java with Examples
- What is Daemon thread in Java
- Implement Runnable vs Extend Thread in Java
- What is the volatile keyword in Java
- What are the basic interfaces of Java Collections Framework
- Difference between ArrayList and Vector | Java
- What is the difference between ArrayList and LinkedList?
- What is the difference between List and Set in Java
- Difference between HashSet and HashMap in Java
- Difference between HashMap and Hashtable in Java?
- How does the hashCode() method of java works?
- Difference between capacity() and size() of Vector in Java
- What is a Java ClassNotFoundException?
- How to fix java.lang.UnsupportedClassVersionError