Function

Python Quiz for Functions – 03

Python Quiz for Functions

Functions in Python enable programmers to group related statements together, making the code more readable, reusable, and efficient. Whether you’re a beginner trying to grasp the basics of function syntax or an experienced coder aiming to test your understanding, this Python quiz for functions is designed to challenge and educate you. From simple function declarations to intricate details about parameters and return values, get ready to put your Python knowledge to the test!

0%
11

Python Functions – 03

Level: Intermediate

1 / 10

1. What is the output of the following code:

def get_circle_info(radius):
  pi = 3.14159
  area = pi * radius ** 2
  circumference = 2 * pi * radius
  return area, circumference

# Example usage:
circle_area, circle_circumference = get_circle_info(5)
print(f"Area: {circle_area}, Circumference: {circle_circumference}")

2 / 10

2. What is the output of the following code:

def print_student_info(student):
  print(f"Name: {student['name']}, Age: {student['age']}, Grade: {student['grade']}")

# Example usage:
student_data = {'name': 'John', 'age': 16, 'grade': 'A'}
print_student_info(student_data)

3 / 10

3. What is the output of the following code:

from collections import defaultdict

def create_default_dict():
  return defaultdict(int)

counts = create_default_dict()
counts['apple'] += 1
print(counts['banana'])

4 / 10

4. What is the output of the following code:

def apply_operation(operation, x, y):
  return operation(x, y)

def add(x, y):
  return x + y

result = apply_operation(add, 7, 3)
print(result)

5 / 10

5. What is the output of the following code:

def find_max(numbers):
  max_num = max(numbers)
  return max_num
num_list = [12, 45, 67, 23, 89]
max_number = find_max(num_list)
print(max_number)

6 / 10

6. What is the output of the following code:

def fibonacci_sequence(n):
  a, b = 0, 1
  for _ in range(n):
    yield a
    a, b = b, a + b

for num in fibonacci_sequence(10):
 print(num, end=" ")

7 / 10

7. What is the output of the following code:

 

global_var = 10
def multiply_by_global(number):
  return global_var * number

result = multiply_by_global(5)
print(result)

8 / 10

8. What is the output of the following code:

def print_info(**kwargs):
  for key, value in kwargs.items():
    print(f"{key}: {value}")

print_info(name="Alice", age=25, city="Los Angeles")

9 / 10

9. What is the output of the following code:

def safe_divide(a, b):
  try:
    result = a / b
  except ZeroDivisionError:
    result = "Division by zero is not allowed."
  return result


print(safe_divide(10, 2))

10 / 10

10. What is the output of the following code:

def greet_person(name, greeting="Hello"):
  """This function greets a person with a default or custom greeting."""
  print(f"{greeting}, {name}!")

greet_person("James") # Output: Hello, James!

Your score is

0%