



















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















































Question 1. Which of the following best describes the role of the Python interpreter when executing a script? A) It compiles Python code directly to machine code. B) It translates source code to bytecode and then executes it on a virtual machine. C) It interprets each line of source code without any intermediate representation. D) It sends the script to a remote server for execution. Answer: B Explanation: The Python interpreter first compiles source code to bytecode (.pyc) and then executes that bytecode on the Python virtual machine. Question 2. In the LEGB rule, which scope is searched first when a name is referenced inside a nested function? A) Global B) Built-in C) Enclosed (non-local) D) Local Answer: D Explanation: Python looks for the name in the Local scope first, then Enclosed, then Global, and finally Built-in. **Question 3. What will be the output of the following code?
def foo(a, b=2, *c, **d): return a + b + len(c) + len(d) print(foo(1, 3, 4, 5, x=6, y=7)) ```** A) 9 ## Ultimate Exam ## B) 10 ## C) 11 ## D) 12 Answer: C Explanation: a=1, b=3, *c receives (4,5) → len(c)=2, **d receives {'x':6,'y':7} → len(d)=2. Sum = 1+3+2+2 = 8? Wait compute: 1+3=4, +2=6, +2=8. Actually answer is 8 which is not listed; re-evaluate: The function returns a + b + len(c) + len(d) = 1 + 3 + 2 + 2 = 8. Since 8 is not an option, the correct answer is **None of the above**; however given the choices, the closest is **A) 9** which is incorrect. Therefore the question is malformed. *Correct answer: 8 (not listed).* Explanation: The calculation yields 8; the provided options were erroneous. **Question 4. Which statement correctly imports the `json` module using an alias? ** A) `import json as js` B) `from json import * as js` C) `import json -> js` D) `include json as js` Answer: A Explanation: `import json as js` imports the module and assigns it the alias `js`. **Question 5. What is the purpose of the `else` clause on a `for` loop in Python?** A) Executes after the loop finishes normally, without a `break`. B) Executes only if the loop encounters a `continue`. C) Executes after each iteration of the loop. D) Executes only when the loop body raises an exception. Answer: A ## Ultimate Exam Answer: C Explanation: Tuples cannot be modified after creation, making them immutable. **Question 8. What does the expression `b'\xff' * 3` evaluate to?** A) `b'\xff\xff\xff'` B) `b'255255255'` C) `b'\\xff\\xff\\xff'` D) `b'\\xFF\\xFF\\xFF'` Answer: A Explanation: Multiplying a bytes object repeats its contents; three repetitions give three 0xFF bytes. **Question 9. Which method of a `dict` object returns a view of its keys?** A) `dict.keys()` B) `dict.values()` C) `dict.items()` D) `dict.iterkeys()` Answer: A Explanation: `dict.keys()` returns a dynamic view object of the dictionary’s keys. **Question 10. When using the `struct` module, what format character represents a 4 - byte unsigned integer in native byte order?** A) `I` B) `L` C) `Q` D) `H` ## Ultimate Exam Answer: A Explanation: `'I'` stands for an unsigned int (4 bytes) in native byte order. **Question 11. Which encoding will correctly encode the character “é” as the two-byte sequence `0xC3 0xA9`?** A) UTF- 8 B) Latin- 1 C) UTF-16BE D) ASCII Answer: A Explanation: In UTF-8, “é” (U+00E9) is encoded as the two bytes `C3 A9`. **Question 12. Which of the following statements about byte order is true?** A) Little-endian stores the most significant byte first. B) Big-endian stores the least significant byte first. C) Network byte order is defined as big-endian. D) Byte order only matters for text strings. Answer: C Explanation: Network protocols use big-endian (network byte order) for transmitting multi-byte integers. **Question 13. Which of these tools can be used to convert a Python script into a single Windows executable without requiring a separate Python interpreter?** A) PyInstaller B) pip C) virtualenv D) conda ## Ultimate Exam A) Opens a UDP connection to port 80. B) Opens a TCP connection to port 80. C) Binds the socket to port 80 on the local machine. D) Listens for incoming connections on port 80. Answer: B Explanation: `socket()` defaults to `AF_INET, SOCK_STREAM`; `connect` establishes a TCP connection to the remote host. **Question 17. Which of the following is a common method for capturing keystrokes in a cross-platform Python keylogger?** A) `os.system('read')` B) `pynput.keyboard.Listener` C) `socket.recv()` D) `subprocess.Popen(['keylogger'])` Answer: B Explanation: `pynput` provides a high-level API for listening to keyboard events on Windows, macOS, and Linux. **Question 18. When automating vulnerability scanning, which Python library is most suitable for parsing JSON responses from a REST API?** A) `csv` B) `json` C) `xml.etree.ElementTree` D) `pickle` Answer: B Explanation: The `json` module decodes JSON strings into Python objects. ## Ultimate Exam **Question 19. In Scapy, which layer class represents the Internet Control Message Protocol?** A) `ICMPv6` B) `ICMP` C) `UDP` D) `TCP` Answer: B Explanation: `scapy.layers.inet.ICMP` models the IPv4 ICMP protocol. **Question 20. Which Scapy function is used to read packets from a PCAP file?** A) `rdpcap()` B) `load_pcap()` C) `read_packets()` D) `pcap.read()` Answer: A Explanation: `rdpcap('file.pcap')` returns a list of packets from the specified capture file. **Question 21. In TCP/IP, the field that indicates the size of the TCP header is called:** A) Data Offset B) Window Size C) Sequence Number D) Urgent Pointer Answer: A Explanation: The Data Offset (header length) field specifies the length of the TCP header in 32-bit words. ## Ultimate Exam Explanation: A `Session` object stores cookies and other settings between requests. **Question 25. Which of the following regular expression patterns will correctly extract the value of a hidden input field named “token” from HTML?** A) `r''` B) `r'(?<=token=).*'` C) `r'\btoken\b'` D) `r'^\s*token\s*$'` Answer: A Explanation: The pattern captures the value attribute of the specific hidden input element. **Question 26. Which SQLite Python library function is used to execute a parameterized INSERT statement safely?** A) `cursor.execute("INSERT INTO t VALUES (%s, %s)" % (a, b))` B) `cursor.execute("INSERT INTO t VALUES (?, ?)", (a, b))` C) `cursor.run("INSERT INTO t VALUES (?, ?)", a, b)` D) `cursor.add("INSERT INTO t VALUES (?, ?)", (a, b))` Answer: B Explanation: Using placeholders (`?`) with a tuple ensures proper escaping and prevents SQL injection. **Question 27. What will be the type of the object returned by `socket.getaddrinfo('localhost', None)`?** A) List of tuples B) Dictionary C) Set D) String ## Ultimate Exam Answer: A Explanation: `getaddrinfo` returns a list where each element is a tuple containing address family, socket type, protocol, canonical name, and socket address. **Question 28. Which of the following statements about Python’s `bytearray` is true?** A) It is immutable. B) It can be directly written to a binary file without conversion. C) It automatically decodes UTF-8 data. D) It behaves exactly like a `bytes` object. Answer: B Explanation: `bytearray` is mutable and can be written to files as raw binary data. **Question 29. In a Python class, what is the effect of defining `__slots__`?** A) It enables multiple inheritance. B) It restricts attribute creation to a predefined set, reducing memory usage. C) It makes the class abstract. D) It forces all methods to be static. Answer: B Explanation: `__slots__` defines a fixed set of attributes, preventing the creation of `__dict__` for each instance and saving memory. **Question 30. Which of the following is the correct syntax to create a set comprehension that contains the squares of numbers 0-9?** A) `{x**2 for x in range(10)}` B) `[x**2 for x in range(10)]` C) `(x**2 for x in range(10))` D) `{x**2 | x in range(10)}` ## Ultimate Exam D) It blocks the interpreter while waiting for I/O. Answer: B Explanation: `asyncio` schedules coroutines to run concurrently within a single thread using non-blocking I/O. **Question 34. In the context of TCP port scanning, which technique is most likely to be stealthy against simple firewalls?** A) Full connect scan (SYN-ACK-FIN). B) SYN scan (half-open). C) FIN scan. D) ACK scan. Answer: B Explanation: SYN scans send only the initial SYN packet and do not complete the three-way handshake, often evading basic logging. **Question 35. Which Scapy field name corresponds to the destination port in a TCP segment?** A) `sport` B) `dport` C) `dst_port` D) `port_dst` Answer: B Explanation: `TCP.dport` holds the destination port number. **Question 36. What is the result of executing `hash((1, 2, 3))` in Python?** A) A TypeError because tuples are unhashable. B) An integer hash value. ## Ultimate Exam C) A string representation of the tuple. D) `None`. Answer: B Explanation: Tuples are immutable and hashable; `hash()` returns an integer. **Question 37. Which of the following statements about the `global` keyword is false?** A) It allows a function to modify a variable defined at module level. B) It can be used inside a nested function to refer to a variable in the enclosing scope. C) It must be declared before the variable is used in the function. D) It creates a new global variable if the name does not exist. Answer: B Explanation: `global` only refers to module-level variables, not variables in an enclosing (non-global) scope; `nonlocal` is used for that. **Question 38. Which of the following is the most appropriate way to safely concatenate a user-provided filename to a directory path?** A) `directory + '/' + filename` B) `os.path.join(directory, filename)` C) `directory + filename` D) `f"{directory}{filename}"` Answer: B Explanation: `os.path.join` correctly handles path separators and avoids malformed paths. **Question 39. In Python 3, what is the default encoding used for source files if no encoding comment is present?** ## Ultimate Exam **Question 42. Which Python built-in function can be used to convert an integer to its binary string representation prefixed with `0b`?** A) `bin()` B) `hex()` C) `oct()` D) `format()` Answer: A Explanation: `bin(10)` returns `'0b1010'`. **Question 43. When using `socket.sendall(data)`, what guarantee does the method provide?** A) The data is sent in a single packet. B) All bytes in `data` are transmitted unless an error occurs. C) The method returns the number of bytes sent. D) It automatically encrypts the data. Answer: B Explanation: `sendall` loops internally until all bytes are sent or an exception is raised. **Question 44. Which of the following statements about the `logging` module is correct?** A) `logging.basicConfig(level=logging.DEBUG)` must be called before any logger is created. B) Loggers cannot have multiple handlers. C) The default logging level is `WARNING`. D) `logging.info()` will always print to the console regardless of configuration. Answer: C ## Ultimate Exam Explanation: If not configured, the logging system only outputs messages with severity `WARNING` or higher. **Question 45. In a Python `dict`, which method removes and returns an arbitrary (key, value) pair?** A) `popitem()` B) `pop()` C) `remove()` D) `discard()` Answer: A Explanation: `dict.popitem()` removes and returns a random (key, value) pair (the last inserted in Python 3.7+). **Question 46. Which of the following is a valid way to create a bytes literal containing the ASCII characters “ABC”?** A) `b'ABC'` B) `bytes('ABC')` C) `b"ABC"` D) Both A and C Answer: D Explanation: Both single and double quotes can be used for bytes literals. **Question 47. Which attribute of a Scapy `Packet` object contains the raw bytes of the packet?** A) `payload` B) `raw` C) `original` D) `bytes` ## Ultimate Exam Answer: A Explanation: A tuple of exception types can be provided to catch any of them. **Question 51. In the context of HTTP, what does the status code 302 indicate?** A) Bad Request B) Not Found C) Found (temporary redirect) D) Internal Server Error Answer: C Explanation: 302 means the requested resource resides temporarily under a different URI. **Question 52. Which of the following commands will display the first 10 lines of a file named `log.txt` using Python?** A) `head -n 10 log.txt` B) `print(open('log.txt').readlines()[:10])` C) `os.system('cat log.txt | head')` D) `subprocess.run(['head', '-n', '10', 'log.txt'])` Answer: B Explanation: Reading the file and slicing the list of lines returns the first ten lines. **Question 53. What is the effect of setting `PYTHONUNBUFFERED=1` in the environment before running a script?** A) Disables all output buffering, making `print` flush immediately. B) Enables line buffering only on Windows. C) Forces the interpreter to use a different bytecode version. D) Has no effect on standard I/O. ## Ultimate Exam Answer: A Explanation: The environment variable forces unbuffered binary stdout and stderr. **Question 54. Which of the following is a valid way to create a daemon thread in Python?** A) `t = threading.Thread(target=func); t.daemon = True; t.start()` B) `t = threading.Thread(target=func, daemon=False)` C) `t = threading.Thread(target=func); t.setDaemon(False)` D) `t = threading.Thread(target=func); t.start(daemon=True)` Answer: A Explanation: Setting `daemon` to `True` before `start()` marks the thread as a daemon. **Question 55. In a Python `set`, which operation returns a new set containing elements present in both operands?** A) `set1 & set2` B) `set1 | set2` C) `set1 - set2` D) `set1 ^ set2` Answer: A Explanation: The `&` operator computes the intersection of two sets. **Question 56. Which of the following statements is true about the `subprocess.run()` function introduced in Python 3.5?** A) It always returns a string containing the command output. B) It replaces `os.system` and provides a `CompletedProcess` object. C) It cannot capture standard error. D) It only works on Windows.