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

18. Consider the following code segment. int num1 = 0; int num2 = 3; while ((num

ID: 3718353 • Letter: 1

Question

18. Consider the following code segment.

int num1 = 0;
int num2 = 3;

while ((num2 != 0) && ((num1 / num2) >= 0))
{
      num1 = num1 + 2;
      num2 = num2 - 1;
}
What are the values of num1 and num2 after the while loop completes its execution?

Question 18 options:

The loop will never complete its execution because a division by zero will generate an ArithmeticException.

num1 = 6, num2 = 0

num1 = 4, num2 = 1

num1 = 8, num2 = -1

num1 = 0, num2 = 3

19. Consider the following code segment. The code is intended to read nonnegative numbers and compute their product until a negative number is read; however, it does not work as intended. (Assume that the readInt mehtod correctly reads the next number from the input stream.)

     int k = 0;
     int prod = 1;
     while (k >= 0)
     {
           System.out.print("enter a number: " );
           k = readInt(); //readInt reads the next number from input
           prod = prod * k;
    }

System.out.println ("product: " + prod);

Which of the following best describes the error in the program?

Question 19 options:

The while condition always evaluates to false.

The variable prod is incorrectly initialized.

The negative number entered to signal no more input is included in the product.

The while condition always evaluates to true

If the user enters a zero, the computation of the product will be terminated prematurely.

The loop will never complete its execution because a division by zero will generate an ArithmeticException.

num1 = 6, num2 = 0

num1 = 4, num2 = 1

num1 = 8, num2 = -1

num1 = 0, num2 = 3

Explanation / Answer

ANSWER 18: In this while loop will run three times so values of num1=6 and num2=0 so option(A) will be correct.

ANSWER 19: Here In this no error in the program but according to your need option(D) is right for you.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote