UNIT II JAVA
UNIT II JAVA
Inheritance
1. Basic Concepts
o Definition and purpose of inheritance.
o Examples of real-world inheritance in programming.
o Benefits of inheritance (code reuse, polymorphism, etc.).
2. Types of Inheritance
o Single, Multilevel, Hierarchical, Multiple, and Hybrid inheritance.
o Examples and diagrams for each type.
3. Member Access Rules
o Public, Private, Protected, and Default access modifiers.
o Accessibility in parent and child classes.
4. Keywords: this and super
o Usage of this for the current object reference.
o Usage of super to access parent class members and constructors.
5. Method Overloading and Overriding
o Difference between overloading and overriding.
o Rules for overriding methods.
o Examples of polymorphism.
6. Abstract Classes
o Definition and purpose.
o Abstract methods and implementation in derived classes.
7. Dynamic Method Dispatch
o Runtime polymorphism.
o How method calls are resolved dynamically.
8. Final Keyword
o Usage of final with variables, methods, and classes.
o Examples and restrictions.
Packages
1. Definition
o What are packages and why are they used?
o Built-in vs user-defined packages.
2. Access Protection
o Public, Protected, Private, and Default access in packages.
o Examples of package-level access.
3. Importing Packages
o Syntax for importing packages (import statement).
o Static imports and wildcard imports.
Interfaces
1. Definition
o Purpose of interfaces in Java.
o Difference between interfaces and abstract classes.
2. Implementation
o How to implement interfaces.
o Example programs.
3. Extending Interfaces
o Concept of multiple inheritance using interfaces.
o Nested and extended interfaces.
Exception Handling
1. Try-Catch
o Syntax and usage of try and catch blocks.
o Nested try-catch blocks.
2. Throw and Throws
o Usage of throw for throwing exceptions.
o Usage of throws for declaring exceptions.
3. Finally Block
o Purpose and execution of finally block.
4. Built-in Exceptions
o Common exceptions like NullPointerException,
ArrayIndexOutOfBoundsException, etc.
5. Custom Exception Classes
o How to create user-defined exceptions.
o Example program.
Example Questions
MCQ Examples
1. Which of the following is not a type of inheritance in Java?
a) Single
b) Multiple
c) Hybrid
d) Multilevel
(Correct answer: b)
2. What is the purpose of the super keyword?
a) To call the current class method.
b) To call the parent class constructor.
c) To create an abstract method.
d) None of the above.
(Correct answer: b)
5 Marks Examples
1. Explain the difference between method overloading and method
overriding with examples.
2. Write a program to demonstrate the use of the final keyword in Java.
10 Marks Examples
1. Write a program to demonstrate dynamic method dispatch in Java.
Explain the output.
2. Create a user-defined exception class to handle invalid input and
demonstrate its usage with a program.
Inheritance: Basic Concepts
1. What is inheritance in Java?
a) A way to create unrelated classes
b) A mechanism to acquire properties of one class into another
c) A process to overload methods
d) A way to implement polymorphism
Answer: b
2. Which of these is NOT a type of inheritance in Java?
a) Single
b) Multiple
c) Multilevel
d) Hybrid
Answer: b
3. Which keyword is used to inherit a class in Java?
a) inherit
b) extends
c) super
d) implements
Answer: b
Abstract Classes
10.What is an abstract class?
a) A class that cannot be instantiated
b) A class with only static methods
c) A class with private constructors
d) A class without any methods
Answer: a
11.Which keyword is used to declare an abstract class?
a) abstract
b) virtual
c) base
d) None of the above
Answer: a
Final Keyword
13.What does the final keyword prevent?
a) Overriding a method
b) Extending a class
c) Reassigning a variable
d) All of the above
Answer: d
Packages
14.What is a package in Java?
a) A collection of classes and interfaces
b) A method of encapsulation
c) A predefined library
d) None of the above
Answer: a
15.Which keyword is used to import a package?
a) package
b) import
c) include
d) use
Answer: b
16.What does access protection in packages ensure?
a) Only public members are accessible
b) Restricted access to certain members
c) No access restrictions
d) None of the above
Answer: b
Interfaces
17.What is an interface?
a) A class with only static methods
b) A blueprint for a class with abstract methods
c) A class with final methods
d) None of the above
Answer: b
18.How is an interface implemented in Java?
a) Using the extends keyword
b) Using the implements keyword
c) Using the interface keyword
d) Using the inherits keyword
Answer: b
19.Can an interface extend another interface?
a) Yes
b) No
Answer: a
Exception Handling
20.Which block is always executed in exception handling?
a) try
b) catch
c) finally
d) throw
Answer: c
21.What is the purpose of the throw keyword?
a) To define a new exception
b) To explicitly throw an exception
c) To handle an exception
d) To terminate a program
Answer: b
22.Which exception is a checked exception?
a) NullPointerException
b) IOException
c) ArithmeticException
d) ArrayIndexOutOfBoundsException
Answer: b
23.What is the purpose of the throws keyword?
a) To throw multiple exceptions
b) To declare exceptions in a method signature
c) To catch exceptions
d) None of the above
Answer: b
24.What happens if an exception is not caught?
a) The program continues
b) The program terminates
c) The exception is ignored
d) None of the above
Answer: b
25.Which method is used to retrieve the description of an exception?
a) getMessage()
b) printStackTrace()
c) getCause()
d) None of the above
Answer: a