Java Exam Answers
Q1. Attempt all questions: (1x5 = 5 Marks)
a) Lifecycle of thread:
- New, Runnable, Running, Blocked/Waiting, Terminated
b) Laptop as an object:
- Attributes: RAM, processor
- Methods: turnOn(), shutDown()
c) Narrowing, Widening, Upcasting:
- Narrowing: double d = 10.5; int i = (int) d;
- Widening: int i = 10; double d = i;
- Upcasting: Dog d = new Dog(); Animal a = d;
d) Platform-independent nature of Java:
- Java bytecode runs on JVM which is platform-independent.
e) Static and instance blocks:
- Static block runs once when class loads.
- Instance block runs with every object.
Q2. Attempt all questions (2.5x4 = 10 Marks)
a) i) Object initialization:
- new keyword, constructor, clone, factory method
ii) Interfaces vs abstract class:
- Supports multiple inheritance, flexibility, contract definition
b) i) public static void main(String[] args):
- public: accessible, static: no object, void: no return, main: entry, String[]: args
ii) No multiple inheritance:
- Java uses interfaces to achieve it
c) i) Overloading, Overriding, Hiding:
- Overloading: same name, diff params
- Overriding: subclass redefines
- Hiding: static methods
ii) Variable types:
- Instance: object-level
- Static: class-level
- Local: method-level
- final: constant
d) i) super() in inheritance:
- Calls parent class constructor
ii) Immutability of String:
- String objects cannot be modified after creation
Q3. Attempt any one (5x1 = 5 Marks)
i) Creating Threads:
- Extend Thread or implement Runnable
ii) Exception Handling:
- try-catch block
- Checked: compile-time (IOException)
- Unchecked: runtime (ArithmeticException)