Unit 4: String; Lists; Tuples and Dictionaries - Practice Quiz

INT108 — Python Programming 50 Questions
0 Correct 0 Wrong 50 Left
0/50

1 What is the output of the following code?
str = 'Python'
print(str[1:4])

A. tho
B. ytho
C. pyt
D. yth

2 Strings in Python are:

A. Immutable
B. Mutable
C. Dynamic arrays
D. None of the above

3 What does the find() function return if the substring is not found?

A. IndexError
B. -1
C. None
D. 0

4 Which of the following constitutes a valid single-element tuple?

A. (5,)
B. (5)
C. {5}
D. [5]

5 What is the result of len([1, 2, [3, 4], 5])?

A. 4
B. 5
C. 3
D. 6

6 What is the output of the following code?
a = [1, 2, 3]
b = a
b[0] = 5
print(a)

A. [1, 2, 3, 5]
B. Error
C. [1, 2, 3]
D. [5, 2, 3]

7 Which operator is used to check if a key exists in a dictionary?

A. has_key
B. in
C. exists
D. within

8 What happens when you try to modify a string like s[0] = 'J'?

A. A TypeError is raised
B. A new string is returned
C. The string is updated
D. The character is deleted

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.value(k)
B. d[k]
C. d.access(k)
D. d.get(k)

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'}

11 What is the output of print('apple' < 'banana')?

A. False
B. None
C. True
D. Error

12 What data type creates a sparse matrix most efficiently in Python?

A. Dictionary
B. List of Lists
C. Tuple
D. String

13 What is the result of [1, 2] * 3?

A. [1, 2, 3]
B. [1, 2, 1, 2, 1, 2]
C. [3, 6]
D. Error

14 Which method removes and returns the last element of a list?

A. discard()
B. delete()
C. pop()
D. remove()

15 What is the output of list('hello')?

A. ['hello']
B. Error
C. ['h', 'e', 'l', 'l', 'o']
D. ('h', 'e', 'l', 'l', 'o')

16 Which of the following keys is NOT valid in a Python dictionary?

A. 42
B. 'name'
C. (1, 2)
D. [1, 2]

17 What does tuple assignment a, b = 10, 20 do?

A. Creates a tuple (10, 20)
B. Raises a SyntaxError
C. Assigns 10 to a and 20 to b
D. Assigns 10 to b and 20 to a

18 How do you traverse a dictionary d to print keys and values?

A. for k, v in d: print(k, v)
B. for k, v in d.keys(): print(k, v)
C. for k, v in d.items(): print(k, v)
D. for k in d.values(): print(k)

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

20 What is the output of 'a' in 'banana'?

A. True
B. False
C. 1
D. 0

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

22 Which function returns the number of items in a dictionary?

A. count()
B. size()
C. len()
D. length()

23 What is the difference between append() and extend() in lists?

A. extend creates a new list
B. append adds to end, extend adds to beginning
C. They are the same
D. append adds one element, extend adds elements from an iterable

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 = a.duplicate()
D. b = copy(a)

25 What is the output of print('Hello'.lower())?

A. hello
B. Hello
C. HELLO
D. hELLO

26 In a dictionary, values can be:

A. Only unique types
B. Only immutable types
C. Any data type
D. Only strings

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

28 Identify the compound data type.

A. int
B. bool
C. string
D. float

29 Given nums = [10, 20, 30], what does nums[-1] access?

A. 30
B. 20
C. Error
D. 10

30 Which statement is true regarding Tuples and Lists?

A. Lists cannot contain other lists
B. Tuples are generally faster than Lists
C. Lists use (), Tuples use []
D. Tuples are mutable, Lists are immutable

31 How do you remove a key 'k' from dictionary d?

A. d.delete('k')
B. d.remove('k')
C. del d['k']
D. remove(d, 'k')

32 What is the output of max([1, 5, 2, 8])?

A. 1
B. 8
C. 5
D. Error

33 What is the index of 'o' in string 'Hello World'?

A. 4 (only the first occurrence)
B. 4
C. 5
D. 4 and 7

34 How can a function return multiple values in Python?

A. It is not possible
B. By returning a tuple
C. By returning a list
D. Using the multi keyword

35 What is the output of print('na' * 3)?

A. na3
B. Error
C. nanana
D. na na na

36 Given m = [[1,2], [3,4]], how do you access the value 3?

A. m[1][0]
B. m[0][1]
C. m[2][1]
D. m[1][1]

37 Which method empties a list entirely?

A. empty()
B. delete()
C. clear()
D. erase()

38 If a = [1, 2, 3], what does a[1:2] = [4, 5] result in?

A. [1, 4, 5, 2, 3]
B. Error
C. [1, [4, 5], 3]
D. [1, 4, 5, 3]

39 What is the correct syntax to sort a list L in place?

A. L.order()
B. L.sort()
C. sorted(L)
D. sort(L)

40 Can a dictionary contain another dictionary as a value?

A. No
B. Yes
C. Only if nested dict is empty
D. Only if keys are integers

41 What is dict.fromkeys([1, 2], 0)?

A. [(1, 0), (2, 0)]
B. Error
C. {1: 1, 2: 2}
D. {1: 0, 2: 0}

42 What is the result of tuple([1, 2])?

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

43 Strings support which of the following operations?

A. append()
B. Item assignment
C. Concatenation
D. Item deletion

44 When iterating through a dictionary using a for loop for x in my_dict:, what is x?

A. The key-value pair
B. The value
C. The key
D. The index

45 What happens if you try d = {[1,2]: 'value'}?

A. SyntaxError
B. Value becomes None
C. Dictionary created successfully
D. TypeError: unhashable type: 'list'

46 What is the length of the string \n\t?

A. 1
B. 4
C. 0
D. 2

47 Which statement creates an empty dictionary?

A. {}
B. []
C. ()
D. empty_dict()

48 What is the output of a = [1, 2, 3]; del a[:]?

A. a becomes []
B. a is deleted from memory
C. a becomes None
D. Error

49 How do you perform a deep copy of a nested list?

A. import copy; copy.deepcopy()
B. assignment =
C. slicing [:]
D. list.copy()

50 What is the purpose of the split() method in strings?

A. Joins list into string
B. Finds substring
C. Removes whitespace
D. Breaks string into list of substrings