Python Arrays

Last Updated :
Discuss
Comments

Question 1

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

  • A

    arr = [1, 2, 3]

  • B

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

  • C

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

  • D

    arr = {1, 2, 3}

Question 2

What is the output of the following code?

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


  • A

    0

  • B

    1

  • C

    5

  • D

    Error

Question 3

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

  • A

    insert()

  • B

    append()

  • C

    extend()

  • D

    add()

Question 4

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)


  • A

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

  • B

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

  • C

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

  • D

    Error

Question 5

What does the index method do in an array?

  • A

    Returns the last occurrence of a specified value

  • B

    Inserts a value at a specified position

  • C

    Returns the index of the first occurrence of a specified value

  • D

    Removes an element at a specified position

Question 6

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

  • A

    arr = array.array('i')

  • B

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

  • C

    Both a and b

  • D

    None of the above

Question 7

What does the following code print?

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


  • A

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

  • B

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

  • C

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

  • D

    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)


  • A

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

  • B

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

  • C

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

  • D

    Error

Question 9

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

  • A

    O(n)

  • B

    O(log n)

  • C

    O(1)

  • D

    O(n^2)

Question 10

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

  • A

    Accessing an element by index

  • B

    Slicing

  • C

    Adding elements of different types

  • D

    Iterating through elements

There are 10 questions to complete.

Take a part in the ongoing discussion