




















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!





















eliminates duplicates?
A) List B) Tuple C) Set D) Dictionary
Answer: C) Set
Rationale: Sets do not preserve order and automatically eliminate duplicates. They are excellent for membership testing and filtering unique values, such as extracting unique IP addresses from log files. Sets use hash-based lookup for O(1) membership testing.
by a descriptive key?
A) List B) Tuple C) Set D) Dictionary
Answer: D) Dictionary
Rationale: Dictionaries store data as key-value pairs, allowing data to be accessed using meaningful labels rather than numeric indexes. They are ideal for representing relationships such as device names and IP addresses. Dictionaries provide O(1) average-time complexity for lookups.
in Python?
A) // This is a comment B) /* This is a comment */ C) # This is a comment D) ``
Answer: C) # This is a comment
Rationale: In Python, single-line comments are created using the hash symbol (#). Option A is used in C/C++/Java, Option B is a multi-line comment in C/C++/Java, and Option D is an HTML comment syntax. Python does not use these other comment styles.
A) 2. B) 2 C) 3 D) 2.
Answer: B) 2
Rationale: The floor division operator (//) returns the largest integer less than or equal to the division result. 5 // 2 equals 2 (not 2.5 or 2.0), as floor division discards the remainder and returns an integer in Python 3.
unique IP addresses in Python?
A) List B) Tuple C) Set D) Dictionary
Rationale: The len() function is the built-in Python function used to return the number of items in a sequence or collection. size() is not a built-in Python function (it's used in other languages like Java), length() is not a Python built-in, and count() returns the number of occurrences of a specific element.
A) [1, 2, 3] B) [1, 2, 3, 4] C) [4] D) Error
Answer: B) [1, 2, 3, 4]
Rationale: In Python, when you assign a list to a new variable (b = a), both variables reference the same list object in memory. Modifying the list through b also affects a because they point to the same object. This is a common pitfall — to create a copy, use b = a.copy() or b = a[:].
A) insert() B) extend() C) append() D) add()
Answer: C) append()
Rationale: append() adds an item to the end of a list, modifying the list in place. For adding multiple elements, use extend(). For adding at a specific position, use insert().
A) append() B) extend() C) insert() D) merge()
Answer: B) extend()
Rationale: extend() adds multiple elements from an iterable (like another list) to the end of a list. Unlike append(), which adds the entire iterable as a single element, extend() adds each element individually.
list?
A) find() B) index() C) locate() D) search()
Answer: B) index()
Rationale: The index() method searches a list and returns the first occurrence's index position. If not found, it raises a ValueError. Essential for locating elements when their position is unknown.
B) They allow values to be associated with descriptive keys C) They only store numeric data D) They consume less memory than lists
Answer: B) They allow values to be associated with descriptive keys
Rationale: Dictionaries store data as key-value pairs, allowing information to be labeled meaningfully. Values are accessed using descriptive keys rather than numeric indexes — ideal for representing device names and IP addresses.
A) **B)** C) **D)**
Answer: A) ``
Rationale: The type() function is used to check an object's type. type(42) returns `` because 42 is an integer.
A) str() B) int() C) float() D) bool()
Answer: B) int()
Rationale: Use Python's built-in conversion functions like int(), float(), str(), and bool(). int('123') converts the string '123' to the integer 123.
A) Using == instead of = B) Using = instead of == in a condition C) Forgetting to indent D) Using != instead of <>
Answer: B) Using = instead of == in a condition
Rationale: A common pitfall is using = (assignment) instead of == (equality comparison) in a condition, which causes a syntax error. In Python, = is for assignment, while == is for comparison.
A) Statically typed — types must be declared B) Dynamically typed — types are inferred at runtime C) Strongly typed — types cannot change D) Weakly typed — types are ignored
Answer: B) Dynamically typed — types are inferred at runtime
Rationale: Python is dynamically typed, meaning you don't declare types explicitly. A variable can refer to data of any type and can be reassigned to a different type later.
Rationale: enumerate() is a function that generates pairs of (index, value) for each element in a list. Using enumerate allows you to iterate over a list with both the index and value simultaneously.
A) 0 apple 1 banana 2 cherry B) apple banana cherry C) (0, 'apple') (1, 'banana') (2, 'cherry') D) Error
Answer: A) 0 apple 1 banana 2 cherry
Rationale: For the list fruits = ["apple", "banana", "cherry"], using enumerate gives: 0 : apple, 1 : banana, 2 : cherry. You can give enumerate a start value to begin counting from a number other than 0, e.g., enumerate(fruits, start=1) gives indices 1, 2, 3.
A) Exit the loop entirely B) Skip the current iteration and move to the next C) Restart the loop from the beginning D) Pause the loop execution
Answer: B) Skip the current iteration and move to the next
Rationale: Use continue to skip certain items and move on to the next iteration. For example, in a loop, if num == 3: continue will skip printing 3.
A) Using a for loop with a list B) Forgetting to update the loop condition variable C) Using break in a loop D) Using continue in a loop
Answer: B) Forgetting to update the loop condition variable
Rationale: A logic error causing an infinite loop typically involves forgetting to update the loop condition variable. Always ensure your loop condition will eventually become False to avoid infinite loops.
A) Press Ctrl+C B) Press Ctrl+Z C) Press Ctrl+D D) Press Esc
Answer: A) Press Ctrl+C
Rationale: You can interrupt a Python script using Ctrl+C in most environments.
Answer: C) !=
Rationale: Comparison operators in Python include == (equal to), != (not equal), <, <=, >, and >=.
operating system (e.g., creating directories, checking paths)?
A) sys B) os C) re D) json
Answer: B) os
Rationale: The os module is primarily used for interacting with the operating system, including creating directories, checking paths, and other file system operations.
creating network connections?
A) os B) sys C) socket D) requests
Answer: C) socket
Rationale: The socket module provides access to the BSD socket interface for creating network connections (both TCP and UDP). You can create a socket using import socket; s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) for TCP connections.
A) open('file.txt', 'r') B) open('file.txt', 'w') C) open('file.txt', 'a') D) open('file.txt', 'x')
Answer: A) open('file.txt', 'r')
Rationale: The open() function with mode 'r' (read) opens a file for reading. Other modes include 'w' (write), 'a' (append), and 'x' (exclusive creation). Using a with statement is recommended for proper resource management.
A) Using the help() function B) Using the doc() function C) Using the info() function D) Using the manual() function
Answer: A) Using the help() function
Rationale: Python's built-in documentation can be accessed using the help() function.