PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Medium

Question 47

Q498 - Functions


What is the expected output of the following code?



  • A

    5

  • B

    3

  • C

    An exception is raised.

  • D

    4

Reveal correct answer

Correct answer: B

Explanation

Q498 (Please refer to this number, if you want to write me about this question.)

A. The lambda function lambda x: x * x squares the input x, resulting in 1 * 1 = 1. Adding x + 1 to this result gives 1 + 1 = 2. Therefore, the expected output is 1 + 2 = 3, not 5.

B. The code defines a function foo that takes two parameters x and y, where y is expected to be a function. The function foo returns the result of applying the function y to x (which is 1 in this case) and adding x + 1. When foo is called with 1 and the lambda function lambda x: x * x, the lambda function squares the input x, which results in 1 * 1 = 1. Adding x + 1 to this result gives 1 + 1 = 2. Therefore, the expected output is 1 + 2 = 3.

C. The code does not contain any operations that would raise an exception. The function foo is defined to handle the input parameters appropriately, and the lambda function provided is valid for the given input. Therefore, the expected output is a valid integer result, not an exception.

D. The lambda function lambda x: x * x squares the input x, resulting in 1 * 1 = 1. Adding x + 1 to this result gives 1 + 1 = 2. Therefore, the expected output is 1 + 2 = 3, not 4.

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need