Computer Applications
Correct the errors of the given program:
class public
{
public static void main(String args{})
{
int a=45,b=70,c=65.45;
sum=a+b;
diff=c-b;
System.out.println(sum,diff);
}
}
Input in Java
101 Likes
Answer
Errors in the snippet
- Name of the class can't be public as public is a keyword.
- Argument of main function should be an array of Strings.
- We cannot assign 65.45 to c as c is an int variable and 65.45 is a floating-point literal.
- Variable sum is not defined.
- Variable diff is not defined.
- println method takes a single argument.
Corrected Program
class A //1st correction
{
public static void main(String args[]) //2nd correction
{
int a=45,b=70;
double c=65.45; //3rd correction
int sum=a+b; //4th correction
double diff=c-b; //5th correction
System.out.println(sum + " " + diff); //6th correction
}
}
Answered By
53 Likes
Related Questions
Predict the output of the given snippet, when executed:
int x = 1,y = 1; if(n > 0) { x = x + 1; y = y + 1; } System.out.println(x + " , " + y);
What will be the values of x and y, if the value of n is given as:
(i) 1
(ii) 0 ?Predict the output of the given snippet, when executed:
int b=3,k,r; float a=15.15,c=0; if(k==1) { r=(int)a/b; System.out.println(r); } else { c=a/b; System.out.println(c);
class Simplify { public static void main(String args[]) { int a,b,c,d; a=10,b=5,c=1; c=2a+2b; d=(a+b)2; p=c/d; System.out.println(c , d , p); } }
It is possible that the user may not be able to write an error-free program at one go. Due to this reason, debugging is significant. Debugging is used to eliminate errors from a program. Some of the examples describing the errors are given below:
(a) A number divided by zero.
(b) The user has applied multiplication sign, instead of division sign.
(c) Sum of p and q is to be divided by their difference using the statement p+q/p-q.
(d) While writing a Java statement, the user forgot to terminate the statement using semicolon.
Based on the above case, answer the following questions:
What is the type of error in (a)?
- Syntax Error
- Logical error
- Runtime error
- Execution error
What is the type of error in (b)?
- Logical error
- Syntax error
- Compiler error
- Execution error
What is the type of error in (c)?
- Logical error
- Syntax error
- Runtime error
- Execution error
What is the type of error in (d)?
- Syntax error
- Logical error
- Runtime error
- Compilation error