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?

    • arr = [1, 2, 3]

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

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

    • 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])
    


    • 0

    • 1

    • 5

    • Error

    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

    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 5

    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 6

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

    • arr = array.array('i')

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

    • Both a and b

    • 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)
    


    • 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