Practice Questions
Practice Questions
Que. 1 Implement a program to find and display the maximum number out of the given three
numbers.
Code:
public class Que_1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
if (num2>max){
max = num2;
}
if (num3>max){
max = num3;
}
Output:
Que 2. Implement a program to calculate the factorial of a given number
Code:
import java.util.*;
System.out.print("Enter n = ");
int n = in.nextInt();
int a = 1;
System.out.println(a);
}
}
Output:
Que 3. Implement a program to display the geometric sequence as given below for a given value n,
where n is the number of elements in the sequence. 1, 2, 4, 8, 16, 32, 64, ......, 1024
Code:
import java.util.*;
//Implement a program to display the geometric sequence as given below for a given
//value n, where n is the number of elements in the sequence.
//1, 2, 4, 8, 16, 32, 64, ......, 1024
int num = 1;
for (int i = 0; i < n; i++) {
num = num*2;
}
}
}
Output:
Que 4. Implement a program to display the sum of two given numbers if the numbers are same. If
the numbers are not same, display the double of the sum.
Code:
import java.util.*;
System.out.print("Enter a = ");
int a = in.nextInt();
System.out.print("Enter b = ");
int b = in.nextInt();
if (a==b){
System.out.println(a+b);
}
else {
System.out.println(2*(a+b));
}
}
}
Output:
Que 5. Quadratic equation is an equation with degree 2 in the form of ax2 +bx + c = 0 where a, b
and c are the coefficients. Implement a program to solve a quadratic equation.
Find the discriminant value using the formula given below. discriminant = b^2 - 4ac
∙ If the discriminant is 0, the values of both the roots will be same. Display the value of the root.
∙ If the discriminant is greater than 0, the roots will be unequal real roots. Display the values of
both the roots.
∙ If the discriminant is less than 0, there will be no real roots. Display the message "The equation
has no real root".
Use the formula given below to find the roots of a quadratic equation. x = (-b ± discriminant)/2a
Code:
Output:
Que 6. Implement a program to calculate the product of three positive integer values. However, if
one of the integers is 7, consider only the values to the right of 7 for calculation. If 7 is the last
integer, then display -1. Note: Only one of the three values can be 7.
Code:
import java.util.*;
if (num1==7){
System.out.println(num2+num3);
} else if (num2==7) {
System.out.println(num3);
}
else {
System.out.println("-1");
}
}
}
Output:
Que 7. Food Corner home delivers vegetarian and non-vegetarian meals to its customers based on
the order.
Code:
import java.util.Scanner;
// Input values
System.out.println("Enter food type (V for Vegetarian, N for Non-
Vegetarian):");
char foodType = sc.next().charAt(0);
sc.close();
}
}
Output:
Que 8. The Metro Bank provides various types of loans such as car loans, business loans and house
loans to its account holders, i.e., customers.
Code:
import java.util.*;
//The Metro Bank provides various types of loans such as car loans, business
//loans and house loans to its account holders, i.e., customers.
//Implement a program to determine the eligible loan amount and the EMI
//that the bank can provide to its customers based on their salary and the
//loan type they expect to avail.
if (accnum.matches("1\\d{3}")) {
System.out.println("Account number accepted");
break;
} else {
System.out.println("Invalid account number. Please try again.");
}
}
// Input Salary
System.out.print("Enter your Salary: ");
int salary = in.nextInt();
switch (type) {
case "car":
eligible = (loanamount < 500000 && emiexp < 36);
loanMessage = "Eligible loan amount = 500000\nEligible EMIs = 36";
break;
case "house":
eligible = (loanamount < 6000000 && emiexp < 60);
loanMessage = "Eligible loan amount = 6000000\nEligible EMIs = 60";
break;
case "business":
if (salary > 75000) { // Only allow business loans for high salary
eligible = (loanamount < 7500000 && emiexp < 84);
loanMessage = "Eligible loan amount = 7500000\nEligible EMIs = 84";
}
break;
default:
System.out.println("Invalid loan type. Please choose car, house, or business.");
return;
}
Output:
Que 9. You have x number of $5 notes and y number of $1 notes. You want to purchase an item
for amount z. The shopkeeper wants you to provide exact change. You want to pay using a
minimum number of notes. How many $5 notes and $1 notes will you use? Implement a program
to find out how many $5 notes and $1 notes will be used. If an exact change is not possible, then
display -1.
Code:
import java.util.Scanner;
sc.close();
}
}
Output:
Que 10. Implement a program to generate and display the next date of a given date.
Code:
import java.util.*;
//Implement a program to generate and display the next date of a given date.
//Assumption = each month has 31 days
Output:
Que 11. Implement a program that displays a message for a given number based on
the below conditions.
● If the number is a multiple of 3, display "Zip".
● If the number is a multiple of 5, display "Zap".
● If the number is a multiple of both 3 and 5, display "Zoom",
● For all other cases, display "Invalid".
Code:
import java.util.Scanner;
Output: