wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Programming Intermediate-1

Total questions: 15

Worksheet time: 12mins

Name
Class
Date
1.

Consider the file "d://test.txt" with the contents as given below:

Hi there,

I am loving Python.

Python is Simple

Choose the correct output from the above code snippet from the given options:

a)

Read String is : Hi there,

b)

Read String is : Python is simple.

c)

Syntax error

d)

Read String is : Hi there,

I am loving Python.

Python is simple.

2.

Consider the file "d://test.txt" with the contents as given below:

Hi there,

I am loving Python.

Python is Simple

Choose the correct output from the above code snippet from the given options:

a)

Read String is : Hi there

b)

Read String is : Hi there,

c)

Syntax error

d)

Hi there,

I am loving Python.

Python is simple.

3.

Consider a file test.txt in D Drive with the following contents:

I Love Python

Choose the output for the following Python code from the given options(Select all applicable):

a)

Stmt2 prints the contents of the file test.txt

b)

Stmt5 will print number of tokens(words) in the file separated by a space ' '

c)

Stmt6 should be uncommented to close the open file.

d)

Stmt3 results in an error

4.

Consider a file read.txt in D Drive with the following contents:

Banana

Apple

Grapes

Choose the output for the following Python code from the given options:

a)

{}

b)

{0: 'Banana', 1: 'Apple', 2: 'Grapes'}

c)

[0: 'Banana', 1: 'Apple', 2: 'Grapes']

d)

{'Banana':0, 'Apple':1, 'Grapes':2}

5.

Consider a file read.txt in D Drive with the following contents:

Banana

Apple

Grapes

Choose the output for the following Python code from the given options:

a)

[]

b)

['Banana', 'Apple', 'Grapes']

c)

{'Banana', 'Apple', 'Grapes'}

d)

Error

6.

Consider a list, fruits=["Mango", "Banana", "Guava", "Strawberry"]. Match the following:

a. fruits[-4:]

b. fruits[-2:]

c. fruits[-4:-1]

d. fruits[1:4]

Select the correct answer for a, b, c & d

a)

['Banana', 'Guava', 'Strawberry']

b)

['Mango', 'Banana', 'Guava', 'Strawberry']

c)

['Mango', 'Banana']

d)

['Mango', 'Banana', 'Guava']

e)

['Guava', 'Banana', 'Mango']

7.

Assume the following list definition:

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

a)

print(a[4::-2])

b)

a[:] is a

c)

max(a[2:4] + ['grault'])

d)

print(a[-5:-3])

8.

List a is defined as follows:

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

Which of the following statements adds 'd' and 'e' to the end of a, so that it then equals ['a', 'b', 'c', 'd', 'e']:

a)

a += 'de'

b)

a += ['d', 'e']

c)

a.append(['d', 'e'])

d)

a[len(a):] = ['d', 'e']

e)

a.extend(['d', 'e'])

9.

What gets printed?

names = ['Amir', 'Barry', 'Chales', 'Dao']

print(names[-1][-1])

a)

A

b)

r

c)

Amir

d)

Dao

e)

o

10.

What gets printed from the above piece of code?

a)

11

b)

12

c)

21

d)

22

e)

33

11.

names1 = ['Amir', 'Barry', 'Chales', 'Dao']

loc = names1.index("Edward")

print(loc)

a)

-1

b)

0

c)

4

d)

Edward

e)

An exception is thrown

12.

Which of the following are true of Python lists?

a)

These represent the same list:['a', 'b', 'c'] ['c', 'a', 'b']

b)

A given object may appear in a list more than once

c)

All elements in a list must be of the same type

d)

A list may contain any type of object except another list

e)

There is no conceptual limit to the size of a list

13.

You have a list a defined as follows:

a = [1, 2, 7, 8]

Write a Python statement using slice assignment that will fill in the missing values so that a equals [1, 2, 3, 4, 5, 6, 7, 8].

(a)  

14.

Same nested list as the previous question:

x = [10, [3.141, 20, [30, 'baz', 2.718]], 'foo']

What expression returns the list ['baz', 2.718]?

(a)  

15.

List a is defined as follows:

a = [1, 2, 3, 4, 5]

Select all of the following statements that remove the middle element 3 from a so that it equals [1, 2, 4, 5]:

a)

a[2:2] = []

b)

del a[2]

c)

a[2] = []

d)

a[2:3] = []

e)

a.remove(3)