Exam2021_01 Flashcards

1
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
A

my_arr is reshaped to have 3 columns, and the number of rows is inferred automatically by numpy (using -1).

The shape of my_arr would be (2, 3) because the original array has 6 elements, and reshaping it to have 3 columns results in 2 rows.

The reshaped array would look like this:

[[0 1 2]
[3 4 5]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
A

The line my_arr[0] would select the first 2D array (of shape (4, 5)) from the 3D numpy array my_arr.

21
Q
A

The line my_arr[0, 0] would select the first 1D array (of shape (3,)) from the 3D numpy array my_arr.

24
Q
A

The AttributeError is raised but not caught by the except block because the except block is only looking for a ValueError. The finally block executes regardless of whether an exception was raised or not, and after the finally block executes, the AttributeError will propagate up the call stack.

So, the output will be:

done

And then the AttributeError will be raised.

26
Q
A

D is correct

The key point is that once an exception is caught in an except block, it stops propagating unless you explicitly re-raise it (using the raise statement). In this code, the except block just prints a message and doesn’t re-raise the error, so the exception is fully handled at that point.

If you wanted the ValueError to continue propagating, you would need to add a raise statement in the except block, like this:

try:
    a_function()
except ValueError:
    print("there was an exception!")
    raise  # This would make the error continue propagating
finally:
    print("done!")

Code Explanation:

  1. The code enters the try block
  2. a_function() is called and raises a ValueError
  3. Because ValueError was raised, execution jumps to the matching except ValueError block
  4. The except block prints “there was an exception!” and handles the error (essentially catching and stopping it from propagating further)
  5. After either the try block completes successfully OR an exception is handled, the finally block always executes
  6. The finally block prints “done!”
  7. The code continues normal execution because the exception was handled