Medium Programs in Java
Medium Programs in Java
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
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
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
Enter the 1st Denomination number of notes: 4
Enter the 2nd Denomination: 100
Enter the 2nd Denomination number of notes: 20
Enter the 3rd Denomination: 200
Enter the 3rd Denomination number of notes: 32
Enter the 4th Denomination: 2000
Enter the 4th Denomination number of notes: 1
Sample Output:
Total Available Balance in ATM: 12400
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 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
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
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 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
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
Sample Input:
Enter the income:200000
Sample Output:
Tax= 20000
Test cases:
1. 400700
2. 2789239
3. 150000
4. 00000
5. -125486
11. 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.
Sample Input & Output:
Enter the marks in python: 90
Enter the marks in c programming: 91
Enter the marks in Mathematics: 92
Enter the marks in Physics: 93
Total= 366
Aggregate = 91.5
DISTINCTION
Test cases:
1. 18, 76,93,65
2. 73,78,79,75
3. 98,106,120,95
4. 96,73, -85,95
5. 78,59.8,76,79
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
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 = %
Sample Input:
Given statement: Modi Birthday @ September 17, #&$% is the wishes code
for him.
Sample Output:
Number of special Characters: 5
20. 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.
Input :
Assign rate of interest for 3 banks
Pseudo
Define the Super class Bank , in which getRateOfInterest() as overriding method
Define subclass SBI, ICICI, AXIS , in which return the rate of interest as per given
values respectively
Declare object and print the rate of interest for the three banks
Output:
Sample Input SBI, 8.4
Sample Output
Test case
1. SBI, 8.3
2. ICICI, 7.3
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
3. AXIS, 9.7
4. SBI, 8.6
5. AXIX, 7.6
21. 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.
Input :
Assign or input values for super class and sub class members.
Pseudo :
Define super class and sub class with one member (has same name)
Define method in super class and sub class with same method signature
Declare the object in main method
Invoke methods using object to display the values
Output :
Sample Input : 100, 200
Sample Output : 100, 200
Test Cases
1. 10, 20
2. -20, -30
3. 0, 0
4. EIGHT FIVE
5. 10.57, 12.58
22. 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
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
4. SIX, SIX
5. 9.8, 9.6
23. Using the concepts of thread with implementing Runnable interface in Java to gener-
ate 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
24. 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
25. 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
Sample Output : 15
Test Cases
1. 4
2. -10
3. 0
4. EIGHT SEVEN
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
5. 11.48
o 26. Given a string s consisting of words and spaces, return the length of the last word
in the string. A word is a maximal substring consisting of non-space characters only.
There will be at least one word, consists of only English letters and spaces ' '.
Example 1:
Input: s = "Hello World"
Output: 5
Explanation: The last word is "World" with length 5.
Test Case
Test Case Inputs-1
1. Maximal Substring Consisting
2. lea@st one wor2d
3. 1254 98076
4. &*( )%#$
5. letters and spaces