0% found this document useful (0 votes)
31 views28 pages

Multiple Choice Questions - Practice Files

Uploaded by

sujanfilms
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views28 pages

Multiple Choice Questions - Practice Files

Uploaded by

sujanfilms
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

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

2. What is the output of the following Python code snippet?


print(10 > 9)
a) True
b) False
c) 1
d) 0

3. Which keyword is used to define a function in Python?


a) def
b) function
c) define
d) func

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

5. In Python, which of the following is a mutable data type?


a) tuple
b) list
c) string
d) dictionary

6. What is the output of the following Python code snippet?


a = [1, 2, 3]
b=a
b[0] = 4
print(a)
a) [1, 2, 3]
b) [4, 2, 3]
c) [1, 2, 3, 4]
d) [4, 2, 3, 4]

7. Which method is used to add an item to the end of a list in Python?


a) add()
b) insert()
c) append()
d) extend()

8. What is the output of the following Python code snippet?


a = [1, 2, 3]
b=a
a = a + [4]
print(b)
a) [1, 2, 3]
b) [1, 2, 3, 4]
c) [4, 2, 3]
d) [1, 2, 3, 4]

9. Which of the following is not a valid way to create a dictionary in Python?


a) {'key1': 'value1', 'key2': 'value2'}
b) dict(key1='value1', key2='value2')
c) dict([('key1', 'value1'), ('key2', 'value2')])
d) {['key1', 'key2']: ['value1', 'value2']}

10. What is the output of the following Python code snippet?


x = 10
y=5
print(x if x < y else y)
a) 10
b) 5
c) Syntax Error
d) None of the above

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

14. What is the output of the following Python code snippet?


x = ['apple', 'banana', 'cherry']
x.remove('banana')
print(x)
a) ['apple', 'cherry']
b) ['apple', 'banana']
c) ['banana', 'cherry']
d) ['apple', 'banana', 'cherry']

15. Which method is used to join two or more strings in Python?


a) join()
b) concatenate()
c) merge()
d) add()

16. What is the output of the following Python code snippet?


a = (1, 2, 3)
b = a + (4,)
print(b)
a) (1, 2, 3, 4)
b) (1, 2, 3)
c) [1, 2, 3, 4]
d) (1, 2, 3, [4])

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

18. What is the output of the following Python code snippet?


x = "Hello, World!"
print(x[7:])
a) Hello
b) World
c) llo, World!
d) o, World!

19. Which method is used to split a string into a list of substrings in Python?
a) split()
b) separate()
c) divide()
d) cut()

20. What is the output of the following Python code snippet?


x = "Hello, World!"
print(x.replace("Hello", "Hi"))
a) Hi, World!
b) Hello, Hi!
c) Hi, Hi!
d) Hi, World!

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 {}

2. Which of the following is the correct syntax to create a tuple in Python?


a) tuple = (1, 2, 3)
b) tuple = [1, 2, 3]
c) tuple = {1, 2, 3}
d) tuple = "1, 2, 3"

3. What is the result of the following operation in Python?


tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
result = tuple1 + tuple
a) (1, 2, 3, 4, 5, 6)
b) (5, 7, 9)
c) Error: Tuples cannot be concatenated
d) (1, 2, 3, 4, 5, 6, 7, 9)

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()

7. What is the output of the following Python code snippet?


tuple1 = (1, 2, 3)
tuple2 = tuple1
tuple1 += (4,)
print(tuple2)
a) (1, 2, 3)
b) (1, 2, 3, 4)
c) (1, 2, 3, 4, 5)
d) Error: Tuples are immutable

8. Which of the following statements is true about tuple unpacking in Python?


a) Tuple unpacking can only be done with tuples of equal length
b) Tuple unpacking allows assigning multiple variables at once
c) Tuple unpacking cannot be performed inside a loop
d) Tuple unpacking is a mutable operation

9. What is the output of the following Python code snippet?


tuple1 = (1, 2, 3)
x, y, z = tuple1
print(y)
a) 1
b) 2
c) 3
d) Error: Too many values to unpack

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()

12. What is the result of the following operation in Python?


tuple1 = (1, 2, 3, 4, 5)
result = tuple1[1:4]
a) (2, 3, 4)
b) (1, 2, 3)
c) (2, 3)
d) (3, 4, 5)

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}

14. What is the output of the following Python code snippet?


tuple1 = (1, 2, 3)
tuple2 = tuple1.copy()
tuple1 += (4,)
print(tuple2)
a) (1, 2, 3)
b) (1, 2, 3, 4)
c) (1, 2, 3, 4, 5)
d) Error: Tuples do not have a copy() method

15. Which method is used to convert a list into a tuple in Python?


a) list()
b) tuple()
c) convert()
d) to_tuple()

16. What is the output of the following Python code snippet?


tuple1 = (1, 2, 3)
print(len(tuple1))
a) 1
b) 2
c) 3
d) 4

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()

18. What is the output of the following Python code snippet?


tuple1 = (1, 2, 3)
tuple2 = (4, 5)
result = tuple1 * 2 + tuple2
print(result)
a) (1, 2, 3, 1, 2, 3, 4, 5)
b) (1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
c) (1, 2, 3, 4, 5, 4, 5)
d) Error: Tuples do not support multiplication

19. Which of the following methods is used to sort the elements of a tuple?
a) sort()
b) sorted()
c) sort_elements()
d) order()

20. What is the output of the following Python code snippet?


tuple1 = (3, 1, 4, 1, 5, 9, 2)
print(tuple1.count(1))
a) 1
b) 2
c) 3
d) 4
Dictionary
1. Which of the following statements about dictionaries in Python is true?
a) Dictionaries are ordered collections
b) Dictionaries can contain duplicate keys
c) Dictionaries are mutable
d) Dictionaries are defined using square brackets []

2. What is the correct syntax to create an empty dictionary in Python?


a) dict = {}
b) dict = []
c) dict = ()
d) dict = {;}
3. Which of the following methods is used to add a new key-value pair to a dictionary in Python?
a) add()
b) insert()
c) append()
d) update()

4. What is the output of the following Python code snippet?


dict1 = {'a': 1, 'b': 2, 'c': 3}
print(dict1['b'])
a) a
b) 1
c) b
d) 2

5. Which method is used to remove a key-value pair from a dictionary in Python?


a) remove()
b) delete()
c) pop()
d) discard()

6. What is the output of the following Python code snippet?


dict1 = {'a': 1, 'b': 2, 'c': 3}
dict1['d'] = 4
print(dict1)
a) {'a': 1, 'b': 2, 'c': 3}
b) {'a': 1, 'b': 2, 'c': 3, 'd': 4}
c) {'d': 4}
d) Error: 'dict' object has no attribute 'append'

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()

8. What is the output of the following Python code snippet?


dict1 = {'a': 1, 'b': 2, 'c': 3}
print(len(dict1))
a) 1
b) 2
c) 3
d) 4
9. Which of the following methods is used to get a list of all keys in a dictionary?
a) keys()
b) get_keys()
c) list()
d) key_list()

10. What is the output of the following Python code snippet?


dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = dict1.copy()
dict1['d'] = 4
print(dict2)
a) {'a': 1, 'b': 2, 'c': 3}
b) {'a': 1, 'b': 2, 'c': 3, 'd': 4}
c) {'d': 4}
d) Error: 'dict' object has no attribute 'copy'

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()

12. What is the output of the following Python code snippet?


dict1 = {'a': 1, 'b': 2, 'c': 3}
del dict1['b']
print(dict1)
a) {'a': 1, 'b': 2}
b) {'a': 1, 'c': 3}
c) {'b': 2, 'c': 3}
d) Error: 'dict' object has no attribute 'remove'

13. Which method is used to get a list of all values in a dictionary?


a) values()
b) get_values()
c) list()
d) value_list()

14. What is the output of the following Python code snippet?


dict1 = {'a': 1, 'b': 2, 'c': 3}
print('a' in dict1)
a) True
b) False
c) 1
d) 'a'

15. Which method is used to return a dictionary with specified keys and values in Python?
a) create()
b) make()
c) build()
d) dict()

16. What is the output of the following Python code snippet?


dict1 = {'a': 1, 'b': 2, 'c': 3}
dict1.pop('b')
print(dict1)
a) {'a': 1, 'b': 2}
b) {'a': 1, 'c': 3}
c) {'b': 2, 'c': 3}
d) Error: 'dict' object has no attribute 'pop'

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()

18. What is the output of the following Python code snippet?


dict1 = {'a': 1, 'b': 2, 'c': 3}
dict1.update({'d': 4})
print(dict1)
a) {'a': 1, 'b': 2, 'c': 3}
b) {'a': 1, 'b': 2, 'c': 3, 'd': 4}
c) {'d': 4}
d) Error: 'dict' object has no attribute 'update'

19. Which method is used to return a shallow copy of a dictionary in Python?


a) copy()
b) clone()
c) replicate()
d) duplicate()

20. What is the output of the following Python code snippet?


dict1 = {'a': 1, 'b': 2, 'c': 3}
print(dict1.get('d', 'Key not found'))
a) 'Key not found'
b) Error: 'dict' object has no attribute 'get'
c) None
d) 'd'

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'

2. What is the result of the following Python code snippet?


string = "Hello, World!"
print(len(string))
a) 5
b) 12
c) 11
d) 13

3. Which method is used to convert a string to uppercase in Python?


a) upper()
b) to_upper()
c) uppercase()
d) toUpperCase()

4. What is the output of the following Python code snippet?


string = "Hello, World!"
print(string[7:])
a) World!
b) o, World!
c) , World!
d) Error: Strings are immutable

5. Which method is used to split a string into a list of substrings in Python?


a) split()
b) split_string()
c) divide()
d) separate()

6. What is the output of the following Python code snippet?


string = "Hello, World!"
print(string.count('l'))
a) 2
b) 3
c) 4
d) 5
7. Which method is used to replace occurrences of a specified substring with another substring
in a string?
a) replace()
b) subst()
c) swap()
d) change()

8. What is the output of the following Python code snippet?


string = " Hello, World! "
print(string.strip())
a) Hello, World!
b) Hello, World!
c) Hello, World!
d) Error: Strings do not have a strip() method

9. Which method is used to concatenate two or more strings in Python?


a) concat()
b) merge()
c) join()
d) combine()

10. What is the output of the following Python code snippet?


string = "Hello, World!"
print(string.startswith('Hello'))
a) True
b) False
c) 5
d) Error: Strings do not have a startswith() method

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()

12. What is the output of the following Python code snippet?


string = "Hello, World!"
print(string.capitalize())
a) Hello, world!
b) hello, world!
c) HELLO, WORLD!
d) Error: Strings do not have a capitalize() method
13. Which of the following is the correct way to check if a string contains a substring in Python?
a) if substring in string:
b) if string.contains(substring):
c) if string.include(substring):
d) if string.exist(substring):

14. What is the output of the following Python code snippet?


string = "Hello, World!"
print(string[::-1])
a) !dlroW ,olleH
b) dlroW ,olleH!
c) dloW olleH
d) Error: Strings do not support slicing

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()

16. What is the output of the following Python code snippet?


string = "Hello, World!"
print(string.split(','))
a) ['Hello', 'World!']
b) ['Hello,', 'World!']
c) ('Hello', 'World!')
d) Error: Strings do not have a split() method

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()

18. What is the output of the following Python code snippet?


string = "Hello, World!"
print(string.upper())
a) HELLO, WORLD!
b) hello, world!
c) Hello, World!
d) Error: Strings do not have an upper() method

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()

20. What is the output of the following Python code snippet?


string = "Hello, World!"
print(string.find('o'))
a) 4
b) 5
c) 6
d) Error: Strings do not have a find() method

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

2. What is the keyword used to define a function in Python?


a) def
b) function
c) define
d) func

3. Which of the following is the correct syntax to define a function in Python?


a) function name(arguments):
b) def function name(arguments):
c) define function_name(arguments):
d) def function_name(arguments):

4. What is the purpose of the "return" statement in a Python function?


a) To terminate the function execution
b) To define the function
c) To return a value from the function
d) To print the output of the function

5. Which of the following is not a valid parameter passing technique in Python?


a) Call by value
b) Call by reference
c) Call by object
d) Call by result
6. What is the output of the following Python code snippet?
def add(a, b):
return a + b
result = add(3, 5)
print(result)
a) 8
b) 35
c) 15
d) Error: Undefined function 'add'

7. Which of the following is the correct way to call a function in Python?


a) function_name(arguments)
b) function(arguments)
c) call function_name(arguments)
d) def function_name(arguments):

8. What is the purpose of docstrings in Python functions?


a) To specify the return type of the function
b) To provide a description of the function
c) To define the function
d) To comment out the function

9. Which of the following is not a valid Python function call?


a) function()
b) function(1, 2, 3)
c) function("hello", 123)
d) function("hello", b=123)

10. What is the output of the following Python code snippet?


def greet(name):
print("Hello, " + name)
greet("Alice")
a) Hello, Alice
b) Alice
c) Hello, World!
d) Error: Undefined function 'greet'

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

12. What is the purpose of the "global" keyword in Python?


a) To declare a variable as global inside a function
b) To access global variables inside a function
c) To define a global function
d) To import global modules

13. What is the output of the following Python code snippet?


def square(x):
return x ** 2
result = square(2) + square(3)
print(result)
a) 10
b) 13
c) 25
d) 14

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):

15. What is the output of the following Python code snippet?


def add(x, y=5):
return x + y
result = add(3)
print(result)
a) 8
b) 3
c) 5
d) Error: Missing argument for 'y'

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):

17. What is the output of the following Python code snippet?


def greet(*names):
for name in names:
print("Hello, " + name)
greet("Alice", "Bob", "Charlie")
a) Hello, Alice
Hello, Bob
Hello, Charlie
b) Hello, Alice Bob Charlie
c) Alice
Bob
Charlie
d) Error: Undefined function 'greet'

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):

19. What is the purpose of the "lambda" keyword in Python?


a) To define anonymous functions
b) To define global functions
c) To declare a variable as lambda
d) To import lambda functions

20. What is the output of the following Python code snippet?


x = 10
def increment():
global x
x += 1
increment()
print(x)
a) 9
b) 10
c) 11
d) Error: x is not defined

21. Which keyword is used to define a generator function in Python?


a) gen
b) generate
c) generator
d) yield

22. What is the output of the following Python code snippet?


def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
print(fibonacci(5))
a) 5
b) 8
c) 3
d) 55

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

24. What is the output of the following Python code snippet?


def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
print(factorial(4))
a) 4
b) 12
c) 24
d) 120

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):

26. What is the purpose of the "nonlocal" keyword in Python?


a) To declare a variable as nonlocal inside a function
b) To access nonlocal variables inside a function
c) To define a nonlocal function
d) To import nonlocal modules

27. What is the output of the following Python code snippet?


def outer():
x = 10
def inner():
nonlocal x
x += 5
print(x)
inner()
print(x)
outer()
a) 15, 15
b) 10, 15
c) 15, 10
d) 10, 10

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):

29. What is the purpose of annotations in Python functions?


a) To specify the return type of the function
b) To provide a description of the function
c) To improve code readability
d) To comment out the function

30. What is the output of the following Python code snippet?


def multiply(x: int, y: int) -> int:
return x * y
print(multiply(3, '4'))
a) 12
b) 7
c) Error: Can't multiply sequence by non-int of type 'str'
d) Error: Invalid type for argument 'y'

File handling - Text file


1. Which module is used in Python for file handling?
a) os
b) sys
c) file
d) io

2. What is the purpose of file handling in Python?


a) To perform arithmetic operations on files
b) To interact with files on the filesystem
c) To create GUI interfaces for file management
d) To display file contents on the console
3. Which mode is used to open a file for reading in Python?
a) r
b) read
c) open
d) read_file

4. What is the default mode for opening a file in Python?


a) r
b) w
c) a
d) t

5. Which mode is used to open a file for writing in Python?


a) w
b) write
c) open
d) write_file

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

7. Which method is used to close a file in Python?


a) close()
b) shutdown()
c) end()
d) stop()

8. What is the purpose of the "with" statement in file handling?


a) To define a block of code
b) To handle exceptions
c) To open and close files automatically
d) To create a loop

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()

10. What is the purpose of the "readline" method in 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

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()

12. What is the output of the following Python code snippet?


with open("data.txt", "w") as file:
file.write("Hello, World!")
with open("data.txt", "r") as file:
print(file.read())
a) Hello, World!
b) data.txt
c) Error: File not found
d) Error: File is not readable

13. Which method is used to write data to a file in Python?


a) write()
b) append()
c) add()
d) update()

14. What is the purpose of the "seek" method in file handling?


a) To move the file pointer to a specified position
b) To read the entire contents of a file
c) To write data to a file
d) To close the file

15. Which method is used to check if a file exists in Python?


a) exists()
b) is_file()
c) file_exists()
d) check_file()

16. What is the purpose of the "tell" method in file handling?


a) To read a specific line from a file
b) To get the current position of the file pointer
c) To write data to a file
d) To close the file
17. Which method is used to delete a file in Python?
a) remove()
b) delete()
c) erase()
d) delete_file()

18. What is the purpose of the "flush" method in file handling?


a) To clear the file contents
b) To move the file pointer to the beginning of the file
c) To write data to a file
d) To force the data to be written to the file

19. What is the output of the following Python code snippet?


with open("data.txt", "w") as file:
file.write("Hello, World!")
with open("data.txt", "a") as file:
file.write("Welcome to Python!")
with open("data.txt", "r") as file:
print(file.read())
a) Hello, World!Welcome to Python!
b) Hello, World!
c) Welcome to Python!
d) Error: File not found

20. Which of the following is not a valid mode for opening a file in Python?
a) x
b) b
c) e
d) t

21. What is the purpose of the "truncate" method in 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

22. What is the output of the following Python code snippet?


with open("data.txt", "r") as file:
print(file.readline())
a) Hello, World!
b) data.txt
c) Error: File not found
d) Error: File is not readable
23. Which method is used to rename a file in Python?
a) rename()
b) move()
c) change_name()
d) update_name()

24. What is the purpose of the "closed" attribute in 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

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()

26. What is the purpose of the "isatty" method in 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

27. What is the output of the following Python code snippet?


with open("data.txt", "w") as file:
file.write("Hello, World!")
print(os.path.getsize("data.txt"))
a) 13
b) Hello, World!
c) data.txt
d) Error: File not found

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()

29. What is the purpose of the "readable" attribute in 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
30. What is the output of the following Python code snippet?
with open("data.txt", "w") as file:
file.write("Hello, World!")
with open("data.txt", "r") as file:
for line in file:
print(line)
a) Hello, World!
b) data.txt
c) Error: File not found
d) Error: File is not readable

File handling - Binary File


1. Which of the following is true about binary files in Python?
a) Binary files contain only text data
b) Binary files store data in a human-readable format
c) Binary files store data in a non-human-readable format
d) Binary files cannot be created using Python

2. Which module is used to work with binary files in Python?


a) os
b) sys
c) file
d) pickle

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()

5. Which method is used to write binary data to a file in Python?


a) write()
b) write_binary()
c) write_bytes()
d) write_binary_data()

6. What is the purpose of the "seek" method in binary file handling?


a) To move the file pointer to a specified position
b) To read the entire contents of a file
c) To write data to a file
d) To close the file

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()

8. What is the purpose of the "tell" method in binary file handling?


a) To get the current position of the file pointer
b) To read a specific line from a file
c) To write data to a file
d) To close the file

9. Which method is used to flush the buffer of a binary file in Python?


a) flush()
b) clear()
c) empty()
d) clean()

10. What is the output of the following Python code snippet?


with open("data.bin", "wb") as file:
file.write(b'Hello, World!')
with open("data.bin", "rb") as file:
print(file.read())
a) b'Hello, World!'
b) Hello, World!
c) data.bin
d) Error: File not found

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()

16. What is the output of the following Python code snippet?


with open("data.bin", "wb") as file:
file.write(b'Hello, World!')
with open("data.bin", "rb") as file:
print(file.readline())
a) b'Hello, World!'
b) Hello, World!
c) data.bin
d) Error: File not found

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

19. Which method is used to check if a file exists in Python?


a) exists()
b) is_file()
c) file_exists()
d) check_file()

20. What is the output of the following Python code snippet?


with open("data.bin", "wb") as file:
file.write(b'Hello, World!')
print(os.path.getsize("data.bin"))
a) 13
b) Hello, World!
c) data.bin
d) Error: File not found

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

22. Which method is used to rename a binary file in Python?


a) rename()
b) move()
c) change_name()
d) update_name()

23. What is the output of the following Python code snippet?


with open("data.bin", "wb") as file:
file.write(b'Hello, World!')
with open("data.bin", "rb") as file:
for line in file:
print(line)
a) b'Hello, World!'
b) Hello, World!
c) data.bin
d) Error: File not found

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

25. Which method is used to delete a binary file in Python?


a) remove()
b) delete()
c) erase()
d) delete_file()

26. What is the output of the following Python code snippet?


with open("data.bin", "wb") as file:
file.write(b'Hello, World!')
with open("data.bin", "rb") as file:
print(file.read(5))
a) b'Hello'
b) Hello
c) data.bin
d) Error: File not found

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

29. What is the output of the following Python code snippet?


with open("data.bin", "wb") as file:
file.write(b'Hello, World!')
with open("data.bin", "rb") as file:
print(file.tell())
a) 0
b) 1
c) 13
d) Error: File not found

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

You might also like