Python Programming Coding Challenges

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Alfredhook3
A
Alfredhook3
Community Contributor
Quizzes Created: 4044 | Total Attempts: 3,041,032
| Questions: 10 | Updated: Jul 7, 2026
Please wait...
Question 1 / 10
🏆 Rank #--
0 %
0/100
Score 0/100

1. What will be the output of the following code?x = [1, 2, 3, 4, 5]print(x[::2])

Explanation

The code uses Python list slicing to extract elements from the list `x`. The expression `x[::2]` specifies a step of 2, meaning it will select every second element starting from index 0. Thus, it includes the elements at indices 0, 2, and 4, which are 1, 3, and 5 respectively. This results in the output being the list `[1, 3, 5]`.

Submit
Please wait...
About This Quiz
Python Programming Coding Challenges - Quiz

This assessment evaluates your understanding of Python programming concepts through practical coding challenges. You'll explore topics like list slicing, recursion, lambda functions, and error handling, enhancing your coding skills and problem-solving abilities. This Python programming assessment is essential for anyone looking to deepen their knowledge and proficiency in the language.

2. What will this code print?def mystery(n): if n == 0: return 1 return n * mystery(n - 1)print(mystery(5))

Explanation

The code defines a recursive function `mystery(n)` that calculates the factorial of a given number `n`. When `n` is 0, it returns 1, which is the base case for factorial calculations. For any positive integer `n`, it returns `n` multiplied by the factorial of `n - 1`. When `mystery(5)` is called, it computes `5 * 4 * 3 * 2 * 1`, resulting in 120, which is the factorial of 5. Thus, the output of the code is 120.

Submit

3. What is the output of this code?nums = [1, 2, 3, 4, 5]result = list(filter(lambda x: x % 2 == 0, nums))print(result)

Explanation

The code uses the `filter` function to create a new list that includes only the even numbers from the original list `nums`. The lambda function checks each element `x` to see if it is divisible by 2 (i.e., `x % 2 == 0`). As a result, only the numbers 2 and 4 meet this condition, leading to the output being the list `[2, 4]`.

Submit

4. What does this code output?d = {'a': 1, 'b': 2, 'c': 3}print({v: k for k, v in d.items()})

Explanation

The code creates a dictionary comprehension that reverses the key-value pairs of the original dictionary `d`. It iterates over each item in `d`, where `k` is the key and `v` is the value. The new dictionary has values as keys and keys as values, resulting in `{1: 'a', 2: 'b', 3: 'c'}`. This transformation effectively swaps the roles of keys and values, which is why the output is the reversed mapping.

Submit

5. What will be the output?class Animal: def speak(self): return 'Generic sound'class Dog(Animal): def speak(self): return 'Woof!'d = Dog()print(d.speak())

Explanation

The code defines a class `Animal` with a method `speak` that returns a generic sound. A subclass `Dog` overrides the `speak` method to return 'Woof!'. When an instance of `Dog` is created and its `speak` method is called, it executes the overridden method in the `Dog` class, producing the output 'Woof!'. This demonstrates method overriding in object-oriented programming, where a subclass provides a specific implementation of a method defined in its superclass.

Submit

6. What is the result of this code?try: x = int('abc')except ValueError: print('Caught ValueError')finally: print('Done')

Explanation

The code attempts to convert the string 'abc' into an integer using `int()`, which raises a `ValueError` because 'abc' is not a valid number. The `except` block catches this error and executes the print statement, displaying "Caught ValueError". Regardless of the error, the `finally` block executes next, printing "Done". Thus, the output combines both messages: "Caught ValueError" followed by "Done".

Submit

7. What does this generator function output?def count_up(n): for i in range(n): yield i * 2for val in count_up(4): print(val, end=' ')

Explanation

The generator function `count_up(n)` yields even numbers starting from 0 up to but not including `2n`. In the case of `count_up(4)`, it generates values for `i` from 0 to 3, yielding `i * 2` which results in the sequence 0, 2, 4, and 6. The `print` statement iterates over these yielded values, displaying them in a single line with spaces in between. Thus, the output is "0 2 4 6".

Submit

8. What is the output of this code?import functoolsnums = [1, 2, 3, 4, 5]result = functools.reduce(lambda x, y: x + y, nums)print(result)

Explanation

The code uses `functools.reduce` to apply a lambda function that sums the elements of the list `nums`. Starting with the first two elements, it adds them together, then takes that result and adds the next element, and so on, until all elements are processed. Specifically, the operations are: 1 + 2 = 3, 3 + 3 = 6, 6 + 4 = 10, and finally 10 + 5 = 15. Thus, the final output printed is 15, which is the sum of all numbers in the list.

Submit

9. What will this code print?a = [1, 2, 3]b = ab.append(4)print(a)

Explanation

The code initializes a list `a` with the values `[1, 2, 3]` and then appends the value `4` to the list using the `append` method. The `append` method modifies the list in place and does not return a new list; instead, it returns `None`. Therefore, when `print(a)` is executed, it outputs the updated list which now includes the appended value, resulting in `[1, 2, 3, 4]`.

Submit

10. What is the output of this code?def add(a, b=10): return a + bprint(add(5))print(add(5, 20))

Explanation

The code defines a function `add` that takes two parameters: `a` and `b`, with `b` having a default value of 10. When `add(5)` is called, it uses the default value for `b`, resulting in `5 + 10`, which equals 15. The second call, `add(5, 20)`, explicitly provides a value for `b`, so it computes `5 + 20`, yielding 25. The two results are printed consecutively, producing the output "1525".

Submit
×
Saved
Thank you for your feedback!
View My Results
Cancel
  • All
    All (10)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
What will be the output of the following code?x = [1, 2, 3, 4,...
What will this code print?def mystery(n): if n == 0: return...
What is the output of this code?nums = [1, 2, 3, 4, 5]result =...
What does this code output?d = {'a': 1, 'b': 2, 'c': 3}print({v: k for...
What will be the output?class Animal: def speak(self): ...
What is the result of this code?try: x = int('abc')except...
What does this generator function output?def count_up(n): for i in...
What is the output of this code?import functoolsnums = [1, 2, 3, 4,...
What will this code print?a = [1, 2, 3]b = ab.append(4)print(a)
What is the output of this code?def add(a, b=10): return a +...
play-Mute sad happy unanswered_answer up-hover down-hover success oval cancel Check box square blue
Alert!