PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Medium

Question 42

Q474 - Modules


Look at the code below:



Which lines of code would you insert so that it is possible for the program

to output the following result:


6 82 0


  • A
  • B
  • C
  • D
Reveal correct answer

Correct answer: B

Explanation

Topics: import random randint() randrange() choice()


Try it yourself:

Explanation:

randint(0, 100) will return a number

between 0 (inclusive) and 100 (inclusive).

randrange(10, 100, 3) will return a number between

10 (inclusive) and 100 (EXCLUSIVE)

with a step of three: 10, 13, 16, ... , 79, 82, 85, ...

choice((0, 100, 3)) will return one element of the tuple (0, 100, 3)


Q474 (Please refer to this number, if you want to write me about this question.)

A. This choice is incorrect because it assigns the result of random.choice() function to variable 'b', which may not generate an integer value between 10 and 100 with a step of 3 as required. The order of functions used for 'a', 'b', and 'c' does not match the expected output of 6, 82, and 0.

B. This choice is correct because it uses the random.randint() function to generate a random integer between 0 and 100 for variable 'a', random.randrange() function to generate a random integer between 10 and 100 with a step of 3 for variable 'b', and random.choice() function to select a random element from the tuple (0, 100, 3) for variable 'c'. This combination of functions ensures that the values assigned to 'a', 'b', and 'c' are within the specified ranges.

C. This choice is incorrect because it assigns the result of random.randrange() function to variable 'a', which may not generate an integer value between 0 and 100 as required. The order of functions used for 'a', 'b', and 'c' does not match the expected output of 6, 82, and 0.

D. This choice is incorrect because it assigns the result of random.choice() function to variable 'a', which may not generate an integer value between 0 and 100 as required. Additionally, the order of functions used for 'a', 'b', and 'c' does not match the expected output of 6, 82, and 0.

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need