Question 1
EasyWhat is the index of the first element of an array in C, Java and Python?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
What is the index of the first element of an array in C, Java and Python?
It depends on the size
−1
0
1
Answer: C
These languages use zero-based indexing.
What is the time complexity of inserting an element at the beginning of an array of n elements?
O(1)
O(n²)
O(n)
O(log n)
Answer: C
Every existing element must shift one place to the right.
What is the output of this code?
a = [1, 2, 3]
a.append(4)
print(a)
[4, 3, 2, 1]
[1, 2, 3, 4]
[1, 2, 3]
[4]
Answer: B
append adds 4 to the end of the list.
What is the output of this code?
a = [1, 2, 3, 4, 5]
print(a[::-1])
[5, 4, 3, 2, 1]
[5]
Answer: A
The slice [::-1] steps backwards through the list, giving a reversed copy.
Which data structure stores elements of the same type in contiguous memory locations?
Array
Graph
Linked list
Tree
Answer: A
Contiguous storage is what makes indexed access O(1).
What is the output of this code?
a = [10, 20, 30, 40]
print(a[-2])
Error
20
Answer: D
Negative indices count from the end in Python: a[-2] is the second-last element, 30.
How many elements are there in a 2D array declared as int a[3][4]?
12
7
4
3
Answer: A
3 rows × 4 columns = 12 elements.
An int takes 4 bytes. How many bytes does the array int a[10] occupy?
40
14
10
4
Answer: A
10 elements × 4 bytes = 40 bytes, stored contiguously.
With 0-based indexing, what is the index of the last element of an array of n elements?
n
n − 1
n + 1
1
Answer: B
Indices run from 0 to n − 1.
In C, what happens if you read a[10] from an array declared as int a[10]?
It returns a[0]
The compiler always reports an error
Undefined behaviour: it reads outside the array's bounds
It returns 0
Answer: C
C does no bounds checking; valid indices are 0 to 9.
[1]
[1, 2, 3, 4, 5]
40
30