When it comes to Java programming, mastering the fundamentals is crucial, especially for those preparing for interviews. To help you succeed, we’ve compiled a detailed guide of Java basic questions. These cover the core concepts every developer should understand, ensuring you’re ready to ace your next interview.
Java is a versatile, object-oriented programming language designed to have minimal implementation dependencies. Key features include:
Platform Independence: Write once, run anywhere.
Object-Oriented Principles: Encapsulation, inheritance, polymorphism, and abstraction.
Robustness and Security: Exception handling, garbage collection, and secure APIs.
Multithreading: Efficiently handle multiple tasks concurrently.
JDK (Java Development Kit): Used for developing Java applications. It includes tools like the compiler and debugger.
JRE (Java Runtime Environment): Provides libraries and JVM for running Java programs.
JVM (Java Virtual Machine): Executes bytecode, making Java platform-independent.
The foundational principles of OOP are:
Encapsulation: Restrict access to class members.
Abstraction: Hide complex implementation details.
Inheritance: Enable code reuse by deriving new classes.
Polymorphism: Allow methods to perform differently based on the context.
==: Compares memory references.
equals(): Compares the actual content of objects.
Access modifiers define the visibility and scope of classes, methods, and variables:
Public: Accessible everywhere.
Protected: Accessible within the package and subclasses.
Default: Accessible within the package only.
Private: Accessible only within the class.
A constructor is a special method used to initialize objects. It has the same name as the class and does not return any value.
Overloading: Same method name with different parameter lists (compile-time polymorphism).
Overriding: A subclass provides a specific implementation of a method in its superclass (runtime polymorphism).
Final Variable: Its value cannot be changed.
Final Method: Cannot be overridden.
Final Class: Cannot be extended.
Interface: Contains abstract methods; supports multiple inheritance.
Abstract Class: Can have both abstract and concrete methods; supports single inheritance.
ArrayList: Faster for retrieving data (random access).
LinkedList: Faster for insertion and deletion operations.
Static Variable: Shared among all instances of a class.
Static Method: Belongs to the class and can be called without creating an object.
Static Block: Executes when the class is loaded.
A package is a namespace that organizes classes and interfaces, helping avoid naming conflicts and managing access.
Java handles exceptions using the try-catch-finally construct. Additionally, you can use throw to explicitly throw exceptions and throws to declare them.
Checked Exceptions: Checked at compile-time (e.g., IOException).
Unchecked Exceptions: Occur at runtime (e.g., NullPointerException).
super is used to access methods, variables, or constructors of a parent class.
this refers to the current instance of a class and is often used to resolve naming conflicts.
Garbage collection automatically deallocates memory occupied by objects that are no longer referenced.
HashMap: Not synchronized; allows null keys and values.
Hashtable: Synchronized; does not allow null keys or values.
Multithreading enables the concurrent execution of multiple threads to optimize CPU utilization.
StringBuilder is ideal when frequent modifications to a string are required, as it is mutable and more efficient than String.
final: Prevents changes to variables, methods, or classes.
finally: Executes a block of code regardless of exceptions.
finalize(): Called by the garbage collector before an object is destroyed.
Numeric types: 0
Boolean: false
Objects: null
Implicit Casting: Automatically performed by the compiler (e.g., int to float).
Explicit Casting: Manually done by the programmer (e.g., double to int).
A singleton class ensures that only one instance exists and provides global access to it.
throw: Used to explicitly throw an exception.
throws: Declares the exceptions that a method can throw.
enum is used to define a collection of constants.
Wrapper classes convert primitive data types into objects (e.g., int to Integer).
Shallow Copy: Copies object references.
Deep Copy: Copies the actual object data.
String: Immutable.
StringBuffer: Mutable and thread-safe.
StringBuilder: Mutable but not thread-safe.
Java’s bytecode can run on any platform with a JVM, making it highly portable and platform-independent.
Conclusion
Understanding these Java basic questions is key to becoming a proficient Java developer. By mastering these concepts, you’ll not only excel in interviews but also build a solid foundation for a successful programming career. Start practicing these concepts today, and you’ll be well on your way to Java mastery!