This Java Quick Revision covers 75 important questions from Java fundamentals, OOP, exception handling, multithreading, I/O, collections, GUI, JDBC and other important concepts.
These questions are useful for Bihar STET, BPSC, SSC, Banking, Railway, Computer Science exams, interviews and programming fundamentals.
Part 1: Java Basics — Q1 to Q15
1. What is Java?
Java is a high-level, object-oriented, class-based and platform-independent programming language developed by Sun Microsystems.
Key Features:
Object-oriented
Platform independent
Robust
Secure
Portable
Multithreaded
Distributed
Automatic garbage collection
2. Why is Java Platform Independent?
Java is platform independent because Java source code is compiled into bytecode, which is executed by the Java Virtual Machine (JVM).
Java Source Code
↓
javac
↓
Bytecode
↓
JVM
↓
Operating System
The same bytecode can run on different operating systems if a compatible JVM is available.
Exam Point:
Write Once, Run Anywhere (WORA) is a commonly associated principle of Java.
3. What is JVM?
JVM (Java Virtual Machine) is the runtime engine that executes Java bytecode.
Main functions:
Loads bytecode
Verifies bytecode
Executes bytecode
Manages memory
Participates in garbage collection
JVM is platform dependent, because different operating systems require different JVM implementations.
4. What is JRE?
JRE (Java Runtime Environment) provides the environment required to run Java applications.
Conceptually:
JRE = JVM + Java Runtime Libraries
JRE is primarily intended for running Java programs rather than developing them.
5. What is JDK?
JDK (Java Development Kit) is used to develop Java applications.
It includes:
Compiler (
javac)Java launcher (
java)Documentation and development tools
Runtime components
Conceptually:
JDK = Development Tools + Runtime Environment
6. Difference Between JDK, JRE and JVM
| Component | Main Purpose |
|---|---|
| JVM | Executes Java bytecode |
| JRE | Provides environment to run Java applications |
| JDK | Provides tools to develop and run Java applications |
Memory Trick:
JDK → Develop
JRE → Run
JVM → Execute
7. What is Bytecode?
Bytecode is the intermediate instruction format generated by the Java compiler.
A Java source file:
Test.java
is compiled into:
Test.class
The .class file contains Java bytecode.
The JVM executes this bytecode.
8. What is a Class?
A class is a blueprint or template for creating objects.
It defines:
Data/fields
Methods
Constructors
Other members
Example:
class Student {
int rollNo;
void display() {
System.out.println(rollNo);
}
}
9. What is an Object?
An object is an instance of a class.
An object generally has:
State
Behavior
Identity
Example:
Student s = new Student();
Here, s refers to a Student object.
10. What is the main() Method?
The main() method is the conventional entry point for a standalone Java application.
public static void main(String[] args)
Components:
public→ accessible to JVMstatic→ can be invoked without creating an objectvoid→ returns no valuemain→ method name recognized as the application entry pointString[] args→ command-line arguments
11. What is a Variable?
A variable is a named storage location used to hold a value during program execution.
Example:
int age = 25;
Here:
int= data typeage= variable25= value
12. Types of Variables in Java
Java commonly has three types of variables based on where they are declared:
1. Local Variable
Declared inside a method, constructor or block.
2. Instance Variable
Declared inside a class but outside methods, without static.
Each object generally has its own copy.
3. Static Variable
Declared using static.
It belongs to the class rather than to individual objects.
13. What are Data Types in Java?
Java data types are broadly divided into:
Primitive Data Types
Java has 8 primitive data types:
byte
short
int
long
float
double
char
boolean
Non-Primitive / Reference Types
Examples include:
Classes
Arrays
Interfaces
Enums
String objects
14. What is Type Casting?
Type casting means converting a value from one data type to another compatible type.
Widening Conversion
Smaller compatible numeric type → larger compatible numeric type.
Example:
int x = 10;
double y = x;
This is generally performed automatically.
Narrowing Conversion
Larger compatible numeric type → smaller compatible numeric type.
double x = 10.5;
int y = (int)x;
Explicit casting may be required.
15. What is a Package?
A package is a namespace used to organize related Java classes, interfaces and other types.
Example:
package mypackage;
Advantages:
Organizes code
Avoids naming conflicts
Helps control access
Improves maintainability
Part 2: OOP Concepts — Q16 to Q30
16. What is OOP?
OOP (Object-Oriented Programming) is a programming paradigm based on objects and classes.
Java strongly supports object-oriented programming.
17. What are the Four Pillars of OOP?
The four commonly cited pillars are:
Encapsulation
Abstraction
Inheritance
Polymorphism
Memory Trick:
E-A-I-P
18. What is Encapsulation?
Encapsulation means bundling data and the methods that operate on that data into a class, while controlling access to the internal state.
Example:
class Account {
private double balance;
public double getBalance() {
return balance;
}
}
Here, balance is protected using the private access modifier.
19. What is Abstraction?
Abstraction means exposing essential features while hiding unnecessary implementation details.
It can be achieved using:
Abstract classes
Interfaces
Example:
abstract class Animal {
abstract void sound();
}
20. What is Inheritance?
Inheritance allows a class to acquire accessible properties and behaviors from another class.
It is commonly implemented using:
extends
Example:
class Animal {
void eat() {
System.out.println("Eating");
}
}
class Dog extends Animal {
}
Here, Dog inherits the accessible members of Animal.
21. What is Polymorphism?
Polymorphism means one interface/name can represent different forms or behaviors.
Two major forms discussed in Java are:
Compile-Time Polymorphism
Usually achieved through method overloading.
Runtime Polymorphism
Achieved through method overriding and dynamic method dispatch.
22. What is Method Overloading?
Method overloading occurs when multiple methods in the same class have the same name but different parameter lists.
Example:
void add(int a, int b) {
}
void add(int a, int b, int c) {
}
Changing only the return type does not constitute method overloading.
23. What is Method Overriding?
Method overriding occurs when a subclass provides its own implementation of an inherited overridable method.
Example:
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Bark");
}
}
24. What is a Constructor?
A constructor is a special class member used to initialize objects.
Important characteristics:
Same name as the class
Has no return type
Called when an object is created
Can be overloaded
Example:
class Student {
Student() {
System.out.println("Constructor called");
}
}
25. What are the Types of Constructors?
Commonly discussed types include:
No-Argument Constructor
Student() {
}
Parameterized Constructor
Student(int id) {
}
Java does not provide a special built-in copy constructor syntax like C++; however, a programmer can create a constructor that accepts an object of the same class.
26. What is the this Keyword?
this refers to the current object in an instance context.
Uses include:
Referencing current object's fields
Calling current class methods
Calling another constructor using
this()Passing current object as an argument
Example:
class Student {
int id;
Student(int id) {
this.id = id;
}
}
27. What is the super Keyword?
super refers to the immediate superclass portion of the current object.
It can be used to:
Access superclass fields
Call superclass methods
Call superclass constructor using
super()
Example:
class Dog extends Animal {
Dog() {
super();
}
}
28. What is the final Keyword?
final is used to restrict modification.
Final Variable
Cannot be reassigned after initialization.
final int x = 10;
Final Method
Cannot be overridden by a subclass.
Final Class
Cannot be extended.
final class A {
}
29. Abstract Class vs Interface
| Abstract Class | Interface |
|---|---|
| Can have abstract and concrete methods | Can declare abstract methods and, depending on Java version, default/static/private methods |
| Can have instance variables | Fields are implicitly public, static and final |
| Can have constructors | Cannot have constructors |
| A class extends one class | A class can implement multiple interfaces |
| Can maintain object state | Primarily defines a contract |
Exam Point
Java does not support multiple inheritance of classes, but a class can implement multiple interfaces.
30. Does Java Support Multiple Inheritance?
Java does not support multiple inheritance of classes.
For example, a class cannot:
class C extends A, B
However, Java supports implementing multiple interfaces:
class C implements A, B {
}
Part 3: Exception Handling & Multithreading — Q31 to Q50
31. What is an Exception?
An exception is an event that disrupts the normal flow of program execution.
Java provides mechanisms to handle exceptional situations.
Examples:
ArithmeticExceptionNullPointerExceptionIOException
32. What are Exception Handling Keywords?
Important keywords are:
trycatchfinallythrowthrows
33. Give an Example of Exception Handling
try {
int x = 10 / 0;
}
catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
}
The try block contains code that may throw an exception, while catch handles the exception.
34. Checked vs Unchecked Exceptions
| Checked Exception | Unchecked Exception |
|---|---|
| Checked by compiler for handling/declaring requirements | Not subject to compile-time checking in the same way |
Usually subclasses of Exception excluding RuntimeException hierarchy | Usually subclasses of RuntimeException |
Example: IOException | Example: ArithmeticException |
Example: SQLException | Example: NullPointerException |
Important
Unchecked exceptions are not simply "compile-time errors"; they generally occur at runtime.
35. What is a finally Block?
The finally block is used for cleanup code and normally executes after the try/catch processing.
Example:
try {
// risky code
}
catch (Exception e) {
// handling
}
finally {
// cleanup
}
Note
The statement "finally always executes" has exceptions, such as abnormal JVM termination.
36. What is throw?
throw is used to explicitly throw an exception.
Example:
throw new ArithmeticException("Invalid operation");
37. What is throws?
throws is used in a method declaration to indicate exceptions that the method may propagate to its caller.
Example:
void readFile() throws IOException {
}
Difference
throw → actually throws an exception
throws → declares possible exceptions
38. What is Multithreading?
Multithreading is the execution of multiple threads within a process.
It can improve responsiveness and allow concurrent execution of tasks.
39. How Can a Thread Be Created in Java?
Two traditional approaches are:
1. Extending Thread
class A extends Thread {
public void run() {
System.out.println("Hello");
}
}
2. Implementing Runnable
class A implements Runnable {
public void run() {
System.out.println("Hello");
}
}
Using Runnable is often preferable when the class needs to extend another class.
40. Give a Simple Thread Example
class A extends Thread {
public void run() {
System.out.println("Hello");
}
public static void main(String[] args) {
A t = new A();
t.start();
}
}
Important
Use:
t.start();
rather than directly calling:
t.run();
when you want the JVM to start a new thread.
41. What is the Thread Lifecycle?
A Java thread can be in states represented by Thread.State, including:
NEW
RUNNABLE
BLOCKED
WAITING
TIMED_WAITING
TERMINATED
Important Correction
Java's official Thread.State enum does not have a separate RUNNING state.
The JVM groups ready/running execution under RUNNABLE.
42. What is sleep()?
Thread.sleep() pauses the currently executing thread for a specified period.
Example:
Thread.sleep(1000);
The value is in milliseconds for this overload.
Important
sleep() does not release intrinsic locks held by the thread.
43. What is the Thread Priority Range?
Java thread priorities range from:
1 → Minimum
5 → Normal
10 → Maximum
Constants include:
Thread.MIN_PRIORITY
Thread.NORM_PRIORITY
Thread.MAX_PRIORITY
Important
Thread priority is a scheduling hint; it does not guarantee execution order.
44. What is Synchronization?
Synchronization controls access to shared resources so that multiple threads do not interfere with each other.
Example:
synchronized void display() {
}
It helps prevent inconsistent results caused by concurrent access to shared mutable data.
45. What is Deadlock?
Deadlock is a situation in which two or more threads/processes wait indefinitely for resources held by each other.
Example:
Thread A → waits for Lock B
Thread B → waits for Lock A
Neither can proceed.
46. What is yield()?
Thread.yield() is a static method that gives the scheduler a hint that the current thread is willing to yield the processor.
Important
It is only a hint. The scheduler is not required to switch to another thread.
47. What is join()?
join() causes the calling thread to wait until another thread completes, subject to the particular overload/time limit.
Example:
t.join();
This means the current thread waits for thread t to terminate.
48. What is isAlive()?
isAlive() checks whether a thread has been started and has not yet terminated.
Example:
if (t.isAlive()) {
System.out.println("Thread is running/not terminated");
}
49. What is a Daemon Thread?
A daemon thread is a background thread that provides services to other threads.
The JVM does not keep running solely because daemon threads remain alive.
Example:
t.setDaemon(true);
This must be set before the thread is started.
50. What is Garbage Collection?
Garbage collection is Java's automatic memory-management mechanism for reclaiming memory occupied by objects that are no longer reachable.
Important
Java programmers normally do not explicitly free objects using a free() function.
The JVM's garbage collector determines when eligible objects are reclaimed.
Part 4: I/O, Collections, GUI & JDBC — Q51 to Q75
51. What is a Stream in Java I/O?
A stream represents a flow of data between a source and a destination.
Java I/O broadly provides:
Byte streams
Character streams
52. What are the Types of Streams?
Byte Streams
Used for binary data.
Important classes:
InputStream
OutputStream
Character Streams
Used for character/text data.
Important classes:
Reader
Writer
53. How Do You Create a File Object?
Example:
File f = new File("test.txt");
The File class represents file and directory pathnames and provides operations related to them.
Important
Creating a File object does not necessarily create a physical file on disk.
54. What is BufferedReader?
BufferedReader is a character-input class that buffers input for efficient reading.
Example:
BufferedReader br =
new BufferedReader(new FileReader("test.txt"));
It provides methods such as:
read()
readLine()
55. What is Serialization?
Serialization is the process of converting an object's state into a byte stream so that it can be stored or transmitted.
A class commonly implements:
Serializable
Example:
class Student implements Serializable {
}
Deserialization reconstructs the object from the stored byte stream.
56. What is the Java Collection Framework?
The Java Collection Framework is a set of interfaces and classes for storing and manipulating groups of objects.
Important interfaces include:
List
Set
Queue
Deque
Map
Many collection classes are available in:
java.util
57. What is the List Interface?
List represents an ordered collection and generally permits duplicate elements.
Examples:
ArrayList
LinkedList
Vector
Example:
List list = new ArrayList<>();
58. What is the Set Interface?
Set represents a collection that does not allow duplicate elements.
Examples:
HashSet
LinkedHashSet
TreeSet
Important
HashSet→ no guaranteed iteration orderLinkedHashSet→ maintains insertion orderTreeSet→ maintains sorted order according to its ordering rules
59. What is the Map Interface?
Map stores data as key-value pairs.
Examples:
HashMap
LinkedHashMap
TreeMap
Hashtable
Example:
Map map = new HashMap<>();
Important
Map is part of the Collections Framework but is not a subtype of Collection.
60. What is ArrayList?
ArrayList is a resizable-array implementation of the List interface.
Advantages
Fast random access
Dynamic size
Allows duplicates
Maintains insertion order
Example:
ArrayList list = new ArrayList<>();
61. What is LinkedList?
LinkedList is a linked-list implementation of the List and Deque interfaces.
In Java's standard implementation, it is a doubly linked list.
Example:
LinkedList list = new LinkedList<>();
62. What is an Iterator?
Iterator is used to traverse elements of a collection sequentially.
Example:
Iterator it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
Important methods include:
hasNext()next()remove()where supported
63. Comparable vs Comparator
| Comparable | Comparator |
|---|---|
| Defines natural ordering | Defines external/custom ordering |
Method: compareTo() | Method: compare() |
| Implemented by the class being compared | Usually implemented separately |
| Generally one natural ordering | Multiple custom orderings possible |
64. What is AWT?
AWT (Abstract Window Toolkit) is Java's original GUI toolkit based on native platform components.
Package:
java.awt
It provides components such as:
Button
Label
TextField
Frame
65. What is Event Handling?
Event handling is the mechanism by which a Java GUI application responds to user or system events.
Examples:
Mouse click
Key press
Window action
Listeners are commonly used.
Example:
ActionListener
MouseListener
KeyListener
66. What is Swing?
Swing is a GUI toolkit built on top of AWT's infrastructure and provides a rich set of mostly lightweight components.
Package:
javax.swing
Examples:
JFrame
JButton
JLabel
JTextField
JTable
67. What is JFrame?
JFrame is a top-level Swing window.
Example:
JFrame frame = new JFrame("My Application");
It can contain GUI components such as buttons, labels and text fields.
68. What are Layout Managers?
Layout managers automatically arrange components inside containers.
Important layout managers include:
FlowLayout
BorderLayout
GridLayout
BoxLayout
GridBagLayout
Examples
FlowLayout → components arranged sequentially.
BorderLayout → five major regions.
GridLayout → rectangular grid.
69. Give a JButton Example
JButton b = new JButton("Click");
A button can generate an action event when the user clicks it.
70. What is a Java Applet?
A Java Applet was a small Java program designed to run inside a browser or applet viewer.
Important
Java applets are obsolete in modern Java/browser environments and should not be treated as a current web-development technology.
For modern Java applications, technologies such as web frameworks, desktop GUI frameworks or other application platforms are used instead.
71. What is JDBC?
JDBC (Java Database Connectivity) is a Java API used to connect Java applications with relational databases and execute SQL operations.
It provides interfaces/classes for:
Database connections
SQL statements
Result sets
Transactions
72. What are the Basic Steps in JDBC?
A simplified JDBC workflow is:
Get Connection
↓
Create Statement / PreparedStatement
↓
Execute SQL
↓
Process Result
↓
Close Resources
Modern JDBC drivers can generally be discovered automatically, so explicitly loading a driver class is often unnecessary.
73. Give an Example of JDBC Connection
Connection con =
DriverManager.getConnection(
url,
"user",
"password"
);
This attempts to establish a database connection using the supplied JDBC URL and credentials.
74. What is PreparedStatement?
PreparedStatement represents a precompiled or prepared SQL statement that can contain parameters.
Example:
PreparedStatement ps =
con.prepareStatement(
"SELECT * FROM student WHERE id = ?"
);
ps.setInt(1, 101);
Advantages
Parameterized SQL
Helps prevent SQL injection when used correctly
Convenient for repeated execution
Supports type-safe parameter binding
75. What are the Advantages of Java?
Important advantages of Java include:
Platform independence
Object-oriented programming support
Robustness
Security features
Portability
Automatic memory management
Multithreading support
Rich standard library
Exception handling
Networking support
Large ecosystem
Strong community support
Quick Java Revision Table
| Topic | Key Point |
|---|---|
| Java | High-level, object-oriented language |
| Bytecode | Intermediate code executed by JVM |
| JVM | Executes bytecode |
| JRE | Runtime environment |
| JDK | Development kit |
| Class | Blueprint/template |
| Object | Instance of class |
| OOP | Object-oriented programming |
| Encapsulation | Bundling + access control |
| Abstraction | Hide implementation details |
| Inheritance | Reuse through class hierarchy |
| Polymorphism | Multiple forms |
| Overloading | Same method name, different parameters |
| Overriding | Subclass redefines inherited method |
| Constructor | Initializes objects |
this | Current object reference |
super | Immediate superclass reference |
final | Restricts modification/inheritance/overriding |
| Exception | Exceptional event disrupting normal flow |
throw | Explicitly throws exception |
throws | Declares possible exceptions |
| Thread | Unit of execution |
sleep() | Temporarily pauses current thread |
join() | Waits for another thread |
yield() | Scheduling hint |
| Synchronization | Controls shared-resource access |
| Garbage Collection | Automatic memory reclamation |
| List | Ordered, duplicates allowed |
| Set | No duplicate elements |
| Map | Key-value mapping |
| ArrayList | Resizable array |
| LinkedList | Doubly linked list implementation |
| Iterator | Traverses collection |
| Comparable | Natural ordering |
| Comparator | Custom ordering |
| JDBC | Database connectivity API |
| PreparedStatement | Parameterized SQL statement |
Important Exam Facts
Remember These 15 Points
1. Java source code → Bytecode → JVM
2. JVM executes bytecode.
3. JDK is used for development.
4. JRE provides the runtime environment.
5. Java has 8 primitive data types.
6. OOP pillars = Encapsulation, Abstraction, Inheritance, Polymorphism.
7. Overloading → different parameter list.
8. Overriding → subclass implementation.
9. this → current object.
10. super → immediate superclass.
11. throw → explicitly throws exception.
12. throws → declares exceptions.
13. Round-style thread states should not be memorized as "Running"; Java's official Thread.State uses RUNNABLE.
14. Map stores key-value pairs and is not a subtype of Collection.
15. Java Applets are obsolete in modern Java environments.