Mettl Fundamental Coding Skills 2 ā Questions and Answers
Question 1: What is the time complexity of accessing an element in an array by index?
- O(n)
- O(log n)
- O(1) (Correct answer)
- O(n²)
Correct answer: O(1)
Array index access is O(1) because the memory address is computed directly from the base address and index.
Question 2: Which of the following correctly declares a constant in JavaScript?
- let PI = 3.14;
- var PI = 3.14;
- constant PI = 3.14;
- const PI = 3.14; (Correct answer)
Correct answer: const PI = 3.14;
The `const` keyword declares a block-scoped constant that cannot be reassigned after initialization.
Question 3: What does the modulo operator (%) return?
- The quotient of division
- The remainder after division (Correct answer)
- The absolute value
- The floor of the division
Correct answer: The remainder after division
The modulo operator returns the remainder when the left operand is divided by the right operand.
Question 4: Which data structure follows the LIFO (Last In, First Out) principle?
- Queue
- Linked List
- Stack (Correct answer)
- Tree
Correct answer: Stack
A stack is a LIFO structure where the last element pushed is the first one popped.
Question 5: What will `print(type([]))` output in Python?
- <class 'tuple'>
- <class 'array'>
- <class 'dict'>
- <class 'list'> (Correct answer)
Correct answer: <class 'list'>
Square brackets `[]` create a list in Python, so `type([])` returns `<class 'list'>`.
Question 6: In most programming languages, what is the index of the first element of an array?
- 1
- 0 (Correct answer)
- -1
- It depends on array length
Correct answer: 0
Most languages (C, Java, Python, JavaScript) use zero-based indexing, so the first element is at index 0.
Question 7: What is the output of the following Python code: `x = 5; x += 3; print(x)`?
- 5
- 3
- 8 (Correct answer)
- 53
Correct answer: 8
`x += 3` is shorthand for `x = x + 3`, so x becomes 5 + 3 = 8.
What is the time complexity of accessing an element in an array by index?