1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
taurus [48]
3 years ago
11

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 method correctly reads the next number from the input
stream.int k = 0;int prod = 1;while (k>=0){System.out.println("Enter a number: ");k= readInt( );prod = prod*k;}System.out.println("product: "+prod);

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

A. The variable prod is incorrectly initialized
B. The while condition always evaluates to false
C. The while condition always evaluates to true
D. The negative number entered to signal no more input is included in the product
E. If the user enters a zero, the computation of the product will be terminated prematurely
Computers and Technology
1 answer:
leonid [27]3 years ago
5 0

Answer:

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

Explanation:

Given the code as follows:

  1.        int k = 0;
  2.        int prod = 1;
  3.        while (k>=0)
  4.        {
  5.            System.out.println("Enter a number: ");
  6.            k= readInt( );
  7.            prod = prod*k;
  8.        }
  9.        System.out.println("product: "+prod);

The line 7 is a logical error. Based on the while condition in Line 3, the loop shall be terminated if k smaller than zero (negative value). So negative value is a sentinel value of this while loop. However, if user enter the negative number to k, the sentinel value itself will be multiplied with prod in next line (Line 7) which result inaccurate prod value.

The correct code should be

  1.        int k = 0;
  2.        int prod = 1;
  3.        while (k>=0)
  4.        {
  5.            prod = prod*k;
  6.            System.out.println("Enter a number: ");
  7.            k= readInt( );
  8.        }
  9.        System.out.println("product: "+prod);
You might be interested in
Which of the following Access objects provides a user-friendly data entry method?
Rina8888 [55]

Answer:

Table

Explanation:

Microsoft Access uses an object called a _____ to enter and organize data.

The answer is table.

6 0
2 years ago
Hi guys, Im making a game. I want to make a collision event, but what is the code for making the wall and the box collide. I rea
professor190 [17]

Answer:

i think its 3337

Explanation:

7 0
3 years ago
Define Rule Of Thirds
Katena32 [7]

Answer:

In photography, the rule of thirds is a type of composition in which an image is divided evenly into thirds, both horizontally and vertically, and the subject of the image is placed at the intersection of those dividing lines, or along one of the lines itself.

6 0
2 years ago
What finger should be on the Y key?
geniusboy [140]

Answer:

Right index

Explanation:

6 0
3 years ago
Read 2 more answers
CODEHS- Please help!
Ksivusya [100]

Answer:

Revisiting "Build a Tower"

Recall in the last section how we made Karel make a tower of tennis balls. We told Karel to move() and turnLeft() and putBall() until we had a tower. At the end of the program, Karel was still at the top of the tower, like as in the picture below.

Stuck at top

Suppose that now we want Karel to come back down from the top of the tower. The first thing we need to do is get Karel facing in the right direction. One way to do this is to tell Karel

turnLeft();

turnLeft();

turnLeft();

And then tell Karel to

move();

move();

move();

back to the bottom of the tower.

However, telling Karel to turnLeft() three times is not very readable. That's a lot of writing when all we really want is to tell Karel to "turn right."

Explanation:

Hopefully it would help.

8 0
3 years ago
Other questions:
  • to prevent long page load time for pages containing images. It is best to use a compress file formatlike jpeg as well as appropr
    6·2 answers
  • If an author is creating a reference list and wants the second and succeeding lines indented for a reference, they should select
    13·2 answers
  • I need urgent help. which of these network has minimum data loss. a. LAN b. MAN c. WAN ​
    11·1 answer
  • How can you crop a photo in PowerPoint?
    15·2 answers
  • Universal Containers (UC) has multi-level account hierarchies that represent departments within their major Accounts. Users are
    13·1 answer
  • Which of the following keywords is used to remove a row in a table? (Points : 2) DROP
    11·1 answer
  • Your dad just gave you his old computer running Windows 7. You want to see how many volumes are contained within it. Which tool
    10·1 answer
  • What is the missing line of code?
    7·1 answer
  • The excerpt is a sample works-cited list.
    5·2 answers
  • A user complains that the pointer movement on his laptop computer is very erratic what should you do?
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!