Python Arrays Quiz - MCQ Questions and Answers

Welcome to the Python Arrays Quiz! This quiz aims to help beginners understand the concept of arrays in Python. Arrays in Python, which are available through the array module, are a collection of elements of the same type, offering efficient storage and handling of data. Test your knowledge on Python arrays and enhance your understanding of how to use them effectively in Python programming!

1. What is an array in Python?

a) A collection of elements of different data types
b) A collection of elements of the same data type
c) A key-value pair collection
d) A collection of functions

2. How do you import the array module in Python?

a) import array
b) import arrays
c) from python import array
d) include array

3. Which of the following is the correct way to create an array of integers in Python?

a) array.array('i', [1, 2, 3])
b) array('i', [1, 2, 3])
c) [1, 2, 3]
d) (1, 2, 3)

4. How do you add an element to the end of an array?

a) append(element)
b) push(element)
c) add(element)
d) insert(element)

5. How do you access the first element of an array named 'myArray'?

a) myArray[0]
b) myArray[1]
c) myArray.first()
d) myArray(0)

6. What is the output of len(myArray) if myArray is an array with 3 elements?

a) 2
b) 3
c) 4
d) Error

7. How do you remove the last element from an array?

a) pop()
b) removeLast()
c) delete()
d) cut()

8. How do you insert an element at the beginning of an array?

a) insert(0, element)
b) append(0, element)
c) prepend(element)
d) addFirst(element)

9. What will be the output of the following code?

   myArray = array.array('i', [1, 2, 3])
   myArray.append(4)
   print(myArray)
a) array('i', [1, 2, 3])
b) array('i', [1, 2, 3, 4])
c) array('i', [4, 1, 2, 3])
d) Error

10. How do you create a new array by copying an existing array?

a) newArray = myArray
b) newArray = myArray.copy()
c) newArray = array.array(myArray.typecode, myArray)
d) newArray = copy(myArray)

11. What is the purpose of the typecode in an array?

a) To specify the size of the array
b) To define the functions available in the array
c) To specify the type of elements in the array
d) To set the maximum value of elements in the array

12. How do you find the index of the first occurrence of an element in an array?

a) index(element)
b) find(element)
c) locate(element)
d) search(element)

13. What does the following code do? myArray.reverse()

a) Sorts the array in ascending order
b) Sorts the array in descending order
c) Reverses the order of elements in the array
d) Does nothing

14. How do you remove a specific element from an array?

a) delete(element)
b) remove(element)
c) pop(element)
d) cut(element)

15. Can you change the value of an existing element in an array?

a) Yes
b) No
c) Only if it is an integer
d) Only if it is the last element

16. What will be the output of the following code?

 myArray = array.array('i', [1, 2, 3])
   del myArray[1]
   print(myArray)
a) array('i', [1, 2])
b) array('i', [1, 3])
c) array('i', [2, 3])
d) Error

17. What is the correct way to iterate over an array in Python?

a) for element in myArray: ...
b) for i in range(myArray.length()): ...
c) while element in myArray: ...
d) for each element in myArray: ...

18. How do you concatenate two arrays in Python?

a) Using the + operator
b) Using the append() method
c) Using the extend() method
d) Using the concatenate() method

19. How do you copy the elements of an array to a list in Python?

a) list(myArray)
b) myArray.toList()
c) list.copy(myArray)
d) myList = myArray

20. What will be the output of the following code?

myArray = array.array('i', [1, 2, 3, 4, 5])
   print(myArray[1:4])
a) array('i', [1, 2, 3])
b) array('i', [2, 3, 4])
c) array('i', [2, 3, 4, 5])
d) array('i', [1, 2, 3, 4])

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare