Question 1:
Write a Java class called "Triangle" that represents a triangle. The class should have the following
attributes: sideA, sideB, and sideC (representing the lengths of the three sides of the triangle).
Implement a constructor that takes these three side lengths as parameters and initializes the
corresponding attributes. The constructor should also check if the provided side lengths form a
valid triangle.
The program should prompt the user to enter the lengths of the three sides and create an instance
of the "Triangle" class using the constructor. If the side lengths do not form a valid triangle (e.g.,
violate the triangle inequality), the program should display an error message and ask the user to re-
enter the side lengths until a valid triangle is formed.
Input Format:
The program expects the user to enter the lengths of the three sides of a triangle. The input should
be provided in the following format:
<sideA>
<sideB>
<sideC>
where <sideA>, <sideB>, and <sideC> are the lengths of the sides, each on a separate line.
Output Format:
The program will provide the following outputs based on the input:
If the entered side lengths form a valid triangle, the program will output:
Triangle created successfully.
If the entered side lengths do not form a valid triangle (violating the triangle inequality), the program
will output:
Error: Invalid triangle. The sum of any two sides must be greater than the third side.
Note: The program will continue to prompt the user for input until a valid triangle is entered.
Therefore, the output format will repeat until a valid triangle is formed.
Title for Question 1: Form a Triangle
Solution:
import java.util.Scanner;
class Triangle {
private double sideA;
private double sideB;
private double sideC;
public Triangle(double sideA, double sideB, double sideC) {
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
}
public boolean isValidTriangle() {
return (sideA + sideB > sideC) && (sideA + sideC > sideB) && (sideB
}
}
class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double sideA, sideB, sideC;
//System.out.println("Enter the length of side A:");
sideA = scanner.nextDouble();
//System.out.println("Enter the length of side B:");
sideB = scanner.nextDouble();
//System.out.println("Enter the length of side C:");
sideC = scanner.nextDouble();
Triangle t = new Triangle(sideA, sideB, sideC);
if (!t.isValidTriangle()) {
System.out.println("Error: Invalid triangle. The sum of any
} else {
System.out.println("Triangle created successfully.");
}
}
TestCases:
S.No Inputs Outputs
Error: Invalid triangle. The sum of any two sides must be greater than the
1 517
third side.
2 345 Triangle created successfully.
Error: Invalid triangle. The sum of any two sides must be greater than the
3 225
third side.
Error: Invalid triangle. The sum of any two sides must be greater than the
4 059
third side.
S.No Inputs Outputs
Error: Invalid triangle. The sum of any two sides must be greater than the
5 832
third side.
6 666 Triangle created successfully.
Question 2:
You are given an array A of length N. An element X is said to be dominant if the frequency of X in A
is strictly greater than the frequency of any other element in the A.
For example, if =[2,1,4,4,4]A=[2,1,4,4,4] then 44 is a dominant element since its frequency is higher
than the frequency of any other element in A.
Find if there exists any dominant element in A.
Input Format:
The first line of input contains an integer N (1 <= N <= 10^5), representing the length of the array
'A'.
The second line of input contains N space-separated integers, representing the elements of the
array 'A'.
Output Format:
The output will be a single line containing either "Is there any dominant element in the array? true"
or "Is there any dominant element in the array? false" depending on whether there exists a
dominant element in the array or not.
Title for Question 2: Dominant element
Solution:
import java.util.Scanner;
public class Main {
private int[] array;
// Constructor to initialize the array 'A'
public Main(int[] array) {
this.array = array;
}
// Custom class to keep track of the element frequency
private static class ElementFrequency {
int element;
int frequency;
public ElementFrequency(int element) {
this.element = element;
this.frequency = 1;
}
}
// Method to find if there exists any dominant element
public boolean hasDominantElement() {
ElementFrequency dominantElement = null;
for (int num : array) {
if (dominantElement == null) {
dominantElement = new ElementFrequency(num);
} else if (dominantElement.element == num) {
dominantElement.frequency++;
} else {
dominantElement.frequency--;
if (dominantElement.frequency == 0) {
dominantElement = null;
}
}
}
if (dominantElement != null) {
// Check if the dominant element appears more than N/2 times
int count = 0;
for (int num : array) {
if (num == dominantElement.element) {
count++;
}
}
return count > (array.length / 2);
}
return false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Get the length of the array 'A'
// System.out.print("Enter the length of the array: ");
int N = sc.nextInt();
// Get the elements of the array 'A'
int[] array = new int[N];
//System.out.println("Enter the elements of the array:");
for (int i = 0; i < N; i++) {
array[i] = sc.nextInt();
}
// Create a Main object and check if there exists any dominant eleme
Main finder = new Main(array);
boolean hasDominantElement = finder.hasDominantElement();
// Output the result
System.out.println("Is there any dominant element in the array? " +
}
}
TestCases:
S.No Inputs Outputs
1 512345 Is there any dominant element in the array? false
S.No Inputs Outputs
2 533433 Is there any dominant element in the array? true
3 77712577 Is there any dominant element in the array? true
4 77712577 Is there any dominant element in the array? true
5 3333 Is there any dominant element in the array? true
6 18 Is there any dominant element in the array? true
Question 3:
Fill Write a Java program to create a class with private instance variables accountNumber and
balance. Provide public getter and setter methods to access and modify these variables.
Input format:
The program expects the user to enter the account number as a string.
The user should then input the account balance as a decimal number.
Output format:
The program will display the account details in the following format:
Account details:
Account Number: [accountNumber]
Balance: [balance]
Constraints:
The account number can be any string, including alphanumeric characters and special characters.
The balance should be a non-negative decimal number.
Title for Question 3: Account details display
Solution:
import java.util.Scanner;
public class Main{
private String accountNumber;
private double balance;
// Getter method for accountNumber
public String getAccountNumber() {
return accountNumber;
}
// Setter method for accountNumber
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
// Getter method for balance
public double getBalance() {
return balance;
}
// Setter method for balance
public void setBalance(double balance) {
this.balance = balance;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Create a BankAccount object
Main account = new Main();
// System.out.print("Enter account number: ");
String accountNumber = scanner.nextLine();
account.setAccountNumber(accountNumber);
// System.out.print("Enter balance: ");
double balance = scanner.nextDouble();
account.setBalance(balance);
// Display the account details
System.out.println("Account details:");
System.out.println("Account Number: " + account.getAccountNumber());
System.out.println("Balance: " + account.getBalance());
scanner.close();
}
}
TestCases:
S.No Inputs Outputs
Account details: Account Number: 123456789 Balance:
1 123456789 1000.50
1000.5
234567890 Account details: Account Number: 234567890 Balance:
2
97587.8467 97587.8467
Account details: Account Number: 987654455 Balance:
3 987654455 455
455.0
Account details: Account Number: 654487 Balance:
4 654487 84767.764
84767.764
5 2816370 0000 Account details: Account Number: 2816370 Balance: 0.0
S.No Inputs Outputs
6 867576 95664 Account details: Account Number: 867576 Balance: 95664.0
Question 4:
Given an array of N integers arr[] where each element represents the maximum length of the jump
that can be made forward from that element. This means if arr[i] = x, then we can jump any
distance y such that y ? x.
Find the minimum number of jumps to reach the end of the array (starting from the first element). If
an element is 0, then you cannot move through that element.
Input Format:
The user will be prompted to enter the length of the array.
Then, the user should enter the elements of the array one by one.
Output Format:
The program will output a single line containing the minimum number of jumps required to reach
the end of the array.
Title for Question 4: End of array
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Get the length of the array
//System.out.print("Enter the length of the array: ");
int N = sc.nextInt();
// Get the elements of the array
int[] arr = new int[N];
//System.out.println("Enter the elements of the array:");
for (int i = 0; i < N; i++) {
arr[i] = sc.nextInt();
}
int minJumps = findMinimumJumps(arr);
// Output the result
System.out.println("Minimum number of jumps to reach the end of the
}
public static int findMinimumJumps(int[] arr) {
int jumps = 0;
int currentReach = 0;
int maxReach = 0;
for (int i = 0; i < arr.length - 1; i++) {
maxReach = Math.max(maxReach, i + arr[i]);
if (i == currentReach) {
jumps++;
currentReach = maxReach;
}
}
return jumps;
}
}
TestCases:
S.No Inputs Outputs
1 6231142 Minimum number of jumps to reach the end of the array: 3
2 3123 Minimum number of jumps to reach the end of the array: 2
3 41034 Minimum number of jumps to reach the end of the array: 2
4 521031 Minimum number of jumps to reach the end of the array: 2
5 41111 Minimum number of jumps to reach the end of the array: 3
6 10 Minimum number of jumps to reach the end of the array: 0
Question 5:
Create a Java program to represent a Book class with a constructor that takes the book's title,
author, and publication year as input from the user. Display the book's information on the console
with dynamic input from the user?
Input format:
The program will prompt the user to enter the book's title, author, and publication year. The user
should provide the inputs by typing them and then pressing the Enter key.
Output format:
The program will display the book's information on the console. The output will be in the form:
Title for Question 5: Book Information
Solution:
import java.util.Scanner;
class Book {
private String title;
private String author;
private int publicationYear;
// Parameterized constructor
public Book(String title, String author, int publicationYear) {
this.title = title;
this.author = author;
this.publicationYear = publicationYear;
}
// Getter methods
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public int getPublicationYear() {
return publicationYear;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//System.out.print("Enter book's title: ");
String title = scanner.nextLine();
//System.out.print("Enter book's author: ");
String author = scanner.nextLine();
// System.out.print("Enter publication year: ");
int publicationYear = scanner.nextInt();
// Create a new Book object using the constructor
Book book = new Book(title, author, publicationYear);
// Display book's information
System.out.println("Book Title: " + book.getTitle());
System.out.println("Author: " + book.getAuthor());
System.out.println("Publication Year: " + book.getPublicationYear())
scanner.close();
}
}
TestCases:
S.No Inputs Outputs
The Lord of the Rings: The Book Title: The Lord of the Rings: The
1 Fellowship of the Ring J.R.R. Fellowship of the Ring Author: J.R.R. Tolkien
Tolkien 1954 Publication Year: 1954
Book Title: 1984 Author: George Orwell
2 1984 George Orwell 1949
Publication Year: 1949
To Kill a Mockingbird Harper Lee - Book Title: To Kill a Mockingbird Author: Harper
3
1960 Lee Publication Year: -1960
Book Title: The Hobbit Author: J.R.R. Tolkien
4 The Hobbit J.R.R. Tolkien 0
Publication Year: 0
5 2000 Book Title: Author: Publication Year: 2000
The Catcher in the Rye J.D. Book Title: The Catcher in the Rye Author: J.D.
6
Salinger 1951 Salinger Publication Year: 1951