Font size
WorksheetsPYTHON PROGRAMMING FINALS
Total questions: 69
Worksheet time: 40mins
What does the list.pop() method do?
Removes the first item from the list
Removes the last item from the list
Adds a new item to the list
Sorts the list in ascending order
Lists in Python are mutable.
True
False
You can nest lists inside other lists in Python.
True
False
How can you concatenate two lists a and b?
a + b
a - b
a * b
a / b
Lists can store different data types, such as integers, strings, and other lists.
True
False
Which method is used to add an item to the end of a list?
insert()
append()
add()
extend()
You can use the len() function to get the number of items in a list.
True
False
The list.index() method returns the index of the first occurrence of a specified value.
True
False
How do you remove the first item from a list?
list.remove(0)
list.remove(1)
del list[0]
del list[1]
What will be the output of list(range(5))?
[1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5, 6]
The method used to sort a list in-place in ascending order is (a) .
The syntax to access the third element of a list my_list is (a) .
To check if an item is in a list, you can use the (a) keyword.
The method to add multiple elements from another list at the end of a list is (a) .
The (a) method returns the number of times a specified value appears in the list.
Given a list a = [1, 2, 3, 4, 5], write a code to remove the number 3 from the list.
(a)
What is the output of the following code?
(a)
Which of the following methods returns the index of the first occurrence of a specified value in a list?
A. index()
B. find()
C. locate()
D. search()
The sort() method returns a sorted version of the list without modifying the original list.
False
True
Lists in Python can contain elements of different data types.
True
False
How do you concatenate two lists in Python?
A. list1 * list2
B. list1 + list2
C. list1 / list2
D. list1 - list2
The method to count the number of occurrences of a specific element in a list is called (a) .
To iterate through each element of a list, you can use a (a) loop.
What is the output of the following code?
(a)
What is the primary function used to open a file in Python?
A. file()
B. open()
C. read()
D. write()
Which mode is used to open a file for reading in Python?
A. r
B. w
C. a
D. b
What does the write() method do?
A. Reads the entire file
B. Writes data to the file
C. Appends data to the file
D. Closes the file
How do you close a file in Python?
A. file.end()
B. file.close()
C. file.quit()
D. file.stop()
Which of the following methods reads the entire content of a file?
What does the with statement do when working with files?
A. Closes the file automatically after the block
B. Opens multiple files simultaneously
C. Reads a file line by line
D. Writes to multiple files simultaneously
What mode is used to open a file for appending data?
A. r
B. w
C. a
D. b
What does the file.seek(0) method do?
A. Moves the cursor to the end of the file
B. Moves the cursor to the beginning of the file
C. Reads the first line of the file
D. Closes the file
Which of the following is a binary file mode?
A. rb
B. r
C. w
D. a
How do you open a file for both reading and writing in binary mode?
A. rb+
B. wb+
C. ab+
D. r+
What will file.readlines() return?
A. A string of all the contents
B. A list where each element is a line in the file
C. A tuple of file lines
D. An integer representing the number of lines
What is the default mode if none is specified when opening a file?
A. r
B. w
C. a
D. b
Which method can be used to write multiple lines to a file?
A. file.write_multiple()
B. file.writelines()
C. file.writemany()
D. file.write()
How can you check if a file is closed in Python?
A. file.closed()
B. file.isclosed()
C. file.closed
D. file.isclosed
Which of the following modes will overwrite the existing file content?
A. r
B. w
C. a
D. x
Which method is used to flush the internal buffer?
A. file.flush()
B. file.buffer()
C. file.empty()
D. file.clear()
Which method reads all lines and returns them as a list of strings?
How do you open a file for reading and writing, but not create if it does not exist?
A. r+
B. w+
C. a+
D. x+
What is the purpose of the file.truncate(size) method?
A. Removes the first size bytes from the file
B. Clears the file content
C. Changes the file size to size bytes
D. Deletes the file
What is the output of the following code?
A. H
B. e
C. l
D. o
How do you get the length of a string s in Python?
A. s.size()
B. s.len()
C. length(s)
D. len(s)
What is the output of the following code?
A. P
B. n
C. o
D. h
Which method can be used to convert all characters in a string to uppercase?
A. upper()
B. uppercase()
C. toUpperCase()
D. capitalize()
What is the output of the following code?
A. hello world
B. Hello World
C. HELLO WORLD
D. Hello world
How do you concatenate two strings str1 and str2 in Python?
A. str1.add(str2)
B. str1 + str2
C. concat(str1, str2)
D. str1.append(str2)
What is the output of the following code?
A. 1
B. 2
C. 3
D. -1
Which method would you use to replace all instances of "a" with "b" in a string s?
A. s.replace("a", "b")
B. s.substitute("a", "b")
C. s.swap("a", "b")
D. s.change("a", "b")
What is the output of the following code?
A. ['Hello', 'World']
B. ['Hello', ' World!']
C. ['Hello World']
D. ['Hello', 'World!']
How do you check if a string s starts with the substring "Py"?
A. s.begins("Py")
B. s.startswith("Py")
C. s.start("Py")
D. s.beginswith("Py")
What will the following code output?
A. Python
B. Programming
C. Programmin
D. Python Pro
How do you remove any whitespace from the beginning and the end of a string s?
A. s.strip()
B. s.ltrim()
C. s.rstrip()
D. s.trim()
What does the following code return?
A. True
B. False
C. Error
D. None
Which method can be used to check if a string contains only digits?
A. isdigit()
B. isnumeric()
C. isnumber()
D. isdecimal()
What is the output of the following code?
A. 00123
B. 12300
C. 123
D. 12345
How do you convert a string s to a list of characters?
A. list(s)
B. split(s)
C. toList(s)
D. s.tolist()
What is the result of the following code?
A. Hello
B. HELLO
C. hello
D. Hello
How do you check if all characters in a string s are uppercase?
A. s.isupper()
B. s.allUpper()
C. s.isUpper()
D. s.alluppercase()
What will the following code output?
A. Python is fun
B. Python is fun
C. Pythonisfun
D. Python-is-fun
How do you replace the first occurrence of a substring "old" with "new" in a string s?
A. s.replace("old", "new", 1)
B. s.sub("old", "new")
C. s.change("old", "new", 1)
D. s.swap("old", "new")
What will the following code output?
A. Hello World
B. Hello\nWorld
C. Hello
World
D. Hello World
How do you check if a string s contains only whitespace?
A. s.isspace()
B. s.iswhitespace()
C. s.iswhite()
D. s.isblank()
What is the output of the following code?
A. Hello
B. olleH
C. H
D. o
Which method would you use to convert a string s to a title case (first letter of each word capitalized)?
A. s.capitalize()
B. s.titlecase()
C. s.title()
D. s.uppercase()
What does the following code do?
A. ---foo---
B. --foo---
C. -foo----
D. ---foo--
