exam questions

Exam 98-381 All Questions

View all questions & answers for the 98-381 exam

Exam 98-381 topic 1 question 26 discussion

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

You are writing code that generates a random integer with a minimum value of 5 and a maximum value of 11.
Which two functions should you use? Each correct answer presents a complete solution. (Choose two.)

  • A. random.randint(5, 12)
  • B. random.randint(5, 11)
  • C. random.randrange(5, 12, 1)
  • D. random.randrange(5, 11, 1)
Show Suggested Answer Hide Answer
Suggested Answer: BC 🗳️
References: https://docs.python.org/3/library/random.html#

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
atxsu
Highly Voted 5 years, 2 months ago
BD, is not the correct answer, it is actually BC, I've checked it on an online python IDE, random.randrange(5, 12, 1) will show numbers from 5 to 11 because it's last parameter excludes 1 number from 12, so 11 will be the maximum number shown
upvoted 12 times
...
shtingdcknipples
Most Recent 4 years, 1 month ago
BC correct
upvoted 2 times
...
sagacious_smith
4 years, 2 months ago
from random import randint, randrange from collections import defaultdict d = defaultdict(int) for n in range(1000): # d[randrange(5, 12, 1)] += 1 # [5, 6, 7, 8, 9, 10, 11] # d[randrange(5, 11, 1)] += 1 # [5, 6, 7, 8, 9, 10] # d[randint(5, 12)] += 1 # [5, 6, 7, 8, 9, 10, 11, 12] d[randint(5, 11)] += 1 # [5, 6, 7, 8, 9, 10, 11] print(sorted(list(d.keys())))
upvoted 1 times
...
FuzzyDunlop
4 years, 10 months ago
It's a tricky question but BC are correct because randint is inclusive and randrange is not inclusive. So when using randint you would need (5,11), which is the equivalent of randrange(5,12)
upvoted 4 times
gargaditya
4 years, 6 months ago
random.randrange(start, stop[, step]) random.randint(a, b) Return a random integer N such that a <= N <= b. Alias for randrange(a, b+1). ----------- random.randint(5,11) chooses between 5,6,...10,11 random.randrange(5,12) chooses between 5,6,...10,11 ie LAST NUMBER is EXCLUDED so BC marked is indeed correct
upvoted 1 times
...
...
JAKJR
4 years, 10 months ago
BC is correct. Here is my sample code to prove it. from random import randint, randrange resultint = [] resultrange = [] for i in range (50): resultint.append(randint(5,11)) resultrange.append(randrange(5,12,1)) print(resultint) print (resultrange)
upvoted 3 times
...
Ferdex
4 years, 11 months ago
BD, and just do it the test
upvoted 1 times
...
Krish3197
5 years ago
Bc only correct
upvoted 4 times
...
Tshego1995
5 years, 2 months ago
The correct answer is BD. The statement says, a minimum of 5 and a maximum of 11,not 12
upvoted 2 times
...
dale
5 years, 2 months ago
bd is the correct answer
upvoted 3 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 ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago