Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Which loop control structure is used to create a data validation loop in the

ID: 3831473 • Letter: 1

Question

1) Which loop control structure is used to create a data validation loop in the example in the supplemental notes?

2) A loop that uses an exit sentinel or exit code is what kind of loop?

3) What will be printed by the following code?
int bart = 6;
int marge = 2;
int lisa = 20;
for ( int homer = 5; homer > 0; homer-- ) {

if ( bart < lisa )

bart = bart + marge * homer;

}
System.out.println( bart );

4) What will be printed by the following code?
int bit = 6;
int mit = 2;
int lit = 20;
for ( int hit = 1; hit <= 5; hit++ ) {

if ( bit < lit )

bit += mit * hit;

}
System.out.println( bit );

5) In Question 4's program, which variable is an accumulator?

6) In Question 4's program, which variable is the loop control variable?

7) What will be printed by the following code?
int x;
for ( x = 2; x < 10; x = x * x )

System.out.print( x + " " );

System.out.println( "Final x = " + x );

8) What will be printed by the following code?
int x;
for (x = 11; x <= 15; x++) {

if (x % 2 == 0)

System.out.print("S ");

else

System.out.print("D ");

}

9) What will be printed by the following code? (This code is DIFFERENT from the previous problem!)
int x;
for (x = 11; x <= 15; x++) {

if (x / 12 == 0)

System.out.print("S ");

else

System.out.print("D ");

}

10) What will be printed by the following code?
int x = 5;
while ( x > 0 ) {

System.out.print( x + " " );
x -= 2;

}
System.out.println( "Final x = " + x );

a) a while loop

Explanation / Answer

1)      Which loop control structure is used to create a data validation loop in the example in the supplemental notes?

Ans) a do...while loop

____________________

2)      A loop that uses an exit sentinel or exit code is what kind of loop?

Ans) an indefinite loop

Reason:As We don’t know how many times the loop will execute,then it is indefinite loop

____________________

3) What will be printed by the following code?

int bart = 6;

int marge = 2;

int lisa = 20;

for (int homer = 5; homer > 0; homer--)

{

    if (bart < lisa)

        bart = bart + marge * homer;

}

System.out.println(bart);

Ans) d)24

____________________

4) What will be printed by the following code?

int bit = 6;

int mit = 2;

int lit = 20;

for (int hit = 1; hit <= 5; hit++)

{

    if (bit < lit)

        bit += mit * hit;

}

System.out.println(bit);

Ans) d)26

____________________

5) In Question 4's program, which variable is an accumulator?

Ans)

a)      bit

Reason:program uses Accumulator is a variable to calculate the sum ,product etc

____________________

6) In Question 4's program, which variable is the loop control variable?

Ans) b) hit

Reason:variable which is declared in for loop and variable which is used only by the for loop

____________________

7) What will be printed by the following code?

int x;

for (x = 2; x < 10; x = x * x)

    System.out.print(x + " ");

System.out.println("Final x = " + x);

Ans) 2 4 Final x = 16

____________________

8) What will be printed by the following code?

int x;

for (x = 11; x <= 15; x++)

{

    if (x % 2 == 0)

        System.out.print("S ");

    else

        System.out.print("D ");

}

Ans) D S D S D

____________________

9) What will be printed by the following code? (This code is DIFFERENT from the previous problem!)

int x;

for (x = 11; x <= 15; x++)

{

    if (x / 12 == 0)

        System.out.print("S ");

    else

        System.out.print("D ");

}

Ans) S D D D D

____________________

10) What will be printed by the following code?

int x = 5;

while (x > 0)

{

    System.out.print(x + " ");

    x -= 2;

}

System.out.println("Final x = " + x);

Ans) e) 5 3 1 Final x = -1

Reason: the value of the variable x is 5.The while loop continues to execute until the value is greater than 0.Every thime when the while loop executes the value 2 will be subtracted from the value x.

so, 5 3 1 will be printed.but in the last iteration x will become -1 as it is less than zero control will come out of the loop.so the finally Final x= -1 will be printed.So fthe final output of this program is 5 3 1 Final x = -1

_____________________Thank You