PCAP Standard Library and Built-ins 2 — Questions and Answers
Question 1: Which module is used to work with regular expressions in Python?
- regex
- re (Correct answer)
- regexp
- pattern
Correct answer: re
The `re` module provides regular expression matching operations.
Question 2: What does `re.match()` do differently from `re.search()`?
- match() is case-insensitive
- match() only matches at the beginning of the string (Correct answer)
- match() returns all matches
- match() uses extended regex
Correct answer: match() only matches at the beginning of the string
`re.match()` only looks at the beginning of the string, while `re.search()` scans the entire string.
Question 3: What does `sys.argv` contain?
- Environment variables
- Command-line arguments as a list (Correct answer)
- System configuration
- Python version info
Correct answer: Command-line arguments as a list
`sys.argv` is a list where `sys.argv[0]` is the script name and the rest are command-line arguments.
Question 4: Which built-in function converts a string to an integer?
- to_int()
- int() (Correct answer)
- parse()
- integer()
Correct answer: int()
`int()` converts a string or number to an integer, raising `ValueError` for invalid input.
Question 5: What does `dir(obj)` return?
- The object's directory path
- A list of valid attributes and methods of the object (Correct answer)
- The class hierarchy
- The object's size
Correct answer: A list of valid attributes and methods of the object
`dir()` returns an alphabetically sorted list of names in the current scope or object's attributes.
Question 6: Which module provides `namedtuple` for creating tuple subclasses with named fields?
- typing
- dataclasses
- collections (Correct answer)
- struct
Correct answer: collections
`collections.namedtuple` creates tuple subclasses with named fields for readable attribute access.
Which module is used to work with regular expressions in Python?