Test Index
Functions in Python
© examsnet.com
Question : 12 of 23
Marks:
+1,
-0
What possible output from the given options is expected to be displayed when the following Python code is executed ?
import randomSignal = ['RED', 'YELLOW', 'GREEN']for K in range(2, 0, -1) : R = random.randrange(K) print (Signal[R], end = '#')
[CBSE 2024 Set 4]
Solution:
Concept:
random.randrange(K) returns a random integer from 0 to K-1.Solution:range(2, 0, -1) gives K = 2 and then K = 1.K = 2: randrange(2) gives 0 or 1, so Signal[R] is RED or YELLOW.K = 1: randrange(1) gives only 0, so the second word is always RED.Possible outputs: RED#RED# or YELLOW#RED#.Option A (YELLOW # RED #) is one of them. Options B, C and D are impossible: GREEN (index 2) can never be chosen, and the second word must be RED.Answer:Option A (YELLOW # RED #)© examsnet.com
Go to Question: