Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

python-2

Total questions: 135

Worksheet time: 2hrs 41mins

Name
Class
Date
1.

Which of these is the best description of a list in Python?

a)

A list is a collection of data that has an order and can be changed

b)

A list is a lot of variables

c)

A list is used for shopping

d)

A list is a collection of data that cannot hold duplicated data and cannot be changed

2.

Which of these is the correct code for creating a list of names?

a)

nameList = John, Harry, Jesse, John, Harry, Harry

b)

nameList = ("John", "Harry", "Jesse", "John", "Harry", "Harry")

c)

nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]

d)

nameList = [John, Harry, Jesse, John, Harry, Harry]

3.

List items have an index number. In the following list, which item has the index number of 3?

["John", "Harry", "Jesse", "John", "Harry", "Harry"]

a)

"John"

b)

"Harry"

c)

"Jesse"

4.

Which of these pieces of code would return the name "Harry" from the following list?

nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]

a)

nameList()

b)

nameList[1]

c)

NameList(4)

d)

nameList["4"]

5.

Which of these codes is correct for creating a list of numbers?

a)

num = ('10','29','37','109')

b)

num = ['10','29','37','109']

c)

num == ('10','29','37','109')

d)

num = ['10 29 37 109']

6.

How do i remove something from a list?

a)

variable.append()

b)

variable.delete()

c)

variable.remove()

d)

variable=(new variable)

7.

Which code would i use to add an item to a list?

a)

variable.add()

b)

variable.append()

c)

append.variable()

d)

add.variable()

8.

What is used to separate elements in a list?

a)

Bracket

b)

Comma

c)

Full Stop

d)

Space

9.

variable.sort() will do what?

a)

Sort the list into alphabetical order

b)

Sort the list into reverse order

c)

Create a new list

d)

Error

10.

What does the '#' allow you to do?

a)

Repeat code

b)

Comment on code

c)

Print code

d)

End code

11.

An empty list can be declared as

a)

fruitList = ""

b)

fruitList = ["empty"]

c)

fruitList == []

d)

fruitList = []

12.

Each item in a list has an "address" or index. The first index in a Python list is what?

a)

1

b)

0

c)

a

d)

A

13.

In programming, lists are often known as...

a)

Arrays

b)

Elements

c)

Indexes

d)

Data

14.

What is the output of the following?

x = ['ab', 'cd']

for i in x:

i.upper()

print(x)

a)

[‘ab’, ‘cd’].

b)

[‘AB’, ‘CD’].

c)

None of the above

d)

[None, None]

15.

Which of the following commands will create a list?

a) list1 = list()

b) list1 = [].

c) list1 = list([1, 2, 3])

d) all of the mentioned

a)

list1 = list()

b)

list1 = [].

c)

list1 = list([1, 2, 3])

d)

all of the mentioned

16.

Suppose list1 is [3, 5, 25, 1, 3], what is min(list1) ?

a)

3

b)

5

c)

1

d)

25

17.

Suppose list1 is [1, 5, 9], what is sum(list1) ?

a)

1

b)

9

c)

15

d)

none

18.

What will be the output?

names1 = ['Amir', 'Bala', 'Chales']

if 'amir' in names1:

print(1)

else: print(2)

a)

None

b)

1

c)

2

d)

error

19.

Which of the following is the correct way to add the number 4 to mylist?

a)

mylist + 4

b)

mylist.append(4)

c)

mylist.append([4])

d)

4.append(mylist)

20.

Which of the following commands will return the length of the list mylist?

a)

length(mylist)

b)

len(mylist)

c)

mylist.length()

d)

mylist.len()

21.

What is the output of the following list assignment

aList = [4, 8, 12, 16]

aList[1:4] = [20, 24, 28]

print(aList)

a)

[4, 20, 24, 28, 8, 12, 16]

b)

[4, 20, 24, 28]

22.

What is the output of the following list operation

aList = [10, 20, 30, 40, 50, 60, 70, 80]

print(aList[2:5])

print(aList[:4])

print(aList[3:])

a)

[20, 30, 40, 50]

[10, 20, 30, 40]

[30, 40, 50, 60, 70, 80]

b)

[30, 40, 50]

[10, 20, 30, 40]

[40, 50, 60, 70, 80]

c)

None of these

23.

Select all the correct options to join two lists in Python

listOne = ['a', 'b', 'c', 'd']

listTwo = ['e', 'f', 'g']

a)

newList = listOne + listTwo

b)

newList = extend(listOne, listTwo)

c)

newList = listOne.extend(listTwo)

d)

newList.extend(listOne, listTwo)

24.

What is the output of the following

aList = [5, 10, 15, 25]

print(aList[::-2])

a)

[15, 10, 5]

b)

[10, 5]

c)

[25, 10]

25.

What is the output of the following list function?

sampleList = [10, 20, 30, 40, 50]

sampleList.append(60)

print(sampleList)


sampleList.append(60)

print(sampleList)

a)

[10, 20, 30, 40, 50, 60]

[10, 20, 30, 40, 50, 60, 60]

b)

[10, 20, 30, 40, 50, 60]

[10, 20, 30, 40, 50, 60]

c)

both are wrong

26.

In Python, list is mutable

a)

False

b)

True

27.

Which of the following would give an error?

a)

list1=[]

b)

list1=[]*3

c)

list1=[2,8,7]

d)

None of the above

28.

What will be the output of below Python code?

list1=[8,0,9,5]


print(list1[::-1])

a)

[5,9,0,8]

b)

[8,0,9]

c)

[8,0,9,5]

d)

[0,9,5]

29.

Which of the following will give output as [23,2,9,75] ?

If list1=[6,23,3,2,0,9,8,75]

a)

print(list1[1:7:2])

b)

print(list1[0:7:2])

c)

print(list1[1:8:2])

d)

print(list1[0:8:2])

30.

Sort () function in list by default sort the values in __________

a)

Ascending order

b)

Decending order

31.

Suppose list1 is [1, 3, 2], What is list1 * 2 ?

a)

[2, 6, 4].

b)

[1, 3, 2, 1, 3]

c)

[1, 3, 2, 1, 3, 2]

d)

[1, 3, 2, 3, 2, 1]

32.

a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']

Which display correct output from below ?

a)

print(a[-5:-3])

['bar', 'baz']

b)

print(a[4::-1])

['quux', 'baz', 'foo']

c)

None

d)

print(a[2])

['bar']

33.

___________ method adds one list at the end of another list.

a)

extend( )

b)

append( )

c)

insert( )

34.

____________function can be used to insert an element/object at a specified index.

a)

extend( )

b)

insert ( )

c)

append( )

35.

___________method adds a single item to the end of the list.

a)

extend( )

b)

append( )

c)

insert( )

36.

how we store string value in variable?

a)

'string'

b)

"string"

c)

%string%

d)

#sring#

37.

index of a string begins with

a)

1

b)

0

c)

a

d)

-1

38.

The pseudocode below assigns two strings to two variables:

title = "computer science"

level = "gcse"

What would the Python code: len(level) return?

a)

2

b)

3

c)

4

d)

1

39.

The pseudocode below assigns two strings to two variables:

title = "computer science"

level = "gcse"

What is title[0]?

a)

'c'

b)

'o'

c)

'g'

d)

An error

40.

The pseudocode below assigns two strings to two variables:

title = "computer science"

level = "gcse"

What is title[4]?

a)

'p'

b)

'u'

c)

't'

d)

An error

41.

string = "abcdef"

What is index of "f" ?

(a)  

42.

string = "Computer Science"

What is string[0:8] ?

(a)  

43.

Sequences of characters within a string can be accessed by taking a string ______ using [ ]

a)

slice

b)

cut

c)

chop

d)

substring

44.

string1 = "Monty"

string2 = "Python"

string3 = string1 + string2

What is value of string3?

a)

Monty Python

b)

MontyPython

c)

Monty+Python

d)

string3

45.
What output will this code produce?
a)
4
b)
2
c)
3
d)
5
46.
What output will this code produce?
a)
1
b)
3
c)
2
d)
4
47.

What does the isdigit() function do in Python?

a)

Returns True if all the characters in the string are in capital letters

b)

Returns True if all the characters in the string are in small letters

c)

Returns True if the string contains only spaces

d)

Returns True if all the characters in the string are digits

48.

What is the output of the code fragment: print('hello'*3, str(123))?

a)

hellohellohello, 123

b)

hellohellohello, hellohellohello

c)

hellohellohello, 123123123

d)

hellohellohello, hello

49.

Which of these will print the length of a string

a)

print(len("a string"))

b)

print("a string".len())

c)

print(length("a string"))

d)

x = len.string()

50.

Which code would get the characters from position 2 to position 5 (not included).

a)

x = txt[2:5]

b)

x = txt[1:5]

c)

x = txt[1:4]

d)

x = txt[0[:4]

51.

Convert the value of txt to upper case.

a)

x = txt.upper()

b)

x = txt.lower()

c)

x = upper(txt)

d)

x = upper.txt()

52.

Replace the character H with a J

a)

x = txt.replace("H", "J")

b)

x = txt.replace("J", "H")

c)

x = replace ("H", "J")

d)

x = txt.replace(H,J)

53.

Which keyword is used for function?*

a)

define

b)

fun

c)

def

d)

function

54.

Which of the following items are present in the function header?

a)

function name

b)

parameter list

c)

return value

d)

Both A and B

55.

If the return statement is not used inside the function, the function will return:

a)

0

b)

None

c)

Null

d)

Arbitary value

56.

What is a recursive function?

a)

A function that calls other function.

b)

A function which calls itself.

c)

Both A and B

d)

None of the above

57.

Which of the following function headers is correct?*

a)

def fun(a = 2, b = 3, c)

b)

def fun(a = 2, b, c = 3)

c)

def fun(a, b = 2, c = 3)

d)

def fun(a, b, c = 3, d)

58.

In which part of memory does the system store the parameter and local variables of a function call?

a)

heap

b)

stack

c)

Uninitialized data segment

d)

None of the above

59.

How is a function declared in Python?*

a)

def function function_name():

b)

declare function function_name():

c)

def function_name():

d)

declare function_name():

60.

Which one of the following is the correct way of calling a function?

a)

function_name()

b)

call function_name()

c)

ret function_name()

d)

function function_name()

61.

Which of the following functions is a built-in function in python?

a)

array()

b)

sqrt()

c)

factorial()

d)

print()

62.

What will be the output of the following Python expression? round(4.576)

a)

4.5

b)

5

c)

4

d)

4.6

63.

What will be the output of the following Python expression? round(4.5676,2)

a)

4.5

b)

4.6

c)

4.57

d)

4.56

64.

A Python module is a file with the _____ file extension that contains valid Python code.

a)

.pym

b)

.pymodule

c)

.module

d)

.py

65.

What is returned by math.ceil(3.4)?

a)

3

b)

4

c)

4.0

d)

3.0

66.

Which statement is correct to import all modules from the package

a)

from package import all

b)

from package import *

c)

from package include all

d)

from package include *

67.

(a)   is a string literal denoted by triple quotes for providing the specifications of certain program elements.

68.

Which of the following is not a valid namespace?

a)

Global namespace

b)

Public namespace

c)

Built-in namespace

d)

Local namespace

69.

What is the order of namespaces in which Python looks for an identifier?

a)

Python first searches the global namespace, then the local namespace and finally the built-in namespace

b)

Python first searches the local namespace, then the global namespace and finally the built-in namespace

c)

Python first searches the built-in namespace, then the global namespace and finally the local namespace

d)

Python first searches the built-in namespace, then the local namespace and finally the global namespace

70.

The collection of modules and packages that together cater to a specific type of applications or requirements, is called______

a)

module

b)

library

c)

classes

d)

documentation

71.

The help statement display ___ from a module.

a)

constants

b)

functions

c)

classes

d)

docstrings

72.
a)

10#40#70#

b)

30#40#50#

c)

50#60#70#

d)

40#50#70#

73.
a)

a=10

b)

a=20

c)

syntax error

d)

value error

74.
4 lines
75.
4 lines
76.

What is the Python function that adds an item on to the end of a list?

a)

Extend

b)

Append

c)

Remove

d)

Count

77.

What is the Python Function for changing all lower case to upper case and all upper case to lower case letter?

a)

upper

b)

lower

c)

replace

d)

swapcase

78.

What python function returns the number of items / characters in a list and string?

a)

remove

b)

pop

c)

len

d)

index

79.

What is the python function that returns the number of times a specified item / character occurs in a list or string?

a)

count

b)

lower

c)

concatenation

d)

index

80.

Which python code will return the character associated with the value 104.

a)

chr(104)

b)

ord(104)

c)

str(104)

d)

int(104)

81.

What is the Python function that adds one list on to the end of another.

a)

Extend

b)

Append

c)

Remove

d)

Count

82.

Which python function enables a new list item to be placed at any point in an existing list.

a)

split

b)

reverse

c)

insert

d)

pop

83.

Which python function takes each word in a string and turns them into items of a list?

a)

split

b)

upper

c)

replace

d)

count

84.

Which python function adds a capital letter to the first letter of a string?

a)

lower

b)

concatenation

c)

capitalize

d)

split

85.

Which python code will add a new line to a python string?

a)

\n

b)

/t

c)

\t

d)

/n

86.

What would be the output of the following python code?

a)

True

b)

False

c)

Yes

d)

No

87.

What would be the output for the following python code?

a)

True

b)

No

c)

False

d)

Yes

88.

What would be the output for the following python code?

a)

worlD

b)

World

c)

world

d)

WORLD

89.

What would be the output for the following python code?

a)

hello WORLD

b)

HelloWORLD

c)

helloWorld

d)

helloWORLD

90.

What would be the output for the following python code?

a)

C

b)

r

c)

e

d)

ter

91.

What would be the output for the following python code?

a)

['banana','cherry','orange']

b)

['cherry','orange','kiwi']

c)

['cherry','orange','kiwi','melon']

d)

['banana','cherry','orange','kiwi']

92.

What would be the output for the following python code?

a)

['six', 'one', 'two', 'three', 'four', 'five']

b)

['one', 'two', 'three', 'four', 'five', '6']

c)

['one', 'two', 'three', 'four', 'five', 'six']

d)

['one', 'two', 'three', 'four', 'five']

93.

What would be the output for the following python code?

a)

[5, 4, 3, 2, 1]

b)

[12345]

c)

[1, 2, 3, 5]

d)

[1, 2, 3, 4, 5]

94.

What would be the output for the following python code?

a)

0

b)

1

c)

2

d)

3

95.

What would be the output for the following python code?

a)

one

b)

two

c)

three

d)

four

96.
What is a variable?
a)
A box(memory location) where you store values
b)
a type of graphics
c)
Data type
d)
a type of memory
97.
A syntax error means your code has a 'grammar' mistake or you used symbols/operations incorrectly
a)
True
b)
False
98.
Which statement correctly assigns the string "Tanner" to the variable name?
a)
name  = print( "Tanner")
b)
input("Tanner")
c)
name = "Tanner"
d)
name = input("Tanner")
99.

What is the output of the following lines of code?

var1 = 12

var1 = 23.4

var1 = "I love AI!"

print(var1)

a)

12

b)

23.4

c)

I love AI!

d)

Error

100.

What is the output from the following lines of code?

a = [2, 4, 8]

a[2] = 6

print(a)

a)

[2, 4, 8]

b)

[2, 6, 8]

c)

[2, 4, 6]

d)

[6, 4, 8]

101.

Which function allows us to add one element to a list at a desired location?

a)

append()

b)

extend()

c)

insert()

d)

pop()

102.

How many parameters are there in the sum() function call below?

sum(1, 3, 5)

a)

0

b)

1

c)

2

d)

3

103.

Which number is item, East, in the displayed list?

regions = ["North", "South", "East", "West"]

a)

1

b)

2

c)

3

d)

0

104.

Which type of variable uses decimal?

a)

String

b)

Floating

c)

Boolean

d)

Integer

105.

True or False? Variables cannot begin with a number.

a)

True

b)

False

106.

True or False? Variables are case-sensitive

a)

True

b)

False

107.

True or False? Variables must include at least one number

a)

True

b)

False

108.

What is the modulus (%) of 17/4

a)

4

b)

1

c)

7

d)

3

109.

What symbol concatenates variables in Python?

a)

&

b)

+

c)

; )

d)

*

110.

Which number will be shown when the sales variable is run in a print statement

a)

53235

b)

30000

c)

31000

d)

32000

111.

Which region and sales will come up based upon the print statement displayed on line 6?

a)

West Sales; 35000

b)

North Sales; 30000

c)

South Sales; 20000

d)

East Sales; 40000

112.

Which number will the price variable show when the displayed code is run?

a)

5

b)

4

c)

3

d)

3.95

113.
What symbol is used in python to assign values to a variable?
a)
equals =
b)
plus + 
c)
forward slash /
d)
asterisk *
114.

What syntax would you use to create a name variable?

a)

name=input()

b)

input=name

c)

name=input{}

d)

name=INPUT

115.

What does the following code do? myAge = int(myAge)

a)

Converts the variable myAge to a integer

b)

Syntax Error

c)

Converts the variable myAge from a integer to a string

d)

Missing input

116.

What will run when the following code is executed?


Count = 0


while count < 10:

print count


count += 1

a)

0,1,2,3,4,5,6,7,8,9,10

b)

0,1,2,3,4,5,6,7,8,9

c)

1,2,3,4,5,6,7,8,9,

d)

0,1,2,3,4,5,6,7,8,9,10,11

117.

What will run when the following code is executed?


def my_function(x):


for i in range(0, len(x)):


x[i] = x[i]

return x


print my_function(range(5))

a)

0,1,2,3,4,5

b)

1,2,3,4,5

c)

0,1,2,3,4

d)

None of these choices

118.

What will run when the following code is executed?


N = [3, 5, 7]


def double_list(x):


for i in range(0, len(x)):


x[i] = x[i] *2

a)

3,10,7

b)

6,10,14

c)

3,5,7

d)

[6,10,14]

119.

What will run when the following code is executed?


N = [3, 5, 7]


def print_list(x):


for i in range(0, len(x)):


print x[i]


print_list(n)

a)

3,5,7

b)

[3,5,7]

c)

{3,5,7}

d)

None of the above

120.

What will run when the following code is executed?


def list_function(x):


x[2] = x[2] +12


return x


n = [26, 29, 32]


print x[2]

a)

32

b)

29

c)

44

d)

None of these choices

121.

What will run when the following code is executed?


N = [10, 20, 35]


n.append(42)


print n

a)

10,20,35,42

b)

{10,20,35,42}

c)

*10,20,35,42*

d)

[10,20,35,42]

122.

What will run when the following code is executed?


N = [22, 13, 5]


N[0] = n[0] * 3


print N[0]

a)

22

b)

66

c)

15

d)

None of the above

123.

What will run when the following code is executed?

n = [1,2,3,5,7,8]

print n[2]

a)

2

b)

3

c)

5

d)

7

124.

What will run when the following code is executed?


n = [10, 15, 25, 35, 45]


n.append(14)


print n

a)

10,15,25,35,45,14

b)

[10,15,25,35,45,14]

c)

{10,15,25,35,45}

d)

None of these options

125.

What will run when the following code is executed?

n = [0, 11, 13, 15, 22, 35]

n.pop(0)

print n

a)

[0, 11, 13, 15, 22, 35]

b)

[0,13, 15, 22, 35]

c)

[0, 11, 13, 15, 22,]

d)

[11, 13, 15, 22, 35]

126.

What will run when the following code is executed?


def list_function(x):


x[1] = x[1] + 3


return x

n = [3, 5, 7]


print list_function(n)

a)

3,5,7

b)

[3,8,7]

c)

3,8,7

d)

{3,8,7}

127.

What will run when the following code is executed?


n = [3, 5, 7]


def list_extender(lst):


lst.append(9)


return lst

print list_extender(n)

a)

[9,3,5,7]

b)

9,3,5,7

c)

3,5,7,9

d)

[3,5,7,9]

128.

What is a condition?

a)

A loop never exits

b)

An expression that decides whether the loop is going to continue being executed or not

c)

A statement that creates a function to determine the variance of a set of items

d)

None of the above

129.

What does the term “break” mean?

a)

A function that has not defined properly

b)

A loop that never exits

c)

A one-line statement that means exit the current loop

d)

None of the above

130.

What does the “+=” represent?

a)

The break statement

b)

The number of items in a list

c)

The increment of how much that specific count will run

d)

None of the above

131.

An “if” statement is an alternate way to stop a counting loop from running.

a)

True

b)

False

132.

What does the term “.randint”?

a)

Random Integer

b)

A method of spell-check

c)

An alternate way of "breaking" a counting loop

d)

None of the above

133.

In Python, the else block will continue to execute anytime the loop condition is evaluated to "False".

a)

True

b)

False

134.

"String manipulation" is useful for loops if you want to modify some content in a string.

a)

True

b)

False

135.

What does the term "enumerate" mean?

a)

Works by supplying a corresponding index to each element in the list

b)

Automatically sets the counting loops “start” number

c)

Automatically sets the counting loops “end” number

d)

None of the above