Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

PYTHON PROGRAMMING

Total questions: 10

Worksheet time: 5mins

Name
Class
Date
1.

Predict the output of the following code:

x=3

If x>2 or x<5 and x==6:

Print(“ok”)

else:

print(“no output”)

a)

ok

b)

okok

c)

no output

d)

none of above

2.

Which one of the following if statements will not execute successfully?

1) if (1, 2);

print(‘foo’)

2) if (1, 2):

print(‘foo’)

3) if (1):

print( ‘foo’ )

4) if (1);

print( ‘foo’ )

a)

1,4

b)

2

c)

2,4

d)

4

3.

Find the output of the following program segments:

for i in range(20,30,2):

print(i)

a)

20 22 24 26 28

b)

20

22

24

26

28

c)

20 22 24 26 28 30

d)

20

22

24

26

28

30

4.

Which one of the following is incorrect?

a)

The variables used inside function are called local variables.

b)

The local variables of a particular function can be used inside other functions, but these cannot be used in global space.

c)

The variables used outside function are called global variables

d)

In order to change the value of global variable inside function, keyword global is used.

5.

Suppose a function called add() is defined in a module called adder.py. Which of the following code snippets correctly show how to import and use the add() function? Select all that apply.

a)

from adder import add

result = adder.add(2, 3)

b)

from adder import add

result = add(2, 3)

c)

import add from adder

result = add(2, 3)

d)

import adder

result = adder.add(2, 3)

6.

What is the output of the given below program?

print("Know Program".split())

a)

‘Know’, ‘Program’

b)

(‘Know’, ‘Program’)

c)

[‘Know’, ‘Program’]

d)

{‘Know’, ‘Program’}

7.

Find the output of the given Python program?

print(list("abcd".split('')))

a)

[‘a’, ‘’, ‘b’, ‘’, ‘c’, ‘*’, ‘d’]

b)

[‘a’, ‘b’, ‘c’, ‘d’]

c)

[‘abcd’]

d)

[‘abc*d’]

8.

What will be the output of the following Python code?

a={1:"A",2:"B",3:"C"}

a.items()

a)

items()

b)

dict_items([(1,2,3)])

c)

dict_items([(‘A’), (‘B’), (‘C’)])

d)

dict_items([(1, ‘A’), (2, ‘B’), (3, ‘C’)])

9.

Which of the following is correctly evaluated for this function?

  1. pow(x,y,z)  

a)
  1. (x**y) / z

b)
  1. (x / y) * z

c)
  1. (x**y) % z

d)
  1. (x / y) / z

10.

Which one of the following syntaxes is the correct syntax to read from a simple text file stored in ''d:\java.txt''?

a)
  1. Infile = open(''d:\\java.txt'', ''r'')

b)
  1. Infile = open(file=''d:\\\java.txt'', ''r'')

c)
  1. Infile = open(''d:\java.txt'',''r'')

d)
  1. Infile = open.file(''d:\\java.txt'',''r'')