Debugging and Optimizing Code: Intermediate Audio Quiz Quiz

  1. Infinite Loop Debugging

    In the following Python code, which change is needed to prevent an infinite loop? nn i = 0nwhile i u003C 5:n print(i)

    1. Add i += 1 inside the loop
    2. Change while to for
    3. Remove print(i)
    4. Add break statement before the loop
    5. Replace i u003C 5 with i u003E 5
  2. Off-by-One Error Identification

    Given this Java code: for(int i = 0; i u003C= arr.length; i++) { sum += arr[i]; }, what is the logical bug present?

    1. The loop will access an index out of bounds
    2. sum should be initialized outside the loop
    3. arr.length should be replaced with arr.size()
    4. i should start from 1
    5. The loop should be in reverse
  3. Memory Leak Detection

    In a C program, what could cause a memory leak?

    1. Allocating memory with malloc but not calling free
    2. Using ++ instead of += in a loop
    3. Not including stdio.h
    4. Forgetting to declare variables
    5. Returning 0 at the end of main
  4. Choosing the Right Data Structure

    When repeatedly searching for an item by key, which is the most optimal data structure in Python?

    1. Dictionary
    2. List
    3. Tuple
    4. Set
    5. String
  5. SQL Query Optimization

    How can you speed up queries on a large SQL table with millions of rows?

    1. Create an index on commonly searched columns
    2. Use SELECT * instead of specific columns
    3. Run VACUUM after every query
    4. Rename the table
    5. Drop unused columns
  6. Debugging Variable Scope Issues

    What is the issue if a function cannot access a variable defined inside another function in Python?

    1. The variable has local scope
    2. The variable name is too short
    3. The function was not called
    4. Python does not support variables
    5. The code needs more comments
  7. Identifying Redundant Computation

    What is one way to optimize this code snippet?nfor i in range(len(data)):n y = expensive_computation()n print(data[i], y)

    1. Move expensive_computation() outside the loop
    2. Use print() twice
    3. Replace range(len(data)) with while
    4. Convert data to a string
    5. Replace print with log
  8. Fixing Typographical Errors

    Which of the following is a correct Python function definition?

    1. def func():
    2. fnc def():
    3. def func[]:
    4. def fun():-
    5. define func():
  9. Resolving Race Conditions

    What does using a lock in multithreaded code help to prevent?

    1. Race conditions
    2. Syntax errors
    3. Compilation warnings
    4. Memory address leaks
    5. Misaligned pointers
  10. Eliminating Unnecessary Loops

    How can you optimize this code for summing list elements in Python?nresult = 0nfor i in range(len(nums)):n result += nums[i]

    1. Use result = sum(nums)
    2. Use result = nums.sum()
    3. Replace range with enumerate
    4. Change for loop to a while loop
    5. Convert nums to a set before summing