You are on page 1of 5

1. What is a list in Python and how is it represented?

A. A built-in function in Python represented by parentheses ( )


B. A data structure in Python represented by square brackets [ ]
C. A data structure in Python represented by parentheses { }

2. What is the syntax for creating a list in Python?


A. A list can be created in Python by enclosing a comma-separated sequence of items inside
parentheses.
B. A list can be created in Python by enclosing a comma-separated sequence of items inside
square brackets.
C. A list can be created in Python by enclosing a comma-separated sequence of items inside
curly braces.

3. What does it mean when we say that lists are ordered in Python?
A. It means that the items in a list have a defined sequence and will not change.
B. It means that the items in a list can be placed in any order.
C. It means that the items in a list are sorted alphabetically.

4. What does it mean when we say that lists are changeable in Python?
A. It means that we cannot modify the items in a list after it has been created.
B. It means that we can only modify the items at the beginning or end of the list.
C. It means that we can add, remove, or modify the items in a list after it has been created.

5. Which of the following is not a way to change a Python list?


A. Adding new items
B. Removing existing items
C. Sorting the items

6. How do you create an empty list in Python?


A. myList = [ ]
B. myList = { }
C. myList = set()

7. Can lists have duplicate values in Python?


A. No, lists cannot have duplicate values in Python.
B. Yes, lists can have duplicate values in Python.
C. Only certain types of lists can have duplicate values in Python.

8. What is the result of this code: thislist = ["apple", "banana", "cherry", "apple", "cherry"]?
A. An error because of the duplicate values
B. A tuple with the values ("apple", "banana", "cherry")
C. A list with the values ["apple", "banana", "cherry", "apple", "cherry"]

9. What is the indexing in Python list and what is the index of the first item in a list?
A. Indexing refers to the arrangement of items in a list, and the index of the first item is 1.
B. Indexing refers to assigning a position number to each element in the list, and the index
of the first item is 0.
C. Indexing refers to the ability to sort a list, and the index of the first item depends on the
sorting algorithm used.

10. How do you access an element in a Python list by its index?


A. using the append() method
B. using square brackets [ ] and the index number
C. using parentheses ( ) and the index number

11. What happens when new items are added to a Python list?
A. The new items will replace existing items in the list
B. The new items will be placed at the end of the list
C. The new items will be placed randomly in the list
12. Is it possible to change the order of items in a list in Python?
A. No, it is not possible to change the order of items in a list in Python.
B. Yes, it is possible to change the order of items in a list in Python using certain list
methods.
C. It depends on the size of the list whether the order can be changed.

13. Which of the following statements is true about accessing elements in a list in Python?
A. You can use negative indices to access elements from the end of the list.
B. You cannot access elements in a list using their indices.
C. You can only access elements in a list using loops.

14. What is the output of the following code snippet?


my_list = ['apple', 'banana', 'cherry']
print(my_list[3])

A. IndexError: list index out of range


B. TypeError: list indices must be integers or slices, not float
C. cherry

15. What is the purpose of the append() method in Python lists?


A. Removes an element from the list
B. Adds an element to the beginning of the list
C. Adds an element to the end of the list

16. Which method is used to add all elements of one list to another list in Python?
A. extend()
B. insert()
C. remove()

17. What does the insert() method do in Python lists?


A. Adds an element to the end of the list
B. Adds an element to the beginning of the list
C. Inserts an item at the defined index

For items 18-20, refer to the codes below:

myList = list()
listLength = int(input("Enter list length: "))

for i in range(listLength):
x = i + 1
n = int(input("Enter element %d: " % x))
myList.append(n)

if len(myList) != len(set(myList)):
print("Has duplicate?: True")
else:
print("Has duplicate?: False")

18. What is the purpose of the list() function call in the first line of the code?
A. It creates an empty list.
B. It converts a string to a list.
C. It sorts a list.

19. What does the for loop do in this code?


A. It prints the elements of the list.
B. It checks if the list has duplicate elements.
C. It adds elements to the list.
20. Which of the following statements is NOT true about Python tuples?
A. Tuples are mutable
B. Tuples are ordered
C. Tuples can contain elements of different data types

21. What is the output of this code if the user enters the following input: Enter list length: 5
and then enters the following elements for the list: 1, 2, 2, 3, 4?
A. Has duplicate?: True
B. Has duplicate?: False
C. It will throw an exception

22. Which of the following is the correct syntax to create a tuple?


A. (1, 2, 3)
B. [1, 2, 3]
C. {1, 2, 3}

23. What is the difference between a tuple and a list?


A. Tuples are mutable, lists are immutable
B. Tuples can contain elements of different data types, lists cannot
C. Tuples use parentheses for syntax, lists use square brackets

24. Which of the following is the correct syntax to access the second element of a tuple?
A. tuple[0]
B. tuple[1]
C. tuple[-1]

25. What happens when you try to modify an element in a tuple?


A. The modification is successful
B. A TypeError is raised
C. The element is deleted from the tuple

26. Which of the following statements is true about tuple comprehension in Python?
A. Tuple comprehension is not supported in Python
B. Tuple comprehension syntax is similar to list comprehension syntax in Python
C. Tuple comprehension can only be used with tuples that contain numeric elements in
Python

27. Which of the following statements is true about concatenating tuples in Python?
A. Tuples cannot be concatenated in Python
B. Tuples can only be concatenated if they have the same number of elements in Python
C. Tuples can be concatenated using the "+" operator in Python

28. Which of the following statements is true about unpacking tuples in Python?
A. Only the first element of a tuple can be unpacked in Python
B. Tuples can be unpacked into multiple variables in Python
C. Unpacking tuples in Python can only be done using negative indices

29. Which of the following statements is true about iterating over a tuple in Python?
A. Tuples can be iterated over using a for loop in Python
B. Tuples can only be iterated over using a while loop in Python
C. Iterating over a tuple in Python requires converting it to a list first

30. Which method is used to get the number of items in a tuple in Python?
A. count()
B. index()
C. len()
For items 31-33, refer to the codes below:

tuple = (5, 10, 15, 20, 25)


(num1, num2, num3, num4, num5) = tuple
sum = num1 + num2 + num3
product = num4 * num5
print(f"Sum of first three elements: {sum}")
print(f"Product of last two elements: {product}")

31. What is the output of the code?


A. Sum of first three elements: 30 Product of last two elements: 500
B. Sum of first three elements: 50 Product of last two elements: 500
C. Sum of first three elements: 30 Product of last two elements: 100

32. What is the purpose of the following line of code:


(num1, num2, num3, num4, num5) = tuple
A. To convert the tuple into a list
B. To store the elements of the tuple in separate variables
C. To sort the elements of the tuple in ascending order

33. What is the value of 'num4' in the code?


A. 5
B. 10
C. 20

For items 34-36, refer to the codes below:

nested_tuple = ((5, 1), (9, 10), (11, 22))


row2 = nested_tuple[1]
(num1, num2) = row2
print(f"Product: {num1*num2}")

34. What is the data type of the variable 'row2'?


A. List
B. Tuple
C. Set

35. What is the purpose of the following line of code:


(num1, num2) = row2
A. To convert the nested tuple into a list
B. To store the elements of the second row in separate variables
C. To sort the elements of the second row in ascending order

36. What is the output of the code?


A. Product: 9
B. Product: 10
C. Product: 90

37. Which of the following is the correct syntax to create an empty set in Python?
A. s = set()
B. s = []
C. s = ()
38. What is the result of the following code?
s1 = {1, 2, 3}
s2 = {3, 4, 5}
s3 = s1.intersection(s2)
print(s3)

A. {1, 2, 3, 4, 5}
B. {1, 2}
C. {3}

39. What is the output of the following code?


s1 = {1, 2, 3}
s2 = {3, 4, 5}
s3 = s1.union(s2)
print(s3)

A. {1, 2, 3, 4, 5}
B. {1, 2, 3}
C. {3, 4, 5}

40. What is the output of the following Python code?


thisSet = {"apple", "banana", "cherry"}
for x in thisSet:
print(x)

A. ["apple", "banana", "cherry"]


B. "apple" \n "banana" \n "cherry"
C. {"apple", "banana", "cherry"}

You might also like