1.
Write a java program on Default or no argument Constructor
Answer:
class Test
int a, b;
Test()
a=10;
b=20;
System.out.println("Value of a: "+a);
System.out.println("Value of b: "+b);
class TestDemo
public static void main(String args[])
Test t1=new Test();
2.Write a java program on Parameterized Constructor?
Answer:
class Test
int a, b;
Test(int n1, int n2)
{
a=n1;
b=n2;
System.out.println("Value of a = "+a);
System.out.println("Value of b = "+b);
class TestDemo
public static void main(String args[])
Test t1=new Test(10, 20);
3.Write a java program using this keyword?
Answer:
class Employee
int id;
String name;
Employee(int id,String name)
this.id = id;
this.name = name;
void show()
System.out.println(id+" "+name);
}
class ThisDemo2
public static void main(String args[])
Employee e1 = new Employee(111,"Harry");
e1.show();
Output: 111 Harry
4.Write a java program on Method Overloading?
ANSWER:
class Addition
void sum(int a, int b)
System.out.println(a+b);
void sum(int a, int b, int c)
System.out.println(a+b+c);
void sum(float a, float b)
System.out.println(a+b);
class Methodload
public static void main(String args[])
{
Addition obj=new Addition();
obj.sum(10, 20);
obj.sum(10, 20, 30);
obj.sum(10.05f, 15.20f);
5.Write a java Program on constructor Overloading?
ANSWER:
public class Person
Person()
System.out.println("Introduction:");
Person(String name)
System.out.println("Name: " +name);
Person(String scname, int rollNo)
System.out.println("School name: "+scname+ ", "+"Roll no:"+rollNo);
public static void main(String[] args)
Person p1 = new Person();
Person p2 = new Person("John");
Person p3 = new Person("ABC", 12);
}
}
6.Write a java program on dynamic binding(Method Overriding)?
ANSWER:
class Animal
void eat()
System.out.println("animal is eating...");
class Dog extends Animal
void eat()
System.out.println("dog is eating...");
public static void main(String args[])
Animal a=new Dog();
a.eat();
7.write a recursive java program for finding factorial of a given number?
Answer:
public class RecursionExample3
static int factorial(int n)
{
if (n == 1)
return 1;
else
return(n * factorial(n-1));
} public static void main(String[] args)
System.out.println("Factorial of 5 is: "+factorial(5));
8.Write a java program on Member Inner class(Non-static inner class)?
Answer:
class TestMemberOuter
private int data=30;
class Inner
void msg()
System.out.println("data is "+data);
public static void main(String args[])
TestMemberOuter obj=new TestMemberOuter();
TestMemberOuter.Inner in=obj.new Inner();
in.msg();
}
9.Write a java program on super keyword at method level?
Answer:
class Animal
{ void eat()
System.out.println("eating..."); }
}class Dog extends Animal
{ void eat()
System.out.println("eating bread..."); }void dispay()
{ eat(); super.eat(); }
}class TestSuper2
public static void main(String args[])
Dog d=new Dog(); d.display(); }
10.Write a java program on final keyword at method level?
ANSWER:
class Bike
final void run()
System.out.println("running");
class Honda extends Bike
void run()
{
System.out.println("running safely with 100kmph");
public static void main(String args[])
Honda honda= new Honda();
honda.run();
11.Write a java program on abstract class?
ANSWER:
abstract class Shape
{ abstract void draw(); } class Rectangle extends Shape
{ void draw()
System.out.println("drawing rectangle");
} } class Circle1 extends Shape
{ void draw()
System.out.println("drawing circle");
} } class TestAbstraction1
{ static void main(String args[])
{ Shape s=new Circle1(); s.draw(); } }
12.Write a java program to implement multiple inheritance in java?
ANSWER:
interface Printable
{ void print(); } interface Showable
{ void show(); } class A implements Printable,Showable
{ public void print()
System.out.println("Hello");
} public void show()
System.out.println("Welcome");
} public static void main(String args[])
{ A obj = new A(); obj.print(); obj.show();
}}
13.Write a java program ON Exception Handling?
class ExceptionDemo
public static void main(String[] args)
int a=30, b=0;
try
int c=a/b;
catch (ArithmeticException e)
System.out.println("Denominator should not be zero");
System.out.println(“rest of the code”);
}
14.Write a java program on Simple Calculator?
ANSWER:
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args)
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first number: ");
int firstNumber = sc.nextInt();
System.out.print("Enter the second number: ");
int secondNumber = sc.nextInt();
System.out.print("Enter the type of operation you want to perform (+, -, *, /, %): ");
String operation = sc.next();
int result = performOperation(firstNumber, secondNumber, operation);
System.out.println("Your answer is: " + result);
public static int performOperation(int firstNumber, int secondNumber, String operation)
int result = 0;
if (operation.equals("+")) {
result = firstNumber + secondNumber;
else if (operation.equals("-")) {
result = firstNumber - secondNumber;
else if (operation.equals("*")) {
result = firstNumber * secondNumber;
}
else if (operation.equals("%")) {
result = firstNumber % secondNumber;
else if (operation.equals("/")) {
result = firstNumber / secondNumber;
else {
System.out.println("Invalid operation");
return result;
15.Write a Java program to create and display a doubly linked list?
ANSWER:
public class DoublyLinkedList {
//Represent a node of the doubly linked list
class Node{
int data;
Node previous;
Node next;
public Node(int data) {
this.data = data;
//Represent the head and tail of the doubly linked list
Node head, tail = null;
//addNode() will add a node to the list
public void addNode(int data) {
//Create a new node
Node newNode = new Node(data);
//If list is empty
if(head == null) {
//Both head and tail will point to newNode
head = tail = newNode;
//head's previous will point to null
head.previous = null;
//tail's next will point to null, as it is the last node of the list
tail.next = null;
else {
//newNode will be added after tail such that tail's next will point to newNode
tail.next = newNode;
//newNode's previous will point to tail
newNode.previous = tail;
//newNode will become new tail
tail = newNode;
//As it is last node, tail's next will point to null
tail.next = null;
//display() will print out the nodes of the list
public void display() {
//Node current will point to head
Node current = head;
if(head == null) {
System.out.println("List is empty");
return;
}
System.out.println("Nodes of doubly linked list: ");
while(current != null) {
//Prints each node by incrementing the pointer.
System.out.print(current.data + " ");
current = current.next;
public static void main(String[] args) {
DoublyLinkedList dList = new DoublyLinkedList();
//Add nodes to the list
dList.addNode(1);
dList.addNode(2);
dList.addNode(3);
dList.addNode(4);
dList.addNode(5);
//Displays the nodes present in the list
dList.display();
16. Develop an applet in Java that displays a simple message.
//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
public void paint(Graphics g){
g.drawString("welcome",150,150);
}
}
myapplet.html
<html>
<body>
<applet code="First.class" width="300" height="300">
</applet>
</body>
</html>
Or
//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
public void paint(Graphics g){
g.drawString("welcome to applet",150,150);
/*
<applet code="First.class" width="300" height="300">
</applet>
*/
17.Write a java Java program To delete a node at a specific position in doubly linked list?
ANSWER:
// Java Program to delete node at a specific position
// in Doubly Linked List
class Node {
int data;
Node prev;
Node next;
Node(int d) {
data = d;
prev = null;
next = null;
class GfG {
// Function to delete a node at a
// specific position in the doubly linked list
static Node delPos(Node head, int pos) {
// If the list is empty
if (head == null) {
return head;
Node curr = head;
// Traverse to the node at the given position
for (int i = 1; curr != null && i < pos; ++i) {
curr = curr.next;
// If the position is out of range
if (curr == null) {
return head;
// Update the previous node's next pointer
if (curr.prev != null) {
curr.prev.next = curr.next;
// Update the next node's prev pointer
if (curr.next != null) {
curr.next.prev = curr.prev;
// If the node to be deleted is the head node
if (head == curr) {
head = curr.next;
// Return the updated head
return head;
static void printList(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
System.out.println();
public static void main(String[] args) {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node head = new Node(1);
head.next = new Node(2);
head.next.prev = head;
head.next.next = new Node(3);
head.next.next.prev = head.next;
head = delPos(head, 2);
printList(head);
18. Java Program to print the sum of all the items of the array?
Answer:
public class SumOfArray {
public static void main(String[] args) {
//Initialize array
int [] arr = new int [] {1, 2, 3, 4, 5};
int sum = 0;
//Loop through the array to calculate sum of elements
for (int i = 0; i < arr.length; i++) {
sum = sum + arr[i];
System.out.println("Sum of all the elements of an array: " + sum);
Output:
Sum of all the elements of an array: 15
19.Write a java program to check string palindrome or not?
Answer:
class SPalin {
public static void main(String[] args) {
String str = "Radar", reverseStr = "";
int strLength = str.length();
for (int i = (strLength - 1); i >=0; --i) {
reverseStr = reverseStr + str.charAt(i);
if (str.toLowerCase().equals(reverseStr.toLowerCase())) {
System.out.println(str + " is a Palindrome String.");
else {
System.out.println(str + " is not a Palindrome String.");
20.Write a java program on user defined package?
Answer:
package com.example;
public class Calculator {
public int add(int a, int b) {
return a + b;
public int subtract(int a, int b) {
return a - b;
public int multiply(int a, int b) {
return a * b;
public int divide(int a, int b) {
if (b != 0) {
return a / b;
} else {
throw new ArithmeticException("Cannot divide by zero!");
Compile above code like javac -d . Calculator.java
PackageExample.java
import com.example.Calculator;
public class PackageExample {
public static void main(String[] args) {
Calculator calculator = new Calculator();
int result = calculator.add(5, 3);
System.out.println("Addition: " + result);
result = calculator.subtract(5, 3);
System.out.println("Subtraction: " + result);
result = calculator.multiply(5, 3);
System.out.println("Multiplication: " + result);
result = calculator.divide(10, 2);
System.out.println("Division: " + result);
Compile: javac PackageExample.java
Execute: java PackageExample
Output:
Addition: 8
Subtraction: 2
Multiplication: 15
Division: 5