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

1. Assume a is 3, b is 4, and x is 1. What does the following Java program fragm

ID: 3859168 • Letter: 1

Question

1. Assume a is 3, b is 4, and x is 1. What does the following Java program fragment display in the output window?

            if (a < b)

            {

                        while ( a <= 30 )

                        {

                                    a = a * 10;

                                    x = x + 5;

                        }

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

                        System.out.print( a );

            }

            else

            {

                        System.out.println("I better find a new career");

            }

Explanation / Answer

The initial values of a,b and x is

a = 3, b = 4, and x = 1.

  if (3 < 4) // condition true

{

                        while ( 3 <= 30 ) // condition true

                        {

                                    a = 3 * 10=30;

                                    x = 1 + 5 =6;

                        }

while ( 30 <= 30 ) // condition true

                        {

                                    a = 30 * 10=300;

                                    x = 6 + 5 = 11;

                        }

while ( 300 <= 30 ) // condition fail

                        System.out.print( x + " " ); // It prints 11

                        System.out.print( a ); // It print 300

            }

Output :

11 300