Computer Science
What are the possible outcome(s) executed from the following code? Also specify the maximum and minimum values that can be assigned to variable NUMBER.
STRING = "CBSEONLINE"
NUMBER = random.randint(0, 3)
N = 9
while STRING[N] != 'L' :
print(STRING[N] + STRING[NUMBER] + '#', end = '')
NUMBER = NUMBER + 1
N = N - 1
- ES#NE#IO#
- LE#NO#ON#
- NS#IE#LO#
- EC#NB#IS#
Python Libraries
6 Likes
Answer
The outcomes are as follows:
ES#NE#IO#
EC#NB#IS#
NUMBER is assigned a random integer between 0 and 3 using random.randint(0, 3). Therefore, the minimum value for NUMBER is 0 and the maximum value is 3.
Explanation
Length of STRING
having value "CBSEONLINE" is 10. So the character at index 9 is "E". Since the value of N starts at 9 the first letter of output will be "E". This rules out LE#NO#ON# and NS#IE#LO# as possible outcomes as they don't start with "E". NUMBER
is a random integer between 0 and 3. So, the second letter of output can be any letter from "CBSE". After that, NUMBER
is incremented by 1 and N
is decremented by 1.
In next iteration, STRING[N]
will be STRING[8]
i.e., "N" and STRING[NUMBER]
will be the letter coming immediately after the second letter of the output in the string "CBSEONLINE" i.e., if second letter of output is "S" then it will be "E" and if second letter is "C" then it will be "B".
In the third iteration, STRING[N]
will be STRING[7]
i.e., "I" and STRING[NUMBER]
will be the letter coming immediately after the fourth letter of the output in the string "CBSEONLINE" i.e., if fourth letter of output is "E" then it will be "O" and if fourth letter is "B" then it will be "S".
After this the while
loop ends as STRING[N]
i.e., STRING[6]
becomes "L". Thus both, ES#NE#IO# and EC#NB#IS# can be the possible outcomes of this code.
Answered By
3 Likes
Related Questions
Import the above module basic.py and write statements for the following :
(a) Compute square of 19.23.
(b) Compute floor division of 1000.01 with 100.23.
(c) Compute product of 3, 4 and 5. (Hint. use a function multiple times).
(d) What is the difference between basic.div(100, 0) and basic.div(0, 100)?
Suppose that after we import the random module, we define the following function called
diff
in a Python session :def diff(): x = random.random() - random.random() return(x)
What would be the result if you now evaluate
y = diff() print(y)
at the Python prompt ? Give reasons for your answer.
Consider the following code :
import random print(int(20 + random.random() * 5), end = ' ') print(int(20 + random.random() * 5), end = ' ') print(int(20 + random.random() * 5), end = ' ') print(int(20 + random.random() * 5))
Find the suggested output options 1 to 4. Also, write the least value and highest value that can be generated.
- 20 22 24 25
- 22 23 24 25
- 23 24 23 24
- 21 21 21 21
Consider the following code :
import random print(100 + random.randint(5, 10), end = ' ' ) print(100 + random.randint(5, 10), end = ' ' ) print(100 + random.randint(5, 10), end = ' ' ) print(100 + random.randint(5, 10))
Find the suggested output options 1 to 4. Also, write the least value and highest value that can be generated.
- 102 105 104 105
- 110 103 104 105
- 105 107 105 110
- 110 105 105 110