Errors Finding Questions in JAVA

Errors Finding Questions:
1. The following code has some errors. Rewrite the corrected code .
int i=2,j=5;
while j>i
{ jTextField1.getText(“j is greater”);
j--; ++i;
}
JOptionPane.showMessageDialog(“Hello”);
Ans:
int i=2,j=5;
while( j>i)
{ jTextField1.getText(“j is greater”);
j--; ++i;
}
JOptionPane.showMessageDialog(“Hello”);

2. Identify the errors :
switch(ch)
{
case ‘a’ :
case ‘A’ :
case ‘e’ :
case ‘E’ :
case ‘i’ :
case ‘i’ :
case ‘u’ :
case ‘U’ : ++vowels;
break;
default : ++others;
Ans: two case constants doesn’t have the same value

3. inti,j=5;
i==j+5;
if(i=j)
 {
jTextField1.setText(“i and j are unequal”);
jTextField2.setText(“they are not equal”); break;
 }
else jTextField1.setText(“i and j are equal”);
Ans:
int i,j=5;
i=j+5;
if(i==j)
 {
jTextField1.setText(“i and j are unequal”);
jTextField2.setText(“they are not equal”); break;
 }
else
jTextField1.setText(“i and j are equal”);

4. Rewrite the code after making correction . Underline the corrections
int sum;value;inct;
int i
for(i==0;i<=10;i++)
sum=sum+i;
inct++;
Ans :
int sum,value,inct;
int i;
for(i=0;i<=10;i++)
sum=sum+i;
inct++;

5. The following code has some error(s). Rewrite the correct code underlining all the corrections made.
int y=3;
switch(y);
{ case 1: System.out.print(“Yes its One”);
case>2: System.out.println(“Yes its more than Two”);
break;
case else: System.out.print(“Invalid Number):
Ans:
int y=3;
switch(y)
{ case 1: System.out.print(“Yes its One”);
break;
case 2: System.out.println(“Yes its more than Two”);
break;
default:System.out.print(“Invalid Number):
}

6. The following has some error(s).Rewrite the correct code underlining all the corrections made:
Inti,j=5;
i==j+5;
if(i=j)
{
jtextfield1.setText(“I and j are unequal”);
jtextfield1.setText(“I and j are not equal”);breaks;
}
else
jtextfield1.setText(“I and j are equal”)
Ans:
inti,j=5;
i=j+5;
if(i==j)
{
jTextField1.setText(“I and j are unequal”);
}
else
jTextField1.setText(“I and j are equal”)

7.int n= Integer parseInt(Jlabel1.getText);
Ans:
int n= Integer parseInt(JLabel1.getText());

8. Find out errors if any:
M=1;
N=0;
For(;m+n<19;++n)
System.out.println(“hello”);
M=m+10;
Ans:
m=1;
n=0;
for(;m+n<19;++n)
System.out.println(“hello”);
m=m+10;

9. The following code has some error(s). Rewrite the correct code underlining all the corrections made.
int y=6,p;
do
{ y=3.14*y;
 p=y%10;
if p=2
System.out.print(“Two”);
while(y>1)
Ans:
int y=6,p;
do
{ y=3.14*y;
 p=y%10;
if (p==2)
System.out.print(“Two”);
}while(y>1);

10. Identify the possible error(s) in the following code fragment: Underline error(s) and
correct the code.
f=1;
for(int a=40; (>30); a)
f*=a;
s=0;
for(int a=1; a<40/a++)
s+=a;
Ans: Error in the first and second for loop line ,and the corrected code should be as follows:
for ( int a =40 ; (a >30); a )
for ( int a =1 ; (a <40); a ++ )

11. The following code has some error(s). Rewrite the correct code underlining all the corrections made.
       int i,c,a=5,b=0;
       for(i=0,i<20,i++)
          { if b=0 then
                  break;
              else
                   c=a/b;
           system.out.show(“Quotient”+c);   
Ans:  int i,c,a=5,b=0;
          for(i=0;i<20;i++) // at place of comma(,) semicolon (;) should be there
          { if (b==0)          // comparison operator = = should be used and brackets are missing.
                  break;
              else
                   c=a/b;
           System..out.Println(“Quotient”+c);