1
What is the output of the following code?
str = 'Python'
print(str[1:4])
A. yth
B. pyt
C. ytho
D. tho
Correct Answer: yth
Explanation: String slicing includes the start index (1, which is 'y') and excludes the end index (4, which is 'o'). So it prints indices 1, 2, and 3.
2
Strings in Python are:
A. Mutable
B. Immutable
C. Dynamic arrays
D. None of the above
Correct Answer: Immutable
Explanation: Once a string is created in Python, its elements cannot be changed. Any operation that modifies it actually creates a new string.
3
What does the find() function return if the substring is not found?
A.
B. -1
C. IndexError
D. None
Correct Answer: -1
Explanation: The find() method returns the lowest index of the substring if found, otherwise it returns -1.
4
Which of the following constitutes a valid single-element tuple?
A. (5)
B. [5]
C. (5,)
D. {5}
Correct Answer: (5,)
Explanation: To define a tuple with a single element, a trailing comma is mandatory. (5) is evaluated as an integer, while (5,) is a tuple.
5
What is the result of len([1, 2, [3, 4], 5])?
Correct Answer: 4
Explanation: The list has 4 elements: 1, 2, the list [3, 4], and 5. The nested list counts as a single element.
6
What is the output of the following code?
a = [1, 2, 3]
b = a
b[0] = 5
print(a)
A. [1, 2, 3]
B. [5, 2, 3]
C. Error
D. [1, 2, 3, 5]
Correct Answer: [5, 2, 3]
Explanation: Lists are mutable and b = a creates an alias (reference), not a copy. Modifying b also modifies a.
7
Which operator is used to check if a key exists in a dictionary?
A. exists
B. has_key
C. in
D. within
Correct Answer: in
Explanation: The in operator is used to check membership of keys in a dictionary (e.g., 'key' in my_dict).
8
What happens when you try to modify a string like s[0] = 'J'?
A. The string is updated
B. A TypeError is raised
C. A new string is returned
D. The character is deleted
Correct Answer: A TypeError is raised
Explanation: Strings are immutable, so item assignment is not supported.
9
How do you safely access a value in a dictionary d with key k to avoid a KeyError if k is missing?
A. d[k]
B. d.get(k)
C. d.access(k)
D. d.value(k)
Correct Answer: d.get(k)
Explanation: The get() method returns the value for the key if it exists, or None (or a specified default) if the key does not exist.
10
Which of the following is a correct way to define a dictionary?
A. {1, 2, 3}
B. [1: 'a', 2: 'b']
C. {1: 'a', 2: 'b'}
D. (1: 'a', 2: 'b')
Correct Answer: {1: 'a', 2: 'b'}
Explanation: Dictionaries use curly braces {} and key-value pairs separated by colons.
11
What is the output of print('apple' < 'banana')?
A. True
B. False
C. Error
D. None
Correct Answer: True
Explanation: String comparison is lexicographical (alphabetical order). 'a' comes before 'b', so 'apple' is less than 'banana'.
12
What data type creates a sparse matrix most efficiently in Python?
A. List of Lists
B. Tuple
C. Dictionary
D. String
Correct Answer: Dictionary
Explanation: Dictionaries are efficient for sparse matrices because they only store non-zero values using coordinate tuples (row, col) as keys.
13
What is the result of [1, 2] * 3?
A. [1, 2, 3]
B. [3, 6]
C. [1, 2, 1, 2, 1, 2]
D. Error
Correct Answer: [1, 2, 1, 2, 1, 2]
Explanation: The * operator allows list repetition, repeating the elements 3 times.
14
Which method removes and returns the last element of a list?
A. remove()
B. delete()
C. pop()
D. discard()
Correct Answer: pop()
Explanation: pop() removes the element at the specified index (default is last) and returns it.
15
What is the output of list('hello')?
A. ['hello']
B. ['h', 'e', 'l', 'l', 'o']
C. Error
D. ('h', 'e', 'l', 'l', 'o')
Correct Answer: ['h', 'e', 'l', 'l', 'o']
Explanation: The list constructor iterates over the string and creates a list of individual characters.
16
Which of the following keys is NOT valid in a Python dictionary?
A. 'name'
B. 42
C. (1, 2)
D. [1, 2]
Correct Answer: [1, 2]
Explanation: Dictionary keys must be immutable (hashable). Lists are mutable and cannot be used as keys.
17
What does tuple assignment a, b = 10, 20 do?
A. Assigns 10 to a and 20 to b
B. Assigns 10 to b and 20 to a
C. Creates a tuple (10, 20)
D. Raises a SyntaxError
Correct Answer: Assigns 10 to a and 20 to b
Explanation: This is tuple unpacking, where values on the right are assigned to variables on the left.
18
How do you traverse a dictionary d to print keys and values?
A. for k, v in d.items(): print(k, v)
B. for k, v in d: print(k, v)
C. for k in d.values(): print(k)
D. for k, v in d.keys(): print(k, v)
Correct Answer: for k, v in d.items(): print(k, v)
Explanation: The items() method returns a view object that displays a list of a dictionary's key-value tuple pairs.
19
What is the slice t[::-1] used for on a tuple t?
A. Deleting the tuple
B. Reversing the tuple
C. Copying the tuple
D. Getting the last element
Correct Answer: Reversing the tuple
Explanation: A slice with a step of -1 creates a copy of the sequence in reverse order.
20
What is the output of 'a' in 'banana'?
Correct Answer: True
Explanation: The in operator checks if the substring 'a' is present in 'banana'.
21
If t = (1, 2, 3), what happens when you run del t[1]?
A. The tuple becomes (1, 3)
B. The tuple becomes (1, None, 3)
C. TypeError is raised
D. The tuple is deleted
Correct Answer: TypeError is raised
Explanation: Tuples are immutable; you cannot delete individual elements. You can only delete the entire tuple using del t.
22
Which function returns the number of items in a dictionary?
A. size()
B. count()
C. len()
D. length()
Correct Answer: len()
Explanation: len() is a built-in function that returns the number of items in a container (including dictionaries).
23
What is the difference between append() and extend() in lists?
A. append adds to end, extend adds to beginning
B. append adds one element, extend adds elements from an iterable
C. They are the same
D. extend creates a new list
Correct Answer: append adds one element, extend adds elements from an iterable
Explanation: append adds its argument as a single element. extend iterates over the argument and adds each element to the list.
24
Which code snippet correctly creates a copy of list a so that modifying the copy does not affect a (shallow copy)?
A. b = a
B. b = a[:]
C. b = copy(a)
D. b = a.duplicate()
Correct Answer: b = a[:]
Explanation: Slicing a list from start to end [:] creates a new list (shallow copy), breaking the alias.
25
What is the output of print('Hello'.lower())?
A. HELLO
B. hello
C. Hello
D. hELLO
Correct Answer: hello
Explanation: lower() converts all uppercase characters in a string to lowercase.
26
In a dictionary, values can be:
A. Only strings
B. Only immutable types
C. Only unique types
D. Any data type
Correct Answer: Any data type
Explanation: While keys must be immutable, dictionary values can be of any type (mutable or immutable) and can include duplicates.
27
What does the count() method do for a list?
A. Returns the number of elements
B. Returns the index of an element
C. Returns the number of occurrences of a specific value
D. Removes duplicates
Correct Answer: Returns the number of occurrences of a specific value
Explanation: list.count(x) returns the number of times x appears in the list.
28
Identify the compound data type.
A. int
B. float
C. string
D. bool
Correct Answer: string
Explanation: Strings are compound data types because they are made up of smaller pieces (characters) that can be accessed individually.
29
Given nums = [10, 20, 30], what does nums[-1] access?
A. 10
B. 20
C. 30
D. Error
Correct Answer: 30
Explanation: Negative indexing allows accessing elements from the end of the list. -1 refers to the last element.
30
Which statement is true regarding Tuples and Lists?
A. Tuples are mutable, Lists are immutable
B. Lists use (), Tuples use []
C. Tuples are generally faster than Lists
D. Lists cannot contain other lists
Correct Answer: Tuples are generally faster than Lists
Explanation: Due to their immutability, tuples are stored more efficiently in memory and are slightly faster for iteration than lists.
31
How do you remove a key 'k' from dictionary d?
A. d.remove('k')
B. del d['k']
C. d.delete('k')
D. remove(d, 'k')
Correct Answer: del d['k']
Explanation: The del statement is used to remove a specific key-value pair from a dictionary.
32
What is the output of max([1, 5, 2, 8])?
Correct Answer: 8
Explanation: The max() function returns the largest item in an iterable.
33
What is the index of 'o' in string 'Hello World'?
A. 4
B. 5
C. 4 and 7
D. 4 (only the first occurrence)
Correct Answer: 4 (only the first occurrence)
Explanation: Standard indexing returns a single value. If using index() or find(), it returns the index of the first occurrence.
34
How can a function return multiple values in Python?
A. It is not possible
B. By returning a list
C. By returning a tuple
D. Using the multi keyword
Correct Answer: By returning a tuple
Explanation: Python functions return multiple values by packing them into a tuple (e.g., return a, b).
35
What is the output of print('na' * 3)?
A. nanana
B. na3
C. Error
D. na na na
Correct Answer: nanana
Explanation: The multiplication operator with a string and an integer performs string repetition.
36
Given m = [[1,2], [3,4]], how do you access the value 3?
A. m[1][0]
B. m[0][1]
C. m[1][1]
D. m[2][1]
Correct Answer: m[1][0]
Explanation: m[1] accesses the second list [3,4], and index [0] accesses the first element of that list, which is 3.
37
Which method empties a list entirely?
A. erase()
B. delete()
C. clear()
D. empty()
Correct Answer: clear()
Explanation: The clear() method removes all items from the list.
38
If a = [1, 2, 3], what does a[1:2] = [4, 5] result in?
A. [1, 4, 5, 3]
B. [1, 4, 5, 2, 3]
C. [1, [4, 5], 3]
D. Error
Correct Answer: [1, 4, 5, 3]
Explanation: Slice assignment replaces the slice [2] (at index 1) with the elements of the assigned list [4, 5].
39
What is the correct syntax to sort a list L in place?
A. L.sort()
B. sorted(L)
C. sort(L)
D. L.order()
Correct Answer: L.sort()
Explanation: L.sort() modifies the list in-place. sorted(L) returns a new list but leaves the original unchanged.
40
Can a dictionary contain another dictionary as a value?
A. Yes
B. No
C. Only if keys are integers
D. Only if nested dict is empty
Correct Answer: Yes
Explanation: Dictionaries can be nested arbitrarily; values can be any data type, including other dictionaries.
41
What is dict.fromkeys([1, 2], 0)?
A. {1: 0, 2: 0}
B. {1: 1, 2: 2}
C. [(1, 0), (2, 0)]
D. Error
Correct Answer: {1: 0, 2: 0}
Explanation: fromkeys creates a new dictionary with keys from the sequence and values set to the second argument.
42
What is the result of tuple([1, 2])?
A. (1, 2)
B. [1, 2]
C. {1, 2}
D. ((1, 2))
Correct Answer: (1, 2)
Explanation: The tuple() constructor converts an iterable (like a list) into a tuple.
43
Strings support which of the following operations?
A. Item assignment
B. Item deletion
C. Concatenation
D. append()
Correct Answer: Concatenation
Explanation: Strings are immutable, so assignment and deletion are invalid. append is for lists. Concatenation (+) is supported.
44
When iterating through a dictionary using a for loop for x in my_dict:, what is x?
A. The value
B. The key
C. The key-value pair
D. The index
Correct Answer: The key
Explanation: Iterating over a dictionary directly yields its keys.
45
What happens if you try d = {[1,2]: 'value'}?
A. Dictionary created successfully
B. TypeError: unhashable type: 'list'
C. SyntaxError
D. Value becomes None
Correct Answer: TypeError: unhashable type: 'list'
Explanation: Keys must be hashable (immutable). Lists are mutable, so they cannot be used as dictionary keys.
46
What is the length of the string \n\t?
Correct Answer: 2
Explanation: The string contains two escape characters: a newline \n and a tab \t. Each counts as one character.
47
Which statement creates an empty dictionary?
A. {}
B. []
C. ()
D. empty_dict()
Correct Answer: {}
Explanation: {} is the literal syntax for an empty dictionary. dict() also works.
48
What is the output of a = [1, 2, 3]; del a[:]?
A. a becomes None
B. a becomes []
C. a is deleted from memory
D. Error
Correct Answer: a becomes []
Explanation: del a[:] deletes all elements in the slice representing the whole list, resulting in an empty list.
49
How do you perform a deep copy of a nested list?
A. list.copy()
B. slicing [:]
C. import copy; copy.deepcopy()
D. assignment =
Correct Answer: import copy; copy.deepcopy()
Explanation: Shallow copies (slicing, .copy()) still reference nested objects. copy.deepcopy() recursively copies all objects.
50
What is the purpose of the split() method in strings?
A. Breaks string into list of substrings
B. Joins list into string
C. Removes whitespace
D. Finds substring
Correct Answer: Breaks string into list of substrings
Explanation: split() divides a string based on a delimiter (default space) and returns a list of the substrings.