DEBUG THE CODE

DEBUG THE CODE

University

10 Qs

quiz-placeholder

Similar activities

python String and comment

python String and comment

3rd Grade - University

13 Qs

Python Dictionary

Python Dictionary

University

10 Qs

Scope of Variables in Python

Scope of Variables in Python

1st Grade - Professional Development

15 Qs

Python Conditionals and If Statements

Python Conditionals and If Statements

8th Grade - University

15 Qs

Python debugging

Python debugging

University

10 Qs

Exam 1 Practice

Exam 1 Practice

University

15 Qs

PYTHON PROGRAMMING

PYTHON PROGRAMMING

University

10 Qs

Python Sets

Python Sets

University

10 Qs

DEBUG THE CODE

DEBUG THE CODE

Assessment

Quiz

Computers

University

Hard

Created by

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

Create resources
Host any resource
Get auto-graded reports
or continue with
Microsoft
Apple
Others
By signing up, you agree to our Terms of Service & Privacy Policy
Already have an account?