11 java question important
1. What is inheritance in Java? Explain its types with example.
Inheritance is a feature in Java where one class can inherit the properties and methods of another class. The class which inherits is called the
subclass or child class, and the class which is inherited from is called the superclass or parent class.
There are mainly three types of inheritance in Java:
1. Single Inheritance: One class inherits another class.
2. Multilevel Inheritance: A class inherits a class which already inherits another class.
3. Hierarchical Inheritance: Multiple classes inherit from one superclass.
Example:
class Animal {
void sound() {
[Link]("Animal makes sound");
}
class Dog extends Animal {
void bark() {
[Link]("Dog barks");
public class Test {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
[Link]();
2. What is method overloading and method overriding? Explain with example.
Method overloading means defining multiple methods with the same name but different parameters in the same class. It is an example of
compile-time polymorphism.
Method overriding means defining the same method with same parameters in the subclass which is already present in the parent class. It is an
example of runtime polymorphism.
Example of overloading:
class Test {
void show(int a) {
[Link]("Integer: " + a);
}
void show(String b) {
[Link]("String: " + b);
Example of overriding:
class Animal {
void sound() {
[Link]("Animal sound");
}
class Cat extends Animal {
void sound() {
[Link]("Meow");
3. What is the difference between abstract class and interface in Java?
An abstract class can have both abstract methods (without body) and non-abstract methods (with body). It can also have constructors and
instance variables.
An interface only contains abstract methods (till Java 7) and static/final variables. A class can implement multiple interfaces but can inherit
only one abstract class.
Example of abstract class:
abstract class Animal {
abstract void sound();
void eat() {
[Link]("Animal eats");
Example of interface:
interface Animal {
void sound();
class Dog implements Animal {
public void sound() {
[Link]("Bark");
}
4. Explain exception handling in Java with try, catch, and finally.
Exception handling in Java is used to handle runtime errors. It prevents the program from crashing unexpectedly.
The try block contains the code which may throw an exception. The catch block handles the exception. The finally block is always executed,
whether an exception occurs or not.
Example:
public class Test {
public static void main(String[] args) {
try {
int a = 5 / 0;
} catch (ArithmeticException e) {
[Link]("Cannot divide by zero");
} finally {
[Link]("This will always execute");
5. What is the difference between array and ArrayList in Java?
An array is a fixed-size data structure that can store multiple elements of the same type. Its size cannot be changed once declared.
ArrayList is a class in Java's Collection Framework that provides dynamic arrays. The size of an ArrayList can be increased or decreased at
runtime.
Example of Array:
int[] arr = new int[5];
Example of ArrayList:
ArrayList<String> list = new ArrayList<>();
[Link]("Apple");
6. What is constructor in Java? Types of constructors with example.
A constructor is a special method which is called automatically when an object is created. It has the same name as the class and has no return
type.
Types of constructors:
1. Default constructor
2. Parameterized constructor
3. Copy constructor (manually created)
Example:
class Student {
String name;
// Default constructor
Student() {
name = "No Name";
// Parameterized constructor
Student(String n) {
name = n;
}
}
7. What is multithreading in Java? How to create a thread?
Multithreading is the ability of a program to execute multiple threads at the same time. A thread is a lightweight process.
There are two ways to create a thread in Java:
1. By extending the Thread class
2. By implementing the Runnable interface
Example using Thread class:
class MyThread extends Thread {
public void run() {
[Link]("Thread is running");
}
}
8. What is polymorphism in Java? Explain with example.
Polymorphism means "many forms". In Java, it means that the same method can behave differently depending on the object that calls it.
There are two types of polymorphism:
1. Compile-time polymorphism (method overloading)
2. Runtime polymorphism (method overriding)
Example of runtime polymorphism:
class Animal {
void sound() {
[Link]("Animal sound");
}
}
class Dog extends Animal {
void sound() {
[Link]("Dog barks");
9. What is a package in Java? How to create and use it?
A package is a group of related classes and interfaces. It helps in avoiding name conflicts and organizing classes properly.
To create a package:
package mypack;
public class A {
public void show() {
[Link]("Hello");
To use a package:
import mypack.A;
class Test {
public static void main(String[] args) {
A obj = new A();
[Link]();
}
}
10. What is the use of final, finally, and finalize in Java?
final: Used to declare constants, prevent method overriding, and inheritance.
finally: A block used in exception handling that always executes.
finalize(): A method called by garbage collector before object is destroyed.
Example:
final int a = 10;
try {
int x = 5 / 0;
} catch(Exception e) {
[Link]("Error");
} finally {
[Link]("Always runs");
protected void finalize() {
[Link]("Object destroyed");
11Q. What is JVM? What is the job of JVM? (8 marks)
JVM stands for Java Virtual Machine. It is a part of the Java Runtime Environment (JRE) and is responsible for executing Java programs. When
we write and compile a Java program, it is converted into an intermediate code called bytecode (stored in .class files). This bytecode is
platform-independent and can be executed on any machine that has a JVM installed.
The JVM takes this bytecode and converts it into machine code using its internal interpreter or Just-In-Time (JIT) compiler, so that it can be run
on the underlying hardware. This is what makes Java a platform-independent language – the same bytecode can run on Windows, Linux, or
Mac, as long as there’s a JVM.
Main responsibilities of JVM:
1. Class Loading: Loads .class files into memory.
2. Bytecode Verification: Checks for security issues or illegal code.
3. Execution: Uses interpreter or JIT compiler to run the code.
4. Memory Management: Allocates and deallocates memory using automatic garbage collection.
5. Exception Handling: Manages runtime exceptions and errors.
6. Security: Provides a secure environment for code execution.
Conclusion:
The JVM is essential for running Java applications. It handles everything from loading classes to executing code, managing memory, and
ensuring security. Because of JVM, Java programs are portable and can run on any device without modification.