CCNA Functions Data Structures Questions

9 of 84 questions · Page 2/2 · Functions Data Structures topic · Answers revealed

76
MCQmedium

Given the tuple t = (1, 2, 3, 4, 5), which expression returns the last element?

A.t[-1]
B.t[5]
C.t[4]
D.t[0]
AnswerA

Negative index -1 refers to the last element.

Why this answer

In Python, negative indices count from the end of a sequence. For the tuple t = (1, 2, 3, 4, 5), t[-1] accesses the last element (5), because -1 refers to the final position. This is a standard feature of Python's sequence indexing.

Exam trap

Python Institute often tests the distinction between zero-based indexing and negative indexing, trapping candidates who mistakenly think t[5] or t[4] is the correct way to access the last element without considering index out-of-range or dynamic length scenarios.

How to eliminate wrong answers

Option B is wrong because t[5] attempts to access index 5, which is out of range for a tuple with indices 0 through 4, raising an IndexError. Option C is wrong because t[4] returns the element at index 4, which is 5, but this is the last element only coincidentally; the question asks for an expression that returns the last element in general, and t[4] is not a robust way to do it if the tuple length changes. Option D is wrong because t[0] returns the first element (1), not the last.

77
MCQhard

Refer to the exhibit. What is the output?

A.[1] [2] [3]
B.Error: default argument is mutable
C.[1] [1, 2] [1, 2, 3]
D.[1] [2] [1, 3]
AnswerD

Correct; default list persists across calls.

Why this answer

First call uses default list, returns [1]. Second call passes a new empty list, so returns [2]. Third call uses the same default list (now [1]), so returns [1, 3].

78
Multi-Selectmedium

Which TWO of the following are valid ways to merge two dictionaries in Python 3.5+? (Assume dict1 = {'a':1} and dict2 = {'b':2})

Select 2 answers
A.dict1 | dict2
B.dict1.update(dict2)
C.dict1 + dict2
D.dict1.merge(dict2)
E.{**dict1, **dict2}
AnswersB, E

The update method merges dict2 into dict1, modifying dict1 in place.

Why this answer

The valid ways are A and B. A (update) modifies dict1 in place. B (unpacking) creates a new dictionary.

C is not valid because dictionaries do not support +. D is not a built-in method. E (the | operator) is new in Python 3.9 and may not be available in older versions, so it is not considered a general solution.

79
Multi-Selecteasy

Which TWO of the following exceptions are built-in Python exceptions? (Select exactly 2)

Select 2 answers
A.ZeroDivisionError
B.KeyError
C.StringError
D.NumberError
E.ListError
AnswersA, B

Built-in exception for division by zero.

Why this answer

Option A is correct because ZeroDivisionError is a built-in Python exception that is raised when the second operand of a division or modulo operation is zero. It is part of Python's standard exception hierarchy and is commonly encountered in arithmetic operations.

Exam trap

Python Institute often tests the distinction between built-in exceptions and non-existent exceptions like StringError, NumberError, or ListError, which are not part of Python's standard library, to catch candidates who guess based on naming patterns rather than actual Python knowledge.

80
MCQmedium

Refer to the exhibit. What is the output?

A.1 2 [3, 4] {'x': 5, 'y': 6}
B.1 2 (3, 4) {}
C.1 2 3 4 5 6
D.1 2 (3, 4) {'x': 5, 'y': 6}
AnswerD

Correct; args are in a tuple, kwargs in a dict.

Why this answer

a=1, b=2, args receives (3,4) as a tuple, kwargs receives {'x':5, 'y':6} as a dict.

81
MCQhard

Refer to the exhibit. What is the output of Line C?

A.TypeError
B.30
C.33
D.10
AnswerC

Correct. 1+2+10+20 = 33.

Why this answer

The function func has parameters a, b (required) and c, d (optional with defaults 10 and 20). Line C calls func with only two arguments (1 and 2), so c and d take their default values. The result is 1+2+10+20 = 33.

82
MCQeasy

A dictionary student = {'name': 'John', 'age': 20}. To safely get the grade with a default of 'N/A', which code should be used?

A.student.get('grade', 'N/A')
B.student['grade']
C.student.fetch('grade', 'N/A')
D.student['grade'] or 'N/A'
AnswerA

Correct: get returns 'N/A' if 'grade' is not present.

Why this answer

Option A is correct because the `get()` method of a dictionary safely retrieves the value for a given key, returning a default value (here `'N/A'`) if the key does not exist. This avoids raising a `KeyError` when the key `'grade'` is missing from the dictionary.

Exam trap

Python Institute often tests the distinction between safe dictionary access methods (`get()`) and direct indexing (`[]`), trapping candidates who think the `or` operator can short-circuit a `KeyError` or who invent non-existent methods like `fetch()`.

How to eliminate wrong answers

Option B is wrong because using `student['grade']` directly raises a `KeyError` if the key `'grade'` does not exist, which is not safe. Option C is wrong because dictionaries have no `fetch()` method; this is not a valid Python dictionary operation. Option D is wrong because `student['grade'] or 'N/A'` still evaluates `student['grade']` first, which raises a `KeyError` if the key is missing, and the `or` expression never executes.

83
MCQeasy

A developer needs to determine the number of elements in a tuple named 't'. Which code snippet will correctly return the length?

A.t.append(5)
B.t[0] = 5
C.len(t)
D.t.pop()
AnswerC

The built-in len() function returns the number of elements in a tuple.

Why this answer

Option B (len(t)) is the correct way to get the length of a tuple. Option A attempts to assign a value to a tuple element, which is not allowed because tuples are immutable. Option C uses append(), which is a list method, not a tuple method.

Option D uses pop(), which also works only on lists.

84
MCQmedium

Consider the following code: def foo(x, y): return x * y result = foo(y=2, 3) What is the error?

A.The function requires at least one argument but none provided.
B.A positional argument follows a keyword argument.
C.Keyword arguments cannot be used in function calls.
D.The function definition has too many parameters.
AnswerB

In Python, positional arguments must come before any keyword arguments.

Why this answer

Option B is correct because in Python, when calling a function, all positional arguments must appear before any keyword arguments. The call `foo(y=2, 3)` violates this rule by placing a positional argument (`3`) after a keyword argument (`y=2`), which raises a SyntaxError. This is enforced by Python's parser to avoid ambiguity in argument binding.

Exam trap

Cisco often tests the rule that positional arguments must precede keyword arguments in a function call, and the trap here is that candidates may mistakenly think the error is about missing arguments or invalid keyword usage, rather than the ordering violation.

How to eliminate wrong answers

Option A is wrong because the function call does provide arguments (a keyword argument `y=2` and a positional argument `3`), so the error is not about missing arguments. Option C is wrong because keyword arguments are fully supported in Python function calls and are commonly used for clarity and optional parameters. Option D is wrong because the function definition `def foo(x, y)` has exactly two parameters, which matches the number of arguments provided; the error is in the call syntax, not the definition.

← PreviousPage 2 of 2 · 84 questions total

Ready to test yourself?

Try a timed practice session using only Functions Data Structures questions.