📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!


    Question

    Complete the Python code snippet to initialize an array

    arr of size N with all zeros, and then iterate through it from the second element (index 1) up to, but not including, the last element (index N-1). N = 5 arr = _________ # Line 1: Initialize arr for i in _________: # Line 2: Complete the loop     # Do something with arr[i]     pass
    A arr = [0] * N ; for i in range(1, N) Correct Answer Incorrect Answer
    B arr = [0 for _ in range(N)] ; for i in range(1, N - 1) Correct Answer Incorrect Answer
    C arr = [0] * N ; for i in range(1, N - 1) Correct Answer Incorrect Answer
    D arr = [0] * N ; for i in range(N) Correct Answer Incorrect Answer
    E arr = [None] * N ; for i in range(1, N) Correct Answer Incorrect Answer

    Solution

    Correct Answer: C (Line 1: [0] * N is a common way to initialize. Line 2: range(1, N-1) iterates from index 1 up to N-2, which means the second element up to the second-to-last element. The  asks "up to, but not including, the last element (index N-1)", so range(1, N-1) is correct. If it meant *including* the last element, it would be range(1, N).)

    Practice Next
    ask-question