Java Question Bank Unit 2 (1)
Java Question Bank Unit 2 (1)
✅ Q1. Explain the different types of inheritance in Java. Write a program to demonstrate the use
of the super keyword.
🔸 Note: Java does not support multiple inheritance with classes (to avoid ambiguity), but it’s
possible with interfaces.
🔹 super Keyword:
class Animal {
String color = "Brown";
void sound() {
System.out.println("Animal makes a sound");
}
}
void printColor() {
System.out.println(super.color); // Access parent color
}
void sound() {
super.sound(); // Call parent method
System.out.println("Dog barks");
}
}
🔹 Interface:
A reference type in Java, like a class, but only abstract methods and constants.
Implements 100% abstraction.
Use interface keyword.
🔹 Differences:
interface Printable {
void print();
}
🔹 Abstract Class:
✅ Example:
🔹 instanceof:
✅ Syntax:
✅ Example:
class Animal {}
class Dog extends Animal {}
✅ Output:
true
true
✅ Q5. What is the use of the final keyword in Java?
🔹 final keyword:
✅ Example:
✅ Q6. What is a package in Java? How to create a user-defined package in Java with an example.
🔹 What is a Package?
A package in Java is a namespace that organizes a set of related classes and interfaces. Think of it as
a folder in your file system.
Benefits:
🔹 Types of Packages:
// File: mypack/Message.java
package mypack;
// File: Test.java
import mypack.Message;
🔹 Method Overloading:
Method overloading means having more than one method with the same name in a class, but with
different parameter types, order, or number.
🔹 Rules:
✅ Example:
class Calculator {
int add(int a, int b) {
return a + b;
}
Used to define class-level members (variables, methods, blocks, or nested classes) that are shared
across all instances of the class.
🔹 Uses of static:
✅ Example:
class Counter {
static int count = 0; // static variable
Counter() {
count++;
System.out.println(count);
}
🔹 Constructor Overloading:
Just like method overloading, you can have multiple constructors with different parameter lists in
the same class. This helps in initializing objects in different ways.
✅ Example:
class Student {
String name;
int age;
// Default constructor
Student() {
name = "Unknown";
age = 0;
}
// Parameterized constructor
Student(String n, int a) {
name = n;
age = a;
}
void display() {
System.out.println(name + " is " + age + " years old.");
}
}
🔹 Purpose of Interface:
To define a contract or capability that a class must implement (like Runnable, Comparable).
Promotes loose coupling and multiple inheritance (since a class can implement multiple
interfaces).
✅ Example:
interface Drawable {
void draw();
}
🔹 Multilevel Inheritance:
It refers to a class inheriting from a class that itself inherits from another class, forming a hierarchy.
✅ Example:
class Animal {
void eat() {
System.out.println("Eating...");
}
}
🔹 Constructor:
A constructor is a special method that is automatically called when an object is created. It has the
same name as the class and no return type.
🔹 Significance:
Initializes objects.
Can be overloaded to initialize differently.
No need to call it explicitly.
class Display {
void show(String name) {
System.out.println("Name: " + name);
}
✅ Q13. What is constructor? Write a program to pass a value to the base class constructor from
subclass constructor.
🔹 Concept:
We use super() to call the constructor of the base class from the subclass.
✅ Example:
class Person {
String name;
Person(String n) {
name = n;
System.out.println("Person Constructor: " + name);
}
}
🔹 Static Variable:
A variable declared with the static keyword belongs to the class, not individual objects.
✅ Features:
class Counter {
static int count = 0; // static variable
Counter() {
count++;
System.out.println(count);
}
}
🔹 Static Method:
✅ Example:
class MathUtil {
static int square(int n) {
return n * n;
}
}
🔹 String:
A sequence of characters. In Java, strings are objects of the String class and are immutable.
Method Description
length() Returns string length
charAt(int index) Returns character at specified index
substring() Extracts a substring
concat() Combines two strings
equals() Compares content of two strings
compareTo() Lexicographic comparison
toLowerCase() Converts to lowercase
toUpperCase() Converts to uppercase
Method Description
trim() Removes leading and trailing spaces
replace() Replaces characters
✅ Example:
✅ Example of Overriding:
class Animal {
void sound() {
System.out.println("Animal Sound");
}
}
✅ Example of Overloading:
class Calculator {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
}
✅ Q18. What is type casting? Illustrate with example. What is meant by automatic type
promotion?
🔹 Type Casting:
int a = 10;
double b = a; // int to double
2. Narrowing/Explicit Casting (possible data loss):
double a = 10.5;
int b = (int) a; // double to int
✅ Example:
public class Demo {
public static void main(String[] args) {
byte b = 10;
int i = b + 20; // byte promoted to int
System.out.println(i);
}
}
✅ Q19. Write a Java program to sort the given names into ascending order using array.
import java.util.Arrays;
System.out.println("Sorted Names:");
for (String name : names) {
System.out.println(name);
}
}
}
✅ Q20. How is a multidimensional array represented in Java? Write code to create a 2×2 array.
🔹 Multidimensional Array:
✅ Example:
public class Matrix {
public static void main(String[] args) {
int[][] arr = new int[2][2]; // 2x2 array
arr[0][0] = 1;
arr[0][1] = 2;
arr[1][0] = 3;
arr[1][1] = 4;
🔹 Generics:
They allow you to write type-safe code by working with different data types using a single class.
✅ Example:
class Box<T> {
T value;
T getValue() {
return value;
}
}
✅ Q22. Explain why the main function is declared as public static void main(String[] args).
🔹 Explanation:
✅ Example:
public class Test {
public static void main(String[] args) {
System.out.println("Hello Java!");
}
}
🔹 1. public
public int a;
🔹 2. private
private int a;
🔹 3. protected
Accessible within the same package and in subclasses (even in different packages).
protected int a;
public ✅ ✅ ✅ ✅
default ✅ ✅ ❌ ❌
private ✅ ❌ ❌ ❌
Occurs at runtime.
Used when a method is overridden in a subclass.
JVM decides which method to call based on actual object type, not reference type.
✅ Example:
class Animal {
void sound() {
System.out.println("Animal sound");
}
}