Questions ▼
Advanced Python MCQ for Experienced Developers. We covered all the Advanced Python MCQ for Experienced Developers in this post for free so that you can practice well for the exam.
Install our MCQTUBE Android app from the Google Play Store and prepare for any competitive government exams for free.
We created all the competitive exam MCQs into several small posts on our website for your convenience.
You will get their respective links in the related posts section provided below.
Related Posts:
- MCQ on Python String Methods and Functions
- MCQ on Python Control Flow Statements For Class 11 Students
- Python MCQ on Basic Data Types Operators and Expressions with Answers
Advanced Python MCQ for Experienced Developers
Quick Quiz
How can you retrieve key-value pairs from one dictionary that are not found in another dictionary?
a. set(d1.items()) – set(d2.items())
b. set(d2.items()) – set(d1.items())
c. Both (a) and (b)
d. None of the above
Option c – Both (a) and (b)
How would you merge another dictionary into an existing one, but only for keys that are not already present?
a. d1.update(d2, ignore_existing=True)
b. d1.update(d2, overwrite=False)
c. d1.setdefault(d2)
d. None of the above
Option a – d1.update(d2, ignore_existing=True)
Which option correctly demonstrates creating a shallow copy of a dictionary in Python?
a. d1 = d2
b. d1 = d2.copy()
c. d1 = dict(d2)
d. All of the above
Option d – All of the above
What is the appropriate method to create a deep copy of a dictionary?
a. d1 = d2.copy()
b. d1 = copy.deepcopy(d2)
c. d1 = dict(d2)
d. None of the above
Option b – d1 = copy.deepcopy(d2)
How can a Python dictionary be converted into a JSON-formatted string?
a. import json
b. json.dumps(d)
c. Both (a) and (b)
d. None of the above
Option c – Both (a) and (b)
Which of the following lets you convert a JSON string into a Python dictionary?
a. import json
b. json.loads(json_string)
c. Both (a) and (b)
d. None of the above
Option c – Both (a) and (b)
What is the correct syntax for using a dictionary as a default parameter in a function?
a. def func(arg=d):
b. def func(arg: d):
c. def func(arg=dict(d)):
d. All of the above
Option d – All of the above
Which of these correctly uses a dictionary to count occurrences?
a. d = {}
b. for key in keys:
c. d[key] += 1
d. All of the above
Option d – All of the above
Which statement about Python dictionaries is incorrect?
a. Values can be accessed using keys
b. Keys can be accessed using values
c. Dictionaries are mutable
d. None of the above
Option b – Keys can be accessed using values
Which of the following cannot be used as a dictionary key in Python?
a. None
b. True
c. False
d. All of the above are valid keys
Option a – None
How do you loop through a dictionary in reverse order?
a. for key in reversed(d.keys())
b. for key in d.keys()[:-1]
c. Both (a) and (b)
d. None of the above
Option c – Both (a) and (b)
What is the correct way to count the total number of entries in a dictionary?
a. len(d)
b. d.size()
c. d.count()
d. None of the above
Option a – len(d)
What is the most efficient method for generating a dictionary from a list of key-value tuples?
a. Iterating through the list with a for loop
b. Utilizing a dictionary comprehension
c. Calling the dict() function
d. Applying the fromkeys() function
Option c – Calling the dict() function
How can a Python dictionary be most effectively sorted based on its keys?
a. By calling the sort() method
b. By using the sorted() function
c. By retrieving the keys using the keys() method
d. None of these options
Option d – None of these options
What is the appropriate method for reversing the order of items in a Python dictionary?
a. Using the reverse() method
b. Applying the reversed() function
c. Calling the items() method
d. None of the above options
Option d – None of these options
Does a file need to be manually closed when opened using the with
statement in Python?
a. Yes, you must close it manually.
b. No, it closes on its own once the block finishes.
c. It varies depending on the Python version.
d. Only if an error occurs.
Option b – No, it closes on its own once the block finishes.
Which statement correctly describes the use of text mode (t
) and binary mode (b
) when working with files in Python?
a. Use t
for text files and b
for binary files.
b. Use t
for binary files and b
for text files.
c. Both modes can be used interchangeably.
d. There is no real difference between t
and b
.
Option a – Use t for text files and b for binary files.
Which function returns the size of a file in bytes in Python?
a. file_size()
b. get_size()
c. os.size()
d. os.path.getsize()
Option d – os.path.getsize()
What is the function of the truncate()
method in Python?
a. It closes the file.
b. It reduces or extends the file to a specified size.
c. It removes the file from the system.
d. It reads the file content completely.
Option b – It reduces or extends the file to a specified size.
How can you create a directory along with any missing parent directories in Python?
a. Call mkdir()
with the parents=True
argument.
b. Use create_directory()
with recursive=True
.
c. Use makedirs()
with exist_ok=True
.
d. Call os.mkdir()
with make_parents=True
.
Option c – Use makedirs() with exist_ok=True.
Which method helps determine if a path refers to a directory or a regular file in Python?
a. is_dir()
b. is_file()
c. is_directory()
d. is_regular_file()
Option b – is_file()
What does the os.path.abspath()
method return in Python?
a. The full path of the current directory.
b. The full path of a given file or folder.
c. A check whether the path is relative or absolute.
d. A conversion from relative to absolute path.
Option b – The full path of a given file or folder.
What method can be used to delete a directory and everything inside it in Python?
a. rmdir()
b. remove_directory()
c. os.remove()
d. shutil.rmtree()
Option d – shutil.rmtree()
What output does the os.path.basename()
function provide in Python?
a. The path excluding the filename.
b. Just the filename without its directory.
c. The file’s extension.
d. The top-level directory.
Option b – Just the filename without its directory.
Which function moves the file pointer to a specified position in Python?
a. position()
b. set_position()
c. move_to()
d. seek()
Option d – seek()
What does os.path.exists()
check in Python?
a. Verifies if a file or path exists.
b. Creates a new file.
c. Opens a file for reading.
d. Deletes the specified file.
Option a – Verifies if a file or path exists.
What does os.path.isdir()
check in Python?
a. Whether a file can be read.
b. Whether a file can be executed.
c. Whether the specified path points to a directory.
d. Whether a file is empty.
Option c – Whether the specified path points to a directory.
How can you determine if a file is empty in Python?
a. Call the file.is_empty() method.
b. Use os.is_empty() to check.
c. Apply os.path.getsize() and compare the result to zero.
d. Use the file.size() method.
Option c – Apply os.path.getsize() and compare the result to zero.
What is returned by os.path.isabs() when the specified path is absolute?
a. True
b. False
c. None
d. An error message
Option a – True
What is a way to verify if a directory contains no files in Python?
a. Call is_empty_directory().
b. Use os.is_empty_dir().
c. Use os.listdir() and evaluate if the list length is zero.
d. Check with directory.is_empty() method.
Option c – Use os.listdir() and evaluate if the list length is zero.
Which function gives the current working directory in Python?
a. current_directory()
b. os.get_cwd()
c. os.getcwd()
d. get_current_directory()
Option c – os.getcwd()
How can a hard link to an existing file be created in Python?
a. Use the hardlink() function from the os module.
b. Call the create_hard_link() method.
c. Use os.link() to generate it.
d. Use file.create_hardlink().
Option c – Use os.link() to generate it.
What does os.path.normpath() do in Python?
a. Converts a relative path into an absolute path.
b. Provides the absolute location of a given file.
c. Cleans up a path by removing extra slashes and “.” references.
d. Identifies if a path points to a directory.
Option c – Cleans up a path by removing extra slashes and “.” references.
How do you check if a file or folder is hidden using Python?
a. Call file.is_hidden().
b. Use os.path.is_hidden().
c. Use os.access() with a flag to detect hidden files.
d. Use file.isHidden().
Option b – Use os.path.is_hidden().
What operation does os.path.split() perform in Python?
a. Extracts the file extension from a path.
b. Breaks a path into its folder and file parts.
c. Returns a file’s size in bytes.
d. Checks whether a given path is a directory.
Option b – Breaks a path into its folder and file parts.
Which file mode opens a file for writing, creates it if it doesn’t exist, and overwrites any existing content?
a. “r”
b. “w”
c. “a”
d. “x”
Option b – “w”
What does Python’s write() function return after writing data to a file?
a. Number of bytes written
b. The data written to the file
c. The size of the file
d. Nothing
Option d – Nothing
When a file is opened in write mode, what is the purpose of the write() method?
a. Appends data to the end of the file
b. Retrieves content from the file
c. Replaces any existing content with new data
d. Verifies the file’s existence
Option c – Replaces any existing content with new data
In Python, what does the writelines() function do?
a. Reads multiple lines from a file into a list
b. Writes a list of lines into a file
c. Reads the complete file as one string
d. Converts a file’s content into a list of lines
Option b – Writes a list of lines into a file
What is the correct way to make a duplicate of a file in Python without replacing the original?
a. Use the backupfile() function
b. Use the copyfile() method from the shutil module
c. Use the file.copy() method
d. Use the file.backup() method
Option b – Use the copyfile() method from the shutil module
Which function allows you to modify file access permissions in Python?
a. change_permissions()
b. set_permissions()
c. os.chmod()
d. file.set_permissions()
Option c – os.chmod()
How can a tab character be written to a file using Python?
a. By calling the tab() function
b. By using the \t escape sequence
c. By using the \t escape sequence
d. By inserting the t character directly
Option b – By using the \t escape sequence
In Python, what does the truncate() method do when working with a file in write mode?
a. Deletes the specified file
b. Shrinks or expands the file to a specific byte size
c. Adds data to the end of the file
d. Loads the full file content
Option b – Shrinks or expands the file to a specific byte size
Which file mode should be used with the open() function to read a binary file in Python?
a. Use mode “rb”
b. Use mode “r”
c. Use mode “wb”
d. Use mode “b”
Option a – Use mode “rb”
Which method in Python returns the next line from an open file?
a. readline()
b. read()
c. readlines()
d. nextline()
Option a – readline()
What happens when you use the “x” mode with the open() function in Python?
a. The file opens in binary format
b. A new file is created if it doesn’t already exist; otherwise, an error occurs
c. Adds content at the end of the file
d. Opens the file for writing
Option b – A new file is created if it doesn’t already exist; otherwise, an error occurs
What method should be used to load the complete contents of a file into a single string?
a. read()
b. readlines()
c. readline() in a loop
d. readall()
Option a – read()
When provided with a list of strings, what is the function of the writelines() method?
a. Combines the list into a single string and writes it
b. Saves each string in the list as an individual file
c. Writes the entire list as one continuous line
d. Writes each string from the list to the file line by line
Option d – Writes each string from the list to the file line by line
How is a newline character added to a file using Python?
a. Using the newline() method
b. Typing the n character
c. Using the \n escape code
d. Using the nline method
Option c – Using the \n escape code
What does the os.path.normcase() function accomplish in Python?
a. Retrieves the file size in bytes
b. Converts a path to its absolute version
c. Changes the path to a standard case format (typically lowercase)
d. Verifies whether a given path is a directory
Option c – Changes the path to a standard case format (typically lowercase)
What is the outcome when a file is opened in append mode (“a”) and it doesn’t exist?
a. An exception is triggered
b. A new file is automatically created
c. The file opens in read-only mode
d. The program halts execution
Option b – A new file is automatically created
Which method is used in Python to shift the file pointer to a particular byte location?
a. move()
b. position()
c. seek()
d. shift()
Option c – seek()
When a file is opened in write mode in Python, where is the file pointer initially placed?
a. At the start of the file (byte 0)
b. At the end of the file
c. In the middle of the file
d. It depends on the contents of the file
Option b – At the end of the file
After changing the file pointer using the seek() function, what value does it return in Python?
a. The updated position of the pointer
b. The total size of the file
c. The content at the new location
d. Nothing
Option a – The updated position of the pointer
What is the correct way to reset the file pointer to the beginning of a file in Python?
a. file.seek(0)
b. file.seek(1)
c. file.seek(0, 2)
d. file.begin()
Option a – file.seek(0)
What information is provided by the tell() function when called on a file in Python?
a. Total size of the file
b. Line number currently being read
c. The current byte offset in the file
d. Last modification time of the file
Option c – The current byte offset in the file
How can you reposition the file pointer to a certain line number in a text file using Python?
a. file.line(number)
b. file.seek(0)
c. file.seek() with an appropriate offset
d. file.position()
Option c – file.seek() with an appropriate offset
If the file pointer is at the start of a file, what does the tell() method return in Python?
a. 0
b. 1
c. -1
d. None
Option a – 0
What approach in Python moves the file pointer to the very end of a file?
a. file.seek(0)
b. file.seek(1)
c. file.seek(0, 2)
d. file.end()
Option c – file.seek(0, 2)
What is the correct method in Python to place the file pointer at the middle of a file?
a. file.middle()
b. file.seek(0.5)
c. file.seek(0.5, 1)
d. file.center()
Option b – file.seek(0.5)
When operating in write mode, what function does seek() serve in Python?
a. It closes the file
b. It adjusts the pointer to a specific byte location
c. It writes data into the file
d. It reads data from the file
Option b – It adjusts the pointer to a specific byte location
If the file pointer is at the end of a file, what does the tell() function return in Python?
a. 0
b. 1
c. -1
d. None
Option b – 1
In Python, how can you move the file pointer to the halfway point of the file?
a. file.middle()
b. file.seek(0.5)
c. file.seek(0.5, 1)
d. file.center()
Option b – file.seek(0.5)
What does the seek() function return when it cannot reposition the file pointer?
a. The new byte location
b. False
c. None
d. An error message
Option c – None
In Python, what is the method for placing the pointer at a specific byte in a binary file?
a. file.byte()
b. file.seek(0, byte)
c. file.seek(byte)
d. file.position(byte)
Option c – file.seek(byte)
We covered all the Advanced Python MCQ for Experienced Developers above in this post for free so that you can practice well for the exam.
Check out the latest MCQ content by visiting our mcqtube website homepage.
Also, check out: