CSA09 Programming in Java Medium
CSA09 Programming in Java Medium
CSA09 Programming in Java Medium
1. Write a program to count all the prime and composite numbers entered by the user.
Sample Input:
Enter the numbers
4
54
29
71
7
59
98
23
Sample Output:
Composite number:3
Prime number:5
Test cases:
1. 33, 41, 52, 61,73,90
2. TEN, FIFTY, SIXTY-ONE, SEVENTY-SEVEN, NINE
3. 45, 87, 09, 5.0 ,2.3, 0.4
4. -54, -76, -97, -23, -33, -98
5. 45, 73, 00, 50, 67, 44
2. Find the Mth maximum number and Nth minimum number in an array and then find the
sum of it and difference of it.
Sample Input:
Array of elements = {14, 16, 87, 36, 25, 89, 34}
M=1
N=3
Sample Output:
1stMaximum Number = 89
3rdMinimum Number = 25
Sum = 114
Difference = 64
Test cases:
1. {16, 16, 16 16, 16}, M = 0, N = 1
2. {0, 0, 0, 0}, M = 1, N = 2
3. {-12, -78, -35, -42, -85}, M = 3 , N = 3
4. {15, 19, 34, 56, 12}, M = 6 , N = 3
5. {85, 45, 65, 75, 95}, M = 5 , N = 7
3. Write a program to print the total amount available in the ATM machine with the
conditions applied.
Total denominations are 2000, 500, 200, 100, get the denomination priority from the user
and the total number of notes from the user to display the total available balance to the
user
Sample Input:
Enter the 1st Denomination: 500
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
Test Cases:
3 Hidden Test cases (Think Accordingly based on Denominations)
5. Write a program to convert Decimal number equivalent to Binary number and octal
numbers?
Sample Input:
Decimal Number: 15
Sample Output:
Binary Number = 1111
Octal = 17
Test cases:
1. 111
2. 15.2
3. 0
4. B12
5. 1A.2
6. In an organization they decide to give bonus to all the employees on New Year. A 5%
bonus on salary is given to the grade A workers and 10% bonus on salary to the grade B
workers. Write a program to enter the salary and grade of the employee. If the salary of
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
the employee is less than $10,000 then the employee gets an extra 2% bonus on salary
Calculate the bonus that has to be given to the employee and print the salary that the
employee will get.
Sample Input & Output:
Enter the grade of the employee: B
Enter the employee salary: 50000
Salary=50000
Bonus=5000.0
Total to be paid:55000.0
Test cases:
1. Enter the grade of the employee: A
Enter the employee salary: 8000
2. Enter the grade of the employee: C
Enter the employee salary: 60000
3. Enter the grade of the employee: B
Enter the employee salary: 0
4. Enter the grade of the employee: 38000
Enter the employee salary: A
5. Enter the grade of the employee: B
Enter the employee salary: -8000
7. Write a program to print the first n perfect numbers. (Hint Perfect number means a
positive integer that is equal to the sum of its proper divisors)
Sample Input:
N=3
Sample Output:
First 3 perfect numbers are: 6 , 28 , 496
Test Cases:
1. N = 0
2. N = 5
3. N = -2
4. N = -5
5. N = 0.2
8. Write a program to enter the marks of a student in four subjects. Then calculate the total
and aggregate, display the grade obtained by the student. If the student scores an
aggregate greater than 75%, then the grade is Distinction. If aggregate is 60>= and <75,
then the grade is First Division. If aggregate is 50 >= and <60, then the grade is Second
Division. If aggregate is 40>= and <50, then the grade is Third Division. Else the grade is
Fail.
Total= 366
Aggregate = 91.5
DISTINCTION
Test cases:
a) 18, 76,93,65
b) 73,78,79,75
c) 98,106,120,95
d) 96,73, -85,95
e) 78,59.8,76,79
9. Write a program to read the numbers until -1 is encountered. Find the average of positive
numbers and negative numbers entered by user.
Sample Input:
Enter -1 to exit…
Enter the number: 7
Enter the number: -2
Enter the number: 9
Enter the number: -8
Enter the number: -6
Enter the number: -4
Enter the number: 10
Enter the number: -1
Sample Output:
The average of negative numbers is: -5.0
The average of positive numbers is : 8.66666667
Test cases:
1. -1,43, -87, -29, 1, -9
2. 73, 7-6,2,10,28,-1
3. -5, -9, -46,2,5,0
4. 9, 11, -5, 6, 0,-1
5. -1,-1,-1,-1,-1
10. Write a program to read a character until a * is encountered. Also count the number of
uppercase, lowercase, and numbers entered by the users.
Sample Input:
Enter * to exit…
Enter any character: W
Enter any character: d
Enter any character: A
Enter any character: G
Enter any character: g
Enter any character: H
Enter any character: *
Sample Output:
Total count of lower case:2
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
11. Write a program to calculate the factorial of number using recursive function.
Sample Input & Output:
Enter the value of n: 6
Sample Input & Output:
The factorial of 6 is: 720
Test cases:
1. N = 0
2. N = -5
3. N = 1
4. N = M
5. N = %
Test cases:
1. N = 0
2. N = -5
3. N = 1
4. N = M
5. N = %
5. 1010.0101
14. Write a program to find the number of special characters in the given statement
Sample Input:
Given statement: Modi Birthday @ September 17, #&$% is the wishes code for him.
Sample Output:
Number of special Characters: 5
16. Bank is a class that provides method to get the rate of interest. But, rate of interest may
differ according to banks. For example, SBI, ICICI and AXIS banks are providing 8.4%,
7.3% and 9.7% rate of interest. Write a Java program for above scenario.
Sample Input SBI, 8.4
Sample Output
Test case
1. SBI, 8.3
2. ICICI, 7.3
3. AXIS, 9.7
4. SBI, 8.6
5. AXIX, 7.6
17. Bring out the situation in which member names of a subclass hide members by the same
name in the super class. How it can be resolved? Write Suitable code in Java and
Implement above scenario with the Parametrized Constructor (accept int type
parameter) of the Super Class can be called from Sub Class Using super () and display
the input values provided.
4. EIGHT FIVE
5. 10.57, 12.58
18. Display Multiplication table for 5 and 10 using various stages of life cycle of the thread
by generating a suitable code in Java.
Sample Input 5, 10
5X1=5
5 X 2 =10
….
10 X 1 =10
10 X 2 = 20
….
Test Cases:
1. 10, 20
2. -10, -30
3. 0, 0
4. SIX, SIX
5. 9.8, 9.6
19. Using the concepts of thread with implementing Runnable interface in Java to generate
Fibonacci series.
Sample Input : 5
Sample Output : 0 1 1 2 3 …..
Test Cases
1. 7
2. -10
3. 0
4. EIGHT FIVE
5. 12.65
20. Generate a Java code to find the sum of N numbers using array and throw
ArrayIndexOutOfBoundsException when the loop variable beyond the size N.
Sample Input : 5
12345
Sample Output : 15
Test Cases
1. 4, 10
2. -10
3. 0
4. EIGHT SEVEN
5. 12.68
21. Using the concepts of thread with implementing Runnable interface in Java to find
whether a given number is prime or not.
Sample Input : 5
Sample Output : 5 is Prime
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
Sample Output : 15
Test Cases
1. 4
2. -10
3. 0
4. EIGHT SEVEN
5. 11.48
23. Generate a Java Code to Write and Read the string “Computer Science and Engineering”
using FileWriter and FileReader Class.
24. Create a java program to construct the volume of Box using default constructor method.
25. Accept the string “Welcome to Saveetha university” from the user and perform the
following operations by writing a suitable Java code.
i) Replace any word in the given String
ii) Find the length
iii) Uppercase Conversion
26. Create a HashTable to maintain a bank detail which includes Account number and
Customer name. Let Account number be the key in the HashTable. Write a Java program
to implement the following operations in the HashTable
i) Add 3 records
ii) Display the size of HashTable
iii) Clear the HashTable
27. Create a employee record using map interface and do the following operations.
i. Add object iii. Remove specified object
ii. isEmpty or not iv. Clear
28. Create a simple generics class with type parameters for sorting values of different types.
29. Develop a Java code to insert the following elements, using ListIterator to append +
symbol in each element and print them in reverse order. {C, A, E, B, D, F}.
30. Generate a Java code to perform simple arithmetic operations and to throw Arithmetic
Exception for Division-by-Zero.
31. Write a Java program to create three threads in parallel and display the natural numbers in
orders using sleep() method.
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
32. If n = 8, then array ‘a’ will have 7 elements in the range from 1 to 8. For example {1,
4, 5, 3, 7, 8, 6}. One number will be missing in ‘a’ (2 in this case). Write a source code to
find out that missing number
33. Create a class with a method that prints "This is parent class" and its subclass with another
method that prints "This is child class". Now, create an object for each of the class and call
1 - method of parent class by object of parent class
2 - method of child class by object of child class
3 - method of parent class by object of child class
34. Write a Java program to create a class Student and create constructor which assigns the values for
the student details such as student name, register number, and five subject marks. Calculate the
total and average of five subject marks and display the marks and average.
35. Generate a code to Count the Number of Words, Character and Lines from the File using
Stream I/O in Java.
36. Generate a code to non-negative integer’s num1 and num2 represented as strings; return
the product of num1 and num2, also represented as a string.
37. Implement pow(x, n), which calculates x raised to the power n
Input: x = 2.00000, n = 10
Output: 1024.00000
38. Given an integer array nums, find the subarray with the largest sum, and return its sum.
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: The subarray [4,-1, 2, 1] has the largest sum 6.
39. Write a Java program which creates only one object. If user attempts to create second
object, he should not be able to create it. (Using Exception Handling).
40. There is an exam room with n seats in a single row labeled from 0 to n - 1.When a student
enters the room, they must sit in the seat that maximizes the distance to the closest person.
If there are multiple such seats, they sit in the seat with the lowest number. If no one is in
the room, then the student sits at seat number 0.Design a class that simulates the
mentioned exam room. Implement the ExamRoom class: ExamRoom (int n) Initializes
the object of the exam room with the number of the seats n. int seat () Returns the label of
the seat at which the next student will set. Void leave (int p) indicates that the student
sitting at seat p will leave the room. It is guaranteed that there will be a student sitting at
seat p.
Input["ExamRoom", "seat", "seat", "seat", "seat", "leave", "seat"]
[[10], [], [], [], [], [4], []]
Output
[null, 0, 9, 4, 2, null, 5]
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
41. You have n tiles, where each tile has one letter tiles[i] printed on it. Return the number of
possible non-empty sequences of letters you can make using the letters printed on those
tiles.
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA",
"BAA".
42. Write a program to read the data from the file and copy it on another file
43. Implementing FileReader and FileWriter class to read and write the data from file
44. Write a program to read the file using BufferedReader class
45. Write a program for counting number of characters, words and lines in a file
46. Write a program in Java to input an NxN matrix and display it row-wise and column-wise
47. Write a java program which creates an interface IterF1 having 2 methods add () and sub
(). Create a class which overloads the given methods for addition and subtraction of two
numbers respectively.
48. Write a Java program to calculate the rate of interest of the employee’s Provident Fund
use the try and catch and finally block.
49. Write a program to create a class MyThread in this class a constructor, call the base class
constructor, using super and starts the thread. The run method of the class starts after this.
It can be observed that both main thread and created child thread are executed
concurrently
50. Create 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 user-defined exception "AgeNotWithinRangeException". If name contains
numbers or special symbols raise exception "NameNotValidException". Define the two
exception classes.