DEBUG THE CODE

Quiz
•
Computers
•
University
•
Hard

Rishita Bansal
FREE Resource
10 questions
Show all answers
1.
FILL IN THE BLANK QUESTION
1 min • 1 pt
def calculate_average(numbers):
total = 0
for num in numbers:
total += num
return total / len(numbers)
numbers = [1, 2, 3, 4]
average = calculate_average(numbers)
print(average)
Question: This code is intended to calculate the average of a list of numbers. However, there's a bug. What's wrong, and how would you fix it?
Options:
A. The `total` variable should be initialized outside the loop.
B. The indentation within the `calculate_average` function is incorrect.
C. The function should return `total` instead of `total / len(numbers)`.
D. The code is working correctly and needs no changes.
2.
FILL IN THE BLANK QUESTION
1 min • 1 pt
def is_even(number):
if number % 2:
return False
else:
return True
if is_even(5):
print("The number is even")
Question: This code tries to determine if a number is even. There's a logical error in the implementation. How would you correct it?
Options:
A. Replace `if number % 2:` with `if number % 2 == 0:`.
B. Change `return False` to `return True` within the `if` block.
C. Swap the positions of the `return True` and `return False` statements.
D. The `else` block is unnecessary.
3.
FILL IN THE BLANK QUESTION
1 min • 1 pt
data = {"name": "Alice", "city": "New York", "age": 30 }
print(data["country"])
Question: This code will cause an error. What kind of error is it, and how do you fix it?
Options:
A. KeyError: The key "country" doesn't exist in the dictionary. Add the "country" key.
B. TypeError: You cannot print a dictionary directly. Replace `print(data["country"])` with `print(data)`.
C. IndexError: The value "country" is not a valid index. Change it to an existing index.
D. SyntaxError: There is a problem with the dictionary structure. Fix the braces or quotes.
4.
FILL IN THE BLANK QUESTION
1 min • 1 pt
x = 10
result = x if x > 5 else 0
print(result)
Question: This code uses a concise way of writing conditional expressions. Rewrite it as a traditional `if...else` statement to show how it works
Options:
A. if x > 5:
result = x
B. if x > 5:
result = x
else:
result = 0
C. result = x > 5 ? x : 0
D. This code cannot be converted to a traditional `if...else` statement.
5.
FILL IN THE BLANK QUESTION
1 min • 1 pt
def some_function(x, y=10):
return x * y
result = some_function(5)
print(result)
Question: What will be printed? Explain the concept of default arguments in Python functions.
Options:
A. 50 – The default value of `y` (10) is used when only `x` is provided.
B. 5 – The default value is ignored since an argument is given for `x`.
C. The code will cause an error because a required argument (`y`) is missing.
D. The code will output a random value based on the arguments.
6.
FILL IN THE BLANK QUESTION
1 min • 1 pt
my_dict = {"a": 1, "b": 2}
new_dict = my_dict
new_dict["a"] = 3
print(my_dict)
Question: What will the output of this code be? Explain how the assignment to `new_dict` works with dictionaries.
Options:
A. `{'a': 1, 'b': 2}` – Dictionaries are copied by value, so changes to `new_dict` won't affect `my_dict`.
B. `{a': 3, 'b': 2}` – Both variables point to the same dictionary object in memory.
C. The code will cause a KeyError because dictionaries are immutable.
D. This code will print an empty dictionary.
7.
FILL IN THE BLANK QUESTION
1 min • 1 pt
numbers = [5, 2, 8, 1]
sorted_numbers = numbers.sort()
print(numbers)
print(sorted_numbers)
Question: Explain the output of this code and why it might be different than what a beginner might expect.
Options:
A. The output will be two lists: one sorted, and the original list unmodified.
B. The output will be the sorted list twice.
C. The first `print` will show the sorted list; the second will print `None`.
D. This code will cause a TypeError because lists cannot be sorted.
Create a free account and access millions of resources
Similar Resources on Wayground
11 questions
Quiz 5

Quiz
•
University
15 questions
Python End of Topic Quiz

Quiz
•
8th Grade - University
15 questions
Python revision

Quiz
•
10th Grade - University
10 questions
Python Basics

Quiz
•
12th Grade - University
15 questions
Round 2 Debug-The-Code

Quiz
•
University
10 questions
Python Sets

Quiz
•
University
10 questions
C - Pointers

Quiz
•
University
10 questions
Web-II-Loop and Function

Quiz
•
University
Popular Resources on Wayground
18 questions
Writing Launch Day 1

Lesson
•
3rd Grade
11 questions
Hallway & Bathroom Expectations

Quiz
•
6th - 8th Grade
11 questions
Standard Response Protocol

Quiz
•
6th - 8th Grade
40 questions
Algebra Review Topics

Quiz
•
9th - 12th Grade
4 questions
Exit Ticket 7/29

Quiz
•
8th Grade
10 questions
Lab Safety Procedures and Guidelines

Interactive video
•
6th - 10th Grade
19 questions
Handbook Overview

Lesson
•
9th - 12th Grade
20 questions
Subject-Verb Agreement

Quiz
•
9th Grade