wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

TUPLES IN PYTHON VAISHALI LAMBA

Total questions: 43

Worksheet time: 46mins

Name
Class
Date
1.

Tuples in Python are mutable

a)

False

b)

True

2.

How to create a single element tuple

a)

(1,)

b)

(1)

c)

{1}

d)

[1]

e)

tuple([1])

3.

Result of the program above is

a)

('1', '2', '3', '4', '5')

b)

(1, 2, 3, 4, 5)

c)

12345

d)

"12345"

4.

What is the output of the program above

a)

<class 'tuple'>

b)

<class 'None'>

c)

<class 'list'>

d)

<class 'int'>

5.

What is the output of the code above

a)

(1, '2', 3, 4, 5)

b)

syntax error

c)

(1, 2, 3, 4, 5)

d)

('1', '2', '3', '4', '5')

6.

Pick the odd one in connection with collection data type

a)

List

b)

Tuple

c)

Dictionary

d)

Loop

7.

Which of the following function is used to count the number of elements in a list ?

a)

count( )

b)

find( )

c)

len( )

d)

index( )

8.

Let list1=[2,4,6,8,10] the print(list1[-2]) will result in

a)

10

b)

8

c)

4

d)

6

9.

What is the use of type( ) function in python ?

a)

To create a tuple

b)

To know the type of an element in tuple

c)

To know the data type of the Python object

d)

To create a List

10.

Which of the following statement is not correct

a)

A list is mutable

b)

A tuple is immutable

c)

The append function is used to add an element

d)

The extend function is used in tuples to add elements in a list

11.

If list=[10,20,30,40,50] then list[2]=35 will result

a)

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

b)

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

c)

[10,20,35,40,50]

d)

[10,35,30,40,50]

12.

If list=[17,23,41,10] then list.append(32) will result

a)

[32,17,23,41,10]

b)

[17,23,41,10,32]

c)

[10,17,23,32,41]

d)

[41,32,23,17,10]

13.

Which of the following python function can be used to add more than one element within an existing list ?

a)

append( )

b)

append_more( )

c)

extend( )

d)

more( )

14.

The keys in Python dictionary is specified by

a)

=

b)

;

c)

+

d)

:

15.

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]

16.

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"

17.

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"]

18.

What would this code show on the screen?

a)

7

b)

6

c)

5

d)

4

e)

An error

19.

What sort of bracket is for lists?

a)

(

b)

<

c)

}

d)

[

20.

For tuples and list which is correct?

a)

List and tuples both are mutable.

b)

List is mutable whereas tuples are immutable.

c)

List and tuples both are immutable.

d)

List is immutable whereas tuples are mutable

21.
people = ["John", "Rob", "Bob"]
print (people[-1])
What would this result be?
a)
John
b)
Rob
c)
Bob
d)
Error
22.

Which is a correctly declared tuple?

a)

a = ("orange", "yellow", "red")

b)

a = "orange", "yellow", "red"

c)

a = ["orange", "yellow", "red"]

d)

a = "orangeyellowred"

23.

Which line of code will give you an error? b = (4, 5, 6, 7, 8)

a)

b[2]

b)

b[0] = 1

c)

b[:3]

d)

b[-2]

24.

How would you refer to 3 in the following tuple? c = ((7,5),(5,8),(0,-1),(4,3))

a)

c[0][0]

b)

c(3)

c)

c[8]

d)

c[3][1]

25.

Which of the following will produce an error? e = (1,2,3)

a)

e = (4, 5, 6)

b)

e = e + 4

c)

e = e + (4, 5, 6)

d)

e = e + (4,)

26.

What is the index of the following tuple: y = ()

a)

0

b)

1

c)

There is none. It is an empty tuple

27.

Can you have an empty tuple z = () or an empty string w = []?

a)

yes

b)

no

28.

Given a = "Hello world" what does list(a) do?

a)

creates a list separated by word (white spaces)

b)

creates a list separated by character

c)

nothing

d)

creates a blank list named a

29.

Given a = "Hello world" what does a.split() do?

a)

creates a list separated by character

b)

nothing

c)

creates a list separated by word (white spaces)

d)

splits the string into two strings

30.

Can you have lists inside of tuples and vice versa?

a)

Yes

b)

No

31.

Suppose you have the following tuple definition:


t = ('foo', 'bar', 'baz')


Which of the following statements replaces the second element ('bar') with the string 'qux':

a)

t[1] = 'qux'

b)

t(1) = 'qux'

c)

t[1:1] = 'qux'

d)

It’s a trick question—tuples can’t be modified.

32.

Which of the following functions is used to count the number of elements in a list ?

a)

count( )

b)

find( )

c)

len( )

d)

index( )

33.

Let list1=[2,4,6,8,10].

What will print(list1[-2]) result in?

a)

-2

b)

8

c)

4

d)

6

34.

Which of the following statements is NOT correct?

a)

A list is mutable

b)

A tuple is immutable

c)

The append function is used to add an element

d)

The extend function adds an element to a tuple

35.

What type of data is: a=[(1,1),(2,4),(3,9)]?

a)

Array of tuples

b)

List of tuples

c)

Tuples of lists

d)

Invalid type

36.

If L1=[2, 4, 8, 16] and L2=[32, 64]

what will result from L1+L2?

a)

[2,4,8,16,32,64]

b)

[2,4,8,16][32,64]

c)

[2,4,8,16],[32,64]

d)

2468163264

37.

If L1=[2,4,8,16] what is the result of print(16 in L1)

a)

True

b)

False

38.

Let x= "Python", the output of

print(x[0])

print(x[-1])

print(x[-2])

a)

P

N

O

b)

p

O

n

c)

t

h

y

d)

P

n

o

39.

1. Examples for list data type

a)

[1,2,3,4,5]

b)

['S','H','R','U','T','H','I']

c)

{1:"a",2:"b",3:"c",7:"d"}

d)

('A','B','C','D')

40.

Write the Python code that prints out Captain Kirk’s email address from the employee dictionary below:


employee = { "id": 1701, "name": "James T Kirk",

"email": "jtkirk@starfleet.com","position": "captain"}




(a)  

41.

Which of the following is not a valid way to define this dictionary in Python:

a)

d = dict([

('foo', 100),

('bar', 200),

('baz', 300)

])

b)

d = {}

d['foo'] = 100

d['bar'] = 200

d['baz'] = 300

c)

d = dict(foo=100, bar=200, baz=300)

d)

d = {'foo': 100, 'bar': 200, 'baz': 300}

e)

d = { ('foo', 100), ('bar', 200), ('baz', 300) }

42.

What would be the output of the following code snippet?


a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}


for key in a_dict:

print(key)

a)

'blue'

'apple'

'dog'

b)

color

fruit

pet

c)

blue

apple

dog

d)

'color'

'fruit'

'pet'

43.

What would be the output of the following code snippet?


a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}

d_items = a_dict.items()

print(d_items)

a)

('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')

b)

dict_items([('color'), ('fruit'), ('pet')])

c)

[('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')]

d)

dict_items([('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')])