Courseiva

CCNA Functions Data Structures Questions

5 of 80 questions · Page 2/2 · Functions Data Structures topic · Answers revealed

76
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.

77
MCQhard

A developer wants to create a tuple that contains a single element, the integer 5. Which of the following expressions produces exactly that?

A.t = (5)
B.t = (5, 5)
C.t = tuple(5)
D.t = (5,)
AnswerD

The trailing comma after 5 is crucial; it tells Python that this is a tuple with one element. Without the comma, (5) would be an int. This is the standard way to create a single-element tuple.

Why this answer

A single-element tuple requires a trailing comma to distinguish it from a parenthesized expression. The expression (5,) creates a tuple containing the integer 5, while (5) is just the integer. The tuple() constructor can also be used with an iterable, but not with a single integer.

The two-element tuple is incorrect because it contains two items.

Exam trap

The trap here is forgetting the trailing comma in a single-element tuple, leading to the creation of an integer instead of a tuple.

78
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

The get method returns the value for the given key, or the supplied default when the key is absent. Using student.get('grade', 'N/A') avoids a KeyError because 'grade' is not present, satisfying the requirement for safe retrieval with a fallback.

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.

79
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

`len(t)` returns the tuple's element count directly, satisfying the requirement to determine how many items `t` holds. Tuples implement `__len__`, so `len()` works on them exactly as on lists or strings, yielding the integer length without modifying the immutable 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.

80
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

Python evaluates arguments left to right, so once y=2 is supplied as a keyword argument, the following positional 3 cannot be matched to a parameter. Python raises a SyntaxError because a positional argument follows a keyword argument.

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 · 80 questions total

Ready to test yourself?

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