0% found this document useful (0 votes)
6 views1 page

Java Programming Error Analysis and Exercises

This worksheet for Class VIII Computer Science at Delhi Public School Megacity, Kolkata, focuses on program coding in Java, including compiling and executing Java programs. It contains multiple-choice questions, true/false statements, error identification tasks, and programming exercises related to Java syntax and logic. The exercises include swapping numbers, temperature conversion, and calculating discounts based on total costs.

Uploaded by

saptamkh
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)
6 views1 page

Java Programming Error Analysis and Exercises

This worksheet for Class VIII Computer Science at Delhi Public School Megacity, Kolkata, focuses on program coding in Java, including compiling and executing Java programs. It contains multiple-choice questions, true/false statements, error identification tasks, and programming exercises related to Java syntax and logic. The exercises include swapping numbers, temperature conversion, and calculating discounts based on total costs.

Uploaded by

saptamkh
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

Delhi Public School Megacity, Kolkata

Class-VIII
Subject-Computer
WORKSHEET
CHAPTER – Program Coding-Unit II: Compiling and Executing Java Programs
Q1. Choose the correct option.
i. A Java statement is given as:
if((a<b) && (a<c))
Which of the following statement is true?
a) a is greatest number
b) b is smaller than a and c
c) a is smallest number
d) none of the above

ii. Sum of p and q is to be divided by their difference using the statement:


p+q/p-q
What type of error in the above statement?
a) Syntax b) Logical c) Runtime d) No error

Q2. State True or False for the following:


i. && operator results in true when all the connecting conditions are true?
ii. In an if-else statement, the condition is to be defined only with ‘if’ and not with ‘else’.

Q3. Find and rectify the errors in the following code:


class public
{
void main ( )
{
int a=45, b=60, c=65.5;
sum=a+b;
diff=c-b;
[Link](sum, diff);
}
}
Q4. Differentiate between Syntax and Logical error.
Write Java programs for the following:
Q5. Accept two unequal numbers. Display the numbers after swapping their values in the
variables, without using a third variable.

Q6. Accept the temperature in Celsius and then convert it into Fahrenheit. If the temperature
in Fahrenheit is more than 98.6 0F then display “Fever”, otherwise display “Normal”.

Q7. ‘Mega Mart’ has announced the following festival discount on the purchase of items
based on the total cost of the items purchased:
Total cost Discount %
Up to ₹ 2000 10 %
₹ 2001 to ₹ 5000 12%
₹ 5001 to ₹ 10000 15%
Above ₹ 10000 20%
Accept total cost. Calculate and display discount and final amount after availing the
discount.
*******************************************

Common questions

Powered by AI

A runtime error occurs during the execution of a program, causing it to terminate unexpectedly. For example, accessing an array with an index that is out of bounds causes a runtime error, as does dividing by zero .

A syntax error occurs when the code does not follow the syntax rules of the Java language, causing it not to compile. For example, a missing semicolon at the end of a statement is a syntax error. A logical error is when the code compiles and runs, but the result is not as expected due to incorrect logic, such as a mistaken formula. For instance, using 'a+b/c' when intending to calculate '(a+b)/c' is a logical error .

To improve the Java program, ensure it checks for valid input types and handles potential exceptions such as arithmetic overflows. Use conditionals to manage illegal operations like division by zero. Employ methods to separate logic and enhance readability, such as 'calculateSum(int a, int b)' and 'calculateDifference(int a, int b)' .

In Java, the condition of an if-else statement is defined with 'if' because the 'if' clause determines whether its block of code executes. The 'else' clause provides an alternative action if the 'if' condition is false, and does not require a condition itself .

To convert Celsius to Fahrenheit, use the formula 'F = (C * 9/5) + 32'. After conversion, check if the temperature in Fahrenheit exceeds 98.6°F to determine the condition: if greater than 98.6, display 'Fever'; otherwise, display 'Normal' .

The '&&' operator in Java results in true only when all the connecting conditions evaluate to true .

The expression 'p+q/p-q' has a logical error. It attempts to divide 'q' by 'p', then subtract 'q' from the result of this division, which is not equivalent to dividing the sum of 'p' and 'q' by their difference. The correct expression should be '(p + q) / (p - q)' .

You can swap two numbers 'a' and 'b' without a third variable using arithmetic operations: 1) Set 'a = a + b'. 2) Then, set 'b = a - b'. 3) Finally, set 'a = a - b'. This sequence changes 'a' and 'b' such that their values are swapped .

The code has several syntax errors: 1) 'class public' should be 'public class'. 2) 'void main()' must be 'public static void main(String[] args)'. 3) The initialization 'int c=65.5;' is incorrect since 'c' cannot store a fractional number being an 'int'. Change 'int' to 'double' for 'c'. 4) System.out.println should have a capital 'S' in 'System'. Also, 'println' cannot take multiple arguments; use 'System.out.println(sum + ", " + diff);' to print both values .

First, determine the discount percentage based on the cost: 10% for up to ₹2000, 12% for ₹2001 to ₹5000, 15% for ₹5001 to ₹10000, and 20% for above ₹10000. Calculate the discount amount: 'discountAmount = totalCost * discountPercentage / 100'. Subtract the discount from total cost to find the final amount: 'finalAmount = totalCost - discountAmount' .

You might also like