what will be the output of the following program in java net beans?
Attachments:
Answers
Answered by
0
JAVA NET BEANS -call for classes: 9717570033
Wednesday, 29 August 2012
Output Questions - JavaNetbeans
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 ++ )
Find the output of the following code:
(a) int I =1;
while(I<5) {
System.out.print( I+ “ ” );
I=1*2;
}
(b) int total=0,sum=0;
for(int I=0;I>=10;I++)
sum += I;
System.out.println(total);
(c) int I =0;
while(I<10) {
if( I % 2 ) = = 0)
{
x = x+ I:
System.out.print(x + “ ” );
}I++;}
(d) int I =0;
for(I=1;I<=20;I++){
System.out.print(i + “ ” );
I =I+2:
}
Ans: a) 1 2 4 b) 0 c) 0 2 6 12 20 d) 1 4 7 10 13 16 19
e) int I =0,x = 0:
do{
if (I% 5 = = 0){
x ++:
System.out.print(x + “ ” );
}
++i:
}
while(I<20);
System.out.print(“\n + x );
Ans:1
2 3 4
4
f) int I =0,x = 0;
for (I=0;I<5;++I)
for (I=0;j<i;j++) {
x += (I+j1);
System.out.print(i + “ ” );}
System.out.print(“\n +x );}
Ans:0
1 3 5 8 12 15 19 24 30
30
g) int I =0,x = 0;
for (I=1;I<10;I* = 2){
x ++:
System.out.print( x + “ ” );
}
System.out.print( “\n” + x);
Ans: 1 2 3 4
4
Rewrite the following fragment using switch:
if ( ch == ‘E’)
eastern ++;
if ( ch == ‘W’)
western ++;
if (ch ==’N’)
northern++;
if (ch == ‘S’)
southern++;
else
unknown++;
Ans: switch(ch) {
case ‘E’ : eastern ++; break;
case ‘W’ : western ++; break;
case ‘N’ : northern ++; break;
case ‘S’ : southern ++; break;
Answered by
0
the output of this question is
12 10 12 34
Similar questions