Open In App

Output of python program | Set 15 (Modules)

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Regular Expressions Note: Output of all these programs is tested on Python3 

1) Which of the options below could possibly be the output of the following program? 

PYTHON




from random import randrange
L = list()
for x in range(5):
    L.append(randrange(0, 100, 2)-10)
 
# Choose which of outputs below are valid for this code.   
print(L)


a) [-8, 88, 8, 58, 0] b) [-8, 81, 18, 46, 0] c) [-7, 88, 8, 58, 0] d) [-8, 88, 94, 58, 0] 

Ans. (a) Explanation: The for loop will result in appending 5 elements to list L. Range of the elements lies from [0, 98] – 10 = [-10, 88], which rules out option (d). The upper range is 98 because the step size is 2, thus option (c) and (b) are invalid. Also note that each time you may not get the same output or the one in the options as the function is random. 
 

2) What is the output of the following program? 

PYTHON




from math import *
a = 2.13
b = 3.7777
c = -3.12
print(int(a), floor(b), ceil(c), fabs(c))


a) 2 3 -4 3 b) 2 3 -3 3.12 c) 2 4 -3 3 d) 2 3 -4 3.12 

Ans. (b) Explanation: int() returns the integer value of a number, int(2.13) = 2. floor() returns the largest integer lesser or equal to the number, floor(3.777) = 3. ceil() returns smallest integer greater or equal to the number, ceil(-3.12) = -3. fabs() return the modulus of the number, thus fabs(-3.12) = 3.12. 
 

3) What is the output of the following program? 

PYTHON




import re
p = re.compile('\d+')
print(p.findall("I met him once at 11 A.M. on 4th July 1886"), end = " ")
p = re.compile('\d')
print(p.findall("I went to him at 11 A.M."))


a) [’11’, ‘4’, ‘1886’, ’11’] b) [‘1141886’] [‘1’, ‘1’] c) [’11’, ‘4’, ‘1886’] [’11’] d) [’11’, ‘4’, ‘1886’] [‘1’, ‘1’] 

Ans. (d) Explanation: \d is equivalent to [0-9] and \d+ will match a group on [0-9], group of one or greater size. In first statement, group of digits are 11, 4, 1886. In the second statement, \d will treat each digit as different entity, thus 1, 1. 
 

4) What is the output of the following program? 

PYTHON




import re
print(re.sub('ge', '**', 'Geeksforgeeks', flags = re.IGNORECASE), end = " ")
print(re.sub('ge', '**', 'Geeksforgeeks'))


a) **eksfor**eks **eksfor**eks b) **eksfor**eks Geeksfor**eks c) **Geeksfor**geeks Geeksfor**geeks d) TypeError: ‘str’ object does not support item assignment 

Ans. (b) Explanation: In the first print statement, all ‘ge’ will be replaced ‘**’, and case is ignored. Case is not ignored in 2nd statement, thus ‘ge’ will be replaced but not ‘Ge’. 
 

5) Which of the options below could possibly be the output of the following program? 

PYTHON




import math
import random
L = [1, 2, 30000000000000]
for x in range(3):
    L[x] = math.sqrt(L[x])
 
# random.choices() is available on Python 3.6.1 only.
string = random.choices(["apple", "carrot", "pineapple"], L, k = 1)
print(string)


a) [‘pineapple’] b) [‘apple’] c) ‘pineapple’ d) both a and b 

Ans. (d) Explanation: Two modules math and random are used, L after the for loop will be [1.0, 1.4142135623730951, 5477225.575051662]. choices() has choice as 1st parameter and their weights as second parameter, k is the number valued needed from choice. The answer will come out to ‘pineapple’ almost always due to it’s weight but ‘apple’ and ‘carrot’ may turn out to be the output at times. 



Last Updated : 26 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads