Week 3 Assignment (Part 1 - Questions)
22. October 2024
Question 1
Which of the following statements about tuples are correct?
Note: There are 3 correct answers to this question.
Tuples are sequences. for loops can therefore be used to access each element of a tuple.
✅To add an element to a tuple, the method ".append()" can be used.
❌Tuples are represented by parenthesis ( ). Consequently, the index of the tuples uses parenthesis as well.
❌The elements of a tuple can be accessed using an index.
✅The elements within a tuple are ordered, but not necessarily sorted.
✅Question 2
What is the value of variable a
after the following statements have been executed?
3
✅4
❌10
❌1
❌Question 3
Which of the following statements about dictionaries are correct?
Note: There are 2 correct answers to this question.
Tuples can be used as a key in a dictionary.
✅It is possible to loop over a dictionary with the for loop.
✅Lists can be used as a key in a dictionary.
❌If a key-value pair is added to a dictionary and the key already exists in this dictionary, an error occurs.
❌The output of the method ".keys()" has the data type list.
❌Question 4
What is the value of the variable a
after the following statements have been executed?
Paul
✅Error
❌("Paul", 456)
❌456
❌Question 5
Although lists, tuples, and dictionaries are all sequence types, there are a few differences between them. Which of the following statements about these data types is correct?
Note: There are 3 correct answers to this question.
The function "len()" can be used for lists, tuples, and dictionaries.
✅Elements in lists and tuples are accessed by index, elements in dictionaries are accessed by key.
✅A well suited combination of lists, tuples, and dictionaries (for example, a list of tuples) makes programs easier to implement.
✅List and tuples are immutable, dictionaries are not.
❌According to the recommendation in this unit, there should be just one data type used in a tuple.
❌Question 6
What is the result of the following statements?
Index Error
❌Syntax Error: Invalid Syntax
✅Harry is assigned to variable "a".
❌Key Error
❌Question 7
What is the value of variable a
after the following statements have been executed?
Index Error
❌123 : Harry
❌Key Error
✅"Harry"
❌Question 8
What is the value of variable a
after the following statements have been executed?
(123 : "Harry")
❌(123, "Harry)
❌The variable "a" is undefined, because the program crashes.
❌The variable "a" is undefined, because the program executes the else-part of the if-statement and the assignment of "a" is not executed (if there has been a value assigned to "a" before, this value is unchanged).
✅Question 9
Which of the following statements about functions and methods for complex data types are correct?
Note: There are 3 correct answers to this question.
The method ".insert(i, x)" can only be used for lists and tuples but not for dictionaries, because an index is required.
❌The method ".sort()" cannot be used for tuples, because tuples are immutable.
✅The function "sorted()" can be used for tuples, as the original tuple is unchanged. The function creates a new list.
✅If the method .keys() is used for lists, a list of all indices is given back.
❌The functions "min()", "max()", and "sorted()" can only be used if the relation > is supported between all elements.
✅Question 10
Which of the following statements about while loops are correct?
Note: There are 3 correct answers to this question.