Java End Semester Exam Solutions
Question 1(a)
The `super` keyword in Java is used to:
1. Call parent class constructor using `super()`.
2. Access parent class methods using `[Link]()`.
3. Access parent class variables using `[Link]`.
Question 1(b)
Difference between `throw` and `throws`:
- `throw`: Used to actually throw an exception. (e.g. `throw new IOException();`)
- `throws`: Declares that a method may throw exceptions. (e.g. `void m() throws
IOException`)
Question 1(c)
Output:
Code:
main(1); → prints 2 (1*2)
main(2,3); → prints 5 (2+3)
Final output: 25
Question 1(d)
Output is 8. Class B overrides display() and prints i + 5, where i = 3.
Question 1(e)
false
false
true
true
(Comparison using == checks references, equals() checks values)
Question 1(f)
Syntax for ActionListener:
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
// action
}
});
Question 1(g)
Thread priority is set using `setPriority()` (1 to 10).
Higher priority threads are given more CPU time.
Question 1(h)
An abstract class may or may not have abstract methods.
Abstract methods cannot be private.
Question 1(i)
Yes, a try block without catch is valid if followed by finally.
Question 1(j)
`ResultSet` in JDBC stores result of SQL queries and allows accessing rows and columns.
Question 2(a)
Class Month example included with methods to display working days and compare months.
Question 2(b)
Byte stream (InputStream/OutputStream) handles bytes.
Character stream (Reader/Writer) handles text.
File copy using byte stream program provided.
Question 3(a)
Thread program to print odd and even numbers using two threads provided.
Question 3(b)
Default package imported: [Link]
Yes, same class can be imported twice.
Sub-packages must be imported separately.
Question 4
Scanner program provided to:
- Accept two strings
- Concatenate
- Split into parts
- Display all 4 strings.
Question 4(b)
Dynamic Method Dispatch:
Base class reference can refer to subclass object and call overridden methods at runtime.
Question 5(a)
Checked vs Unchecked exceptions tabulated.
User-defined exception `CheckArgumentException` implemented to check argument count.
Question 5(b)
Java Swing calculator with buttons for +, -, *, / using ActionListener.
Question 6(a)
Thread synchronization example using synchronized method to ensure thread-safe
increments.
Question 6(b)
JDBC program to connect to MySQL and display student records using ResultSet.