Multiple Choice Questions - Practice Files
Multiple Choice Questions - Practice Files
List
1. Which of the following is not a valid data type in Python?
a) int
b) float
c) long
d) char
4. What will be the value of `result` after executing the following Python code?
x=5
y=2
result = x ** y
a) 10
b) 25
c) 7
d) 32
11. Which module is used for file input and output operations in Python?
a) sys
b) os
c) fileio
d) io
12. What will be the value of `x` after executing the following Python code?
x=5
x += 3
a) 5
b) 8
c) 3
d) 15
13. Which of the following is not a valid way to open a file in Python?
a) open("file.txt")
b) open("file.txt", "r")
c) open("file.txt", "w+")
d) open("file.txt", mode="r")
17. Which of the following is not a valid way to create a tuple in Python?
a) (1, 2, 3)
b) tuple(1, 2, 3)
c) tuple([1, 2, 3])
d) 1, 2, 3
19. Which method is used to split a string into a list of substrings in Python?
a) split()
b) separate()
c) divide()
d) cut()
Tuples
1. Which of the following statements about tuples in Python is true?
a) Tuples are mutable
b) Tuples are ordered collections
c) Tuples can contain duplicate elements
d) Tuples are defined using curly braces {}
4. Which method is used to find the number of occurrences of a specified value in a tuple?
a) count()
b) find()
c) index()
d) length()
5. What is the output of the following Python code snippet?
tuple1 = (1, 2, 3, 4, 5)
print(tuple1[2])
a) 2
b) 3
c) 4
d) Error: Tuple indices must be integers
6. Which of the following methods can be used to delete an entire tuple in Python?
a) clear()
b) remove()
c) del()
d) pop()
10. Which of the following is a correct way to check if an element exists in a tuple in Python?
a) if element in tuple:
b) if tuple.contains(element):
c) if tuple.includes(element):
d) if tuple.exist(element):
11. Which of the following methods is used to reverse the order of elements in a tuple?
a) reverse()
b) reversed()
c) invert()
d) flip()
13. Which of the following is a correct way to create a single-element tuple in Python?
a) tuple = (1)
b) tuple = (1,)
c) tuple = [1]
d) tuple = {1}
17. Which of the following methods is used to return the index of the first occurrence of a
specified value in a tuple?
a) find()
b) search()
c) index()
d) position()
19. Which of the following methods is used to sort the elements of a tuple?
a) sort()
b) sorted()
c) sort_elements()
d) order()
7. Which method is used to get the value associated with a specified key in a dictionary?
a) get()
b) value()
c) fetch()
d) access()
11. Which of the following methods is used to remove all key-value pairs from a dictionary in
Python?
a) clear()
b) delete_all()
c) remove_all()
d) erase()
15. Which method is used to return a dictionary with specified keys and values in Python?
a) create()
b) make()
c) build()
d) dict()
17. Which method is used to return a list of tuples containing key-value pairs in a dictionary?
a) items()
b) tuples()
c) pairs()
d) key_values()
Strings
1. Which of the following is the correct way to define a string variable in Python?
a) str = "Hello"
b) string = 'Hello'
c) string = "Hello"
d) str = 'Hello'
11. Which method is used to find the index of the first occurrence of a specified substring in a
string?
a) index()
b) find()
c) search()
d) locate()
15. Which method is used to convert the characters of a string to lowercase in Python?
a) lower()
b) to_lower()
c) lowercase()
d) toLowerCase()
17. Which method is used to find the number of occurrences of a specified character in a string?
a) count()
b) occurrences()
c) find()
d) search()
19. Which method is used to check if a string ends with a specified suffix in Python?
a) endswith()
b) ends()
c) suffix()
d) end()
Functions
1. What is a function in Python?
a) A function is a sequence of statements that performs a specific task
b) A function is a data type in Python
c) A function is a keyword in Python
d) A function is a variable in Python
11. Which keyword is used to pass a variable number of arguments to a function in Python?
a) args
b) varargs
c) *args
d) variable_args
14. Which of the following is the correct syntax to define a function with default arguments in
Python?
a) def function_name(arguments=default):
b) def function_name(default=arguments):
c) def function_name(arguments=default_value):
d) def function_name(default_value=arguments):
16. Which of the following is the correct way to define a function that accepts a variable number
of keyword arguments in Python?
a) def function_name(**kwargs):
b) def function_name(*args):
c) def function_name(**args):
d) def function_name(*kwargs):
18. Which of the following is the correct syntax to call a function with keyword arguments in
Python?
a) function_name(key=value)
b) function_name(value=key)
c) call function_name(key=value)
d) def function_name(arguments=default_value):
23. Which of the following is the correct way to define a recursive function in Python?
a) By calling itself within the function body
b) By using the "recurse" keyword
c) By defining a function inside another function
d) By using the "recursive" decorator
25. Which of the following is the correct syntax to define a function with a variable number of
positional arguments in Python?
a) def function_name(*args):
b) def function_name(**kwargs):
c) def function_name(*args, **kwargs):
d) def function_name(*args, **args):
28. Which of the following is the correct syntax to define a function with annotations in Python?
a) def function_name(arguments: type) -> return_type:
b) def function_name(arguments) -> return_type:
c) def function_name(arguments: type) return_type:
d) def function_name(arguments, return_type):
6. What happens if you open a file in write mode ('w') that does not exist?
a) It creates a new file
b) It raises an error
c) It opens an existing file for writing
d) It prompts the user to create the file
9. Which method is used to read the entire contents of a file as a string in Python?
a) read()
b) read_line()
c) read_all()
d) get_contents()
11. Which method is used to read a specific number of characters from a file in Python?
a) read()
b) read_line()
c) read_all()
d) read_chars()
20. Which of the following is not a valid mode for opening a file in Python?
a) x
b) b
c) e
d) t
25. Which of the following methods is used to read lines from a file into a list in Python?
a) read()
b) read_line()
c) readlines()
d) get_lines()
28. Which method is used to change the current position of the file pointer in Python?
a) seek()
b) move()
c) position()
d) change_position()
3. What is the purpose of binary mode ('b') when opening a file in Python?
a) It indicates that the file is in binary format
b) It indicates that the file is in text format
c) It indicates that the file is empty
d) It indicates that the file is readable
4. Which of the following is used to read binary data from a file in Python?
a) read()
b) read_binary()
c) read_bytes()
d) read_binary_data()
7. Which method is used to check if the end of a binary file has been reached in Python?
a) eof()
b) is_eof()
c) end_of_file()
d) end_of_file_reached()
11. Which of the following methods is used to read a specific number of bytes from a binary file
in Python?
a) read()
b) read_binary()
c) read_bytes()
d) read_binary_data()
12. What is the purpose of the "truncate" method in binary file handling?
a) To resize the file to a specified size
b) To delete the file contents
c) To move the file pointer to the end of the file
d) To read the entire contents of the file
13. Which method is used to change the current position of the file pointer in Python?
a) seek()
b) move()
c) position()
d) change_position()
14. What is the purpose of the "readline" method in binary file handling?
a) To read a specific line from a file
b) To read the entire contents of a file
c) To read the next line from a file
d) To read a random line from a file
15. Which method is used to read lines from a binary file into a list in Python?
a) readlines()
b) read_binary_lines()
c) read_lines()
d) get_lines()
17. Which of the following methods is used to write a specific number of bytes to a binary file in
Python?
a) write()
b) write_binary()
c) write_bytes()
d) write_binary_data()
18. What is the purpose of the "closed" attribute in binary file handling?
a) To check if the file is closed
b) To check if the file is open
c) To close the file
d) To open the file
21. What is the purpose of the "readable" attribute in binary file handling?
a) To check if the file is readable
b) To check if the file is open
c) To read the entire contents of the file
d) To close the file
24. What is the purpose of the "writable" attribute in binary file handling?
a) To check if the file is writable
b) To check if the file is open
c) To write data to a file
d) To close the file
27. Which method is used to flush the buffer of a binary file in Python?
a) flush()
b) clear()
c) empty()
d) clean()
28. What is the purpose of the "isatty" method in binary file handling?
a) To check if the file is a tty device
b) To check if the file is a text file
c) To check if the file is empty
d) To check if the file is open in text mode
30. What is the purpose of the "truncate" method in binary file handling?
a) To resize the file to a specified size
b) To delete the file contents
c) To move the file pointer to the end of the file
d) To read the entire contents of the file