Java Imp
Java Imp
Q1. Write a program to accept a number from user and generate multiplication table of a
number
:-
import java.util.Scanner;
scanner.close();
Q2. Construct a linked List containing names of colours: red, blue, yellow and orange. Then
ii. Display the contents of the List in reverse order using a ListIterator
iii. Create another list containing pink and green. Insert the elements of this list between
:-
import java.util.*;
public class ColorLinkedList {
colorList.add("red");
colorList.add("blue");
colorList.add("yellow");
colorList.add("orange");
while (iterator.hasNext()) {
System.out.println(iterator.next());
// ii. Display the contents of the List in reverse order using a ListIterator
while (listIterator.hasPrevious()) {
System.out.println(listIterator.previous());
// iii. Create another list containing pink and green. Insert elements between blue and yellow
newColors.add("pink");
newColors.add("green");
while (insertionPoint.hasNext()) {
if (insertionPoint.next().equals("blue")) {
break;
System.out.println("\nUpdated list with pink and green inserted between blue and yellow:");
System.out.println(color);
Slip 2
Q1. Write a program to accept ‘n’ integers from the user & store them in an Array List collection.
:-
import java.util.ArrayList;
import java.util.Scanner;
// Read 'n' integers from the user and add them to the ArrayList
arrayList.add(num);
scanner.close();
Q2. Define a class MyNumber having one private integer data member. Write a default constructor
above operations.
:-
OR
Q2. Write a program to accept Doctor Name from the user and check whether it is valid or not.(It
should not contain digits and special symbol) If it is not valid then throw user defined Exception -
Name is Invalid -- otherwise display it
:-
import java.util.Scanner;
class InvalidNameException extends Exception {
super(message);
try {
validateDoctorName(doctorName);
} catch (InvalidNameException e) {
} finally {
scanner.close();
if (name.matches(".[0-9!@#$%^&()_+\\-=\\[\\]{};':\"\\\\|,.<>/?].*")) {
}
Slip 3
Q1. Write a program to accept the 'n' different numbers from user and store it in array. Also
:-
import java.util.Scanner;
int n = scanner.nextInt();
// Prompt the user to enter 'n' different numbers and store them in the array
arr[i] = scanner.nextInt();
int sum = 0;
sum += arr[i];
scanner.close();
Q2. Write a program to create class Account (accno, accname, balance). Create an array of 'n'
Account objects. Define static method “sortAccount” which sorts the array on the basis of balance.
Display account details in sorted order.
:-
OR
Q2.Write a program to copy the contents from one file into another file in upper case
:-
import java.io.*;
try {
String line;
fileWriter.write(upperCaseLine + "\n");
fileReader.close();
bufferedReader.close();
fileWriter.close();
} catch (IOException e) {
Slip 4
Q1. Write a program to accept the user name and greets the user by name. Before displaying the
user's name, convert it to upper case letters. For example, if the user's name is Raj, then display
:-
import java.util.Scanner;
scanner.close();
Q2. Write a program which define class Product with data member as id, name and price. Store
the information of 5 products and Display the name of product having minimum price (Use arrayof
object
:-
class Product {
int id;
String name;
double price;
this.id = id;
this.name = name;
this.price = price;
class ProductDemo {
minPriceProduct = products[i];
Slip 5
Q1. Write a program to accept a number from the user, if number is zero then throw user defined
exception ―Number is 0, otherwise display factorial of a number.
:-
import java.util.Scanner;
public NumberIsZeroException() {
super("Number is 0");
if (num == 0 || num == 1) {
return 1;
} else {
try {
if (num == 0) {
} else {
long factorial = calculateFactorial(num);
} catch (NumberIsZeroException e) {
} catch (java.util.InputMismatchException e) {
} finally {
scanner.close();
Q2. Define a “Point” class having members – x,y (coordinates). Define default constructor and
parameterized constructors. Define subclass “ColorPoint” with member as color. Write display
:-
class Point {
int x;
int y;
// Default constructor
public Point() {
this.x = 0;
this.y = 0;
// Parameterized constructor
this.x = x;
this.y = y;
// Parameterized constructor
super(x, y);
this.color = color;
@Override
System.out.println("ColorPoint: (" + super.x + ", " + super.y + "), Color: " + color);
point1.display();
point2.display();
colorPoint.display();
Slip 6
Q1. Accept 'n' integers from the user and store them in a collection. Display them in the sorted
order. The collection should not accept duplicate elements. (Use a suitable collection). Search for a
particular
:-
import java.util.Scanner;
import java.util.TreeSet;
int n = scanner.nextInt();
numbers.add(num);
if (numbers.contains(searchElement)) {
} else {
scanner.close();
Q2. Write a program which define class Employee with data member as id, name and salary Store
the information of 'n' employees and Display the name of employee having maximum salary (Use
array of object).
:-
import java.util.Scanner;
class Employee {
int id;
String name;
double salary;
this.id = id;
this.name = name;
this.salary = salary;
}
class EmployeeMain {
int n = sc.nextInt();
System.out.print("ID: ");
int id = sc.nextInt();
System.out.print("Name: ");
System.out.print("Salary: ");
maxSalaryEmployee = employees[i];
}
}
sc.close();
Slip 7
Q1. Create a Hash table containing Employee name and Salary. Display the details of the hash
table.
:-
import java.util.Hashtable;
import java.util.Scanner;
employeeSalaries.put("John", 50000.0);
employeeSalaries.put("Alice", 60000.0);
employeeSalaries.put("Bob", 55000.0);
employeeSalaries.put("Eve", 62000.0);
System.out.println("Employee Details:");
Q2. Define a class student having rollno, name and percentage. Define Default and
parameterized constructor. Accept the 5 student details and display it. (Use this keyword
:-
import java.util.Scanner;
class Student {
int rollNo;
String name;
double percentage;
// Default constructor
public Student() {
this.rollNo = 0;
this.name = "";
this.percentage = 0.0;
// Parameterized constructor
this.rollNo = rollNo;
this.name = name;
this.percentage = percentage;
System.out.println();
class Main {
System.out.print("Name: ");
System.out.print("Percentage: ");
System.out.println("Student Details:");
student.displayDetails();
}
Slip 8
Q1. Write a program to reverse a number. Accept number using command line argument. [10
Marks]
:-
if (args.length != 1) {
return;
try {
} catch (NumberFormatException e) {
int reversedNumber = 0;
while (number != 0) {
number /= 10;
return reversedNumber;
}
Q2. Define a class MyDate (day, month, year) with methods to accept and display MyDate object.
Accept date as dd, mm, yyyy. Throw user defined exception “InvalidDateException” ifthe date is
:-
super(message);
class MyDate {
this.day = day;
this.month = month;
this.year = year;
return false;
}
switch (month) {
case 4:
case 6:
case 9:
case 11:
maxDays = 30;
break;
case 2:
break;
try {
date1.displayDate();
} catch (InvalidDateException e) {
}
try {
date2.displayDate();
} catch (InvalidDateException e) {
try {
date3.displayDate();
} catch (InvalidDateException e) {
Slip 9
Q1. Write a program to accept a number from user. Check whether number is perfect or not. Use
BufferedReader class for accepting input from user. [10 Marks]
:-
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
} else {
if (num <= 1) {
return false;
int sum = 1;
if (num % i == 0) {
if (i * i != num) {
} else {
sum = sum + i;
Q2. Define a “Point” class having members – x,y(coordinates). Define default constructor and
parameterized constructors. Define subclass “Point3D” with member as z (coordinate). Write display
class Point {
int x;
int y;
// Default constructor
public Point() {
x = 0;
y = 0;
// Parameterized constructor
this.x = x;
this.y = y;
private int z;
this.z = z;
}
@Override
class Main {
OR
Q2. Write a program that displays the number of characters, lines and words
Slip 10
Q1. Write a program to accept a number from user. Check whether number is prime or not.
:-
import java.util.Scanner;
} else {
scanner.close();
if (number <= 1) {
return false;
if (number % i == 0) {
return false;
return true;
Q2. Create a package “utility”. Define a class CapitalString under “utility” package which will contain
a method to return String with first letter capital. Create a Person class (members – name, city)
outside the package. Display the person name with first letter as capital by making use of
CapitalString.
:-
package utility;
public class CapitalString {
return input;
import utility.CapitalString;
class main
String capitalizedName=CapitalString.capitalizeFirstLetter(person.getName());
this.name = name;
this.city = city;
}
public String getName() {
return name;
return city;
OR
Q2. Define a class SavingAccount (acno, name, balance). Define appropriate operations as,
withdraw(), deposit(), and viewbalance(). The minimum balance must be 500. Create an objectand
Slip 11
Q1. Write a program create class as MyDate with dd,mm,yy as data members. Write
parameterized constructor. Display the date in dd-mm-yy format. (Use this keyword)
:-
this.dd = dd;
this.mm = mm;
this.yy = yy;
date.displayDate();
Q2. Create an abstract class Shape with methods area & volume. Derive a class Sphere (radius).
Calculate and display area and volume. [20 Marks]
:-
import java.lang.Math;
this.radius = radius;
}
// Implementation of the area method for a sphere
@Override
double area() {
@Override
double volume() {
class Main {
OR
Q2. Write a program to accept details of 'n' customers (c_id, cname, address, mobile_no) from
Q1. Create a package named “Series” having a class to print series of Square of numbers. Write a
:-
package Series;
int square = i * i;
//import Series.SquareSeries;
class Main {
int n = 10; // You can change 'n' to the number of terms you want in the series
SquareSeries.printSquareSeries(n);
Q2. Create an abstract class Shape with methods area & volume. Derive a class Cylinder (radius,
height). Calculate area and volume.
:-
this.radius = radius;
this.height = height;
@Override
@Override
class Main {
public static void main(String[] args) {
Slip 13
Q1. Construct a Linked List having names of Fruits: Apple, Banana, Guava and Orange. Display the
:-
import java.util.LinkedList;
import java.util.Iterator;
fruitList.add("Apple");
fruitList.add("Banana");
fruitList.add("Guava");
fruitList.add("Orange");
System.out.println("Fruit List:");
while (iterator.hasNext()) {
System.out.println(fruit);
Q2. Define an interface “Operation” which has methods area(),volume(). Define a constant PI having
value 3.142. Create a class circle (member – radius) which implements this interface. Calculate and
:-
interface Operation {
this.radius = radius;
@Override
@Override
return 0;
circle.displayArea();
circle.displayVolume();
OR
Q2. Write a class Student with attributes roll no, name, age and course. Initialize values through
parameterized constructor. If age of student is not in between 15 and 21 then generate userdefined
exception ―Age Not Within The Range.
Slip 14
Q1. Write a program to create JDBC connection. On successful connection with database display
:-
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database","your_username","you
r_password");
if (connection != null) {
connection.close();
} else {
} catch (ClassNotFoundException e) {
} catch (SQLException e) {
e.printStackTrace();
Q2. Define an interface “Operation” which has methods area(),volume(). Define a constant PI having
a
value 3.142. Create a class cylinder (members – radius, height) which implements this interface.
Calculate and display the area and volume. [20 Marks]
:-
interface Operation {
this.radius = radius;
this.height = height;
@Override
@Override
class Main {
cylinder.displayResults();
OR
Q2. Write a class Student with attributes roll no, name, age and course. Initialize values through
Slip 15
Q1. Construct a Linked List having names of Fruits: Apple, Banana, Guava and Orange. Displaythe
:-
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
fruitList.add("Apple");
fruitList.add("Banana");
fruitList.add("Guava");
fruitList.add("Orange");
while (listIterator.hasPrevious()) {
System.out.println(listIterator.previous());
Q2. Write a program to create a super class Employee (members – name, salary). Derive a sub-
classas
Developer (member – projectname). Create object of Developer and display the detailsof it.
:-
class Employee {
String name;
double salary;
this.name = name;
this.salary = salary;
String projectName;
super(name, salary);
this.projectName = projectName;
@Override
super.displayDetails();
class Main {
developer.displayDetails();
OR
Slip 16
Q1. Define a class MyNumber having one private integer data member. Write a parameterized
constructor to initialize to a value. Write isEven() to check given number is even or not. Use
command
:-
this.value = value;
return value % 2 == 0;
if (args.length != 1) {
return;
try {
int inputValue = Integer.parseInt(args[0]);
if (myNumber.isEven()) {
} else {
} catch (NumberFormatException e) {
Q2. Write a program to create a super class Employee (members – name, salary). Derive a sub- class
Programmer (member – proglanguage). Create object of Programmer and display the details of it.
:-
class SuperEmp {
String name;
double salary;
this.name = name;
this.salary = salary;
String proglanguage;
this.proglanguage = proglanguage;
class Main {
programmer.displayDetails();
OR
Q2. Write a JDBC program to update number_of_students of “BCA Science” to 1000. Create a
in the table
Slip 17
Q1. Define a class MyNumber having one private integer data member. Write a parameterized
constructor to initialize to a value. Write isOdd() to check given number is odd or not. Use
commandline
:-
number = value;
return number % 2 != 0;
if (args.length == 1) {
try {
if (myNumber.isOdd()) {
} else {
} catch (NumberFormatException e) {
} else {
Q2. Define a class Student with attributes rollno and name. Define default and parameterized
constructor. Keep the count of Objects created. Create objects using parameterized constructor and
:-
class Student {
public Student() {
objectCount++;
this.rollno = rollno;
this.name = name;
objectCount++;
return objectCount;
class Main {
Student student1 = new Student(); // Create an object using the default constructor
Student student2 = new Student(101, "Alice"); // Create an object using the parameterized
constructor
OR
Q2. Write a JSP program to perform Arithmetic operations such as Addition and Subtraction. Design
a HTML to accept two numbers in text box and radio buttons to display operations. On
submit display result as per the selected operation on next page using JS
Slip 18
Q1. Write a program to print the factors of a number. Accept a number using command line
argument.
:-
if (args.length != 1) {
return;
try {
if (number % i == 0) {
}
} catch (NumberFormatException e) {
Q2. Write a program to read the contents of “abc.txt” file. Display the contents of file in uppercase
as output.
:-
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
try {
String line;
content.append(line).append("\n");
reader.close();
// Convert the content to uppercase
System.out.println(upperCaseContent);
} catch (IOException e) {
OR
Slip 19
Q1.Write a program to accept the 'n' different numbers from user and store it in array. Display
:-
import java.util.Scanner;
int n = scanner.nextInt();
numbers[i] = scanner.nextInt();
max = numbers[i];
Q2. Create an abstract class “order” having members id, description. Create a subclass “Purchase
Order” having member as customer name. Define methods accept and display. Create 3 objects
:-
this.id = id;
this.description = description;
}
public abstract void accept();
super(id, description);
this.customerName = customerName;
@Override
// You can implement the input logic here to accept the customer name
@Override
class Main {
order2.accept();
order3.accept();
order1.display();
order2.display();
order3.display();
OR
Slip 20
Q1. Write a program to accept 3 numbers using command line argument. Sort and display the
numbers.
:-
if (args.length != 3) {
return;
try {
} catch (NumberFormatException e) {
:-
public Employee() {
objectCount++;
this.id = id;
this.name = name;
this.deptName = deptName;
this.salary = salary;
displayObjectDetails();
System.out.println("---------------");
OR
Q2. Write a JSP program to perform Arithmetic operations such as Multiplication and Divison. Design
a HTML to accept two numbers in text box and radio buttons to display operations. On
submit display result as per the selected operation on next page using