wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

PYTHON_Random Module

Total questions: 50

Worksheet time: 2hrs 40mins

Name
Class
Date
1.

_____ represents an entity in the real world with its identity and behaviour.

a)

A method

b)

An object

c)

A class

d)

An operator

2.

What type of inheritance is illustrated in the following Python code?

class A():

pass

class B():

pass

class C(A,B):

pass

a)

Multi-level inheritance

b)

Multiple inheritance

c)

Hierarchical inheritance

d)

Single-level inheritance

3.

What will be the output of the following Python code?


class A:

def __init__(self):

self._x = 5


class B(A):

def display(self):

print(self._x)


def main():

obj = B()

obj.display()


main()

a)

Error, invalid syntax for object declaration

b)

Nothing is printed

c)

5

d)

Error, private class member can’t be accessed in a subclass

4.

What will be the output of the following Python code?

def say(message, times = 1):

print(message * times)


say('Hello')

say('World', 5)

a)

Hello

WorldWorldWorldWorldWorld

b)

Hello

World 5

c)

Hello

World,World,World,World,World

d)

Hello

HelloHelloHelloHelloHello

5.

What will be the output of the following Python code?

def func(a, b=5, c=10):

print('a is', a, 'and b is', b, 'and c is', c)


func(3, 7)

func(25, c = 24)

func(c = 50, a = 100)

a)

a is 7 and b is 3 and c is 10

a is 25 and b is 5 and c is 24

a is 5 and b is 100 and c is 50

b)

a is 3 and b is 7 and c is 10

a is 5 and b is 25 and c is 24

a is 50 and b is 100 and c is 5

c)

a is 3 and b is 7 and c is 10

a is 25 and b is 5 and c is 24

a is 100 and b is 5 and c is 50

d)

Error

6.

What will be the output of the following Python code?

a=10

b=20


def change():

global b

a=45

b=56


change()

print(a)

print(b)

a)

10

56

b)

45

56

c)

10

20

d)

Error

7.

What will be the output of the following Python code?

class A:

def test1(self):

print(" test of A called ")


class B(A):

def test(self):

print(" test of B called ")


class C(A):

def test(self):

print(" test of C called ")


class D(B,C):

def test2(self):

print(" test of D called ")


obj=D()

obj.test()

a)

test of B called

test of C called

b)

test of C called

test of B called

c)

test of B called

d)

Error, both the classes from which D derives has same method test()

8.
What is the difference between a class and an object?
a)
A class is a blueprint to make an object
b)
An object is a blueprint to make a class
c)
A blueprint is an object to make a class
d)
Blueprint class is an object make a
9.
These have identitystate, and behavior.
a)
class
b)
object
c)
method
d)
void
10.

Exceptions


try:

n = int(input("How old are you? "))

percent = round(n*100/80, 1)

print("You've gone through", percent, "% of your life!")

except ValueError:

print("Oops, must enter a number.")

except ZeroDivisionError:

print("Division by zero.")

except:

print("Something went very wrong.")


If the user enters "0" in the code above what does the program do?

a)

prints "You've gone through 0.0 % of your life!"

b)

prints "Division by zero."

11.

Suppose B is a subclass of A, to invoke the __init__ method in A from B, what is the line of code you should write?

a)

A.__init__(self)

b)

B.__init__(self)

c)

A.__init__(B)

d)

B.__init__(A)

12.

a)

Error because class B inherits A but variable x isn’t inherited

b)

0 0

c)

0 1

d)

Error, the syntax of the invoking method is wrong

13.
a)

Error, the syntax of the invoking method is wrong

b)

The program runs fine but nothing is printed

c)

1 0

d)

1 2

14.
a)

Error as age isn’t defined

b)

True

c)

False

d)

7

15.
a)

Old

b)

New

c)

error

d)

Nothing is printed

16.
a)

12

b)

52

c)

13

d)

60

17.
a)

test of B called

test of C called

b)

test of C called

test of B called

c)

test of B called

d)

test of B called

18.

people = ["John", "Rob", "Bob"]

print (people[4])

what would this result be?

a)
John
b)
Rob
c)
Bob
d)
Error
19.

What does __init__ do?

a)

Constructs the instance

b)

Initialize the instance values

c)

Calls the Super class

d)

Creates a circle

20.

What is the process of using a function for more than one purpose that allows the use of different internal structures of the object by keeping the same external interface?

a)

Inheritance

b)

Polymorphism

c)

Abstraction

d)

Encapsulation

21.

What is the process in which objects of one class can link and share some common properties from the objects of another class?

a)

Abstraction

b)

Inheritance

c)

Polymorphism

d)

Encapsulation

22.

Which of the following is correct with respect to the OOP concept in Python?

a)

Objects are real-world entities while classes are not real.

b)

Classes are real-world entities while objects are not real.

c)

Both objects and classes are real-world entities.

d)

Both objects and classes are not real.

23.

In which approach data hiding is not possible ?

a)

POP

b)

OOP

24.

How many Access Specifiers are available ?

a)

1

b)

2

c)

3

25.

In which approach all the variable are Global Variable ?

a)

POP

b)

OOP

26.

In POP who can communicate with each other ?

a)

Object

b)

Procedure

27.

What is the main advantage for OOP ?

a)

Data Hiding

b)

Code Reusability

28.

POP follows

a)

Top-Down Approach

b)

Bottom-Up Approach

29.

OOP follows

a)

Top-Down Approach

b)

Bottom-Up Approach

30.

The name given to a procedure or function residing within a class.

a)

method

b)

attribute

c)

modifier

d)

parameter

31.

What does an object consist of?

a)

Methods

b)

Attributes

c)

Methods and Attributes

d)

Procedures

e)

Variables

32.

It is a person who writes, develops, and debugs the program.

a)

Web developer

b)

Computer technician

c)

IT Expert

d)

Programmer

33.

It refers to the third step in creating program.

a)

Code the program.

b)

Define the scope of the problem.

c)

Test and Debug the program.

d)

Creating a visual interface.

34.
What is the name of the environment in Python that write your programs and then test them out?
a)
Shell
b)
Window
c)
IDLE
d)
CLI
35.

A block within a block is called ________block.

a)

Nested

b)

Control

c)

Compressed

d)

Called

36.
What will print?
a)
1 2
b)
1
c)
3
d)
5
37.

What is the output of the following program:


import re

sentence = 'horses are fast'

regex = re.compile('(?P<animal>\w+) (?P<verb>\w+) (?P<adjective>\w+)')

matched = re.search(regex, sentence)

print(matched.groupdict())

a)

{‘animal’: ‘horses’, ‘verb’: ‘are’, ‘adjective’: ‘fast’}

b)

(‘horses’, ‘are’, ‘fast’)

c)

‘horses are fast’

d)

‘are’

38.

7. How many types of functions available in Python?

a)

3

b)

2

c)

1

d)

4

39.

random.randrange(6)

a)

0,1,2,3,4,5,6

b)

1,2,3,4,5,6

c)

0,1,2,3,4,5

d)

1,2,3,4,5

40.

random.uniform() requires two parameters

a)

True

b)

False

41.

random.random() generates a floating point number between _____ and ______

a)

1 and 2

b)

1 and 0

c)

0 and 1

d)

0 and 10

42.

docstrings

a)

single quoted comments

b)

double quoted comments

c)

triple quoted comments

d)

hash tagged comments

43.

Write the output of the following code :


import random

for i in range(random.randint(2,2)):

print(i)

a)

0

1

b)

1

2

c)

0

1

2

d)

Generate different number

44.

Write the output of the following code :


import random

for i in range(random.randint(2,3)):

print("A")

a)

A

A

b)

A

A

A

c)

A

A

OR

A

A

A

d)

ERROR

45.

What is the minimum and maximum value of 'A' in the following statement..

import random

A=random.randint(2,30)

print(A)

a)

3 and 30

b)

2and 30

c)

2 and 29

46.

Write the output of the following code :

import random

A=random.randrange(20)

print(A)

a)

Any number between 0 to 19 (including both)

b)

Any number between 0 to 20 (including both)

c)

Any number between 1 to 20 (including both)

47.

Write the output of the following code :

import random

print(int(random.random()))

a)

Any random number less than 1 and greater than 0

b)

Always generate 0

c)

Always generate 1

d)

Shows Error

48.

Which of the following subject will never printed in following code?


import random

L=["English","Hindi","Math","Science","SST","CS","IP"]

for i in range(random.randint(1,6)):

print(L[i])

a)

CS and IP both

b)

English

c)

IP

d)

English and Hindi both

49.

Which of the following method is not included in random module?

a)

Shuffle( )

b)

randrange( )

c)

randomrange( )

d)

uniform( )

50.

A = random.______([1,2,3,4,5])

Value of A can be 1 to 5(including both)

a)

shuffle

b)

choice

c)

uniform

d)

none of these