Courseiva

CCNA Functions, Tuples, Dictionaries and Exceptions Questions

7 of 82 questions · Page 2/2 · Functions, Tuples, Dictionaries and Exceptions · Answers revealed

76
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

The function uses a mutable default argument `lst=[]`. On the first call, `append_to_list(1)` uses the default list and appends 1, returning `[1]`. On the second call, `append_to_list(2, [])` passes an explicit empty list, so it appends 2 to that new list, returning `[2]`.

On the third call, `append_to_list(3)` uses the default list again, which now contains `[1]` from the first call, so appending 3 yields `[1, 3]`. Thus the output is `[1] [2] [1,3]` printed on separate lines.

Exam trap

The PCEP exam often tests the mutable default argument trap, where candidates mistakenly believe each function call creates a fresh default list, rather than understanding that the default object is created once and reused, leading to cumulative modifications across calls.

How to eliminate wrong answers

Option A is wrong because it assumes each call creates a new list, ignoring Python's mutable default argument behavior where the same list object is reused across calls without an argument. Option B is wrong because Python does not raise an error for mutable default arguments; it is a common pitfall but syntactically and semantically valid. Option C is wrong because it incorrectly suggests the second call returns `[2]` instead of `[1, 2]`, misunderstanding that the default list persists and accumulates values from previous calls.

77
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

`dict1.update(dict2)` merges `dict2` into `dict1` in-place, updating existing keys and adding new ones. Option E is correct because `{**dict1, **dict2}` uses dictionary unpacking to create a new merged dictionary, available since Python 3.5.

Exam trap

The PCEP exam often tests the version-specific availability of operators like `|` and the existence of methods like `merge()`, exploiting candidates who assume all modern syntax works in older Python versions or that methods from other languages (e.g., JavaScript's `Object.assign`) exist in Python.

78
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

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.

79
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

The code uses a tuple (3, 4) and a dictionary {'x': 5, 'y': 6} as arguments to the print() function. The print() function outputs each argument separated by a space, so the tuple is printed as (3, 4) and the dictionary as {'x': 5, 'y': 6}, preceded by 1 and 2. Option D correctly shows this output.

Exam trap

The PCEP exam often tests the distinction between data types in output, and the trap here is that candidates mistakenly think print() unpacks or flattens compound objects like tuples and dictionaries into individual elements, rather than printing them as single objects.

How to eliminate wrong answers

Option A is wrong because it incorrectly shows the tuple as a list [3, 4] and the dictionary as {'x': 5, 'y': 6} without the tuple parentheses, misrepresenting the data types. Option B is wrong because it shows an empty dictionary {} instead of the actual dictionary {'x': 5, 'y': 6}, omitting the key-value pairs. Option C is wrong because it flattens the tuple and dictionary into individual integers (3, 4, 5, 6), which does not happen since print() outputs the tuple and dictionary as single objects, not unpacking them.

80
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

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.

81
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

The correct way to determine the number of elements in a tuple in Python is to use the built-in `len()` function, which returns the length (number of items) of any sequence or collection, including tuples. Option C is correct because `len(t)` directly gives the count of elements in tuple `t`.

Exam trap

The PCEP exam often tests the immutability of tuples by presenting list-specific methods (like `append`, `pop`, or item assignment) as distractors, knowing that candidates may confuse tuples with lists.

How to eliminate wrong answers

Option A is wrong because `t.append(5)` attempts to call the `append()` method, which is available for lists but not for tuples; tuples are immutable and do not have an `append()` method, so this would raise an AttributeError. Option B is wrong because `t[0] = 5` tries to assign a new value to an index of a tuple, which is not allowed since tuples are immutable; this would raise a TypeError. Option D is wrong because `t.pop()` attempts to call the `pop()` method, which is available for lists but not for tuples; tuples are immutable and do not have a `pop()` method, so this would raise an AttributeError.

82
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

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

The PCEP exam 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 · 82 questions total

Ready to test yourself?

Try a timed practice session using only Functions, Tuples, Dictionaries and Exceptions questions.