Exam PCAP-31-03 All QuestionsBrowse all questions from this exam
Question 37

What is the expected behavior of the following code?

my_list = [i for i in range(5)]

m = [my_list[i] for i in range(4, 0, -1) if my_list[i] % 2 != 0] print(m)

    Correct Answer: C

    The code creates a list 'my_list' with elements [0, 1, 2, 3, 4] using list comprehension. It then attempts to create a new list 'm' by iterating over 'my_list' in reverse order from index 4 to 1. The condition within the list comprehension checks for elements that are odd (my_list[i] % 2 != 0). From indices 4 to 1 in reverse, the odd elements are 3 and 1. Therefore, the output of the print(m) statement is [3, 1].

Discussion
Dave304409Option: C

C is correct

DKAT2023Option: C

c is correct

b4779c5Option: C

it outputs [3,1]