Unit 6 - Practice Quiz

INT108

1 Which function is used to open a file in Python?

A. file()
B. open()
C. read()
D. start()

2 What is the default mode when opening a file using the open() function?

A. Write ('w')
B. Append ('a')
C. Read ('r')
D. Binary ('b')

3 Which 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'

4 To 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

5 Which method is used to read the entire content of a file as a single string?

A. readline()
B. read()
C. readlines()
D. scan()

6 What is the return type of the readlines() method?

A. String
B. Integer
C. List of strings
D. Dictionary

7 Which keyword is used to handle file closing automatically in Python?

A. do
B. try
C. with
D. for

8 Which module allows you to interact with the operating system to handle directories?

A. sys
B. os
C. file
D. dir

9 Which function is used to create a new directory?

A. os.create_dir()
B. os.mkdir()
C. os.newdir()
D. os.make()

10 What 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

11 Which module is required to perform pickling?

A. os
B. json
C. pickle
D. serialize

12 Which file mode must be used when writing pickled data to a file?

A. 'w'
B. 'wb'
C. 'r'
D. 'rb'

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

14 What exception is raised when a number is divided by zero?

A. ValueError
B. TypeError
C. ZeroDivisionError
D. ArithmeticError

15 Which block contains the code that might raise an exception?

A. except
B. try
C. else
D. finally

16 When 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

17 What exception is raised when trying to open a file that does not exist in read mode?

A. IOError
B. FileError
C. FileNotFoundError
D. NameError

18 Which of the following correctly handles a specific exception named 'MyError'?

A. catch MyError:
B. except MyError:
C. handle MyError:
D. try MyError:

19 What 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

20 How 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:

21 Which module supports Regular Expressions in Python?

A. regex
B. pyre
C. re
D. regexp

22 What does the special character '^' represent in a regular expression?

A. End of string
B. Start of string
C. Any character
D. Escape character

23 Which regular expression quantifier matches 0 or more occurrences of the preceding character?

A. +
B. ?
C. *
D. .

24 What does the pattern '.' (dot) match?

A. Only a literal dot
B. Any character except a newline
C. Any digit
D. Start of string

25 Which function searches for a pattern only at the beginning of a string?

A. re.search()
B. re.findall()
C. re.match()
D. re.find()

26 What does the special sequence '\d' match?

A. Any digit (0-9)
B. Any non-digit
C. Any whitespace
D. Any alphabet

27 Which function returns a list of all non-overlapping matches in the string?

A. re.match()
B. re.search()
C. re.findall()
D. re.group()

28 What does the quantifier '+' match?

A. 0 or 1 occurrence
B. 0 or more occurrences
C. 1 or more occurrences
D. Exactly 1 occurrence

29 Which character is used to define a range of characters, like 'a' through 'z'?

A. ()
B. {}
C. []
D. <>

30 What is the output of re.sub()?

A. A match object
B. A boolean
C. A list
D. A string with replacements made

31 Which 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

32 In 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

33 What 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

34 Which special sequence matches any whitespace character (space, tab, newline)?

A. \s
B. \S
C. \w
D. \W

35 Why 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

36 What method of a match object returns the string matched by the regex?

A. return()
B. get()
C. group()
D. match()

37 Which flag is used to perform case-insensitive matching?

A. re.A
B. re.C
C. re.I
D. re.M

38 How do you define a 'group' within a regular expression to extract specific parts?

A. Using []
B. Using {}
C. Using ()
D. Using ||

39 What happens if re.match() does not find a match?

A. Raises an error
B. Returns None
C. Returns False
D. Returns -1

40 In Web Scraping, why might regex be used on HTML content?

A. To render the page
B. To execute JavaScript
C. To extract text matching specific patterns
D. To connect to the server

41 What is the function os.getcwd() used for?

A. Get Current Working Directory
B. Get Current Write Directory
C. Generate Code Working Directory
D. Get Count Working Directory

42 Which method is used to check if a file exists at a specified path?

A. os.exists()
B. os.path.exists()
C. os.check()
D. file.exists()

43 To write a list of strings to a file, which method is most direct?

A. write()
B. writelines()
C. print()
D. putlines()

44 What does the '$' symbol represent in regex?

A. Start of string
B. End of string
C. Currency
D. Variable

45 Which of the following is NOT a valid file mode?

A. rb
B. w+
C. ab
D. z

46 If a try block raises an exception that is not defined in the except block, what happens?

A. The program crashes (terminates)
B. It is ignored
C. The else block runs
D. The try block repeats

47 Which regex pattern matches any non-digit character?

A. \d
B. \D
C. \n
D. \w

48 What is the purpose of the re.split() function?

A. To join strings
B. To split a string by the occurrences of a pattern
C. To remove patterns
D. To find duplicates

49 When using pickle.dump(obj, file), what is the 'file' argument?

A. The file path string
B. A file object opened in binary write mode
C. A string of data
D. The directory name

50 What is the standard Python library used to fetch URLs for web scraping before using regex?

A. os
B. sys
C. urllib
D. pickle