Explore topic-wise InterviewSolutions in Current Affairs.

This section includes 7 InterviewSolutions, each offering curated multiple-choice questions to sharpen your Current Affairs knowledge and support exam preparation. Choose a topic below to get started.

1.

Write a program for interchanging two axes of the NumPy array.

Answer»

This can be achieved by using the swapaxes method of NumPy. The below image illustrates the meaning of swapping axes.

import numpy as np
arr = np.array([[1,2,3]])
print("Original array: ")
print(arr)

#Swap axes
axis_swapped_arr = np.swapaxes(arr,0,1)
print("Transformed array: ")
print(axis_swapped_arr)

Output:

Original array:
[[1 2 3]]
Transformed array:
[[1]
[2]
[3]] Conclusion

The popularity of the NumPy package has grown immensely among the data science and python developers community ever since it was first introduced in 2005 due to the wide range of high-performing functionalities it offers. This is why it becomes essential to learn and be prepared for the interview questions about this package. In this article, we have seen the most commonly asked NumPy interview questions for freshers and experienced people, along with some questions on writing python programs which make use of NumPy functions.

References Interview Guides

  • https://www.interviewbit.com/technical-interview-questions/

  • https://www.interviewbit.com/coding-interview-questions/

  • https://www.interviewbit.com/mock-interview/

  • https://www.interviewbit.com/blog/

  • https://www.interviewbit.com/blog/pandas-vs-numpy/


2.

Write a program for changing the dimension of a NumPy array.

Answer»

We can achieve this by overriding the shape attribute of the NumPy array.

Sample Solution:

import numpy as np

#Create NumPy array
arr = np.array([1,2,3,4,5,6,7,8,9])
print("Original Shape: ", arr.shape)

# Change the shape/dimension of the array
arr.shape = (3, 3)
print("Transformed Matrix :")
print(arr)
print("Transformed Shape: ",arr.shape)

Output:

Original Shape: (9,)
Transformed Matrix :
[[1 2 3]
[4 5 6]
[7 8 9]]
Transformed Shape: (3, 3)

In this approach, care has to be taken w.r.t the number of elements present in the original array before changing the dimensions. Otherwise, it will result in the ValueError as shown below:

import numpy as np

# We have array of 8 elements
arr = np.array([1,2,3,4,5,6,7,8])

# We are trying to convert the 1D array to a 3D array which expects 9 elements
arr.shape = (3, 3)
print(arr)

Running this code would result in:

1 import numpy as np
2 arr = np.array([1,2,3,4,5,6,7,8])
----> 3 arr.shape = (3, 3)
4 print(arr)

ValueError: cannot reshape array of size 8 into shape (3,3)
3.

Write a program to add a border of zeros around the existing array.

Answer»

For example,
If you have the below array:

[[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]]

The resultant array should be: (zeros on the border and 1s within it)

[[ 0. 0. 0. 0. 0. 0.]
[ 0. 1. 1. 1. 1. 0.]
[ 0. 1. 1. 1. 1. 0.]
[ 0. 1. 1. 1. 1. 0.]
[ 0. 0. 0. 0. 0. 0.]]

Solution:-
This can be achieved by using the pad method of the NumPy library.

import numpy as np

# Create NumPy arrays filled with ones
ones_arr = np.ones((4,4))

print("Transformed array:")
transformed_array = np.pad(ones_arr, pad_width=1, mode='constant', constant_values=0)
print(transformed_array)

Output:

Transformed array:
[[0. 0. 0. 0. 0. 0.]
[0. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 0.]
[0. 0. 0. 0. 0. 0.]]
4.

Write a program for creating an integer array with values belonging to the range 10 and 60

Answer»
import numpy as np
arr = np.arange(10, 60)
print(arr)

Output:

[10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
58 59]
5.

Write a program to repeat each of the elements five times for a given array.

Answer»

Sample Solution:-

import numpy as np
# Create Sample NumPy Array
arr = np.array(['i', 'love', 'NumPy', 'AND', 'interviewbit'], dtype=str)

transformed_array = np.char.multiply(arr, 5)
print("Transformed array:")
print(transformed_array)

Output:

Transformed array:
['iiiii' 'lovelovelovelovelove' 'NumPyNumPyNumPyNumPyNumPy'
'ANDANDANDANDAND'
'interviewbitinterviewbitinterviewbitinterviewbitinterviewbit']
6.

Write a program for inserting space between characters of all elements in a NumPy array.

Answer»

Sample Solution:-

import numpy as np

# Create Sample NumPy Array
arr = np.array(['i', 'love', 'NumPy', 'AND', 'interviewbit'], dtype=str)

transformed_arr = np.char.join(" ", arr)

print("Transformed Array: ")
print(transformed_arr)

Output:

Transformed Array:
['i' 'l o v e' 'N u m P y' 'A N D' 'i n t e r v i e w b i t']
7.

Write a program to transform elements of a given string to a numeric string of 10 digits by making all the elements of a given string to a numeric string of 8 digits with zeros on the left.

Answer»

Sample Solution:-

import numpy as np

# Create Sample NumPy array
arr = np.array(['22', '9', '1234', '567', '89102'], dtype=str)

zeroes_filled_arr = np.char.zfill(arr, 8)
print("Transformed array: ")
print(zeroes_filled_arr)

Output:

Transformed array:
['00000022' '00000009' '00001234' '00000567' '00089102']
8.

Write a program to convert a string element to uppercase, lowercase, capitalise the first letter, title-case and swapcase of a given NumPy array.

Answer»

Sample Solution:-

import numpy as np

# Create Sample NumPy array
arr = np.array(['i', 'love', 'NumPy', 'AND', 'interviewbit'], dtype=str)

upper_case_arr = np.char.upper(arr)
lower_case_arr = np.char.lower(arr)
capitalize_case_arr = np.char.capitalize(arr)
titlecase_arr = np.char.title(arr)
swapcase_arr = np.char.swapcase(arr)

print("Upper Conversion: ", upper_case_arr)
print("Lower Conversion: ", lower_case_arr)
print("Capitalize First Letter Conversion: ", capitalize_case_arr)
print("Titlecase Conversion: ", titlecase_arr)
print("Swapcase Conversion: ", swapcase_arr)

Output:

Upper Conversion: ['I' 'LOVE' 'NUMPY' 'AND' 'INTERVIEWBIT']
Lower Conversion: ['i' 'love' 'numpy' 'and' 'interviewbit']
Capitalize First Letter Conversion: ['I' 'Love' 'Numpy' 'And' 'Interviewbit']
Titlecase Conversion: ['I' 'Love' 'Numpy' 'And' 'Interviewbit']
Swapcase Conversion: ['I' 'LOVE' 'nUMpY' 'and' 'INTERVIEWBIT']
Previous Next