All Exams  >   Software Development  >   Basics of Python  >   All Questions

All questions of Arrays for Software Development Exam

Which of the following statements about arrays in Python is correct?
  • a)
    Arrays can store elements of different data types.
  • b)
    Arrays can be resized dynamically.
  • c)
    Arrays can be directly created using the built-in array module in Python.
  • d)
    Arrays are mutable objects.
Correct answer is option 'C'. Can you explain this answer?

Explanation:

Arrays in Python:
In Python, arrays are not built-in data types like lists or tuples. To work with arrays, we need to import the array module from the Python standard library.

Correct Option:
The correct statement about arrays in Python is option C: "Arrays can be directly created using the built-in array module in Python."

Explanation of Incorrect Options:
a) Arrays cannot store elements of different data types. Unlike lists in Python, arrays in the array module can only store elements of the same data type. This is because arrays are designed for efficient storage and manipulation of homogeneous elements.

b) Arrays in Python cannot be resized dynamically. Once an array is created, its size is fixed and cannot be changed. If we need to add or remove elements, we would need to create a new array with the desired size and copy the elements from the old array to the new one.

d) Arrays are mutable objects, meaning their elements can be modified. However, arrays themselves are not mutable. Once an array is created, its size and data type cannot be changed. If we need to modify an element, we can access it using its index and assign a new value to it.

Creating Arrays using the array module:
To create an array using the array module, we need to import it first. Then we can use the array() function to create an array by specifying its type code and the initial values.

Example:
```
import array

# Create an array of integers
arr = array.array('i', [1, 2, 3, 4, 5])

# Print the array
print(arr)
```

In the above example, we import the array module and create an array of integers using the 'i' type code. The initial values are provided as a list [1, 2, 3, 4, 5]. The resulting array can only store integers and has the values [1, 2, 3, 4, 5].

Therefore, the correct statement is option C: "Arrays can be directly created using the built-in array module in Python."
1 Crore+ students have signed up on EduRev. Have you? Download the App

What will be the output of the following code?
arr = [1, 2, 3, 4, 5]
print(arr[-3:-1])
  • a)
    [2, 3]
  • b)
    [3, 4]
  • c)
    [2, 3, 4]
  • d)
    [3, 4, 5]
Correct answer is option 'B'. Can you explain this answer?

Explanation:

The given code demonstrates slicing in Python. Slicing allows us to extract a portion of a list or string. In this case, the list `arr` contains the elements [1, 2, 3, 4, 5].

The slicing syntax for a list is `list[start:end]`. It returns a new list containing the elements from the `start` index (inclusive) to the `end` index (exclusive).

In the given code, `arr[-3:-1]` is used to slice the list `arr`. Here, the start index is -3 and the end index is -1.

Steps:

1. The start index -3 refers to the element at the third position from the end of the list, which is 3.
2. The end index -1 refers to the element at the first position from the end of the list, which is 4.
3. The slice `arr[-3:-1]` returns a new list containing the elements from index -3 (inclusive) to index -1 (exclusive), which are [3, 4].

Therefore, the output of the code is [3, 4], which corresponds to option 'B'.

What is the time complexity of accessing an element by index in an array?
  • a)
    O(1)
  • b)
    O(log n)
  • c)
    O(n)
  • d)
    O(n2)
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
Accessing an element by index in an array has a time complexity of O(1) since it directly accesses the memory location based on the index.

What is the time complexity for accessing an element in an array in Python?
  • a)
    O(1)
  • b)
    O(log n)
  • c)
    O(n)
  • d)
    O(n2)
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
Accessing an element in an array in Python has a time complexity of O(1), as the index is used to directly access the element.

Which of the following statements is true about arrays in Python?
  • a)
    Arrays can have a variable length.
  • b)
    Array elements cannot be modified once assigned.
  • c)
    Arrays in Python are implemented as linked lists.
  • d)
    Python does not have built-in support for arrays.
Correct answer is option 'D'. Can you explain this answer?

Fawaz Al Ameri answered
Introduction:
Python is a high-level programming language that provides various data structures to store and manipulate data. One of these data structures is the array, which allows us to store multiple values of the same data type in a single variable. In Python, arrays are not as commonly used as other data structures like lists or tuples. Let's examine the given statements and determine which one is true about arrays in Python.

Explanation:

a) Arrays can have a variable length:
This statement is false. In Python, arrays have a fixed length, meaning that once an array is created, its length cannot be changed. If we need a dynamic data structure that can grow or shrink in size, we should use a list instead.

b) Array elements cannot be modified once assigned:
This statement is false. Array elements in Python can be modified. We can access individual elements of an array using their index and assign new values to them. For example, if we have an array `arr` and we want to modify its element at index 0, we can do so by using the syntax `arr[0] = new_value`.

c) Arrays in Python are implemented as linked lists:
This statement is false. Arrays in Python are not implemented as linked lists. Instead, they are typically implemented as contiguous blocks of memory, where each element occupies a fixed amount of space. This allows for efficient random access to elements based on their index.

d) Python does not have built-in support for arrays:
This statement is true. Although arrays exist in Python, they are not part of the built-in data structures provided by the language. Instead, Python provides more versatile data structures like lists, which can be used in a similar way to arrays. Lists in Python can have a variable length and allow for modification of elements. They also support additional operations like appending, slicing, and sorting.

Conclusion:
In conclusion, the statement that is true about arrays in Python is that Python does not have built-in support for arrays. While arrays can be used in Python, they are not as commonly used as other data structures like lists. It is important to choose the appropriate data structure based on the specific requirements of the program.

Given the following code, what will be the output?
arr = [1, 2, 3, 4, 5]
arr[2] = 10
print(arr)
  • a)
    [1, 2, 10, 4, 5]
  • b)
    [1, 2, 3, 4, 5]
  • c)
    [1, 10, 3, 4, 5]
  • d)
    [1, 2, 3, 10, 5]
Correct answer is option 'C'. Can you explain this answer?

Reem Al Saadi answered
Explanation:

The given code initializes a list `arr` with the values [1, 2, 3, 4, 5].

Then, it modifies the value at index 2 of the list `arr` by assigning the value 10 to it. The index of a list starts from 0, so the value at index 2 is the third element in the list.

Finally, it prints the modified list `arr`.

Step-by-step execution:

1. Initialize the list `arr` with the values [1, 2, 3, 4, 5].
2. Modify the value at index 2 of the list `arr` to 10. Now, the list becomes [1, 2, 10, 4, 5].
3. Print the modified list `arr`.

Output:

The output of the code will be:
[1, 2, 10, 4, 5]

Explanation:

When the code modifies the value at index 2 of the list `arr` by assigning it the value 10, it changes the third element of the list. The modified list becomes [1, 2, 10, 4, 5].

Thus, when the code prints the list `arr`, it will output [1, 2, 10, 4, 5].

Therefore, the correct answer is option C: [1, 10, 3, 4, 5].

What will be the output of the following code?
my_array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(my_array[1][2])
  • a)
    1
  • b)
    2
  • c)
    6
  • d)
    8
Correct answer is option 'C'. Can you explain this answer?

Explanation:

Accessing elements in a nested list:
- The given code creates a 2-dimensional list called my_array with three sublists inside it.
- Each sublist represents a row in the 2D list.
- To access elements in a 2D list, you need to specify the index of the row first, then the index of the element within that row.

Understanding the code:
- my_array[1] refers to the second sublist in the 2D list (indexing starts at 0).
- my_array[1][2] refers to the third element in the second sublist, which is 6.

Output:
- Therefore, when we print my_array[1][2], the output will be 6.

Given the following code, what will be the output?
arr = [1, 2, 3, 4, 5]
arr.pop(2)
print(arr)
  • a)
    [1, 2, 3, 4, 5]
  • b)
    [1, 2, 4, 5]
  • c)
    [1, 2, 4]
  • d)
    [1, 2, 5]
Correct answer is option 'C'. Can you explain this answer?

Explanation:

The given code snippet is written in Python. Let's break down the code and analyze it step by step to understand the output.

Step 1:
The code initializes a list named "arr" with the values [1, 2, 3, 4, 5].

Step 2:
The code then calls the "pop()" method on the list "arr" with the index value 2 as the parameter. The "pop()" method removes and returns the element at the specified index from the list. In this case, it removes the element at index 2, which is the number 3.

Step 3:
After removing the element at index 2, the list "arr" becomes [1, 2, 4, 5].

Step 4:
Finally, the code prints the modified list "arr" using the "print()" function. The output will be [1, 2, 4, 5].

Therefore, the correct answer is option C: [1, 2, 4].

What will be the output of the following code?
import numpy as np
my_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(my_array[2][1])
  • a)
    1
  • b)
    2
  • c)
    6
  • d)
    8
Correct answer is option 'D'. Can you explain this answer?

Output Explanation:

The given code defines a 2-dimensional array using the `numpy` library and then prints the value at the specified index.

Here's a step-by-step explanation of the code:

1. The `numpy` library is imported using the line `import numpy as np`. This allows us to use the `numpy` functions and data types in our code.

2. The variable `my_array` is defined as a 2-dimensional array using the `np.array()` function. The array contains three rows and three columns, with the following values:
```
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
```

3. The line `print(my_array[2][1])` prints the value at the index `[2][1]` of the `my_array` array. In Python, indexing starts from 0, so `[2][1]` refers to the element in the third row, second column of the array.

4. The output is the value `8`, which is the element at the specified index `[2][1]`.

Explanation of the options:

a) Option '1': This is not the correct answer because the value at the specified index `[2][1]` is `8`, not `1`.

b) Option '2': This is not the correct answer because the value at the specified index `[2][1]` is `8`, not `2`.

c) Option '6': This is not the correct answer because the value at the specified index `[2][1]` is `8`, not `6`.

d) Option '8': This is the correct answer because the value at the specified index `[2][1]` is indeed `8`.

What will be the output of the following code?
arr = [1, 2, 3, 4, 5]
print(arr[2:4])
  • a)
    [1, 2]
  • b)
    [2, 3]
  • c)
    [3, 4]
  • d)
    [4, 5]
Correct answer is option 'C'. Can you explain this answer?

Output Explanation:

The given code creates a list named 'arr' with elements [1, 2, 3, 4, 5]. It then prints a slice of the list using the syntax 'arr[2:4]'.

Understanding Slicing:
When we slice a list, we specify a range of indices to extract a portion of the list. The syntax for slicing is 'start_index:end_index', where the start_index is inclusive and the end_index is exclusive.

In our case, arr[2:4] means that we want to extract a portion of the list starting from index 2 and ending at index 4 (exclusive).

Explanation:

Let's break down the given code step by step:

1. Create a list named 'arr' with elements [1, 2, 3, 4, 5].
2. Print a slice of the list using the syntax 'arr[2:4]'.

The slice 'arr[2:4]' means that we want to extract elements from index 2 to index 4 (exclusive).

- The element at index 2 is 3.
- The element at index 3 is 4.

Therefore, the output of the code will be [3, 4].

Summary:

The given code prints a slice of the list 'arr' starting from index 2 and ending at index 4 (exclusive). This slice includes the elements [3, 4]. Therefore, the correct answer is option C) [3, 4].

What will be the output of the following code?
import numpy as np
my_array = np.array([1, 2, 3, 4, 5])
new_array = my_array.copy()
new_array[2] = 10
print(my_array[2])
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    10
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The code my_array[2] = 10 modifies the element at index 2 in the array new_array, not affecting the original array my_array. Therefore, the value of my_array[2] remains 3.

Given an array arr, how can we pass it as an argument to a function my_function in Python?
  • a)
    my_function(arr)
  • b)
    my_function(*arr)
  • c)
    my_function(arr[])
  • d)
    my_function([arr])
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
To pass an array arr as an argument to a function my_function in Python, we can directly pass the array name arr as an argument. No special syntax is required.

Chapter doubts & questions for Arrays - Basics of Python 2024 is part of Software Development exam preparation. The chapters have been prepared according to the Software Development exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Software Development 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of Arrays - Basics of Python in English & Hindi are available as part of Software Development exam. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.

Basics of Python

49 videos|38 docs|18 tests

Top Courses Software Development

Signup to see your scores go up within 7 days!

Study with 1000+ FREE Docs, Videos & Tests
10M+ students study on EduRev