Question: 18. Consider the following code segment. int num1 = 0; int num2 = 3; while ((num2 != 0) && ((num1 / num2) >= 0)) {
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. |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
