Question 1:
Write a Java program that takes three integers as input from the user and calculates their average.
Display the average as a floating-point number.
Input Format:
The input format for the AverageCalculator code is as follows:
The program prompts the user to enter the first number. The user should provide an integer value
for this input.
The program prompts the user to enter the second number. The user should provide an integer
value for this input.
The program prompts the user to enter the third number. The user should provide an integer value
for this input.
Please ensure that you provide valid integer inputs following the specified format.
Output Format:
Display the average of three numbers
Title for Question 1: Average of Three Numbers using Type Casting
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
//System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
//System.out.print("Enter the third number: ");
int num3 = scanner.nextInt();
double average = (num1 + num2 + num3) / 3.0;
System.out.println("Average: " + average);
}
}
TestCases:
S.No Inputs Outputs
1 235 Average: 3.3333333333333335
2 1000 2000 3000 Average: 2000.0
3 100 -50 25 Average: 25.0
4 -5 10 15 Average: 6.666666666666667
5 000 Average: 0.0
6 10 20 30 Average: 20.0
Question 2:
Write a program to calculate the area of a circle. The user will input the radius of the circle, and the
program should output the area.
Input Format:
The input format for the AreaOfCircle program is a single line containing a floating-point number
representing the radius of the circle.
Enter the radius of the circle: <radius>
Output Format:
The output format for the AreaOfCircle program is a single line displaying the calculated area of the
circle, which is also a floating-point number.
Area of the circle: <area>
Title for Question 2: Find area of a Circle using Type Casting
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// System.out.print("Enter the radius of the circle: ");
double radius = scanner.nextDouble();
double area = Math.PI * Math.pow(radius, 2);
System.out.println("Area of the circle: " + area);
}
}
TestCases:
S.No Inputs Outputs
1 10 Area of the circle: 314.1592653589793
2 100 Area of the circle: 31415.926535897932
3 48 Area of the circle: 7238.229473870883
4 4 Area of the circle: 50.26548245743669
5 6 Area of the circle: 113.09733552923255
6 2 Area of the circle: 12.566370614359172
Question 3:
Write a Java program that takes dynamic input from the user for the scores of a student in three
subjects: Mathematics, Physics, and Chemistry. The program should then calculate the average
score of the student and display it along with any applicable remarks based on the following
criteria:
Average Score >= 90: "Excellent"
70 <= Average Score < 90: "Good"
50 <= Average Score < 70: "Average"
Average Score < 50: "Needs Improvement"
Your program should handle type casting appropriately to ensure accurate calculations and
display. Make sure to guide the user throughout the input process and handle invalid Input.
Input Format:
The program expects the user to enter three scores for the subjects Mathematics, Physics, and
Chemistry. The input is provided through the console, and each score is entered on a new line.
=== Student Score Averaging ===
Enter Mathematics score: [Enter Mathematics score here]
Enter Physics score: [Enter Physics score here]
Enter Chemistry score: [Enter Chemistry score here]
Output Format:
After receiving the input, the program calculates the average score and displays it along with the
applicable remarks based on the given criteria. The output is displayed on the console.
Average Score: [Calculated average score] ([Applicable remark])
Title for Question 3: Find average mark of student using Type Casting
Solution:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// System.out.print("Enter Mathematics score: ");
double mathScore = scanner.nextDouble();
// System.out.print("Enter Physics score: ");
double physicsScore = scanner.nextDouble();
// System.out.print("Enter Chemistry score: ");
double chemScore = scanner.nextDouble();
double averageScore = (mathScore + physicsScore + chemScore) / 3;
String remarks;
if (averageScore >= 90) {
remarks = "Excellent";
} else if (averageScore >= 70) {
remarks = "Good";
} else if (averageScore >= 50) {
remarks = "Average";
} else {
remarks = "Needs Improvement";
}
System.out.printf("Average Score: %.2f (%s)%n", averageScore, remark
}
}
TestCases:
S.No Inputs Outputs
1 95 92 96 Average Score: 94.33 (Excellent)
2 80 85 70 Average Score: 78.33 (Good)
3 65 55 60 Average Score: 60.00 (Average)
4 30 40 45 Average Score: 38.33 (Needs Improvement)
5 80 80 80 Average Score: 80.00 (Good)
6 -70 -80 -65 Average Score: -71.67 (Needs Improvement)
Question 4:
Transcribe the given DNA strand into corresponding mRNA - a type of RNA, that will be formed
from DNA after transcription. DNA has the bases A, T, G and C, while RNA converts to U, A, C and
G respectively.
Examples
dnaToRna("ATTAGCGCGATATACGCGTAC") ? "UAAUCGCGCUAUAUGCGCAUG"
dnaToRna("CGATATA") ? "GCUAUAU"
dnaToRna("GTCATACGACGTA") ? "CAGUAUGCUGCAU"
Notes
Transcription is the process of making complementary strand.
A, T, G and C in DNA converts to U, A, C and G respectively in mRNA.
Title for Question 4: Transcribe To mRNA
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// System.out.println("Enter the DNA strand: ");
String dnaStrand = scanner.nextLine().toUpperCase();
String mRNA = transcribeToMRNA(dnaStrand);
System.out.println("The corresponding mRNA strand is: " + mRNA);
}
/**
* Transcribes the given DNA strand into its mRNA counterpart.
* @param dna the DNA strand
* @return the transcribed mRNA strand
*/
public static String transcribeToMRNA(String dna) {
StringBuilder mRNA = new StringBuilder();
for (char base : dna.toCharArray()) {
switch (base) {
case 'A':
mRNA.append('U');
break;
case 'T':
mRNA.append('A');
break;
case 'G':
mRNA.append('C');
break;
case 'C':
mRNA.append('G');
break;
default:
System.out.println("Invalid base detected: " + base);
return "Invalid DNA strand";
}
}
return mRNA.toString();
}
}
TestCases:
S.No Inputs Outputs
The corresponding mRNA strand is:
1 ATTAGCGCGTACTAGCGGATATACGCGTAC
UAAUCGCGCAUGAUCGCCUAUAUGCGCA
Invalid base detected: B The corresponding
2 ABCGTA
mRNA strand is: Invalid DNA strand
3 ATCG The corresponding mRNA strand is: UAGC
4 TACGTA The corresponding mRNA strand is: AUGCA
5 GCTA The corresponding mRNA strand is: CGAU
The corresponding mRNA strand is:
6 ATCGTGAT
UAGCACUA
Question 5:
You are given the length of a video in minutes. The format is mm:ss (e.g.: "02:54"). Create a
function that takes the video length and return it in seconds.
Examples
minutesToSeconds("01:00") ? 60
minutesToSeconds("13:56") ? 836
minutesToSeconds("10:60") ? -1
Notes
The video length is given as a string.
If the number of seconds is 60 or over, return -1 (see example #3).
You may get a number of minutes over 99 (e.g. "121:49" is perfectly valid).
Title for Question 5: Find the seconds of the video
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// System.out.println("Enter video length in the format mm:ss: ");
String videoLength = scanner.nextLine();
int result = convertToSeconds(videoLength);
System.out.println("Video length in seconds: " + result);
}
/**
* Convert the video length from mm:ss format to seconds.
* @param length the video length as a string in mm:ss format.
* @return the video length in seconds or -1 if the number of seconds is
*/
public static int convertToSeconds(String length) {
// Splitting the string at ":"
String[] parts = length.split(":");
int minutes = Integer.parseInt(parts[0]);
int seconds = Integer.parseInt(parts[1]);
if (seconds >= 60) {
return -1;
}
return minutes * 60 + seconds;
}
}
TestCases:
S.No Inputs Outputs
1 6:34 Video length in seconds: 394
2 7:54 Video length in seconds: 474
3 10:6 Video length in seconds: 606
4 8:9 Video length in seconds: 489
5 2:30 Video length in seconds: 150
6 7:21 Video length in seconds: 441