Computer Applications
Correct the errors in 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);
}
}
Java Conditional Stmts
52 Likes
Answer
Explanation
- public is a keyword so it can't be used as an identifier for naming the class. Change the class name from public to any valid identifier, for example
class Sample
- Argument of main method is an array of Strings. Use square brackets instead of curly brackets —
String args[]
- c is an int variable. We cannot assign a double literal 65.45 to it.
- Variables sum & diff are not defined
- The line
System.out.println(sum,diff);
should be written like thisSystem.out.println(sum + " " + diff);
Corrected Program
class Sample //1st correction
{
public static void main(String args[]) //2nd correction
{
int a=45,b=70,c=65; //3rd correction
int sum=a+b; //4th Correction
int diff=c-b;
System.out.println(sum + " " + diff); //5th Correction
}
}
Answered By
36 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); }
Predict the Output of the given snippet, when executed:
switch (opn) { case 'a': System.out.println("Platform Independent"); break; case 'b': System.out.println("Object Oriented"); case 'c': System.out.println("Robust and Secure"); break; default: System.out.println("Wrong Input"); }
When (i) opn = 'b' (ii) opn = 'x' (iii) opn = 'a'
Correct the errors in the given program:
class Square { public static void main(String args[]) { int n=289,r; r=sqrt(n); if(n==r) System.out.println("Perfect Square"); else System.out.println("Not a Perfect Square"); } }
Correct the errors in the given program:
class Simplify { public static void main(String args[]) { int a,b,c,d; a=10,b=5,c=1,d=2; c=a2+b2; d=(a+b)2; p=c/d; System.out.println(c + " "+ " "+d+ " "+p); } }