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. file()
B. start()
C. read()
D. open()

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

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

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

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

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

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. Integer
B. Dictionary
C. List of strings
D. String

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

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

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

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

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. Converting a byte stream back into an object
C. Compressing a file
D. Encrypting a file

11 Which module is required to perform pickling?

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

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.write()
B. pickle.load()
C. pickle.push()
D. pickle.dump()

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

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

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

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

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

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

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

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

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

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

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

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

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

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

21 Which module supports Regular Expressions in Python?

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

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

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

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

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

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

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

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

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

28 What does the quantifier '+' match?

A. Exactly 1 occurrence
B. 0 or 1 occurrence
C. 1 or more occurrences
D. 0 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 match object
B. A string with replacements made
C. A boolean
D. A list

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. span 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 1 or more times
D. Match exactly 1 time

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 allow binary data
B. They avoid conflict with Python's escape characters
C. They match case insensitively
D. They run faster

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

47 Which regex pattern matches any non-digit character?

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

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

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

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. The directory name
D. A string of data

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