Object Oriented Programming using Java - Unit 3 (2 Marks Answers)
1. What is inheritance?
Inheritance allows one class to use the properties and methods of another class. The class being
inherited is the superclass, and the class that inherits is the subclass.
2. What is the purpose of the extends keyword?
The extends keyword is used to make one class inherit another. It helps the child class use the
features of the parent class.
Example: class Child extends Parent { }
3. What is the purpose of super()?
super() is used to call the constructor of the parent class from the child class. It must be the first line
in the child class constructor.
4. What are abstract classes?
Abstract classes are classes that cannot be directly created as objects. They may have abstract
methods (without body) and normal methods.
5. What are abstract methods?
Abstract methods are methods without a body, declared using the abstract keyword. Subclasses
must give them a body by overriding.
6. How to prevent methods from overriding?
To prevent a method from being overridden, use the final keyword.
Example: final void show() { }
7. What are final variables?
Final variables are constants. Once assigned, their value cannot be changed.
Example: final int MAX = 100;
8. What is the purpose of a final class?
A final class cannot be inherited by any other class. It ensures the class design remains unchanged.
Example: final class Shape { }
9. What are final methods?
Final methods cannot be overridden in subclasses. This protects the original implementation.
10. How to define a package?
A package is defined using the package keyword at the beginning of a file.
Example: package myPack;
11. How to import a package?
Use the import keyword to bring in a package or class.
Example: import java.util.Scanner;
12. List any four API packages of Java.
- java.util
- java.io
- java.lang
- java.net
13. What is the purpose of an exception handler?
Exception handlers manage errors during program execution, preventing program crashes and
allowing recovery.
14. List any 4 types of exceptions in Java.
- ArithmeticException
- NullPointerException
- ArrayIndexOutOfBoundsException
- FileNotFoundException
15. What is the purpose of try and catch block?
try is used to write code that might cause an error. catch is used to handle the error if it occurs,
preventing the program from crashing.
16. What are different methods of creating threads?
- By extending Thread class
- By implementing Runnable interface
17. List different states of a thread.
- New
- Runnable
- Blocked
- Waiting
- Terminated
18. List any four thread methods.
- start()
- run()
- sleep()
- join()
19. What is the purpose of notify() and notifyAll()?
- notify() wakes up one waiting thread.
- notifyAll() wakes up all waiting threads on the object.
20. Differentiate suspend() and stop() methods.
- suspend() pauses the thread temporarily.
- stop() completely ends the thread's execution.
21. What is the purpose of resume() method?
It restarts a thread that was suspended using suspend(). It works only if suspend() was used earlier.
22. What is the purpose of wait() and notify() methods?
- wait() makes a thread wait and release the lock.
- notify() wakes one thread waiting on the same object.