wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Programming Quiz

Total questions: 25

Worksheet time: 17mins

Name
Class
Date
1.

Which of the following is the output of the below Python code?


var1 = lambda var: var * 2

ret = lambda var: var * 3

result = 2

result = var1(result)

result = ret(result)

result = var1(result)

print(result)

a)

24

b)

12

c)

7

d)

36

e)

48

2.

Which of the following is the output of the below Python code?

[Note: Python 2.7.5 and above]


mylist=['a', 'aa', 'aaa', 'b', 'bb', 'bbb']

print(mylist[int(-1/2)])

a)

b

b)

bbb

c)

'bbb'

d)

a

3.

What is a correct syntax to output "Hello World" in Python?

a)

p("Hello World")

b)

print("Hello World")

c)

echo("Hello World");

d)

echo "Hello World"

4.

How do you insert COMMENTS in Python code?

a)

#This is a comment

b)

/*This is a comment*/

c)

//This is a comment

5.

Which one is NOT a legal variable name?

a)

my_var

b)

my-var

c)

_myvar

d)

Myvar

6.

How do you create a variable with the numeric value 5?

a)

Both the other answers are correct

b)

x = 5

c)

x = int(5)

7.

What is the correct file extension for Python files?

a)

.pyt

b)

.pyth

c)

.py

d)

.pt

8.

What is the correct syntax to output the type of a variable or object in Python?

a)

print(typeof(x))

b)

print(typeof x)

c)

print(typeOf(x))

d)

print(type(x))

9.

How do you create a variable with the floating number 2.8?

a)

x = 2.8

b)

x = float(2.8)

c)

Both the other answers are correct

10.

What is the correct way to create a function in Python?

a)

function myfunction():

b)

def myFunction():

c)

create myFunction():

11.

In Python, 'Hello', is the same as "Hello"

a)

True

b)

False

12.

What is a correct syntax to return the first character in a string?

a)

x = sub("Hello", 0, 1)

b)

x = "Hello"[0]

c)

x = "Hello".sub(0, 1)

13.

Which of the following is the correct output of the call to below line of code?


print(list("hello"))

a)

['h', 'e', 'l', 'l', 'o']

b)

None of these

c)

[h,e,l,l,o]

d)

['h' 'e' 'l' 'l' 'o']

14.

Which of the following is the output of the instructions given below?


mylist=[1, 5, 9, int('0')]

print(sum(mylist))

a)

16

b)

5

c)

1

d)

15

15.

Determine the output of the below Python code fragment?


var1 = True

var2 = False

var3 = False


if var1 or var2 and var3:

print("True")

else:

print("False")

a)

False

b)

None of these

c)

Runtime error

d)

True

16.

Which of the following statements best describes the behavior of the random.shuffle(mylist) as being used in the below code fragment?


import random

mylist = [10, 20, 30]

random.shuffle(mylist)

print(mylist)

a)

return a list where elements 10, 20 and 30 would be at random positions.

b)

It'll not modify the list. This function is just a placeholder and yet to be implemented.

c)

shuffle the elements of the list in-place.

d)

shuffle the elements for the no. of times equal to the size of the list.

17.

Which of the following is the output of the Python code fragment given below?


var1 = 4.5

var2 = 2

print(var1//var2)

a)

22.5

b)

2.5

c)

2.0

d)

2.25

e)

20.0

18.

Which method can be used to remove any whitespace from both the beginning and the end of a string?

a)

ptrim()

b)

len()

c)

trim()

d)

strip()

19.

Which method can be used to return a string in upper case letters?

a)

upperCase()

b)

uppercase()

c)

toUpperCase()

d)

upper()

20.

Which of these collections defines a LIST?

a)

("apple", "banana", "cherry")

b)

{"apple", "banana", "cherry"}

c)

{"name": "apple", "color": "green"}

d)

["apple", "banana", "cherry"]

21.

Which of these collections defines a TUPLE?

a)

{"name": "apple", "color": "green"}

b)

("apple", "banana", "cherry")

c)

["apple", "banana", "cherry"]

d)

{"apple", "banana", "cherry"}

22.

Which of these collections defines a SET?

a)

{"apple", "banana", "cherry"}

b)

{"name": "apple", "color": "green"}

c)

("apple", "banana", "cherry")

d)

["apple", "banana", "cherry"]

23.

Which of these collections defines a DICTIONARY?

a)

["apple", "banana", "cherry"]

b)

{"apple", "banana", "cherry"}

c)

{"name": "apple", "color": "green"}

d)

("apple", "banana", "cherry")

24.

Which collection is ordered, changeable, and allows duplicate members?

a)

LIST

b)

DICTIONARY

c)

SET

d)

TUPLE

25.

How do you start writing a for loop in Python?

a)

for x in y:

b)

for each x in y:

c)

for x > y: