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
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).)
More IT Operating System Questions
- Which probability distribution is most widely used to model quantization noise in pulse code modulation ?
- Which algorithm is commonly used for Part-of-Speech tagging?
- CDATA is used in XML to:
- Consider the following Java code: class Animal { Â Â String type = "Generic Animal"; Â Â void eat() { Â Â Â Â System.out.println("Animal eats food."); ...
- In a Breadth-First Search (BFS) algorithm for graph traversal, what is the primary role of a queue?
- Complete a simple hash function for a string s that sums the ASCII values of its characters and then takes the modulo of a prime number M. def simple_ha...
- A C function print_matrix(int rows, int cols, int matrix[rows][cols]) is designed to print a matrix. #include void print_matrix(int rows, int cols, ...
- Which is used for C shell?
- Which keyword is used for inheritance in C++?
- Which Hadoop component is responsible for resource management?