Output of python program | Set 15 (Modules)

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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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)

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?



filter_none

edit
close

play_arrow

link
brightness_4
code

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."))

chevron_right


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 each digit as different entity, thus 1, 1.

4) What is the output of the following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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 igonred. Case is not igonred 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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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)

chevron_right


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.

This article is contributed by Piyush Doorwar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.