Java Programming (22412) - Important Questions and Answers
Unit 4: Classes and Objects
Q7. Class and object program:
class Student {
int rollNo;
String name;
void display() {
System.out.println("Roll No: " + rollNo + ", Name: " + name);
}
}
Q8. Types of constructors:
1. Default Constructor: No parameters.
2. Parameterized Constructor: Accepts parameters to initialize objects.
Q9. this keyword:
Used to refer to the current object. Often used to resolve variable shadowing.
Unit 5: Inheritance and Interfaces
Q10. Types of inheritance in Java:
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
Note: Java does not support multiple inheritance via classes but supports it via interfaces.
Q11. Abstract class vs Interface:
Abstract Class:
- Can have both abstract and concrete methods.
- Can include constructors and instance variables.
Interface (Java 7):
- Contains only abstract methods.
- All methods are public and abstract by default.
Q12. Method Overriding:
When a child class redefines a parent class method using the same name and parameters.
Unit 6: Exception and File Handling
Q13. Types of exceptions:
Checked: Checked at compile-time (e.g., IOException).
Unchecked: Occurs at runtime (e.g., ArithmeticException).
Q14. try-catch-finally example:
try {
int a = 5 / 0;
} catch (ArithmeticException e) {
System.out.println("Exception: " + e);
} finally {
System.out.println("Always executes");
}
Q15. FileReader/FileWriter example:
FileWriter fw = new FileWriter("file.txt");
fw.write("Hello Java");
fw.close();
FileReader fr = new FileReader("file.txt");
int i;
while ((i = fr.read()) != -1)
System.out.print((char)i);
fr.close();
Q16. Difference between JDBC and ODBC:
JDBC (Java Database Connectivity):
- Java API for connecting and executing queries with databases.
- Platform-independent.
- Designed for Java applications.
ODBC (Open Database Connectivity):
- Language-independent API.
- Requires native libraries and drivers.
- Commonly used in Windows environments.