M7) Variable Scope

In programming, variable scope defines the visibility and accessibility of variables within different parts of the code. In Listing 1, we have a Python code snippet with a function calculate and a main function. The function calculate takes two input parameters, a and b, and returns a floating-point value. The main function initializes variables a, b, and c, calls the calculate function, and prints the result.

We have three breakpoints (BREAKPOINT 1, BREAKPOINT 2, and BREAKPOINT 3) in the code to inspect the variable values at different stages of execution.

Listing 1: Functions calculate and main. The code breaks at three different points (BREAKPOINT 1, BREAKPOINT 2, and BREAKPOINT 3) to inspect the variable values (Question 1).
def calculate(a: float, b: float) -> float:
    c: float = 0.0
    result: float = 0.0

    # BREAKPOINT 2
    if c < b + a:
        c = a * 2
        result = -1.0

    # BREAKPOINT 3
    result = a + b + c
    return result


def main() -> None:
    a: float = 2.0
    b: float = 3.0
    c: float = 4.0

    c = calculate(b, a)

    # BREAKPOINT 1
    print(c)


if __name__ == "__main__":
    main()

Starting from the main function (i.e., you run the Python file), you are executing the code line by line and inspecting the variable values at each breakpoint. What are the values of a, b, and c in their respective scopes at the breakpoints (BREAKPOINT 1, BREAKPOINT 2, and BREAKPOINT 3) while executing the code?

Choose the correct option:

  1. BREAKPOINT 1: a=2.0, b=3.0, c=-1.0 / BREAKPOINT 2: a=2.0, b=3.0, c=4.0 / BREAKPOINT 3: a=2.0, b=3.0, c=6.0.
  2. BREAKPOINT 1: a=2.0, b=3.0, c=11.0 / BREAKPOINT 2: a=2.0, b=3.0, c=0.0 / BREAKPOINT 3: a=2.0, b=3.0, c=11.0.
  3. BREAKPOINT 1: a=2.0, b=3.0, c=11.0 / BREAKPOINT 2: a=3.0, b=2.0, c=0.0 / BREAKPOINT 3: a=3.0, b=2.0, c=6.0.
  4. BREAKPOINT 1: a=2.0, b=3.0, c=-1.0 / BREAKPOINT 2: a=3.0, b=2.0, c=0.0 / BREAKPOINT 3: a=3.0, b=2.0, c=0.0.