Java Programming Question Bank with Solutions
This PDF contains important Java programming questions with complete solutions and explanations. The questions
are designed to help you learn syntax, logic, concepts, and problem solving for university practicals, assignments,
viva, and exams. Each section starts with a quick overview of the topic.
Classes and Objects
Quick Info: Objects are real-world entities and classes are blueprints used to create objects.
Q1. Create a class Student with attributes name and roll number. Create objects and display data.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
class Student {
String name;
int rollNo;
void display() {
[Link]("Name: " + name);
[Link]("Roll No: " + rollNo);
}
public static void main(String[] args) {
Student s1 = new Student();
[Link] = "Ali";
[Link] = 101;
[Link]();
}
}
Q2. Create a Rectangle class with length and breadth. Calculate area.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
class Rectangle {
int length, breadth;
int area() {
return length * breadth;
}
public static void main(String[] args) {
Rectangle r = new Rectangle();
[Link] = 10;
[Link] = 5;
[Link]("Area = " + [Link]());
}
}
Q3. Create a Car class and demonstrate multiple objects.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
class Car {
String brand;
void show() {
[Link]("Brand: " + brand);
}
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car();
[Link] = "Toyota";
[Link] = "Honda";
[Link]();
[Link]();
}
}
Q4. Write a program to initialize object values using methods.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
class Employee {
int id;
String name;
void setData(int i, String n) {
id = i;
name = n;
}
void display() {
[Link](id + " " + name);
}
public static void main(String[] args) {
Employee e = new Employee();
[Link](1, "Ahmed");
[Link]();
}
}
Q5. Demonstrate object as method argument.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
class Test {
int value;
void change(Test t) {
[Link] = 100;
}
public static void main(String[] args) {
Test t1 = new Test();
[Link] = 10;
[Link](t1);
[Link]([Link]);
}
}
Method Overloading
Quick Info: Method overloading means same method name with different parameters.
Q1. Write a program to overload add() method.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
class Demo {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
Demo d = new Demo();
[Link]([Link](5, 6));
[Link]([Link](5.5, 6.5));
}
}
Q2. Overload constructor with different parameters.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
class Box {
Box() {
[Link]("Default Constructor");
}
Box(int x) {
[Link]("Value: " + x);
}
public static void main(String[] args) {
new Box();
new Box(10);
}
}
Q3. Create overloaded methods for area calculation.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
class Area {
int area(int side) {
return side * side;
}
int area(int l, int b) {
return l * b;
}
public static void main(String[] args) {
Area a = new Area();
[Link]([Link](4));
[Link]([Link](4, 5));
}
}
Method Overriding
Quick Info: Method overriding allows child class to redefine parent class method.
Q1. Create parent and child class to override show() method.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
class Animal {
void sound() {
[Link]("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
[Link]("Dog barks");
}
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
}
}
Q2. Demonstrate runtime polymorphism.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
class A {
void display() {
[Link]("Class A");
}
}
class B extends A {
void display() {
[Link]("Class B");
}
public static void main(String[] args) {
A obj = new B();
[Link]();
}
}
Constructors
Quick Info: Constructors initialize objects automatically when objects are created.
Q1. Write a program using default constructor.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
class Test {
Test() {
[Link]("Constructor called");
}
public static void main(String[] args) {
new Test();
}
}
Q2. Demonstrate parameterized constructor.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
class Student {
int id;
String name;
Student(int i, String n) {
id = i;
name = n;
}
void display() {
[Link](id + " " + name);
}
public static void main(String[] args) {
Student s = new Student(1, "Sara");
[Link]();
}
}
Q3. Demonstrate constructor chaining using this().
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
class Demo {
Demo() {
this(10);
[Link]("Default");
}
Demo(int x) {
[Link](x);
}
public static void main(String[] args) {
new Demo();
}
}
Exception Handling
Quick Info: Exception handling prevents abnormal program termination.
Q1. Handle divide by zero exception.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
class ExceptionDemo {
public static void main(String[] args) {
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
[Link]("Cannot divide by zero");
}
}
}
Q2. Use finally block in exception handling.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
class FinallyDemo {
public static void main(String[] args) {
try {
int x = 10 / 2;
[Link](x);
} finally {
[Link]("Finally block executed");
}
}
}
Q3. Demonstrate multiple catch blocks.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
class MultiCatch {
public static void main(String[] args) {
try {
int arr[] = new int[2];
arr[5] = 10;
} catch (ArithmeticException e) {
[Link]("Arithmetic Exception");
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("Array Index Exception");
}
}
}
Inheritance and Types
Quick Info: Inheritance helps one class acquire properties of another class.
Q1. Demonstrate single inheritance.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
class Animal {
void eat() {
[Link]("Eating");
}
}
class Dog extends Animal {
void bark() {
[Link]("Barking");
}
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
[Link]();
}
}
Q2. Demonstrate multilevel inheritance.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
class A {
void showA() {
[Link]("A");
}
}
class B extends A {
void showB() {
[Link]("B");
}
}
class C extends B {
void showC() {
[Link]("C");
}
public static void main(String[] args) {
C obj = new C();
[Link]();
[Link]();
[Link]();
}
}
Q3. Demonstrate hierarchical inheritance.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
class Parent {
void display() {
[Link]("Parent");
}
}
class Child1 extends Parent {}
class Child2 extends Parent {}
class Demo {
public static void main(String[] args) {
Child1 c1 = new Child1();
Child2 c2 = new Child2();
[Link]();
[Link]();
}
}
Abstract Class
Quick Info: Abstract classes may contain abstract and non-abstract methods.
Q1. Create abstract class with abstract method.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
[Link]("Drawing Circle");
}
public static void main(String[] args) {
Circle c = new Circle();
[Link]();
}
}
Q2. Demonstrate abstract class constructor.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
abstract class Vehicle {
Vehicle() {
[Link]("Vehicle Constructor");
}
abstract void run();
}
class Bike extends Vehicle {
void run() {
[Link]("Bike running");
}
public static void main(String[] args) {
Bike b = new Bike();
[Link]();
}
}
Interface
Quick Info: Interfaces provide complete abstraction and support multiple inheritance.
Q1. Create simple interface implementation.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
interface Animal {
void sound();
}
class Cat implements Animal {
public void sound() {
[Link]("Meow");
}
public static void main(String[] args) {
Cat c = new Cat();
[Link]();
}
}
Q2. Demonstrate multiple inheritance using interfaces.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
interface A {
void showA();
}
interface B {
void showB();
}
class Demo implements A, B {
public void showA() {
[Link]("A");
}
public void showB() {
[Link]("B");
}
public static void main(String[] args) {
Demo d = new Demo();
[Link]();
[Link]();
}
}
Threads
Quick Info: Threads enable multitasking and concurrent execution in Java.
Q1. Create thread using Thread class.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
class MyThread extends Thread {
public void run() {
[Link]("Thread Running");
}
public static void main(String[] args) {
MyThread t = new MyThread();
[Link]();
}
}
Q2. Create thread using Runnable interface.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
class Demo implements Runnable {
public void run() {
[Link]("Runnable Thread");
}
public static void main(String[] args) {
Thread t = new Thread(new Demo());
[Link]();
}
}
Q3. Use sleep() method in thread.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
class SleepDemo extends Thread {
public void run() {
try {
for(int i=1;i<=3;i++) {
[Link](i);
[Link](1000);
}
} catch(Exception e) {
[Link](e);
}
}
public static void main(String[] args) {
new SleepDemo().start();
}
}
Packages
Quick Info: Packages organize Java classes and avoid naming conflicts.
Q1. Create and use package in Java.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
package mypack;
public class Hello {
public void show() {
[Link]("Package Example");
}
}
Q2. Import package and use class.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
import [Link];
class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter name:");
String name = [Link]();
[Link](name);
}
}
Applets
Quick Info: Applets are small Java programs that run in browsers or applet viewers.
Q1. Create basic applet.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
import [Link];
import [Link];
public class MyApplet extends Applet {
public void paint(Graphics g) {
[Link]("Hello Applet", 100, 100);
}
}
Q2. Draw shapes using Graphics class.
The following solution demonstrates the correct syntax and logic for the problem. Carefully observe class
declarations, object creation, method calls, loops, exception blocks, and inheritance relationships depending on the
topic.
import [Link];
import [Link];
public class Shapes extends Applet {
public void paint(Graphics g) {
[Link](50, 50, 100, 80);
[Link](200, 50, 100, 80);
[Link](50, 200, 200, 200);
}
}