Search Header Logo

DEBUG THE CODE

Authored by Rishita Bansal

Computers

University

DEBUG THE CODE
AI

AI Actions

Add similar questions

Adjust reading levels

Convert to real-world scenario

Translate activity

More...

    Content View

    Student View

10 questions

Show all answers

1.

FILL IN THE BLANKS 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.

(a)  

2.

FILL IN THE BLANKS 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.

(a)  

3.

FILL IN THE BLANKS 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.

 

(a)  

4.

FILL IN THE BLANKS 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.

(a)  

5.

FILL IN THE BLANKS 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.

(a)  

6.

FILL IN THE BLANKS 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.

(a)  

7.

FILL IN THE BLANKS 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.

(a)  

Access all questions and much more by creating a free account

Create resources

Host any resource

Get auto-graded reports

Google

Continue with Google

Email

Continue with Email

Microsoft

Continue with Microsoft

or continue with

Facebook

Facebook

Apple

Apple

Others

Others

Already have an account?