exam questions

Exam 98-381 All Questions

View all questions & answers for the 98-381 exam

Exam 98-381 topic 1 question 9 discussion

Actual exam question from Microsoft's 98-381
Question #: 9
Topic #: 1
[All 98-381 Questions]

The ABC company has hired you as an intern on the coding team that creates e-commerce applications.
You must write a script that asks the user for a value. The value must be used as a whole number in a calculation, even if the user enters a decimal value.
You need to write the code to meet the requirements.
Which code segment should you use?

  • A. totalItems = input("How many items would you like?")
  • B. totalItems = float(input("How many items would you like?"))
  • C. totalItems = str(input("How many items would you like?"))
  • D. totalItems = int(input("How many items would you like?"))
Show Suggested Answer Hide Answer
Suggested Answer: D 🗳️

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
CERTIFICATION1990
Highly Voted 4 years, 9 months ago
the ans should be totalItems = int(float(input("How many items would you like?")))
upvoted 16 times
...
JeffJupiter
Highly Voted 5 years, 1 month ago
answer should be D
upvoted 11 times
Vaibhavdhingra24
5 years ago
no actually, because if you run the option with int(), it throws an error when you input a float value. Therefore, float option will be most apt
upvoted 17 times
...
...
KeiMie
Most Recent 1 year, 9 months ago
Selected Answer: B
IF you handle decimals, when input, you must use float. Later you can change this as int
upvoted 1 times
...
klinkklonk
2 years, 4 months ago
Selected Answer: D
Whole number means all positive numbers including 0. Doesn't include fractions, decimals or negatives numbers.
upvoted 1 times
...
SP_RSA
2 years, 8 months ago
B. totalItems = float(input("How many items would you like?")) The number will be used in a calculation at a later stage, meaning we will convert it later. Selecting D will give us an error when we enter an integer as a parameter.
upvoted 2 times
...
BeeThree33
2 years, 8 months ago
Lets look at: "The value must be used as a whole number in a calculation, even if the user enters a decimal value." "...even if the user enters a decimal value." - This indicates that we have to allow the user to enter a float or integer. "..must be used as a whole number in a calculation.." - This does not state that we have to immediately convert the value to an integer. Therefore, the two implementations below should be correct: 1. totalItems = int(float(input("How many items would you like?"))) 2. totalItems = float(input("How many items would you like?")) totalItems = int(totalItems) # We convert to int later in our program. Thus, the nearest correct answer will be B. Option D will fail when we enter a decimal value is entered by the user, which makes our program non-compliant to the requirement that we have to allow integers and float values.
upvoted 3 times
...
PavanKumpatlaAccountTwo
3 years, 5 months ago
Selected Answer: D
Wait, I'm pretty sure the answer is supposed to be D
upvoted 3 times
...
Soumya_R
3 years, 6 months ago
Selected Answer: D
Answer should be Option D
upvoted 2 times
...
gautam92
3 years, 11 months ago
x=int(input("please enter the number float:")) please enter the number float:13.56 Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> x=int(input("please enter the number float:")) ValueError: invalid literal for int() with base 10: '13.56' x=float(input("please enter the number float:")) please enter the number float:25.556 >>> print(int(x)) 25 So D is correct option as that code snipped will be used in the function first , to convert string into float .
upvoted 3 times
Shw7
3 years, 11 months ago
But whole numbers are not decimal numbers rite
upvoted 1 times
PavanKumpatlaAccountTwo
3 years, 5 months ago
Ya. Whole numbers don't have decimals from what I know. So answer D is correct although it says B
upvoted 1 times
...
...
...
Satyam1234
4 years ago
In this the right answers is D because we have to print a whole number and in whole number never a decimal and fraction occur so no float() will be used
upvoted 1 times
...
shtingdcknipples
4 years, 3 months ago
The answer B is correct.
upvoted 6 times
...
codibel
4 years, 3 months ago
should this code have error handling? except statement?
upvoted 2 times
...
Gichia
4 years, 7 months ago
the answer should be D
upvoted 2 times
...
nowisthetime
4 years, 11 months ago
this is a bad question. is had to be "B" "D" will throw an error. totalItems = float(input("How many items would you like?")) totalItems = int(totalItems)
upvoted 6 times
PavanKumpatlaAccountTwo
3 years, 5 months ago
Wdym??? It says:- totalItems = int(input(totalItems)) . for D
upvoted 1 times
...
...
JeffJupiter
4 years, 11 months ago
Vaibhavdhingra24 is correct. When trying to convert int(input("enter number")) when a float is entered by user does indeed throw a ValueError. This question is worded wrong when stated the value needs to used as a whole number.
upvoted 1 times
...
BW_20
5 years, 1 month ago
doesn't float handle integers that are calculated when entered as a string?
upvoted 2 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...