Python Arrays

In Python, arrays are a data structure that allows you to store multiple items of the same data type in a single variable.

Last Updated :
Discuss
Comments

Question 1

What is the output of the following code?

Python
from array import array
arr = array('i', [1, 2, 3, 4, 5])
print(arr[0])


  • 0

  • 1

  • 5

  • Error

Question 2

Which of the following is the correct way to create an array in Python using the array module?

  • array = [1, 2, 3]

  • array = array('i', [1, 2, 3])

  • array = (1, 2, 3)

  • array = {1, 2, 3}

Question 3

Which of the following methods is used to add an element to the end of an array?

  • insert()

  • append()

  • extend()

  • add()

Question 4

How to create an empty array of integers in Python using the array module?

  • arr = array('i')

  • arr = array('i', [])

  • Both a and b

  • None of the above

Question 5

What will be the output of the following code?

Python
from array import array
arr = array('i', [1, 2, 3])
arr.insert(1, 4)
print(arr)


  • array('i', [1, 4, 2, 3])

  • array('i', [4, 1, 2, 3])

  • array('i', [1, 2, 3, 4])

  • Error

Question 6

What does the index method do in an array?

  • Returns the last occurrence of a specified value

  • Inserts a value at a specified position

  • Returns the index of the first occurrence of a specified value

  • Removes an element at a specified position

Question 7

What does the following code print?

Python
from array import array
arr = array('i', [1, 2, 3])
arr[1] = 5
print(arr)


  • array('i', [1, 2, 3])

  • array('i', [5, 2, 3])

  • array('i', [1, 5, 3])

  • Error

Question 8

What is the result of the following code?

Python
from array import array
arr1 = array('i', [1, 2, 3])
arr2 = array('i', [4, 5])
arr1 += arr2
print(arr1)


  • array('i', [1, 2, 3, 4, 5])

  • array('i', [4, 5, 1, 2, 3])

  • array('i', [1, 2, 3, 9, 10])

  • Error

Question 9

What is the time complexity of accessing an element in an array by index?

  • O(n)

  • O(log n)

  • O(1)

  • O(n^2)

Question 10

Which of the following operations is not allowed on a Python array?

  • Accessing an element by index

  • Slicing

  • Adding elements of different types

  • Iterating through elements

There are 10 questions to complete.

Take a part in the ongoing discussion