CSE 1205 Theory
CSE 1205 Theory
CSE 1205 Theory
Q11. What does "static" keyword do? Define static variables and static methods.
The static keyword is used to define class-level members (variables and methods) that are shared among
all instances of the class. It allows access to these members without needing to create an object of the
class.
Static Variables (Class Variables): When a variable is declared as static, only a single copy of that variable
is created and shared among all instances of the class. They are initialized only once, at the start of the
program execution, and can be accessed using the class name.
Static Methods: Similarly, when a method is declared as static, it belongs to the class rather than to
instances of the class. Static methods can be called directly using the class name, without the need to
create an object of the class. They cannot access instance variables directly (unless through an object
reference passed to them), but they can access other static members.
Q33. Why an exception handling approach is preferable then a if-else if-else approach?
Why Exception Handling is Preferable:
1. Separation of Logic:
o Exception handling separates error-handling code from business logic, improving
code readability and maintainability.
2. Readability & Simplicity:
o Exception handling avoids deeply nested if-else chains, keeping the code clean
and easier to understand.
3. Scalability:
o It scales better with complex applications, allowing easy management of multiple
error types without cluttering the main logic.
4. Consistency:
o Exception handling provides a consistent mechanism across the application,
reducing inconsistency compared to scattered if-else blocks.
5. Unexpected Errors:
o Exceptions can handle rare and unforeseen errors more effectively, whereas if-
else is limited to predefined conditions.
6. Recovery:
o Exception handling allows for better recovery mechanisms, such as retrying an
operation or fallback logic.
7. Performance:
o Exception handling is better suited for handling rare errors, while too many if-else
checks can introduce performance overhead.
Q34. When the finally block is used? Provide a scenario in natural language for a proper explanation.
The finally block in Java is used to execute code regardless of whether an exception occurs or not. It's
typically used for resource cleanup like closing files, database connections, or releasing system
resources.
Scenario:
Imagine you're writing a program that reads data from a file. Even if an error occurs while reading the
file (e.g., the file doesn't exist), you still need to close the file or release resources to prevent resource
leaks. The finally block ensures this cleanup happens even if an exception is thrown during file reading.
Key Points:
1. Executes after try and catch, whether an exception is handled or not.
2. Commonly used for cleanup operations (closing files, connections, etc.).
3. Guarantees resource release even if exceptions occur.
Q35. Explain the ways in which a thread can arrive to "Ready" state in its lifecycle.
A thread can enter the "Ready" (Runnable) state in several ways:
1. After calling start() on a new thread.
2. Returning from a blocked or waiting state, such as after a lock is released or a wait condition is
satisfied.
3. When a thread's sleep time is over or after calling notify() on a waiting thread.
Q36. Which are the two ways that a thread can be created in Java? Why one approach is preferable to
the other one?
Two Ways to Create a Thread in Java:
1) Extending the Thread class: