Java Basic Interview Questions

What are the variable types in Java?

Variable types in Java include primitive types (int, double, boolean, etc.) and reference types (classes, interfaces, arrays, etc.). Primitive types store values directly, while reference types store memory addresses of objects.

What operators does Java support?

Java supports arithmetic operators (+, -, *, /, %), relational/comparison operators (> , < , == , != ), logical operators (&& || !), and assignment operators (+= -=, etc.).

Object-Oriented Programming

How is inheritance implemented in Java?

Inheritance is achieved using the extends keyword. A subclass inherits attributes and methods from its superclass. The subclass can override or rewrite superclass methods.

How is polymorphism implemented in Java?

Polymorphism is realized through method overriding (overriding) and interface implementation. At runtime, objects invoke corresponding methods based on their actual type rather than the reference type.

Data Structures & Collections

What are the components of the Java Collection Framework?

The framework primarily includes interfaces such as List, Set, and Map, along with their concrete implementations (e.g., ArrayList, HashSet, HashMap).

How do you iterate over collections in Java?

Iteration can be performed using a standard for loop, an Iterator, or the enhanced for-loop (for-each).

Multithreading & Concurrency

How is a thread started in Java?

A thread can be initiated by calling the start() method of the Thread class or implementing and invoking the run() method of the Runnable interface.

What is synchronization?

Synchronization ensures safe concurrent access to shared resources among multiple threads, implemented via the synchronized keyword or locking mechanisms (e.g., ReentrantLock).

Exception Handling

How does Java handle exceptions?

Java uses a try-catch-finally block structure to capture and manage exceptions. The finally block executes regardless of whether an exception is thrown.

How do you create custom exceptions in Java?

Custom exceptions are defined by extending the Exception or RuntimeException classes.

I/O Streams & Network Programming

What types of streams exist in Java's IO system?

Input and output streams include byte streams (InputStream, OutputStream) and character streams (Reader, Writer). Buffered streams (e.g., BufferedInputStream) are also available for performance optimization.

What is Socket programming?

A socket acts as an endpoint for network communication. A server listens for connections using a ServerSocket, while clients send data through a Socket.

Other Core Concepts

How does Java's garbage collection mechanism work?

Java employs the Generational Collection strategy, automatically reclaiming memory occupied by objects that are no longer in use via Garbage Collector (GC) processes.

What is Java generics?

Generics allow type parameters to be specified when defining classes, interfaces, and methods, enabling compile-time type safety and code reusability.