OA. free
Free
Fujitsu Data Structures & Algorithms Data Structures & Algorithms Medium

What is the output of the following Python code?

Fujitsu technical mcq question, verified with a worked answer. Free to practise - no sign-up.

What is the output of the following Python code? @oY fg @® @ oonro7Mmin3esec G)+ What is the output of the following Python code? def func(x): return x * 2 result = list(map(fune, [1, 2, 3, 4])) print(result) AP - A) @ - B) [1,4,9,16] - C) [2,4,6, 8] - D) [2, 2, 2,2] - E) 112,3,4]

Choose one option.
Show answer & explanation
Answer: C. [2,4,6, 8]

The code uses the map() function to apply a doubling function (x * 2) to each element of the list [1, 2, 3, 4], resulting in a list of doubled values.

Step-by-step Derivation:
Step 1: Analyze the function definition: def func(x): return x * 2. This function takes an input x and returns its double.
Step 2: Analyze the map operation: map(func, [1, 2, 3, 4]). The map function applies func to every item in the iterable [1, 2, 3, 4] sequentially.
Step 3: Trace the execution:

  • For x = 1: 1 * 2 = 2
  • For x = 2: 2 * 2 = 4
  • For x = 3: 3 * 2 = 6
  • For x = 4: 4 * 2 = 8
    Step 4: The list() constructor converts the map object into a list: [2, 4, 6, 8].
    Step 5: The print(result) statement outputs the resulting list.