E TR 7 V HGCsy Cbei A2 RP 5 H A9
E TR 7 V HGCsy Cbei A2 RP 5 H A9
Array Compatiblilty
Two arrays are said to be compatible if they are of the same size and if the ith element in the
first array is greater than or equal to the ith element in the second array for all i elements.If
the array size is zero or lesser then display the message "Invalid array size".Write a Java
program to find whether 2 arrays are compatible or not.If the arrays are compatible display
the message as "Arrays are Compatible" ,if not then display the message as "Arrays are Not
Compatible".
Sample Input 1:
Enter the size for First array:
5
Enter the elements for First array:
5
14
17
19
15
Enter the size for Second array:
5
Enter the elements for Second array:
2
5
9
15
7
Sample Output 1:
Arrays are Compatible
Sample Input 2:
Enter the size for First array:
3
Enter the elements for First array:
1
4
7
Enter the size for Second array:
5
Enter the elements for Second array:
2
5
9
5
7
Sample Output 2:
Arrays are Not Compatible
Sample Input 3:
Enter the size for First array:
-2
Sample Output 3:
Invalid array size
Program
import java.util.Scanner;
class CompatibleArrays {
private static void test(int size) {
if (size <= 0) {
System.out.println("Invalid array size");
System.exit(0);
}
}
return true;
}
if (isCompatible(arr1, arr2)) {
System.out.println("Arrays are Compatible");
} else {
System.out.println("Arrays are Not Compatible");
}
}
}
Sample Input 2:
Enter the number of elements in the array
2
Enter the price details
50
80
Enter the index of the array element you want to access
9
Sample Output 2:
Array index is out of range
Sample Input 3:
Enter the number of elements in the array
2
Enter the price details
30
j
Sample Output 3:
Input was not in the correct format
Program
import java.util.InputMismatchException;
import java.util.Scanner;
try {
int index = scanner.nextInt();
return "The array element is " + arr[index];
} catch (InputMismatchException ignore) {
return "Input was not in the correct format";
} catch (ArrayIndexOutOfBoundsException ignore) {
return "Array index is out of range";
}
}
Sample Input 2:
Enter the id:
123
Enter the name:
Tom
Enter the no of subjects:
0
Invalid number of subject
Enter the no of subjects:
3
Enter mark for subject 1:
75
Enter mark for subject 2:
49
Enter mark for subject 3:
90
Sample Output 2:
Id:123
Name:Tom
Average:71.33
Grade:F
Program:
public class Student {
private int id;
private String name;
private int[] marks;
private float average;
private char grade;
Rs.100/pizza Rs.20/puffs Rs.10/cooldrink Generate a bill for What Tom has bought.
Sample Input 1:
Sample Output 1:
Bill Details
No of pizzas:10
No of puffs:12
No of cooldrinks:5
Total price=1290
Program:
import java.util.*;
class SnacksDetails {
public static void main(String[] args) {
System.out.println("Bill Details");
5. Question 5
Problem Statement – Write a program to calculate the fuel consumption of your truck.The program
should ask the user to enter the quantity of diesel to fill up the tank and the distance covered till the
tank goes dry.Calculate the fuel consumption and display it in the format (liters per 100 kilometers).
Convert the same result to the U.S. style of miles per gallon and display the result. If the quantity or
distance is zero or negative display ” is an Invalid Input”.
[Note: The US approach of fuel consumption calculation (distance / fuel) is the inverse of the
European approach (fuel / distance ). Also note that 1 kilometer is 0.6214 miles, and 1 liter is 0.2642
gallons.]
The result should be with two decimal place.To get two decimal place refer the below-mentioned
print statement :
float cost=670.23;
Sample Input 1:
20
150
Sample Output 1:
● Liters/100KM
13.33
● Miles/gallons
17.64
Explanation:
Sample Input 2:
-5
Sample Output 2:
● -5 is an Invalid Input
Sample Input 3:
25
-21
Sample Output 3:
● -21 is an Invalid Input
Java
import java.util.*;
import java.text.*;
class Main{
if(ltt<1){
System.exit(0);
if(diss<1){
System.exit(0);
System.out.println("Liters/100KM");
System.out.println(df2.format(hundered));
double mg = miles/gallons;
System.out.println("Miles/gallons");
System.out.println(df2.format(mg));
6. Question
Problem Statement – Vohra went to a movie with his friends in a Wave theatre and during break
time he bought pizzas, puffs and cool drinks. Consider the following prices :
● Rs.100/pizza
● Rs.20/puffs
● Rs.10/cooldrink
Sample Input 1:
Sample Output 1:
Bill Details
● No of pizzas:10
● No of puffs:12
● No of cooldrinks:5
● Total price=1290
Java
import java.util.Scanner;
{
int totalprice;
7. Question
Problem Statement – Ritik wants a magic board, which displays a character for a corresponding
number for his science project. Help him to develop such an application.
For example when the digits 65,66,67,68 are entered, the alphabet ABCD are to be displayed.
[Assume the number of inputs should be always 4 ]
Sample Input 1:
● Enter the digits:
65
66
67
68
Sample Output 1:
65-A
66-B
67-C
68-D
Sample Input 2:
Sample Output 2:
115-s
116-t
101-e
112-p
import java.util.Scanner;
char q = (char) a;
char w = (char) b;
char e = (char) c;
char r = (char) d;
System.out.println ();
System.out.print (a);
System.out.print (b);
System.out.print (c);
System.out.print (d);
8. Question
Problem Statement – FOE college wants to recognize the department which has succeeded in getting
the maximum number of placements for this academic year. The departments that have participated
in the recruitment drive are CSE,ECE, MECH. Help the college find the department getting maximum
placements. Check for all the possible output given in the sample snapshot
Note : If any input is negative, the output should be “Input is Invalid”. If all department has equal
number of placements, the output should be “None of the department has got the highest
placement”.
Sample Input 1:
Sample Output 1:
● Highest placement
CSE
Sample Input 2:
● Enter the no of students placed in CSE:55
Sample Output 2:
● Highest placement
ECE
MECH
Sample Input 3:
Sample Output 3:
Sample Input 4:
Sample Output 4:
● Input is Invalid
import java.util.Scanner;
else{
}
//System.out.println("Highest Placement:");
// First, check if any two values are equal and greater than the third
System.out.println ("CSE");
System.out.println ("ECE");
}
System.out.println ("CSE");
System.out.println ("MECH");
}
System.out.println ("ECE");
System.out.println ("MECH");
}
System.out.println ("CSE");
}
System.out.println ("ECE");
}
else{
System.out.println ("MECH");
}
9. Question
Problem Statement – In a theater, there is a discount scheme announced where one gets a 10%
discount on the total cost of tickets when there is a bulk booking of more than 20 tickets, and a
discount of 2% on the total cost of tickets if a special coupon card is submitted. Develop a program to
find the total cost as per the scheme. The cost of the k class ticket is Rs.75 and q class is Rs.150.
Refreshments can also be opted by paying an additional of Rs. 50 per member.
Hint: k and q and You have to book minimum of 5 tickets and maximum of 40 at a time. If fails
display “Minimum of 5 and Maximum of 40 Tickets”. If circle is given a value other than ‘k’ or ‘q’
the output should be “Invalid Input”.
Sample Input 1:
Sample Output 1:
● Ticket cost:4065.25
Sample Input 2:
● Enter the no of ticket:1
Sample Output 2:
import java.util.Scanner;
import java.text.DecimalFormat;
int noTicket;
System.exit (0);
co = s.next ();
else {
total = cost;
total = cost;
10.Question
Problem Statement – Rhea Pandey’s teacher has asked her to prepare well for the lesson on seasons.
When her teacher tells a month, she needs to say the season corresponding to that month. Write a
program to solve the above task.
Month should be in the range 1 to 12. If not the output should be “Invalid month”.
Sample Input 1:
Sample Output 1:
● Season:Autumn
Sample Input 2:
● Enter the month:13
Sample Output 2:
● Invalid month
import java.util.Scanner;
switch (entry){
case 12:
case 1:
case 2:
System.out.println ("Season:Winter");
break;
case 3:
case 4:
case 5:
System.out.println ("Season:Spring");
break;
case 6:
case 7:
case 8:
System.out.println ("Season:Summer");
break;
case 9:
case 10:
case 11:
System.out.println ("Season:Autumn");
break;
default:
11.Question
Problem Statement – To speed up his composition of generating unpredictable rhythms, Blue Bandit
wants the list of prime numbers available in a range of numbers.Can you help him out?
Write a java program to print all prime numbers in the interval [a,b] (a and b, both inclusive).
Note
● Input 1 should be lesser than Input 2. Both the inputs should be positive.
● If any of the condition mentioned above fails, then display “Provide valid input”
Sample Input 1:
15
Sample Output 1:
2 3 5 7 11 13
Sample Input 2:
Sample Output 2:
import java.util.*;
int b=sc.nextInt();
int flag;
else{
Inner:
while(a<=b){
if(a==2)
System.out.print(a+" ");
else if(a==1){
a++;
continue;
else{
flag=0;
outer:
for(int i=2;i<=a/2;i++){
if(a%i==0){
flag=1;
break outer;
if(flag==0)
System.out.print(a+" ");
a++;
}
}
12.Question
Problem Statement – Goutam and Tanul plays by telling numbers. Goutam says a number to
Tanul. Tanul should first reverse the number and check if it is same as the original. If yes, Tanul
should say “Palindrome”. If not, he should say “Not a Palindrome”. If the number is negative, print
“Invalid Input”. Help Tanul by writing a program.
Sample Input 1 :
21212
Sample Output 1 :
Palindrome
Sample Input 2 :
6186
Sample Output 2 :
Not a Palindrome
import java.util.Scanner;
int sum = 0, r;
int temp = n;
if (n > -1)
while (n > 0)
{
r = n % 10;
}
if (temp == sum)
System.out.println ("Palindrome");
else
else
13.Question
XYZ Technologies is in the process of increment the salary of the employees. This increment is done
based on their salary and their performance appraisal rating.
1. If the appraisal rating is between 1 and 3, the increment is 10% of the salary.
2. If the appraisal rating is between 3.1 and 4, the increment is 25% of the salary.
3. If the appraisal rating is between 4.1 and 5, the increment is 30% of the salary.
Help them to do this, by writing a program that displays the incremented salary. Write a class
“IncrementCalculation.java” and write the main method in it.
Note : If either the salary is 0 or negative (or) if the appraisal rating is not in the range 1 to 5
(inclusive), then the output should be “Invalid Input”.
Sample Input 1 :
8000
Sample Output 1 :
8800
Sample Input 2 :
4.3
Sample Output 2 :
9750
Sample Input 3 :
-5000
Sample Output 3 :
● Invalid Input
import java.util.*;
class Main{
if(salary<1||rating<1.0||rating>5.0){
System.out.println("Invalid Input");
System.exit(0);
}
else if(rating>=1&&rating<=3){
salary=salary+(int)(0.1*salary);
System.out.println(salary);
} else if(rating>3&&rating<=4){
salary=salary+(int)(0.25*salary);
System.out.println(salary);
Else
/*if(rating>4&&rating<=5)*/
salary=salary+(int)(0.3*salary);
System.out.println(salary);
14.Question-10
Problem Statement – Chaman planned to choose a four digit lucky number for his car. His lucky
numbers are 3,5 and 7. Help him find the number, whose sum is divisible by 3 or 5 or 7. Provide a
valid car number, Fails to provide a valid input then display that number is not a valid car number.
Note : The input other than 4 digit positive number[includes negative and 0] is considered as invalid.
Sample Input 1:
● Lucky Number
Sample Input 2:
Sample Output 2:
Sample Input 3:
Sample Output 3:
import java.util.*;
class Main {
int sum = 0;
else
while (carNum != 0)
{
sum = sum + l;
}
if (sum % 3 == 0 || sum % 5 == 0 || sum % 7 == 0)
{
}
else
{
}
15.Question
Problem Statement –
IIHM institution is offering a variety of courses to students. Students have a facility to check whether
a particular course is available in the institution. Write a program to help the institution accomplish
this task. If the number is less than or equal to zero display “Invalid Range”.
Sample Input 1:
● Enter no of course:
Java
Oracle
C++
Mysql
Dotnet
C++
Sample Output 1:
● Enter no of course:
C++
Sample Output 2:
Sample Input 3:
● Enter no of course:
Sample Output 3:
Invalid Range
import java.util.*;
class Main
int n=0,flag=0;
String courseSearch;
System.out.println("Enter no of course:");
n= sc.nextInt();
if(n<=0||n>20){
System.out.println("Invalid Range");
System.exit(0);
sc.nextLine();
for (int i = 0; i < course.length; i++) {
course[i] = sc.nextLine();
courseSearch=sc.nextLine();
if(course[i].equals(courseSearch))
flag=1;
if(flag==1){
else {
16.Question
Problem Statement – Mayuri buys “N” no of products from a shop. The shop offers a different
percentage of discount on each item. She wants to know the item that has the minimum discount
offer, so that she can avoid buying that and save money.
[Input Format: The first input refers to the no of items; the second input is the item name, price and
discount percentage separated by comma(,)]
Assume the minimum discount offer is in the form of Integer.
Note: There can be more than one product with a minimum discount.
Sample Input 1:
4
mobile,10000,20
shoe,5000,10
watch,6000,15
laptop,35000,5
Sample Output 1:
shoe
Explanation: The discount on the mobile is 2000, the discount on the shoe is 500, the discount on
the watch is 900 and the discount on the laptop is 1750. So the discount on the shoe is the minimum.
Sample Input 2:
Mobile,5000,10
shoe,5000,10
WATCH,5000,10
Laptop,5000,10
Sample Output 2:
Mobile
shoe
WATCH
Laptop
import java.util.*;
import java.io.*;
//String[] values;
float dis[] = new float[num];
itemName[i] =s[0];
//System.out.println(itemName[i]);
itemPrice[i]=Integer.parseInt(s[1]);
// System.out.println(itemPrice[i]);
itemDis[i]=Integer.parseInt(s[2]);
// System.out.println(itemDis[i]);
//float x = itemDis[i]
dis[i]=(float)((itemDis[i]*itemPrice[i])/100);
// System.out.println(dis[i]);
int j=0;
if(dis[i]<=min)
min=dis[i];
idx[j++]=i;
//System.out.println(min);
System.out.println(itemName[idx[i]]);
//System.out.println(idx[i]);
}
}
17.Question
Problem Statement – Raj wants to know the maximum marks scored by him in each semester. The
mark should be between 0 to 100 ,if goes beyond the range display “You have entered invalid mark.”
Sample Input 1:
● Enter no of semester:
50
60
70
90
98
76
67
89
76
Sample Output 1:
Sample Input 2:
● Enter no of semester:
55
67
98
67
-98
Sample Output 2:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main{
System.out.println("Enter no of semester:");
arr[i]=sc.nextInt();
}
int maxMarks[] = new int[sems];
if(max<0||max>100) {
System.exit(0);
int marks=sc.nextInt();
if(marks<0||marks>100)
System.exit(0);
if(max< marks)
max=marks;
maxMarks[i]= max;
}
18.Question
Problem Statement – Bela teaches her daughter to find the factors of a given number. When she
provides a number to her daughter, she should tell the factors of that number. Help her to do this, by
writing a program. Write a class FindFactor.java and write the main method in it.
Note :
● If the input provided is negative, ignore the sign and provide the output. If the input is zero
Sample Input 1 :
54
Sample Output 1 :
1, 2, 3, 6, 9, 18, 27, 54
Sample Input 2 :
-1869
Sample Output 2 :
import java.util.*;
int i;
if(num==0){
System.out.println("No Factors");
else if(num>0){
for(i=1;i< num;i++){
if(num%i==0){
System.out.print(i+", ");
System.out.println(num);
}
else{
num=num*-1;
for(i=1;i< num;i++){
if(num%i==0){
System.out.print(i+", ");
System.out.println(num);
19.Book Manipulation
The district central library needs an application to store book details of their library. The clerk who
has all the rights to add a new book,search for any book,display the book details and should update
the count of total number of books.
You are provided with a Book with the following private attributes:
● int isbnno
● String bookName
● String author
● void addBook(Book bobj) - This method should add the book object to the booklist.
● boolean isEmpty() - This method should return true if the booklist is empty else return false.
● ArrayList<Book> viewAllBooks() - This method should return the list of books maintained in
the library.
● ArrayList<Book> viewBooksByAuthor(String author) - This method should return a list of
books written by the author passed as argument. When you display an empty list it should
print the message "The list is empty".
● int countnoofbook(String bname) - this method should return the count of books with the
name passed as argument.
1.Add Book
5.Exit
123
Java
Bruce Eckel
1.Add Book
5.Exit
124
C++
1.Add Book
5.Exit
Henry
1.Add Book
5.Exit
Eric Nagler
1.Add Book
5.Exit
5
Program
import java.util.List;
import java.util.Scanner;
while (true) {
System.out.println("1.Add Book\n" +
"5.Exit\n" +
switch (choice) {
case 1: {
book.setIsbnno(isbn);
book.setBookName(bookName);
book.setAuthor(authorName);
library.addBook(book);
break;
case 2: {
break;
case 3: {
if (books.isEmpty()) {
} else {
break;
}
case 4: {
System.out.println(count);
case 5: {
System.exit(0);
Samir wants to play a mind game with his father. The game is – when Sameer calls out a number his
father should say whether that number is odd or even. Since his father doesn't like Mathematics
much he needs some help to play the game.
Help his father to identify whether the called out number is odd or even by using the Lambda
Expressions.
Samir's father has to identify whether the number is odd or even. By using the method
checkNumberType the given number is identified as odd or even.
Type(Interface
Methods Responsibilities
)
Don’t create an object for NumberType. Use the lambda expression. In the NumberTypeUtility class
write the main method and perform the given steps :
Invoke the checkNumberType method for the number received as input from the user.
Sample Input 1 :
58
Sample Output 1 :
58 is not odd
Sample Input 2 :
77
Sample Output 2 :
77 is odd
Program
import java.util.Scanner;
if (idOdd().checkNumber(num)) {
} else {