Java Loop Questions | Loop Questions with Answers
Q1. What will be the value of A and B after execution of the following code ?
int A=100, B;
for (B=10; B<=12; B++)
{
A+=B;
}
JOptionPane.showMessageDialog(this, “A:” + A+ “B:” +B+ “”);
Output:
A: 133 B: 13
Q2. What will be the content of the jTextArea1 after executing the following code (Assuming that the jTextArea1 had no content before executing this code ) ?
for(int i=2; i<=5;i++)
{
jTextArea1.setText(jTextArea1.getText() + “ “ + Integer.toString(i*i));
}
Output:
4
9
16
25
Q3. What will be displayed in of jTextField1 after executing the following code ?
int N=20;
N=N+1;
if(N<21)
jTextField1.setText(Integer.toString(N+10));
else
jTextField1.setText(Integer.toString(N+15));
Output :
Q4. The following code has some errors. Rewrite the correct code.
Int P=3; Sum=0;
{
Sum=P;
P+=3;
}
while(P=<12)
jTextField1(Integer.toString(Sum));
Output:
int P=3; Sum=0;
do
{
Sum=P;
P+=3;
}while(P<=12);
jTextField1.setText(Integer.toString(Sum));
Q5. Rewrite the code
Int total=0,jump=5;
Int I;
For (i=0;I=<5;I++)
{
Jump+=5;
Total+=jump;
}
jTextArea1.showText(“”+total);
Output:
int total=0,jump=5;
int I;
for (I=0;I<=5;I++)
{
Jump+=5;
Total+=jump;
}
jTextArea1.append(“”+total);
Q6. Rewrite the following program code after finding errors.
Int k=2;
sum=0;
{
Sum=k;
K+=3;
}
While (k=<20)
jTextField1 (Integer.toString(sum));
Output:
int k=2, sum=0;
do
{
sum=k;
k+=3;
}
while(k<=20);
jTextField1.setText (Integer.toString(sum));
Q7. Rewrite the correct code.
Int sum=0; step=5;
Int I;
for(i=0; i=<5;i++)
{
Step +=5;
Sum+=step;
}
jTextArea1.showText(“” +sum);
Output:
int sum=0; step=5;
int i;
for(i=0; i=<5;i++)
{
step +=5;
sum+=step;
}
jTextArea1.setText(“” +sum);
Q8. What will be the values of variables ‘m’ and ‘n’ after the execution of the given code?
int m, n=0;
for (m=1; m<=4; m++)
{
n+=m;
n--;
}
Output:
m=5
n=6
The syntax of a do loop in java is :
do
{
// statements
}while( condition );
Here is a java do loop that prints the number 1 even though the test fails.
int loopvar=1; // Declare and initialize the loop counter
do {
jTextField1.setText(String.valueOf(loopvar)); // prints the variable
loopvar=loopvar+1; // Increment the loop counter
}while(loopvar>=10); //test the loop
Ans. A java while loop is a looping construct which continually executes a block of statements while a condition remains true. The syntax of a while loop in java is:
while(expression/condition)
{
// statements
}
Here is java while loop that prints the numbers 1 through 10.
int loopvar=1; // Declare and initialize the loop counter
while(loopvar<=10) // test the loop
{
jTextField1.setText(String.valueOf(loopvar)); // prints the variable
loopvar=loopvar+1; // Increment the loop counter
}
Q11. Write a For loop to print out the values 1 to 10 on separate lines.
Ans.
int loop;
for(loop=1; loop<=10; loop= loop+1)
{
System.out.println(loop);
}
Q12. Write a for loop program to print pattern as :
1
22
333
4444
55555
Ans.
int loop=1;
for(loop=1; loop<=5; loop=loop+1)
{
for(int count=1; count<=loop; count=count+1)
{
jTextArea1.append(String.valueOf(loop));
}
jTextArea1.append("\n");
}
Q13. Write a for loop which sums all values between 10 and 100 into a variable called total. Assume that total has not been initialized to 0.
Ans.
int loop,total;
for(loop=10, total=0;loop<=100; loop= loop+1)
{
total=total+loop;
}
Q14. Write a program to print out the character set from A-Z using for loop.
Ans.
char ch;
for(ch='A'; ch<='Z'; ch=ch+1)
{
jTextArea1.append(String.valueOf(ch));
}
Q15. Find the output of the following program :
for(int i=0; i<10; i++)
jTextArea1.append(String.valueOf(++i)+"\n");
Ans.
1
3
5
7
9
No comments:
Post a Comment