Write a java program to calculate the sum of all the no. divisible by 5 in the range 1 to 50.
Ans: int sum=0;
for(int i=1;i<=50;++i)
{ if(i%5==0)
sum=sum+i;
}
jLabel1.setText(“”+sum);
2. What do you mean by infinite loop. Write one program that has infinite loop
Ans: A loop that never terminates is called infinite loop.
Example : for(;;)
{ jLabel1.setText(“Hello”);}
3. Write method in java that takes a number returns the sum of its digits.
Ans int sumdig(int n)
{ int sum=0, r;
while(n!=0)
{int r=n%10;
n=n/10;
sum=sum+r;
}
return (sum);
}
4. How many times, the following loop gets executed?
i=0; while(i>20) {//Statements } Ans: 0 times 5. How many times, the following loop gets executed? int i=0; do { //Statements }while(i>20); Ans: 1time Output Finding Questions 1. Write the output : (i) jTextField1.setText(“Hello”.charAt(3)); (ii) jTextField1.setText(“Good morning”.substring(4)); Ans: (i) l (ii) morning 2. Write the value stored on y variable after executing the following code : int x , y = 0; for(x=1;x<=5;++x) y=x++; Ans: 5 3. Find the output of the code: intf=1,i=2; do {f*=i; }while(++i<5); jTextField1.setText (“”+f); Ans: 24 4. What will be the value of j and k after execution of the following code: intj=10,k=12; if(k>=j) {k=j; J=k;} Ans: 10 10 5. What will be the contents of jTextield after executing the following statement: int num=4; num=num+1; if(num>5) jTextField1.setText(Integer.toString(num)); else jTextField1.setText(Integer.toString(num*4)); Ans: 20 6. Find the output of the following code: intFirst=7; intSecond=73; First++; if(First+Second>90) jlabel1.setText("valueis90"); else jlabel1.setText("valueisnot90"); Ans: value is not 90 :: 54 :: 7. Find the output int Number1=7,Number2=8; int Second=73; if(Number1>0||Number2>5) if(Number1>7) jTextField1.setText("CodeWorked"); else jTextField1.setText("CodeMightWork"); else jTextField1.setText("CodewillnotWork"); Ans: CodeMightWork 8. What will be the content of the jTextArea1 after executing the following code? intNum =1; do { jTextArea1.setText(Integer.toString(++Num)+"\n"); Num= Num + 1; }while(Num<=10); Ans: 10 9. What will be the contents of jTextfield1 and jTextfield2 after executing the following code: Strings=”KENDRIYAVIDYALAYA SANGATHAN” jTextField1.setText(s.length()+””); jTextField2.setText(Math.round(2.34)+“”); Ans: 282 10. Give the value of x after executing following Java code. int a=10,b=12,x=5,y=6; while(a<=b) { if(a%2==0) x=x+y; else x=x-y; a=a+1; } Ans: 11 11. What will be the output produced by following code fragment? flaot x=9, y=5; int z=(int)(x/y); switch(z) { case1: x=x+2; case2: x=x+3; default: x=x+1; } System.out.println(“valueof x:”+x); Ans: value of x: 15 12. What values will be assigned to the variable ua ,ub, uc and fail after execution of the following program segment: inti=0,ua=0,ub=0,uc=0,fail=0; while(i<=5){ switch(i++) { case1:++ua; case2:++ub; uc++; break; case3: case 4:++uc; :: 55 :: ua++; ub++; break; default:++fail; } Ans: ua=1 ub=1 uc=0 13. What will be the contents of jTextField1 and jTextField2 after executing the following code: Strings=“SunMicroSystems”; jTextField1.setText(s.length()+””); jTextField2.setText(s.toLowerCase()); Ans: jTextField1:17 jTextField2: sunmicrosystems 14. Give the output of the following code: int m=100; while(m>0) { if(m<10)break; m=m-10; } System.out.println(“mis”+m); Ans: m is 0 Errors finding and conversion questions: 1. Thefollowingcodehassomeerrors.Rewritethecorrectedcode. inti=2, j=5; whilej>i { jTextField1.getText(“jisgreater”); 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. Rewrite the code after making correction. int sum; value; inct; intifor(i==0;i<=10;i++) sum=sum+i; inct++; Ans: int sum, value, inct; for(int i=0;i<=10;i++) sum=sum+i; inct++; 3. The following code has some error(s). Rewrite the correct code. inty=3; switch(y); {case1:System.out.print(“YesitsOne”); case>2:System.out.println(“YesitsmorethanTwo”); :: 56 :: break; case else: System.out.print(“InvalidNumber): } Ans: inty=3; switch(y) {case1:System.out.print(“YesitsOne”); break; case2:System.out.println(“YesitsmorethanTwo”); break; default: System.out.print(“InvalidNumber); } 4. Find out errors and rewrite the code: 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; 5. Rewrite the following program code using a for loop: int i,sum=0; while(i<10) {sum+=i;i+=2; } Ans: inti, sum=0; for(i=0;i<10;i+=2) {sum+=i; } 6. Rewrite the following code using while loop : int i,j; for(i=1;i<=4;i++) {for(j=1;j<=i;++j) { System.out.print(j); } System.out.println(); } Ans: inti=1, j=0; while(i<=4) { j=1; while(j<=i) { System.out.print(j); ++j; } i++; System.out.println(); } :: 57 :: 7. Rewrite the following if-else segment using switch-case statement. charch='A'; if(ch=='A')System.out.println("Account"); if((ch=='C')||(ch=='G'))System.out.println("Admin"); if(ch=='F')System.out.println("Advisor"); Ans: charch='A';s witch(ch) { case‘A':System.out.println("Account");break; case'C': case'G’: System.out.println("Admin");break; case'F': System.out.println("Advisor"); } 8. Rewrite the following code using while loop: int i,j; for(i=1,j=2;i<=6;i++,j+=2) System.out.println(i++);System.out.println (“Finished!!!”); Ans: inti=1,j=2;whi le(i<=6) {System.out.println(i++); i++; j+=2;} System.out.println(“Finished!!!”); 9. Rewrite the following code using for loop. int i=0; while(++i<20) { if( i==8) break; System.out.println(i++); } Ans: inti; for (i=1;i<20;++i) { if( i==8)break; System.out.println(i++);} 10. Write the equivalent switch case for the following code : if(num1==1) jTextField1.setText(“Numberisone”); elseIf(num1==2) jTextField1.setText(“Numberistwo”); elseIf(num1==3) jTextField1.setText(“Numberisthree”); else jTextField1.setText(“Numberismorethanthree”); Ans: Switch(num1) { case1:jTextField1.setText(“Numberisone”);break; case2: jTextField1.setText(“Numberistwo”);break; :: 58 :: case3: jTextField1.setText(“Numberisthree”);break; default: jTextField1.setText(“Numberismorethanthree”); } 11. Write an alternative code(Using if)of given code that saves on number of comparisons. if(a==0) System.out.println(“zero”); if(a==1) System.out.println(“one”); if(a==2) System.out.println(“two”); if(a==3) System.out.println(“three”); Ans: if(a==0)System.out.println(“zero”); else if(a==1)System.out.println(“one”); else if(a==2)System.out.println(“two”); else if(a==3)System.out.println(“three”);
i=0; while(i>20) {//Statements } Ans: 0 times 5. How many times, the following loop gets executed? int i=0; do { //Statements }while(i>20); Ans: 1time Output Finding Questions 1. Write the output : (i) jTextField1.setText(“Hello”.charAt(3)); (ii) jTextField1.setText(“Good morning”.substring(4)); Ans: (i) l (ii) morning 2. Write the value stored on y variable after executing the following code : int x , y = 0; for(x=1;x<=5;++x) y=x++; Ans: 5 3. Find the output of the code: intf=1,i=2; do {f*=i; }while(++i<5); jTextField1.setText (“”+f); Ans: 24 4. What will be the value of j and k after execution of the following code: intj=10,k=12; if(k>=j) {k=j; J=k;} Ans: 10 10 5. What will be the contents of jTextield after executing the following statement: int num=4; num=num+1; if(num>5) jTextField1.setText(Integer.toString(num)); else jTextField1.setText(Integer.toString(num*4)); Ans: 20 6. Find the output of the following code: intFirst=7; intSecond=73; First++; if(First+Second>90) jlabel1.setText("valueis90"); else jlabel1.setText("valueisnot90"); Ans: value is not 90 :: 54 :: 7. Find the output int Number1=7,Number2=8; int Second=73; if(Number1>0||Number2>5) if(Number1>7) jTextField1.setText("CodeWorked"); else jTextField1.setText("CodeMightWork"); else jTextField1.setText("CodewillnotWork"); Ans: CodeMightWork 8. What will be the content of the jTextArea1 after executing the following code? intNum =1; do { jTextArea1.setText(Integer.toString(++Num)+"\n"); Num= Num + 1; }while(Num<=10); Ans: 10 9. What will be the contents of jTextfield1 and jTextfield2 after executing the following code: Strings=”KENDRIYAVIDYALAYA SANGATHAN” jTextField1.setText(s.length()+””); jTextField2.setText(Math.round(2.34)+“”); Ans: 282 10. Give the value of x after executing following Java code. int a=10,b=12,x=5,y=6; while(a<=b) { if(a%2==0) x=x+y; else x=x-y; a=a+1; } Ans: 11 11. What will be the output produced by following code fragment? flaot x=9, y=5; int z=(int)(x/y); switch(z) { case1: x=x+2; case2: x=x+3; default: x=x+1; } System.out.println(“valueof x:”+x); Ans: value of x: 15 12. What values will be assigned to the variable ua ,ub, uc and fail after execution of the following program segment: inti=0,ua=0,ub=0,uc=0,fail=0; while(i<=5){ switch(i++) { case1:++ua; case2:++ub; uc++; break; case3: case 4:++uc; :: 55 :: ua++; ub++; break; default:++fail; } Ans: ua=1 ub=1 uc=0 13. What will be the contents of jTextField1 and jTextField2 after executing the following code: Strings=“SunMicroSystems”; jTextField1.setText(s.length()+””); jTextField2.setText(s.toLowerCase()); Ans: jTextField1:17 jTextField2: sunmicrosystems 14. Give the output of the following code: int m=100; while(m>0) { if(m<10)break; m=m-10; } System.out.println(“mis”+m); Ans: m is 0 Errors finding and conversion questions: 1. Thefollowingcodehassomeerrors.Rewritethecorrectedcode. inti=2, j=5; whilej>i { jTextField1.getText(“jisgreater”); 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. Rewrite the code after making correction. int sum; value; inct; intifor(i==0;i<=10;i++) sum=sum+i; inct++; Ans: int sum, value, inct; for(int i=0;i<=10;i++) sum=sum+i; inct++; 3. The following code has some error(s). Rewrite the correct code. inty=3; switch(y); {case1:System.out.print(“YesitsOne”); case>2:System.out.println(“YesitsmorethanTwo”); :: 56 :: break; case else: System.out.print(“InvalidNumber): } Ans: inty=3; switch(y) {case1:System.out.print(“YesitsOne”); break; case2:System.out.println(“YesitsmorethanTwo”); break; default: System.out.print(“InvalidNumber); } 4. Find out errors and rewrite the code: 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; 5. Rewrite the following program code using a for loop: int i,sum=0; while(i<10) {sum+=i;i+=2; } Ans: inti, sum=0; for(i=0;i<10;i+=2) {sum+=i; } 6. Rewrite the following code using while loop : int i,j; for(i=1;i<=4;i++) {for(j=1;j<=i;++j) { System.out.print(j); } System.out.println(); } Ans: inti=1, j=0; while(i<=4) { j=1; while(j<=i) { System.out.print(j); ++j; } i++; System.out.println(); } :: 57 :: 7. Rewrite the following if-else segment using switch-case statement. charch='A'; if(ch=='A')System.out.println("Account"); if((ch=='C')||(ch=='G'))System.out.println("Admin"); if(ch=='F')System.out.println("Advisor"); Ans: charch='A';s witch(ch) { case‘A':System.out.println("Account");break; case'C': case'G’: System.out.println("Admin");break; case'F': System.out.println("Advisor"); } 8. Rewrite the following code using while loop: int i,j; for(i=1,j=2;i<=6;i++,j+=2) System.out.println(i++);System.out.println (“Finished!!!”); Ans: inti=1,j=2;whi le(i<=6) {System.out.println(i++); i++; j+=2;} System.out.println(“Finished!!!”); 9. Rewrite the following code using for loop. int i=0; while(++i<20) { if( i==8) break; System.out.println(i++); } Ans: inti; for (i=1;i<20;++i) { if( i==8)break; System.out.println(i++);} 10. Write the equivalent switch case for the following code : if(num1==1) jTextField1.setText(“Numberisone”); elseIf(num1==2) jTextField1.setText(“Numberistwo”); elseIf(num1==3) jTextField1.setText(“Numberisthree”); else jTextField1.setText(“Numberismorethanthree”); Ans: Switch(num1) { case1:jTextField1.setText(“Numberisone”);break; case2: jTextField1.setText(“Numberistwo”);break; :: 58 :: case3: jTextField1.setText(“Numberisthree”);break; default: jTextField1.setText(“Numberismorethanthree”); } 11. Write an alternative code(Using if)of given code that saves on number of comparisons. if(a==0) System.out.println(“zero”); if(a==1) System.out.println(“one”); if(a==2) System.out.println(“two”); if(a==3) System.out.println(“three”); Ans: if(a==0)System.out.println(“zero”); else if(a==1)System.out.println(“one”); else if(a==2)System.out.println(“two”); else if(a==3)System.out.println(“three”);
No comments:
Post a Comment