2-Mark Questions with Answers
1. What is inheritance in Java?
-> Inheritance is a mechanism in Java by which one class can acquire the properties and behaviors
(fields and methods) of another class.
Syntax: class Subclass extends Superclass {}
2. Define method overloading with an example.
-> Method overloading allows multiple methods in the same class with the same name but different
parameters.
Example:
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
3. What is the use of the super keyword in Java?
-> super is used to refer to the immediate parent class object. It can be used to access parent class
methods, constructors, and variables.
4. What do you mean by dynamic method dispatch?
-> It is the process by which a call to an overridden method is resolved at runtime. It enables runtime
polymorphism in Java.
5. Mention any two types of inheritance supported in Java.
-> Single Inheritance, Multilevel Inheritance.
6. What is the role of the final keyword in Java?
-> final is used to declare constants, prevent method overriding, and inheritance of a class.
7. What is an abstract class?
-> An abstract class is a class that cannot be instantiated and may contain abstract methods
(methods without a body).
8. How do you define a package in Java?
-> Using the package keyword at the top of the source file. Example: package mypackage;
9. What is the purpose of an interface in Java?
-> An interface is used to achieve abstraction and multiple inheritance. It defines a contract for
classes to implement.
10. Differentiate between byte stream and character stream.
-> Byte streams process raw binary data (InputStream/OutputStream). Character streams process
text data (Reader/Writer).
11. What is the use of this keyword?
-> this refers to the current class object and is used to resolve ambiguity between instance variables
and parameters.
12. What is method overriding?
-> Method overriding allows a subclass to provide a specific implementation of a method already
defined in its superclass.
13. Give an example of single inheritance in Java.
-> class A { void display() {} } class B extends A { void show() {} }
14. How do you import a package in Java?
-> Using import keyword. Example: import java.util.Scanner;
15. Mention two classes used for reading console input in Java.
-> Scanner, BufferedReader.
6-Mark Questions with Answers
1. Explain different types of inheritance in Java with examples.
-> Types: Single, Multilevel, Hierarchical.
Example: class A { void msg() {} } class B extends A { void show() {} }
2. Write a program to demonstrate method overloading.
class Overload {
void show(int a) { System.out.println("int: " + a); }
void show(String a) { System.out.println("String: " + a); }
public static void main(String[] args) {
Overload obj = new Overload();
obj.show(10);
obj.show("Hello");
3. Explain the usage of this and super keywords with examples.
class A { int x = 10; } class B extends A { int x = 20; void display() { System.out.println(this.x);
System.out.println(super.x); } }
4. Differentiate between method overloading and method overriding.
Overloading: same name, different parameters, compile-time.
Overriding: same name and parameters, runtime, subclass.
5. What are abstract classes? Explain with a program.
abstract class Animal { abstract void sound(); } class Dog extends Animal { void sound() {
System.out.println("Bark"); } }
6. Explain dynamic method dispatch with an example.
class A { void show() { System.out.println("A's show"); } } class B extends A { void show() {
System.out.println("B's show"); } } class Test { public static void main(String[] args) { A obj = new B();
obj.show(); } }
7. What are packages? How are they defined and imported in Java?
package mypack; public class Demo { public void msg() {} } // another file import mypack.Demo;
8. Explain how to define and implement an interface in Java.
interface Animal { void sound(); } class Dog implements Animal { public void sound() {
System.out.println("Bark"); } }
9. Write a program to read and write console input using streams.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter name: "); String name = br.readLine();
10. Explain differences between byte streams and character streams.
Byte Stream: InputStream/OutputStream (binary).
Character Stream: Reader/Writer (text).