0% found this document useful (0 votes)
4 views8 pages

Java Practical Question Answer Output

The document contains a series of Java programs that demonstrate basic programming concepts such as arithmetic operations, conditional statements, and control flow. Each program includes a brief description, the code, and the expected output. The examples cover topics like addition, pass/fail checks, voting eligibility, leap year determination, and grading based on marks.

Uploaded by

ishaan1132
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

Java Practical Question Answer Output

The document contains a series of Java programs that demonstrate basic programming concepts such as arithmetic operations, conditional statements, and control flow. Each program includes a brief description, the code, and the expected output. The examples cover topics like addition, pass/fail checks, voting eligibility, leap year determination, and grading based on marks.

Uploaded by

ishaan1132
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA PRACTICAL PROGRAMS

Question – Program – Output

Q1. Write a Java program to perform addition, subtraction, multiplication and


division of two numbers.
Program:
class Arithmetic {
public static void main(String[] args) {
int a = 20, b = 10;
[Link]("Addition = " + (a + b));
[Link]("Subtraction = " + (a - b));
[Link]("Multiplication = " + (a * b));
[Link]("Division = " + (a / b));
}
}
Output:
Addition = 30
Subtraction = 10
Multiplication = 200
Division = 2
Q2. Write a Java program to check whether a student is pass or fail.
Program:
class PassFail {
public static void main(String[] args) {
int marks = 45;
if (marks >= 35)
[Link]("Student is Pass");
else
[Link]("Student is Fail");
}
}
Output:
Student is Pass
Q3. Write a Java program to check whether a person is eligible for voting.
Program:
class Vote {
public static void main(String[] args) {
int age = 20;
if (age >= 18)
[Link]("Person can Vote");
else
[Link]("Person cannot Vote");
}
}
Output:
Person can Vote
Q4. Write a Java program to check whether a year is leap year or not.
Program:
class LeapYear {
public static void main(String[] args) {
int year = 2024;
if (year % 4 == 0)
[Link]("Leap Year");
else
[Link]("Not a Leap Year");
}
}
Output:
Leap Year
Q5. Write a Java program to find the greater of two numbers.
Program:
class Greater {
public static void main(String[] args) {
int a = 10, b = 25;
if (a > b)
[Link]("A is Greater");
else
[Link]("B is Greater");
}
}
Output:
B is Greater
Q6. Write a Java program to check whether a number is odd or even.
Program:
class OddEven {
public static void main(String[] args) {
int num = 7;
if (num % 2 == 0)
[Link]("Even Number");
else
[Link]("Odd Number");
}
}
Output:
Odd Number
Q7. Write a Java program to check whether the first number is divisible by the
second number.
Program:
class Divisible {
public static void main(String[] args) {
int a = 10, b = 5;
if (a % b == 0)
[Link]("Divisible");
else
[Link]("Not Divisible");
}
}
Output:
Divisible
Q8. Write a Java program to display grade using nested if statement.
Program:
class Grade {
public static void main(String[] args) {
int marks = 78;
if (marks >= 90)
[Link]("Grade A");
else if (marks >= 75)
[Link]("Grade B");
else if (marks >= 60)
[Link]("Grade C");
else if (marks >= 35)
[Link]("Grade D");
else
[Link]("Fail");
}
}
Output:
Grade B

Common questions

Powered by AI

The order of precedence in the 'Grade' Java class is enforced through sequential 'if' statements with descending thresholds ('>= 90', '>= 75', etc.), ensuring that the highest possible grade is assigned first. This hierarchy is crucial, as overlapping criteria might cause incorrect grading if not handled properly. By checking higher grades before lower ones, the program maintains logical accuracy and avoids mistakenly assigning an inappropriate grade .

To improve the 'LeapYear' program for accuracy, additional conditions should address century years—those divisible by 100—and ensure they are not leap years unless divisible by 400. The revised condition would first check 'year % 4 == 0', then 'year % 100 != 0' or 'year % 400 == 0'. This adjustment accommodates exceptions observed in the Gregorian calendar, where every 100 years a year is not a leap year, except every 400 years .

The 'Vote' Java class program checks voter eligibility based solely on age being 18 or above. A limitation of this approach is it assumes no local laws or other factors might influence eligibility, such as citizenship or legal disqualifications. Additionally, age validation does not accommodate future considerations like computing based on birthdates beyond the current year, potentially leading to incorrect eligibility determination. Furthermore, the program does not consider leap year intricacies, which can miscount age by a day .

The 'Arithmetic' Java class uses variables 'a' and 'b' to hold integer values for arithmetic operations, facilitating direct computation and output display. While these variable names are concise, they are not descriptive, reducing code readability. For enhanced maintainability, adopting descriptive names like 'firstNumber' and 'secondNumber' would promote clearer understanding of each variable's role, without altering logic or performance. Naming variables meaningfully makes future programmer adaptation and debugging more straightforward .

The 'LeapYear' Java class uses the conditional statement 'if (year % 4 == 0)' to check if a year is divisible by 4, which is the basic criterion for leap years. This is a simplified leap year condition that does not account for century years unless further conditions like divisibility by 400 are added. In this case, the class only checks divisibility by 4, which provides a partial criteria for determining leap years .

The 'Divisible' Java class uses the modulus operator to verify divisibility, checking if 'a % b' equals zero to determine if 'a' is evenly divisible by 'b'. This method is efficient for division checks as it directly computes the remainder. However, efficiency is contingent on computational resources; with large integers, computation time may increase marginally. Importantly, this approach does not handle division by zero, which could lead to runtime errors .

The modulus operation is used in the 'OddEven' Java class to determine the remainder when a number is divided by 2. If the remainder is zero, the number is even; otherwise, it is odd. This concept is based on the definition of even numbers, which are divisible exactly by 2, hence resulting in a zero remainder .

To modify the 'Greater' Java class to identify and display the greater number's value, the program can be enhanced by adding an additional print statement that outputs the variable itself. For instance, after determining which number is greater, use 'System.out.println("Greater number is: " + Math.max(a, b));'. Utilizing the Math.max() function simplifies the code and eliminates redundancy, clearly communicating which number has been identified as greater .

The 'PassFail' Java class operates on the assumption that a passing criterion is solely based on scoring 35 or more marks. Real-world applications could face issues as they often involve more nuanced grading systems with multiple passing requirements (e.g., practical tests, attendance). This binary pass/fail approach might not account for partial credit or curve adjustments, leading to inaccurate conclusions about a student's standing. Moreover, educational stipulations could demand subject-specific pass conditions, unseen here .

The 'Grade' Java class uses nested if statements to assign grades according to given marks thresholds. Each 'if' branch checks if marks meet or exceed specific thresholds: 90 for Grade A, 75 for Grade B, 60 for Grade C, and 35 for Grade D. For each range, appropriate grades are output using System.out.println(). If none of the conditions are met (i.e., marks are below 35), the output defaults to 'Fail'. This hierarchical checking allows assigning correct grades based on consecutive mark ranges .

You might also like