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
This assignment is to code a simple hangman game. The game should choose a random word out of a list of words that are coded int
pshichka [43]

Answer:

Programming language not stated.

I'll use python for this question

Explanation:

import random

# library that we use in order to choose

# on random words from a list of words

words = ['rain, 'computer', 'science', 'program, 'python', 'mathematics', 'player', 'condition','reverse', 'water', 'board', 'geeks','learn','school','days','scholar','collar','flood','house','flies']

# Function will choose one random word from this list of words

word = random.choice(words)

print("Guess the word")

guesses = ''"

# 5 turns

turns = 5

while turns > 0:

# counts the number of times a user fails

failed = 0

# all characters from the input word taking one at a time.

for char in word:

# comparing that character with the character in guesses

if char in guesses:

print(char)

else:

print("_")

# for every failure 1 will be incremented in failure

failed += 1

if failed == 0:

# user will win the game if failure is 0 and 'You Win' will be given as output

print("You Win")

# this print the correct word

print("The word is: ", word)

break

# if user has input the wrong alphabet then it will ask user to enter another alphabet

guess = input("guess a character:")

# every input character will be stored in guesses

guesses += guess

# check input with the character in word

if guess not in word:

turns -= 1

# if the character doesn’t match the word then “Wrong” will be given as output

print("Wrong")

# this will print the number of turns left for the user

print("You have", + turns, 'more guesses')

if turns == 0:

print("You Loose")

7 0
2 years ago
What appears in the document after you have inserted a video?
Nitella [24]

Answer:

text with a hyperlink to the video website

Explanation:

documents do not auto-upload thumbnails or embedded videos for playback, so external links are pasted there

6 0
3 years ago
Read 2 more answers
HELP PLEASE!! WILL MARK FIRST AND MOST RELIABLE ANSWER BRAINLIEST!!~~~~What is the primary difference between sort and filter?
tatiyna

Answer:

A

Explanation:

Example: when scrolling through a list of shows, if you filter for action shows, only shows that match the action description would appear

3 0
3 years ago
In cell I5, enter a formula to calculate the total admission fees collected for the 2018
V125BC [204]
The formula is to calculate the total admission fees collected is = F5*B14

1. Click on cell I5
2. Type equal sign =
3. Type F5 or click on F5 cell
3. Type multiplication sign *
4. Type B14 or click on B14 cell
4 0
3 years ago
Read 2 more answers
Security measures are sometimes described as a combination of physical, technical, and administrative (PTA) safeguards. Which of
attashe74 [19]

Answer:

Measures including device data encryption, anti-malware software, and communications encryption.

6 0
3 years ago
Other questions:
  • Which activity cannot be done on a social networking site?
    12·1 answer
  • Should I learn Python, C++, C# or VB to start off programming games for Windows. Open to other language recommendations as well!
    6·1 answer
  • The PICC team is scheduled to remove a PICC before client discharge. Assessment of the catheter indicates the PICC and determine
    7·1 answer
  • A(n) ______ system is a set of programs that coordinates all the activities among computer or mobile device hardware. a. managem
    10·1 answer
  • Does Buzz or APEX shows all your due assignments on the left of the main home screen?
    11·1 answer
  • How should I do it Please code for Java
    15·1 answer
  • Write, in your own words, a one-two paragraph summary on the Running Queries and Reports tutorials. Apply critical thinking and
    10·1 answer
  • _____ is rampant with bugs and shortcomings because most programmers do not know how or do not take the time to incorporate secu
    14·1 answer
  • Which one do we use to send signals as IR light waves?
    11·1 answer
  • Limitations of systems analysis and design​
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!