wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

PYTHON PROGRAMMING FINALS

Total questions: 69

Worksheet time: 40mins

Name
Class
Date
1.

What does the list.pop() method do?

a)

Removes the first item from the list

b)

Removes the last item from the list

c)

Adds a new item to the list

d)

Sorts the list in ascending order

2.

Lists in Python are mutable.

a)

True

b)

False

3.

You can nest lists inside other lists in Python.

a)

True

b)

False

4.

How can you concatenate two lists a and b?

a)

a + b

b)

a - b

c)

a * b

d)

a / b

5.

Lists can store different data types, such as integers, strings, and other lists.

a)

True

b)

False

6.

Which method is used to add an item to the end of a list?

a)

insert()

b)

append()

c)

add()

d)

extend()

7.

You can use the len() function to get the number of items in a list.

a)

True

b)

False

8.

The list.index() method returns the index of the first occurrence of a specified value.

a)

True

b)

False

9.

How do you remove the first item from a list?

a)

list.remove(0)

b)

list.remove(1)

c)

del list[0]

d)

del list[1]

10.

What will be the output of list(range(5))?

a)

[1, 2, 3, 4, 5]

b)

[0, 1, 2, 3, 4, 5]

c)

[0, 1, 2, 3, 4]

d)

[0, 1, 2, 3, 4, 5, 6]

11.

The method used to sort a list in-place in ascending order is (a)   .

12.

The syntax to access the third element of a list my_list is (a)   .

13.

To check if an item is in a list, you can use the (a)   keyword.

14.

The method to add multiple elements from another list at the end of a list is (a)   .

15.

The (a)   method returns the number of times a specified value appears in the list.

16.

Given a list a = [1, 2, 3, 4, 5], write a code to remove the number 3 from the list.

(a)  

17.

What is the output of the following code?

(a)  

18.

Which of the following methods returns the index of the first occurrence of a specified value in a list?

a)

A. index()

b)

B. find()

c)

C. locate()

d)

D. search()

19.

The sort() method returns a sorted version of the list without modifying the original list.

a)

False

b)

True

20.

Lists in Python can contain elements of different data types.

a)

True

b)

False

21.

How do you concatenate two lists in Python?

a)

A. list1 * list2

b)

B. list1 + list2

c)

C. list1 / list2

d)

D. list1 - list2

22.

The method to count the number of occurrences of a specific element in a list is called (a)   .

23.

To iterate through each element of a list, you can use a (a)   loop.

24.

What is the output of the following code?

(a)  

25.

What is the primary function used to open a file in Python?

a)

A. file()

b)

B. open()

c)

C. read()

d)

D. write()

26.

Which mode is used to open a file for reading in Python?

a)

A. r

b)

B. w

c)

C. a

d)

D. b

27.

What does the write() method do?

a)

A. Reads the entire file

b)

B. Writes data to the file

c)

C. Appends data to the file

d)

D. Closes the file

28.

How do you close a file in Python?

a)

A. file.end()

b)

B. file.close()

c)

C. file.quit()

d)

D. file.stop()

29.
  • Which of the following methods reads the entire content of a file?

a)
b)
  • B. file.readline()

c)
  • C. file.readlines()

d)
  • D. file.readfile()

30.

What does the with statement do when working with files?

a)

A. Closes the file automatically after the block

b)

B. Opens multiple files simultaneously

c)

C. Reads a file line by line

d)

D. Writes to multiple files simultaneously

31.

What mode is used to open a file for appending data?

a)

A. r

b)

B. w

c)

C. a

d)

D. b

32.

Which method is used to read a single line from a file?

a)

A. file.read()

b)

B. file.readline()

c)

C. file.readlines()

d)

D. file.read_single()

33.

What does the file.seek(0) method do?

a)

A. Moves the cursor to the end of the file

b)

B. Moves the cursor to the beginning of the file

c)

C. Reads the first line of the file

d)

D. Closes the file

34.
  • Which of the following is a binary file mode?

a)
  • A. rb

b)
  • B. r

c)
  • C. w

d)
  • D. a

35.

How do you open a file for both reading and writing in binary mode?

a)

A. rb+

b)

B. wb+

c)

C. ab+

d)

D. r+

36.

What will file.readlines() return?

a)

A. A string of all the contents

b)

B. A list where each element is a line in the file

c)

C. A tuple of file lines

d)

D. An integer representing the number of lines

37.
  • What is the default mode if none is specified when opening a file?

a)
  • A. r

b)
  • B. w

c)
  • C. a

d)
  • D. b

38.

Which method can be used to write multiple lines to a file?

a)

A. file.write_multiple()

b)

B. file.writelines()

c)

C. file.writemany()

d)

D. file.write()

39.

How can you check if a file is closed in Python?

a)

A. file.closed()

b)

B. file.isclosed()

c)

C. file.closed

d)

D. file.isclosed

40.

Which of the following modes will overwrite the existing file content?

a)

A. r

b)

B. w

c)

C. a

d)

D. x

41.

Which method is used to flush the internal buffer?

a)

A. file.flush()

b)

B. file.buffer()

c)

C. file.empty()

d)

D. file.clear()

42.

Which method reads all lines and returns them as a list of strings?

a)

A. file.read()

b)

B. file.readline()

c)

C. file.reads()

d)

D. file.readlines()

43.
  • How do you open a file for reading and writing, but not create if it does not exist?

a)
  • A. r+

b)
  • B. w+

c)
  • C. a+

d)
  • D. x+

44.

What is the purpose of the file.truncate(size) method?

a)

A. Removes the first size bytes from the file

b)

B. Clears the file content

c)

C. Changes the file size to size bytes

d)

D. Deletes the file

45.

What is the output of the following code?

a)

A. H

b)

B. e

c)

C. l

d)

D. o

46.

How do you get the length of a string s in Python?

a)

A. s.size()

b)

B. s.len()

c)

C. length(s)

d)

D. len(s)

47.

What is the output of the following code?

a)

A. P

b)

B. n

c)

C. o

d)

D. h

48.

Which method can be used to convert all characters in a string to uppercase?

a)

A. upper()

b)

B. uppercase()

c)

C. toUpperCase()

d)

D. capitalize()

49.

What is the output of the following code?

a)

A. hello world

b)

B. Hello World

c)

C. HELLO WORLD

d)

D. Hello world

50.

How do you concatenate two strings str1 and str2 in Python?

a)

A. str1.add(str2)

b)

B. str1 + str2

c)

C. concat(str1, str2)

d)

D. str1.append(str2)

51.

What is the output of the following code?

a)

A. 1

b)

B. 2

c)

C. 3

d)

D. -1

52.

Which method would you use to replace all instances of "a" with "b" in a string s?

a)

A. s.replace("a", "b")

b)

B. s.substitute("a", "b")

c)

C. s.swap("a", "b")

d)

D. s.change("a", "b")

53.

What is the output of the following code?

a)

A. ['Hello', 'World']

b)

B. ['Hello', ' World!']

c)

C. ['Hello World']

d)

D. ['Hello', 'World!']

54.

How do you check if a string s starts with the substring "Py"?

a)

A. s.begins("Py")

b)

B. s.startswith("Py")

c)

C. s.start("Py")

d)

D. s.beginswith("Py")

55.

What will the following code output?

a)

A. Python

b)

B. Programming

c)

C. Programmin

d)

D. Python Pro

56.

How do you remove any whitespace from the beginning and the end of a string s?

a)

A. s.strip()

b)

B. s.ltrim()

c)

C. s.rstrip()

d)

D. s.trim()

57.

What does the following code return?

a)

A. True

b)

B. False

c)

C. Error

d)

D. None

58.

Which method can be used to check if a string contains only digits?

a)

A. isdigit()

b)

B. isnumeric()

c)

C. isnumber()

d)

D. isdecimal()

59.

What is the output of the following code?

a)

A. 00123

b)

B. 12300

c)

C. 123

d)

D. 12345

60.

How do you convert a string s to a list of characters?

a)

A. list(s)

b)

B. split(s)

c)

C. toList(s)

d)

D. s.tolist()

61.

What is the result of the following code?

a)

A. Hello

b)

B. HELLO

c)

C. hello

d)

D. Hello

62.

How do you check if all characters in a string s are uppercase?

a)

A. s.isupper()

b)

B. s.allUpper()

c)

C. s.isUpper()

d)

D. s.alluppercase()

63.

What will the following code output?

a)

A. Python is fun

b)

B. Python is fun

c)

C. Pythonisfun

d)

D. Python-is-fun

64.

How do you replace the first occurrence of a substring "old" with "new" in a string s?

a)

A. s.replace("old", "new", 1)

b)

B. s.sub("old", "new")

c)

C. s.change("old", "new", 1)

d)

D. s.swap("old", "new")

65.

What will the following code output?

a)

A. Hello World

b)

B. Hello\nWorld

c)

C. Hello
World

d)

D. Hello World

66.

How do you check if a string s contains only whitespace?

a)

A. s.isspace()

b)

B. s.iswhitespace()

c)

C. s.iswhite()

d)

D. s.isblank()

67.

What is the output of the following code?

a)

A. Hello

b)

B. olleH

c)

C. H

d)

D. o

68.

Which method would you use to convert a string s to a title case (first letter of each word capitalized)?

a)

A. s.capitalize()

b)

B. s.titlecase()

c)

C. s.title()

d)

D. s.uppercase()

69.

What does the following code do?

a)

A. ---foo---

b)

B. --foo---

c)

C. -foo----

d)

D. ---foo--