




























































































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!





























































































A) string B) number C) boolean D) float E) whole CORRECT ANSWER: D) float Python has several built-in data types including int, float, str, bool, and complex. While "string," "boolean," and "number" are conceptual terms, they are not the actual Python data type names. "Float" is the correct Python data type for decimal numbers. "Whole" is not a valid Python data type.
3. What will be the output of the following code? x = 5; y = 2; print(x ** y) A) 7 B) 10 C) 25 D) 32 E) 2. CORRECT ANSWER: C) 25 The ** operator in Python is the exponentiation operator. It raises the first number to the power of the second. Therefore, 5 ** 2 = 5 × 5 = 25. Option A is addition, B is multiplication, D would be 2^5, and E is division. 4. Which statement correctly uses the modulo operator in Python? A) result = 10 % 3 B) result = 10 & 3
CORRECT ANSWER: B) int("123") The int() function is the correct built-in Python function for converting a string to an integer. Option A is JavaScript syntax, C and E are not valid Python functions, and D uses Java syntax with a capital I.
7. What will be printed by the following code? x = "Hello"; print(x[1]) A) H B) e C) l D) o E) Hello CORRECT ANSWER: B) e In Python, strings are indexed starting at 0. So index [0] is 'H', index [1] is 'e', index [2] is 'l', and so on. The code accesses the character at index 1, which is 'e'. 8. Which operator is used for string concatenation in Python? A) & B) + C). D) , E) | CORRECT ANSWER: B) + The plus sign (+) is used to concatenate strings in Python. For example, "Hello" + "World" results in "HelloWorld". The other operators are used for different purposes: & is bitwise AND,. is for attribute access, , is for separating values, and | is bitwise OR.
9. What is the correct way to create a list in Python? A) my_list = [1, 2, 3] B) my_list = (1, 2, 3) C) my_list = {1, 2, 3} D) my_list = <1, 2, 3> E) my_list = {1: 2, 3} CORRECT ANSWER: A) my_list = [1, 2, 3] Lists in Python are created using square brackets []. Parentheses () create tuples, curly braces {} create sets or dictionaries, and angle brackets <> are not valid for creating collections in Python. 10. How do you access the third element of a list in Python? A) my_list[2] B) my_list[3] C) my_list(3) D) my_list.get(3) E) my_list{3} CORRECT ANSWER: A) my_list[2] Python uses zero-based indexing, so the third element is at index 2. Option B would access the fourth element, C uses incorrect syntax, D is a dictionary method, and E uses dictionary access syntax. 11. What will be the output of print(len("Python"))? A) 5
E) Prints numbers 0 through 5 CORRECT ANSWER: A) Prints numbers 0 through 4 The range(5) function generates numbers from 0 to 4 (not including 5). So this loop prints 0, 1, 2, 3, 4. Option B would require range(1, 6), and E is incorrect because range(5) doesn't include 5.
14. How do you define a function in Python? A) function my_func(): pass B) def my_func(): C) define my_func(): D) func my_func(): E) create my_func(): CORRECT ANSWER: B) def my_func(): The def keyword is used to define functions in Python. The other options show syntax from other languages or use incorrect keywords. Option A is JavaScript, C and E are not valid Python syntax, and D is incorrect. 15. What will be returned by the following function? def add(a, b): return a + b A) The values of a and b B) The sum of a and b C) The string "a + b" D) Nothing E) An error CORRECT ANSWER: B) The sum of a and b
The function returns the result of adding a and b together. The return statement sends this value back to the caller. Option A describes the parameters, C would be a string concatenation, D is wrong because return sends a value, and E is incorrect because the function is valid.
16. Which statement correctly calls a function in Python? A) add(3, 4) B) add 3, 4 C) add[3, 4] D) add{3, 4} E) call add(3, 4) CORRECT ANSWER: A) add(3, 4) Functions are called using parentheses with arguments inside. Options B, C, and D use incorrect syntax, and E unnecessarily uses the word "call." 17. What is a parameter in a function? A) The value passed to a function when it is called B) A variable defined in the function definition C) The return value of the function D) The name of the function E) A comment explaining the function CORRECT ANSWER: B) A variable defined in the function definition A parameter is a variable listed in the function definition that receives values when the function is called. Arguments are the actual values passed to the function. Option A describes an argument, C describes a return value, D is the function name, and E is incorrect.
C) To pause the loop D) To restart the loop E) To convert the loop to a function CORRECT ANSWER: B) To exit the loop immediately The break statement terminates the loop and control passes to the statement following the loop. Option A describes continue, C and D don't accurately describe break, and E is incorrect.
21. What does the continue statement do in a loop? A) Exits the loop completely B) Skips the current iteration and goes to the next C) Pauses the loop execution D) Returns a value from the loop E) Prints a message CORRECT ANSWER: B) Skips the current iteration and goes to the next The continue statement causes the loop to skip the rest of the current iteration and proceed to the next iteration. Option A describes break, C isn't accurate, D requires return, and E doesn't describe continue. 22. How do you access a value from a dictionary in Python? A) my_dict[key] B) my_dict.get(key) C) my_dict[key] D) Both A and B E) my_dict->key
CORRECT ANSWER: D) Both A and B Both my_dict[key] and my_dict.get(key) are valid ways to access dictionary values. The bracket notation raises KeyError if the key doesn't exist, while .get() returns None by default. Option E uses arrow notation which is not valid in Python.
23. What is a tuple in Python? A) An ordered, mutable collection B) An unordered, immutable collection C) An ordered, immutable collection D) A single element in a list E) A type of string CORRECT ANSWER: C) An ordered, immutable collection Tuples are sequences of elements that cannot be changed after creation. They maintain order (like lists) but are immutable. Option A describes lists, B describes sets, D is incorrect, and E is wrong. 24. How do you create a tuple in Python? A) my_tuple = [1, 2, 3] B) my_tuple = (1, 2, 3) C) my_tuple = {1, 2, 3} D) my_tuple = <1, 2, 3> E) my_tuple = {1: 2, 3: 4} CORRECT ANSWER: B) my_tuple = (1, 2, 3) Tuples are created using parentheses. Option A creates a list, C creates a set, D is invalid syntax, and E creates a dictionary.
B) my_list.remove(2) C) my_list.pop(2) D) Both B and C E) my_list.erase(2) CORRECT ANSWER: D) Both B and C Both remove() and pop() can remove elements. remove() removes by value, pop() removes by index. delete() and erase() are not Python list methods.
28. What does the pop() method do? A) Removes the first element B) Removes the last element and returns it C) Removes an element at a specific index and returns it D) Removes all elements E) Adds an element to the list CORRECT ANSWER: C) Removes an element at a specific index and returns it The pop() method removes and returns an element at a specified index. If no index is provided, it removes the last element. Option B is partially correct but incomplete, A is wrong, D and E don't describe pop. 29. How do you sort a list in Python? A) my_list.sort() B) sorted(my_list) C) my_list.arrange() D) Both A and B E) sort(my_list)
CORRECT ANSWER: D) Both A and B Both my_list.sort() (sorts in-place) and sorted(my_list) (returns a new sorted list) work. Option C is not a valid method, E is not valid Python syntax.
30. What is a set in Python? A) An ordered collection of unique elements B) An unordered collection of unique elements C) An ordered collection of elements that can be duplicated D) A collection that stores key-value pairs E) A type of list CORRECT ANSWER: B) An unordered collection of unique elements Sets are unordered and automatically eliminate duplicates. Option A is wrong about unordered nature, C is wrong about duplicates, D describes dictionaries, and E describes lists. 31. How do you create a set in Python? A) my_set = [1, 2, 3] B) my_set = (1, 2, 3) C) my_set = {1, 2, 3} D) my_set = <1, 2, 3> E) my_set = "123" CORRECT ANSWER: C) my_set = {1, 2, 3} Sets are created using curly braces {}. Option A creates a list, B creates a tuple, D is invalid, and E creates a string.
D) my_dict->key E) my_dict.get("key") CORRECT ANSWER: B) my_dict["key"] Dictionary values are accessed using square bracket notation with the key. Option E (using .get()) is also valid but B is the most direct answer. A uses dot notation which doesn't work for all keys, C uses function call syntax, and D uses arrow notation.
35. What will be the output? my_dict = {"a": 1, "b": 2}; print(my_dict["a"]) A) a B) 1 C) "a": 1 D) {"a": 1} E) Key not found CORRECT ANSWER: B) 1 Accessing my_dict["a"] returns the value associated with the key "a", which is 1. Option A returns the key, C returns the key-value pair as a string, D returns the entire dictionary, and E is incorrect. 36. What does the .keys() method do in a dictionary? A) Returns all values B) Returns all keys C) Returns key-value pairs D) Deletes all keys E) Adds a new key CORRECT ANSWER: B) Returns all keys
The .keys() method returns a view of all dictionary keys. Option A describes .values(), C describes .items(), D is incorrect, and E describes adding a key.
37. What does the .values() method do in a dictionary? A) Returns all keys B) Returns all values C) Returns key-value pairs D) Checks if a value exists E) Removes all values CORRECT ANSWER: B) Returns all values The .values() method returns a view of all dictionary values. Option A describes .keys(), C describes .items(), and D and E are incorrect operations. 38. What does the .items() method do in a dictionary? A) Returns all keys B) Returns all values C) Returns all key-value pairs D) Returns the number of items E) Returns the first item CORRECT ANSWER: C) Returns all key-value pairs The .items() method returns a view of all key-value pairs. Options A and B describe .keys() and .values(), D would be len(), and E is incorrect. 39. How do you add a new key-value pair to a dictionary? A) my_dict.add("key", "value")
CORRECT ANSWER: A) True The in operator checks if 3 exists in the list, which it does, so it returns True. Option B would be correct if 3 wasn't in the list, C returns the element itself, D is incorrect, and E returns the entire list.
42. What is string slicing in Python? A) Removing characters from a string B) Extracting a portion of a string using indices C) Combining strings together D) Converting a string to lowercase E) Splitting a string into words CORRECT ANSWER: B) Extracting a portion of a string using indices String slicing allows you to extract a substring by specifying start and end indices. Option A is string manipulation but not slicing, C is concatenation, D is a string method, and E is the split() method. 43. What will be the output? text = "Python"; print(text[0:3]) A) Python B) Py C) Pyt D) yth E) on CORRECT ANSWER: C) Pyt Slicing with [0:3] extracts characters from index 0 up to (but not including) index 3. That's indices 0, 1, and 2, which are 'P', 'y', 't'. Option B only goes to index 1, D starts at index 1, and E is at the end.
44. What does the .split() method do? A) Removes characters from a string B) Divides a string into a list based on a separator C) Joins a list into a string D) Finds characters in a string E) Reverses a string CORRECT ANSWER: B) Divides a string into a list based on a separator The .split() method breaks a string into substrings based on a specified separator (default is space). Option A describes removal, C describes .join(), D describes .find(), and E describes string reversal. 45. What will be the output? text = "Hello World"; print(text.split()) A) "Hello World" B) ['Hello', 'World'] C) ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd'] D) Hello World E) 2 CORRECT ANSWER: B) ['Hello', 'World'] The split() method without arguments splits on whitespace, creating a list of words. Option A is the original string, C splits into individual characters, D shows the string without quotes, and E counts items. 46. What does the .join() method do? A) Splits a string into parts