Explanation:The built-in function open() is used to open a file and returns a file object.
Incorrect! Try again.
2What is the default mode when opening a file using the open() function?
A.Write ('w')
B.Append ('a')
C.Read ('r')
D.Binary ('b')
Correct Answer: Read ('r')
Explanation:If the mode argument is omitted, Python defaults to 'r', which opens the file for reading.
Incorrect! Try again.
3Which file mode opens a file for writing and creates the file if it does not exist, but truncates it if it does?
A.'r'
B.'a'
C.'w'
D.'x'
Correct Answer: 'w'
Explanation:The 'w' mode opens a file for writing. It creates a new file if it doesn't exist or truncates (overwrites) the file if it exists.
Incorrect! Try again.
4To write a variable that is an integer to a text file, what must be done first?
A.Cast it to a string
B.Cast it to a float
C.Write it directly
D.Pickle it
Correct Answer: Cast it to a string
Explanation:Text files accept only strings. Integers must be converted using str() before writing.
Incorrect! Try again.
5Which method is used to read the entire content of a file as a single string?
A.readline()
B.read()
C.readlines()
D.scan()
Correct Answer: read()
Explanation:The read() method reads the whole file content into a single string variable.
Incorrect! Try again.
6What is the return type of the readlines() method?
A.String
B.Integer
C.List of strings
D.Dictionary
Correct Answer: List of strings
Explanation:readlines() reads all lines in a file and returns them as a list of strings.
Incorrect! Try again.
7Which keyword is used to handle file closing automatically in Python?
A.do
B.try
C.with
D.for
Correct Answer: with
Explanation:The 'with' statement acts as a context manager and automatically closes the file once the block of code is executed.
Incorrect! Try again.
8Which module allows you to interact with the operating system to handle directories?
A.sys
B.os
C.file
D.dir
Correct Answer: os
Explanation:The 'os' module provides a portable way of using operating system-dependent functionality, including directory manipulation.
Incorrect! Try again.
9Which function is used to create a new directory?
A.os.create_dir()
B.os.mkdir()
C.os.newdir()
D.os.make()
Correct Answer: os.mkdir()
Explanation:os.mkdir() is the standard function to create a directory named path.
Incorrect! Try again.
10What does 'Pickling' refer to in Python?
A.Compressing a file
B.Converting an object hierarchy into a byte stream
C.Encrypting a file
D.Converting a byte stream back into an object
Correct Answer: Converting an object hierarchy into a byte stream
Explanation:Pickling (serialization) is the process of converting a Python object into a byte stream.
Incorrect! Try again.
11Which module is required to perform pickling?
A.os
B.json
C.pickle
D.serialize
Correct Answer: pickle
Explanation:The standard module for object serialization in Python is named 'pickle'.
Incorrect! Try again.
12Which file mode must be used when writing pickled data to a file?
A.'w'
B.'wb'
C.'r'
D.'rb'
Correct Answer: 'wb'
Explanation:Pickling creates a byte stream, so the file must be opened in binary write mode ('wb').
Incorrect! Try again.
13Which function is used to deserialize a byte stream back into a Python object?
A.pickle.dump()
B.pickle.write()
C.pickle.load()
D.pickle.push()
Correct Answer: pickle.load()
Explanation:pickle.load() reads the pickled byte stream from a file and reconstructs the object.
Incorrect! Try again.
14What exception is raised when a number is divided by zero?
A.ValueError
B.TypeError
C.ZeroDivisionError
D.ArithmeticError
Correct Answer: ZeroDivisionError
Explanation:Python raises a ZeroDivisionError when the second argument of a division or modulo operation is zero.
Incorrect! Try again.
15Which block contains the code that might raise an exception?
A.except
B.try
C.else
D.finally
Correct Answer: try
Explanation:The 'try' block lets you test a block of code for errors.
Incorrect! Try again.
16When is the code inside the 'else' block of a try-except statement executed?
A.Always
B.When an exception occurs
C.When no exception occurs
D.When the try block fails
Correct Answer: When no exception occurs
Explanation:The else block allows you to execute code if the try block does not raise an exception.
Incorrect! Try again.
17What exception is raised when trying to open a file that does not exist in read mode?
A.IOError
B.FileError
C.FileNotFoundError
D.NameError
Correct Answer: FileNotFoundError
Explanation:FileNotFoundError is raised when a file or directory is requested but cannot be found.
Incorrect! Try again.
18Which of the following correctly handles a specific exception named 'MyError'?
A.catch MyError:
B.except MyError:
C.handle MyError:
D.try MyError:
Correct Answer: except MyError:
Explanation:The syntax to handle a specific exception in Python is 'except ExceptionName:'.
Incorrect! Try again.
19What is the primary purpose of the 'finally' block?
A.To handle errors
B.To execute code regardless of errors
C.To stop the program
D.To return a value
Correct Answer: To execute code regardless of errors
Explanation:The finally block lets you execute code, regardless of the result of the try- and except blocks (often used for cleanup).
Incorrect! Try again.
20How can you access the error message associated with an exception?
A.except Exception with e:
B.except Exception as e:
C.except e in Exception:
D.catch Exception e:
Correct Answer: except Exception as e:
Explanation:Using 'as e' assigns the exception object to the variable 'e', allowing access to its details.
Incorrect! Try again.
21Which module supports Regular Expressions in Python?
A.regex
B.pyre
C.re
D.regexp
Correct Answer: re
Explanation:The built-in module for regular expressions in Python is named 're'.
Incorrect! Try again.
22What does the special character '^' represent in a regular expression?
A.End of string
B.Start of string
C.Any character
D.Escape character
Correct Answer: Start of string
Explanation:The caret symbol '^' matches the start of the string.
Incorrect! Try again.
23Which regular expression quantifier matches 0 or more occurrences of the preceding character?
A.+
B.?
C.*
D..
Correct Answer: *
Explanation:The asterisk '*' matches 0 or more repetitions of the preceding regex.
Incorrect! Try again.
24What does the pattern '.' (dot) match?
A.Only a literal dot
B.Any character except a newline
C.Any digit
D.Start of string
Correct Answer: Any character except a newline
Explanation:In default mode, the dot matches any character except the newline character.
Incorrect! Try again.
25Which function searches for a pattern only at the beginning of a string?
A.re.search()
B.re.findall()
C.re.match()
D.re.find()
Correct Answer: re.match()
Explanation:re.match() checks for a match only at the beginning of the string.
Incorrect! Try again.
26What does the special sequence '\d' match?
A.Any digit (0-9)
B.Any non-digit
C.Any whitespace
D.Any alphabet
Correct Answer: Any digit (0-9)
Explanation:\d matches any decimal digit; this is equivalent to the class [0-9].
Incorrect! Try again.
27Which function returns a list of all non-overlapping matches in the string?
A.re.match()
B.re.search()
C.re.findall()
D.re.group()
Correct Answer: re.findall()
Explanation:re.findall() scans the string and returns all non-overlapping matches as a list of strings.
Incorrect! Try again.
28What does the quantifier '+' match?
A.0 or 1 occurrence
B.0 or more occurrences
C.1 or more occurrences
D.Exactly 1 occurrence
Correct Answer: 1 or more occurrences
Explanation:The plus sign '+' matches 1 or more repetitions of the preceding regex.
Incorrect! Try again.
29Which character is used to define a range of characters, like 'a' through 'z'?
A.()
B.{}
C.[]
D.<>
Correct Answer: []
Explanation:Square brackets [] are used to indicate a set of characters or a range, e.g., [a-z].
Incorrect! Try again.
30What is the output of re.sub()?
A.A match object
B.A boolean
C.A list
D.A string with replacements made
Correct Answer: A string with replacements made
Explanation:re.sub(pattern, repl, string) returns the string obtained by replacing the leftmost non-overlapping occurrences of pattern with repl.
Incorrect! Try again.
31Which regex pattern would match a valid email address roughly containing an '@' symbol?
A.\w+@\w+.\w+
B.\d+@\w+
C.\w#\w
D.email:\w
Correct Answer: \w+@\w+.\w+
Explanation:This pattern looks for alphanumeric characters (\w+), followed by '@', followed by domain name characters, a dot, and an extension.
Incorrect! Try again.
32In the context of web scraping with regex, what are usually targeted to extract links?
A.<div> tags
B.href attributes
C.src attributes
D.span tags
Correct Answer: href attributes
Explanation:In HTML, hyperlinks are defined in the 'href' attribute of <a> tags, making them the target for extracting links.
Incorrect! Try again.
33What does the '?' quantifier signify?
A.Match 0 or 1 times
B.Match exactly 1 time
C.Match 1 or more times
D.Match 0 or more times
Correct Answer: Match 0 or 1 times
Explanation:The question mark '?' causes the resulting RE to match 0 or 1 repetitions of the preceding RE.
Incorrect! Try again.
34Which special sequence matches any whitespace character (space, tab, newline)?
A.\s
B.\S
C.\w
D.\W
Correct Answer: \s
Explanation:\s matches any whitespace character.
Incorrect! Try again.
35Why are raw strings (r'text') preferred for regular expressions in Python?
A.They run faster
B.They avoid conflict with Python's escape characters
C.They allow binary data
D.They match case insensitively
Correct Answer: They avoid conflict with Python's escape characters
Explanation:Raw strings treat backslashes as literal characters, preventing Python from interpreting things like '\b' (backspace) before the regex engine sees it.
Incorrect! Try again.
36What method of a match object returns the string matched by the regex?
A.return()
B.get()
C.group()
D.match()
Correct Answer: group()
Explanation:The group() method returns the part of the string where there was a match.
Incorrect! Try again.
37Which flag is used to perform case-insensitive matching?