You are on page 1of 3

CHAPTER 4

Understanding Arrays & Collections


Standard Questions
IMPORTANT 1.) What are collections?
Answer: A collection is nothing but a container that groups multiple items into a single
object. Collections are used to store data. We can also retrieve and manipulate data that
is stored in the Collections. Examples of collections are a deck of cards which contains a
collection of cards, a phone book directory which contains a collection of all the names
and phone numbers in an order, and a mail folder which contains a collection of cards.

IMPORTANT 2.) What are different types of collections?


Answer: The Different types of collections are-
• Array

• List

• Sets
• Maps

• Dictionary
• Queue

• Stack
• LinkedList
3.) How can we iterate over collections?
Answer: The iterators are used to provide users with a uniform way of accessing
collections sequentially. An iteration takes elements from a collection one after another,
from one element to the last one. We always need to traverse through the elements of
these collections to fetch the data or make any modifications to that data.
We cannot directly add or remove elements while iterating through the collection that
includes them.
Ex: Program to iterate a collection
collection = [“apple”, “mango”, “cherry”, “banana”]
for i in range(len(collection)):
print(collection[i])
4.) What are the different types of modifications that we can perform on
collections?
Answer: The modifications that we can do on collections.
• Adding Elements During Iteration
To add elements while iterating a list, set or map, keep the new elements in a temporary
list, set, or map and add them to the original after you finish iterating the collection.
• Removing Elements During Iteration

To remove elements from a list, you can create a new list, and then insert the elements
you wish to keep. Or, add the elements you wish to remove to a different list and remove
them after you finish iterating the collection.

IMPORTANT 5.) What is an Array Index?


Answer: Array index can be defined as the location of an item in an array. The variables
that are present in the array are always ordered sequentially with an index starting from
0. The indexes continue through the natural numbers i.e 1,2,3,4…
6.) Do collections allow backward traversing of data? Support your answer with a
proper explanation.
Answer: Yes, collections do allow backward traversing of data just like we can iterate a
collection from the front with the index position being 0. In reverse traversing, we start
from the last element whose index position is -1.
For example, if we want to print all the elements from the collections backward, we have
to iterate it from the last index.
IMPORTANT 7.) Name three real-life collections which work like Arrays.
Answer:
Arranging books: Having a pile of books and arranging them in your book rack is
essentially a real-life example of an array.
An Array of chairs: An array of chairs in an auditorium is also an example of a real-life
array.
An egg tray: An egg tray is a real-life example of an array. Having eggs placed in rows.
Higher Order Thinking Skills (HOTS)
IMPORTANT 1.) Draw a flowchart for the optimized algorithm for finding the
square of numbers that we learnt in this chapter
Answer:

2.) Write a program to create an array of prime numbers from 50 to 100.


Answer: Program to Create array of prime numbers from 50 to
100.
low = 50
high = 100
result = []
for i in range ( low, high + 1 ):
for j in range ( 2, i ):
if ( i % j == 0 ):
break
else:
result.append(i)
print(result)

You might also like