Test Paper: Python Basics + NumPy
Total Marks: 70
Time: 90 Minutes
Instructions:
• Section A: Attempt all 30 MCQs (1 mark each)
• Section B: Attempt all 10 Programming/Logical questions (5 marks each)
• Use only standard Python and NumPy libraries
Section A: Multiple Choice Questions (1 mark each)
1. What is the output of print(type([]))?
A) <class 'list'> B) <class 'tuple'> C) <class 'dict'> D) <class 'set'>
2. Which of the following is not a valid Python data type?
A) list B) map C) set D) float
3. What does the len() function return for len("Dalvik")?
A) 5 B) 6 C) 7 D) Error
4. What is the output of: print(3**2)?
A) 6 B) 9 C) 8 D) 12
5. What keyword is used to define a function in Python?
A) fun B) def C) function D) define
6. Which of the following is used to comment a single line in Python?
A) /* */ B) <!-- --> C) # D) //
7. Which method adds an element at the end of a list?
A) add() B) append() C) insert() D) extend()
8. What is the output of:
x = [1, 2, 3]
print(x[1])
A) 1 B) 2 C) 3 D) Error
9. Which of the following is immutable?
A) List B) Dictionary C) Set D) Tuple
10.Which of the following is a Python tuple?
A) [1, 2, 3] B) (1, 2, 3) C) {1, 2, 3} D) "1, 2, 3"
11.What will type(10.5) return?
A) int B) float C) double D) complex
12.How do you import NumPy in Python?
A) import numpy B) import numpy as np C) from numpy import * D) All of the above
13.What is the output of np.zeros(3)?
A) [0, 0, 0] B) (0, 0, 0) C) [0. 0. 0.] D) Error
14.What does np.arange(2, 10, 2) return?
A) [2, 4, 6, 8, 10] B) [2, 4, 6, 8] C) [2, 3, 4, 5, 6, 7, 8, 9] D) [3, 5, 7, 9]
15.Which function returns the shape of a NumPy array?
A) shape() B) getshape() C) .shape D) .get_shape()
16.What is broadcasting in NumPy?
A) Converting lists to arrays
B) Repeating smaller arrays for arithmetic operations
C) Sorting elements
D) Transposing arrays
17.Which NumPy function creates an identity matrix?
A) np.eye() B) np.ident() C) np.ones() D) np.identity()
18.np.array([[1,2],[3,4]]).shape returns:
A) (4,) B) (2,2) C) (1,4) D) (2,1)
19.Which operation is invalid in NumPy arrays?
A) Element-wise addition
B) Matrix multiplication
C) Element-wise exponentiation
D) Adding arrays of unequal shape without broadcasting
20.What does np.linspace(0, 1, 5) return?
A) [0, 0.25, 0.5, 0.75, 1]
B) [0, 1, 2, 3, 4]
C) [0.2, 0.4, 0.6, 0.8, 1.0]
D) Error
21.What is slicing in Python used for?
A) To access characters B) To modify variables C) To loop D) None
22.Correct way to declare a dictionary?
A) {"key": "value"} B) ["key": "value"] C) ("key": "value") D) {"key", "value"}
23.What is the output of bool([])?
A) True B) False C) Error D) None
24.np.sum([1,2,3]) gives:
A) 1 B) 3 C) 6 D) Error
25.What does np.mean([2,4,6,8]) return?
A) 5 B) 4 C) 6 D) 10
26.np.random.rand(2,2) returns:
A) Identity matrix
B) Random integers
C) Random floats in [0,1)
D) Zeros
27.NumPy is used for:
A) Machine learning only
B) Web development
C) Numerical computing
D) UI designing
28.How to reshape a 1D array to 2D?
A) arr.reshape(rows, cols)
B) arr.to_2d()
C) reshape(arr)
D) arr.flatten()
29.arr = np.array([1, 2, 3]), What is arr * 2?
A) [1,2,3,1,2,3] B) [2,4,6] C) Error D) [1, 2, 3, 2]
30.Which function is used to create random integers in NumPy?
A) np.random.randint() B) np.rand() C) np.random() D) np.randint()
Section B: Programming / Logical Questions (5 marks each)
1. Write a Python function to check if a number is prime.
2. Write a program to reverse a string without using any built-in function.
3. Using NumPy, create a 3x3 matrix filled with random integers from 1 to 10.
4. Write a NumPy program to add two 2D matrices.
5. Write a Python function to find the factorial of a number using recursion.
6. Create a NumPy array from a Python list and multiply all elements by 3.
7. Write a function to check whether a string is a palindrome or not.
8. Write a program using NumPy to create a 5x5 identity matrix