a) Explain the concept of scope and lifetime of variables in Java.
Scope refers to the region of the program where a variable is accessible. In Java, variables can be
declared inside a method (local), inside a class (instance or static), or as parameters. Lifetime refers
to how long the variable exists in memory. Local variables exist during method execution, instance
variables exist as long as the object is alive, and static variables exist for the duration of the
program.
b) Describe the difference between a constructor and a method in Java.
A constructor is used to initialize objects and is called when an object is created. It has the same
name as the class and no return type. A method performs operations and can return values.
Methods must have a return type and can be called multiple times after object creation.
c) What is the meaning of 'static' keyword in Java? Explain the difference between a static
method and an instance method.
The 'static' keyword indicates that the member belongs to the class rather than instances. A static
method can be called without creating an object, while an instance method needs an object. Static
methods cannot access instance variables directly.
d) Describe the usage of the 'final' keyword in Java. How does it apply to methods, classes,
and variables?
The 'final' keyword prevents modification. Final variables cannot be reassigned, final methods
cannot be overridden, and final classes cannot be extended.
e) Explain the difference between checked and unchecked exceptions.
Checked exceptions are checked at compile time (e.g., IOException), requiring try-catch or throws.
Unchecked exceptions occur at runtime (e.g., NullPointerException) and do not require explicit
handling.
f) Differentiate between AWT and Swing in Java.
AWT is Java's original GUI toolkit, using native OS components (heavyweight). Swing is built on
AWT but is lightweight and provides more advanced components like JTable, JTree, etc.
g) What is a Configuration manager in Java AWT?
There is no standard component called 'Configuration Manager' in AWT. Possibly, it refers to layout
managers or classes managing UI component properties.
h) Compare the key differences between a Swing-based GUI application and an applet in
Java.
Swing applications run as standalone desktop applications with rich GUI components. Applets run in
browsers or applet viewers and are more restricted in functionality and security.
i) What is dynamic method dispatch in Java? Give an example.
Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at
runtime. Example:
class A { void show() { System.out.println("A"); } }
class B extends A { void show() { System.out.println("B"); } }
A obj = new B(); obj.show(); // prints B
j) Explain the difference between a static method and an instance method.
Static methods belong to the class and can be called without an object. Instance methods belong to
objects and can access instance variables. Static methods can't access instance data directly.