JAVA
Difference Questions — 4 to 3 Marks
1) throw vs throws
Basis throw throws
Purpose Used to explicitly throw an exception from within Used in method signature to declare that a
a method or block. method may throw an exception.
Syntax throw new ExceptionType("message"); returnType methodName() throws ExceptionType
Location Used inside the method body. Used in the method declaration / signature.
No. of Exceptions Can throw only one exception at a time. Can declare multiple exceptions separated by
commas.
Exception Type Usually used to throw checked or unchecked Primarily used for checked exceptions to warn
exceptions explicitly. the caller.
Example throw new IOException("File not found"); void readFile() throws IOException
2) == Operator vs equals() Method
Basis == Operator equals() Method
Type A relational / comparison operator. A method defined in the Object class.
Comparison Compares references (memory addresses) of Compares the content / value of two objects.
two objects.
For Primitives Compares actual values of primitive types. Cannot be used directly on primitives.
For Strings Returns true only if both variables point to the Returns true if the character sequences of both
exact same object in memory. strings are identical.
Overridable Cannot be overridden. Can be overridden in user-defined classes to
define custom equality.
Example new String("abc") == new String("abc") → false new String("abc").equals(new String("abc")) →
true
3) Abstract Class vs Interface
Basis Abstract Class Interface
Keyword Declared using the abstract class keyword. Declared using the interface keyword.
Methods Can have both abstract and concrete Prior to Java 8, only abstract methods; Java 8+
(implemented) methods. allows default and static methods.
Variables Can have instance variables of any access type. Can only have public static final constants.
Constructor Can have constructors. Cannot have constructors.
Inheritance A class can extend only one abstract class A class can implement multiple interfaces
(single inheritance). (multiple inheritance).
Access Modifiers Methods can have any access modifier. Methods are public by default.
Use Case Used when classes share common code and an Used to define a contract for unrelated classes.
IS-A relationship.
4) Call by Value vs Call by Reference
Basis Call by Value Call by Reference
Definition A copy of the actual argument value is passed to The reference (address) of the actual argument is
the method. passed to the method.
Effect on Original Changes made inside the method do NOT affect Changes made inside the method DO affect the
the original variable. original variable.
Java Support Java strictly uses call by value for primitive types. Java passes object references by value, so
object state can be modified.
Memory Uses separate memory for the copy. Both formal and actual parameters point to the
same memory location.
Safety Safer as original data is protected. Less safe as original data can be inadvertently
modified.
5) Method Overloading vs Method Overriding
Basis Method Overloading Method Overriding
Definition Same method name with different parameter lists Subclass redefines a method already defined in
within the same class. its parent class with the same name and
signature.
Polymorphism Type Compile-time (static) polymorphism. Runtime (dynamic) polymorphism.
Parameters Must differ in number, type, or order of Must have the same name, return type, and
parameters. parameters.
Inheritance Not required; occurs within a single class. Requires an inheritance relationship between
classes.
Return Type Can differ. Must be the same (or covariant).
@Override Not used. Should use @Override annotation for clarity.
Binding Resolved at compile time. Resolved at runtime by JVM.
6) Applet vs Application
Basis Applet Application
Definition A small Java program designed to run inside a A standalone Java program that runs on the local
web browser or applet viewer. machine via JVM.
Entry Point Has no main() method; uses life cycle methods Execution begins from the public static void
— init(), start(), stop(), destroy(). main(String[] args) method.
Execution Runs within a browser or applet viewer with Runs directly on the JVM with full system access.
Environment security restrictions.
Security Restricted from accessing local file system and Has unrestricted access to system resources.
system resources.
Network Typically requires a network to be downloaded Does not require a network; runs locally.
Requirement and run.
Base Class Extends [Link] class. Does not extend any special class.
7) final vs finally vs finalize()
Basis final finally finalize()
Type Keyword Keyword (block) Method
Purpose Restricts modification — makes Ensures a block of code executes Called by garbage collector before
variable constant, method after try-catch regardless of destroying an object to perform
non-overridable, class whether an exception occurs or cleanup operations.
non-inheritable. not.
Context Used with variables, methods, Used with try-catch blocks. Used in object lifecycle
and classes. management.
Execution Enforced at compile / runtime. Always executes after try-catch, Not guaranteed to execute;
even if exception occurs. depends on the Garbage
Collector.
Example final int x = 10; finally { [Link](); } protected void finalize() { }
8) Error vs Exception
Basis Error Exception
Definition Represents serious, unrecoverable problems in Represents conditions that a program can
the JVM or system environment. reasonably catch and recover from.
Package [Link] [Link]
Recoverability Generally unrecoverable; should not be caught. Recoverable; should be caught and handled.
Cause Caused by system-level failures such as memory Caused by application-level problems such as
exhaustion or stack overflow. null pointer or divide by zero.
Examples OutOfMemoryError, StackOverflowError IOException, NullPointerException,
ArithmeticException
Handling Not recommended to handle with try-catch. Should be handled using try-catch or declared
with throws keyword.
9) String vs StringBuffer
Basis String StringBuffer
Mutability Immutable — content cannot be changed once Mutable — content can be modified in place.
created.
Memory Each modification creates a new object in the Modification occurs on the same object without
heap. creating new ones.
Performance Slower for repeated modifications due to new Faster for repeated modifications.
object creation.
Thread Safety Thread-safe due to immutability. Thread-safe due to synchronized methods.
Storage Stored in the String Pool (if literal). Stored in the heap memory.
Methods Limited modification methods: concat(), replace(). Rich set of methods: append(), insert(), delete(),
reverse().
Use Case Suitable when string content is fixed and Suitable when frequent modification of string
constant. content is needed.
10) Procedure-Oriented Programming (POP) vs Object-Oriented Programming
(OOP)
Basis Procedure-Oriented (POP) Object-Oriented (OOP)
Focus Focuses on functions and procedures. Focuses on objects and classes.
Data Security Data is global and less secure. Data is encapsulated and more secure.
Approach Top-down approach. Bottom-up approach.
Reusability Limited code reusability. High reusability through inheritance.
Data Sharing Data moves freely between functions. Data is shared through controlled methods
(getters/setters).
Real World Modelling Does not model real-world entities well. Models real-world entities naturally through
objects.
Examples C, Pascal, FORTRAN Java, C++, Python
11) Applet vs Frame vs Panel
Basis Applet Frame Panel
Definition A Java component designed to A top-level window with a title bar A generic container used to group
run in a browser or applet viewer. and borders. and organise components inside a
window.
Top-Level Acts as a top-level container in Yes, it is a top-level window No, it must be placed inside
browser context. container. another container.
Title Bar No title bar. Has a title bar and window No title bar.
controls.
Class Extends [Link] Extends [Link] Extends [Link]
Standalone Cannot run standalone without a Can run as a standalone window. Cannot run standalone.
browser / viewer.
Usage Used for web-embedded Java Used for creating desktop GUI Used to divide and organise parts
programs. windows. of a GUI layout.