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
15 questions
Milking Minds 2 17-01-24

Quiz
•
University
10 questions
PYTHON PROGRAMMING POP QUIZ ON TOPIC 2!

Quiz
•
University
12 questions
Python Basics

Quiz
•
University
10 questions
PYTHON PROGRAMMING

Quiz
•
University
15 questions
Python introduction

Quiz
•
University
10 questions
Exploring Python: Input and Output Essentials

Quiz
•
9th Grade - University
15 questions
Java Bootcamp Day 4

Quiz
•
University
15 questions
ARRAYLIST - JAVA

Quiz
•
University
Popular Resources on Wayground
10 questions
Lab Safety Procedures and Guidelines

Interactive video
•
6th - 10th Grade
10 questions
Nouns, nouns, nouns

Quiz
•
3rd Grade
10 questions
9/11 Experience and Reflections

Interactive video
•
10th - 12th Grade
25 questions
Multiplication Facts

Quiz
•
5th Grade
11 questions
All about me

Quiz
•
Professional Development
22 questions
Adding Integers

Quiz
•
6th Grade
15 questions
Subtracting Integers

Quiz
•
7th Grade
9 questions
Tips & Tricks

Lesson
•
6th - 8th Grade
Discover more resources for Computers
21 questions
Spanish-Speaking Countries

Quiz
•
6th Grade - University
20 questions
Levels of Measurements

Quiz
•
11th Grade - University
7 questions
Common and Proper Nouns

Interactive video
•
4th Grade - University
12 questions
Los numeros en español.

Lesson
•
6th Grade - University
7 questions
PC: Unit 1 Quiz Review

Quiz
•
11th Grade - University
7 questions
Supporting the Main Idea –Informational

Interactive video
•
4th Grade - University
12 questions
Hurricane or Tornado

Quiz
•
3rd Grade - University
7 questions
Enzymes (Updated)

Interactive video
•
11th Grade - University