Computer Applications
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);
}
}
Input in Java
19 Likes
Answer
Errors in the snippet
- Multiple variable assignment statements cannot be separated by a comma. Semicolon should be used instead.
- The line
c=2a+2b
needs an operator between 2 and a, 2 and b. - The line
d=(a+b)2
needs an operator between (a+b) and 2. - Variable p is not defined.
- println method takes a single argument.
Corrected Program
class Simplify
{
public static void main(String args[])
{
int a,b,c,d;
a=10;b=5;c=1; //1st correction
c=2*a+2*b; //2nd correction
d=(a+b) / 2; //3rd correction
int p=c/d; //4th correction
System.out.println(c + " " + d + " " + p); //5th correction
}
}
Answered By
13 Likes
Related Questions
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);
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); } }
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
Write the syntax of 'if' statement.