Docsity

GIAC Python Coder GPYC Exam, Exams of Technology

Toggle sidebar
bg1
GIAC Python Coder GPYC Exam
**Question 1. Which built‑in function returns the number of items in an iterable?**
A) len()
B) count()
C) size()
D) total()
Answer: A
Explanation: len() is the standard built‑in that returns the length of sequences, mappings, or other
iterable containers.
**Question 2. In the LEGB rule, which scope is searched first when a name is referenced inside a nested
function?**
A) Global
B) Enclosed
C) Local
D) Built‑in
Answer: C
Explanation: Python looks in the Local scope of the innermost function first, then Enclosed, Global, and
finally Built‑in.
**Question 3. What is the result of the expression `0.1 + 0.2 == 0.3` in Python 3?**
A) True
B) False
C) TypeError
D) SyntaxError
Answer: B
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download GIAC Python Coder GPYC Exam and more Exams Technology in PDF only on Docsity!

Question 1. Which built‑in function returns the number of items in an iterable? A) len() B) count() C) size() D) total() Answer: A Explanation: len() is the standard built‑in that returns the length of sequences, mappings, or other iterable containers. Question 2. In the LEGB rule, which scope is searched first when a name is referenced inside a nested function? A) Global B) Enclosed C) Local D) Built‑in Answer: C Explanation: Python looks in the Local scope of the innermost function first, then Enclosed, Global, and finally Built‑in. Question 3. What is the result of the expression 0.1 + 0.2 == 0.3 in Python 3? A) True B) False C) TypeError D) SyntaxError Answer: B

Explanation: Due to binary floating‑point representation, 0.1 + 0.2 yields 0.30000000000000004, which is not exactly equal to 0.3. Question 4. Which statement correctly defines a function that accepts any number of positional arguments? A) def func(*args): B) def func(**args): C) def func(args...): D) def func(args=*): Answer: A Explanation: The *args syntax collects extra positional arguments into a tuple. Question 5. How can you import only the randint function from the random module? A) import random.randint B) from random import randint C) import randint from random D) random import randint Answer: B Explanation: The from module import name syntax imports a specific attribute directly into the current namespace. Question 6. Which loop construct allows execution of a block after the loop finishes normally (no break)? A) for … else: B) while … finally: C) do … while:

B) <class 'str'> C) <class 'bytearray'> D) <class 'list'> Answer: A Explanation: A literal prefixed with b creates a bytes object. Question 10. Which method removes and returns an arbitrary element from a set? A) pop() B) remove() C) discard() D) clear() Answer: A Explanation: set.pop() removes and returns an arbitrary element; remove and discard require a specific value. Question 11. How can you convert an integer 65 to its corresponding ASCII character? A) chr(65) B) ord(65) C) int(65) D) str(65) Answer: A Explanation: chr() returns the character represented by the Unicode code point; ord() does the reverse. Question 12. Which of the following statements about tuples is FALSE?

A) Tuples are immutable. B) Tuples can be used as dictionary keys. C) Tuples support item assignment. D) Tuples can contain heterogeneous types. Answer: C Explanation: Because tuples are immutable, you cannot assign to individual items. Question 13. What does the struct.pack('>I', 1024) call return? A) A bytes object containing big‑endian unsigned int 1024 B) A string '1024' C) A list [1024] D) An integer 1024 Answer: A Explanation: '>' specifies big‑endian, 'I' is unsigned int; pack returns a bytes object with the binary representation. Question 14. Which encoding can represent any Unicode character? A) ASCII B) UTF‑ 8 C) Latin‑ 1 D) ISO‑ 8859 ‑ 2 Answer: B Explanation: UTF‑8 is a variable‑length encoding capable of representing all Unicode code points.

Question 18. In Scapy, which layer is automatically added when you create an IP(dst='1.2.3.4')/TCP() packet? A) Ethernet B) UDP C) ARP D) ICMP Answer: A Explanation: When a layer that requires a lower‑level protocol is missing, Scapy adds an Ether layer for Ethernet encapsulation. Question 19. Which HTTP method is typically used to submit form data that modifies server state? A) GET B) HEAD C) POST D) OPTIONS Answer: C Explanation: POST sends data in the request body and is used for actions that create or modify resources. Question 20. Which requests function would you use to maintain a session across multiple HTTP calls? A) requests.get() B) requests.post() C) requests.Session() D) requests.connect() Answer: C

Explanation: Session objects keep cookies and connection pooling across requests. Question 21. What does the json.loads() function do? A) Convert a Python dict to a JSON string B) Parse a JSON string into a Python object C) Write JSON data to a file D) Validate JSON syntax only Answer: B Explanation: json.loads parses a JSON‑formatted string and returns the corresponding Python data structure. Question 22. Which SQLite command creates a new table named hosts with columns id INTEGER PRIMARY KEY and ip TEXT NOT NULL? A) CREATE TABLE hosts (id INT PRIMARY KEY, ip TEXT); B) CREATE TABLE hosts (id INTEGER PRIMARY KEY, ip TEXT NOT NULL); C) NEW TABLE hosts (id INTEGER, ip TEXT NOT NULL); D) TABLE hosts CREATE (id INTEGER, ip TEXT); Answer: B Explanation: The correct SQLite syntax includes CREATE TABLE followed by column definitions; INTEGER PRIMARY KEY creates an auto‑incrementing column. Question 23. When using subprocess.run(['ls', '-l'], capture_output=True, text=True), what type is result.stdout? A) bytes B) str C) list

C) AttributeError D) ValueError Answer: A Explanation: Attempting to retrieve a missing key using dict[key] raises KeyError. Question 27. Which of the following is the correct syntax to open a file for binary reading? A) open('data.bin', 'rb') B) open('data.bin', 'r') C) open('data.bin', 'wb') D) open('data.bin', 'b') Answer: A Explanation: 'rb' mode opens a file for reading binary data. Question 28. What will bool([]) evaluate to? A) True B) False C) Raises TypeError D) None Answer: B Explanation: Empty containers are considered falsy in Python. Question 29. Which of the following expressions correctly checks if variable x is an integer type? A) type(x) == int B) isinstance(x, int)

C) x is int D) x instanceof int Answer: B Explanation: isinstance works with inheritance and is the idiomatic way to test type. Question 30. Which Python statement is used to raise a custom exception with the message “Invalid input”? A) raise Exception("Invalid input") B) throw Exception("Invalid input") C) error Exception("Invalid input") D) assert Exception("Invalid input") Answer: A Explanation: The raise keyword creates and raises an exception object. Question 31. When creating an executable with PyInstaller, which flag tells it to bundle everything into a single file? A) --onefile B) --single C) --bundle D) --compact Answer: A Explanation: --onefile produces a single standalone executable. Question 32. Which of the following techniques is most likely to evade a signature‑based antivirus when packaging a Python script? A) Using a plain .py file

B) Raw binary bytes of the digest C) An integer value of the digest D) A base64‑encoded string Answer: A Explanation: hexdigest() returns the digest as a human‑readable hexadecimal string (64 characters for SHA‑256). Question 36. Which of the following is a valid way to copy a list a into a new independent list b? A) b = a B) b = a[:] C) b = a.copy() D) Both B and C Answer: D Explanation: Slicing and the list.copy() method both produce shallow copies; b = a would only create a reference. Question 37. In Python 3, what is the result of repr('\n')? A) '\\n' B) '\n' C) '\r' D) Error Answer: A Explanation: repr returns a printable representation; the newline character is escaped as '\\n'. Question 38. Which of the following statements about the asyncio event loop is FALSE?

A) It runs coroutines until they complete. B) It can be started with loop.run_forever(). C) It automatically creates new OS threads for each coroutine. D) It uses cooperative multitasking. Answer: C Explanation: asyncio does not create OS threads for coroutines; it schedules them cooperatively within a single thread. Question 39. Which decorator turns a regular function into a generator? A) @generator B) @yield C) No decorator; using yield inside the function makes it a generator. D) @coroutine Answer: C Explanation: A function becomes a generator simply by containing a yield statement; no decorator is needed. **Question 40. What will the following code print?

d = {'a':1, 'b':2} print(d.get('c', 0)) ```** A) `None` B) `0` C) `KeyError` D) Append the password to a comment block. Answer: B Explanation: Environment variables keep secrets out of source code and can be accessed at runtime. **Question 44. What does the `socket.recvfrom(1024)` method return?** A) A tuple `(data, address)` where `data` is up to 1024 bytes. B) Only the raw data received. C) A list of all packets received. D) The number of bytes received. Answer: A Explanation: `recvfrom` is used with UDP sockets and returns both the payload and the sender’s address. **Question 45. Which of the following statements about Python’s `@property` decorator is true?** A) It makes a method callable without parentheses. B) It forces the method to be static. C) It creates a read‑only attribute unless a setter is defined. D) It automatically caches the result. Answer: C Explanation: `@property` creates a getter; adding `@<prop>.setter` defines a writable attribute. **Question 46. In a `for` loop, which statement correctly skips the current iteration and proceeds to the next?** A) `continue` B) `break` C) `pass` D) `skip` Answer: A Explanation: `continue` ends the current loop cycle and starts the next iteration. **Question 47. Which built‑in function can be used to get an iterator over the keys of a dictionary?** A) `dict.keys()` B) `dict.itervalues()` C) `dict.iteritems()` D) `dict.values()` Answer: A Explanation: `dict.keys()` returns a view object that is iterable; in Python 2 `itervalues`/`iteritems` existed, but not in Python 3. **Question 48. What is the purpose of the `__repr__` method in a class?** A) Provide a user‑friendly string for printing. B) Return an official string representation that can be used to recreate the object. C) Store the object’s memory address. D) Define the class’s inheritance hierarchy. Answer: B Explanation: `__repr__` should return a string that, when passed to `eval`, reproduces the object (or at least a detailed representation). **Question 49. Which of the following statements about `with open(...) as f:` is FALSE?** A) The file is automatically closed when the block exits. A) `Point = collections.namedtuple('Point', ['x', 'y'])` B) `Point = namedtuple('Point', 'x y')` C) Both A and B (assuming `namedtuple` is imported) D) `Point = tuple('Point', 'x', 'y')` Answer: C Explanation: Both syntaxes are valid; the second requires `from collections import namedtuple`. **Question 53. Which HTTP status code indicates that the requested resource has moved permanently?** A) 301 B) 302 C) 404 D) 500 Answer: A Explanation: 301 is the standard code for a permanent redirect. **Question 54. Which of the following is a safe way to execute a shell command with arguments that may contain spaces or special characters?** A) `subprocess.run('ls - l /tmp')` B) `subprocess.run(['ls', '-l', '/tmp'])` C) `os.system('ls - l /tmp')` D) `subprocess.call('ls - l /tmp')` Answer: B Explanation: Passing a list avoids shell parsing and prevents injection; the others invoke the shell. **Question 55. What does the `functools.lru_cache(maxsize=128)` decorator do?** A) Limits the function to 128 calls per minute. B) Caches up to 128 recent results to speed up repeated calls. C) Raises an error after 128 calls. D) Logs each call to a file. Answer: B Explanation: `lru_cache` stores recent function results (Least Recently Used) up to the specified size. **Question 56. Which of the following is the correct syntax to define a class method?** A) `def method(cls):` B) `@classmethod\ndef method(cls):` C) `@staticmethod\ndef method(self):` D) `def method(self, cls):` Answer: B Explanation: `@classmethod` decorates a method that receives the class (`cls`) as its first argument. **Question 57. In a TCP three‑way handshake, which packet is sent by the server after receiving the client’s SYN?** A) SYN‑ACK B) ACK C) FIN D) RST Answer: A Explanation: The server replies with a SYN‑ACK packet to acknowledge the client’s SYN and synchronize its own sequence number.