Unit 6: Files and Exceptions; Regular Expressions - Practice Quiz

INT108 — Python Programming 50 Questions
0 Correct 0 Wrong 50 Left
0/50

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

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

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

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

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. 'a'
B. 'w'
C. 'r'
D. 'x'

4 To write a variable that is an integer to a text file, what must be done first?

A. Pickle it
B. Cast it to a string
C. Cast it to a float
D. Write it directly

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

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

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

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

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

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

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

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

9 Which function is used to create a new directory?

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

10 What does 'Pickling' refer to in Python?

A. Converting an object hierarchy into a byte stream
B. Compressing a file
C. Converting a byte stream back into an object
D. Encrypting a file

11 Which module is required to perform pickling?

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

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

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

13 Which function is used to deserialize a byte stream back into a Python object?

A. pickle.write()
B. pickle.push()
C. pickle.dump()
D. pickle.load()

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

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

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

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

16 When is the code inside the 'else' block of a try-except statement executed?

A. When an exception occurs
B. Always
C. When the try block fails
D. When no exception occurs

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

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

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

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

19 What is the primary purpose of the 'finally' block?

A. To execute code regardless of errors
B. To stop the program
C. To handle errors
D. To return a value

20 How can you access the error message associated with an exception?

A. except e in Exception:
B. except Exception as e:
C. catch Exception e:
D. except Exception with e:

21 Which module supports Regular Expressions in Python?

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

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

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

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. Start of string
B. Any character except a newline
C. Only a literal dot
D. Any digit

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

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

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

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

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

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

28 What does the quantifier '+' match?

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

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 string with replacements made
B. A list
C. A match object
D. A boolean

31 Which regex pattern would match a valid email address roughly containing an '@' symbol?

A. email:\w
B. \w#\w
C. \d+@\w+
D. \w+@\w+.\w+

32 In the context of web scraping with regex, what are usually targeted to extract links?

A. span tags
B. <div> tags
C. href attributes
D. src attributes

33 What does the '?' quantifier signify?

A. Match 0 or 1 times
B. Match 0 or more times
C. Match exactly 1 time
D. Match 1 or more times

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

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

35 Why are raw strings (r'text') preferred for regular expressions in Python?

A. They allow binary data
B. They match case insensitively
C. They avoid conflict with Python's escape characters
D. They run faster

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

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

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

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

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. Returns False
B. Returns -1
C. Raises an error
D. Returns None

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

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

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

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

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

A. os.path.exists()
B. os.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. putlines()
C. writelines()
D. print()

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

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

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

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

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

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

47 Which regex pattern matches any non-digit character?

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

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

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

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

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

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

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