Free Mettl Coding Fundamentals and Logic Questions and Answers — Questions and Answers
Question 1: Which of the following statements accurately describes the primary characteristic of a Stack data structure?
- It allows direct access to any element using an index.
- Elements are processed in a First-In, First-Out (FIFO) order.
- Elements are inserted and removed from the same end, following a Last-In, First-Out (LIFO) principle. (Correct answer)
- It stores key-value pairs for efficient lookup.
Correct answer: Elements are inserted and removed from the same end, following a Last-In, First-Out (LIFO) principle.
A Stack is an abstract data type that serves as a collection of elements. Its principal operations are 'push' (adding an element) and 'pop' (removing the most recently added element). This behavior is known as Last-In, First-Out (LIFO).
Question 2: Analyze the following pseudocode snippet. What is its time complexity in Big O notation, where 'N' is the size of the `data_array`? ``` FUNCTION process_data(data_array) FOR i FROM 0 TO N-1 PRINT data_array[i] ENDFOR ENDFUNCTION ```
- O(N^2) - Quadratic time
- O(1) - Constant time
- O(log N) - Logarithmic time
- O(N) - Linear time (Correct answer)
Correct answer: O(N) - Linear time
The code contains a single loop that iterates through the `data_array` once. The number of operations is directly proportional to the number of elements (N) in the array. Therefore, the time complexity is linear, represented as O(N).
Question 3: What will be the final value of the variable `counter` after the following pseudocode is executed? ``` SET counter = 0 SET i = 1 WHILE i <= 5 IF i % 2 == 0 THEN counter = counter + i ENDIF i = i + 1 ENDWHILE ```
- 15
- 6 (Correct answer)
- 9
- 5
Correct answer: 6
The loop runs for `i` values from 1 to 5. The `IF` condition `i % 2 == 0` checks if `i` is an even number. The `counter` is only incremented when `i` is even. The even numbers in the range are 2 and 4. Thus, the final value of `counter` will be 0 + 2 + 4 = 6.
Question 4: A program has three boolean variables: `is_registered`, `has_paid`, and `is_admin`. Access is granted if a user is an admin, OR if they are registered AND have paid. Which boolean expression correctly represents this logic?
- is_registered AND (has_paid OR is_admin)
- (is_registered OR has_paid) AND is_admin
- is_registered OR (has_paid AND is_admin)
- (is_registered AND has_paid) OR is_admin (Correct answer)
Correct answer: (is_registered AND has_paid) OR is_admin
The condition has two main parts connected by 'OR': 1) the user is an admin (`is_admin`), or 2) the user is registered AND has paid (`is_registered AND has_paid`). Combining these with OR gives the correct expression: `(is_registered AND has_paid) OR is_admin`.
Question 5: Consider the following recursive function written in pseudocode. What is the result of calling `calculate(4)`? ``` FUNCTION calculate(n) IF n <= 1 THEN RETURN 1 ELSE RETURN n + calculate(n - 1) ENDIF ENDFUNCTION ```
- 24
- 10 (Correct answer)
- 9
- 4
Correct answer: 10
This function recursively calculates the sum of numbers from `n` down to 1. The calls would be: `calculate(4)` -> 4 + `calculate(3)` -> 4 + (3 + `calculate(2)`) -> 4 + (3 + (2 + `calculate(1)`)) -> 4 + (3 + (2 + 1)). The final result is 10.
Question 6: In the context of programming, what is the primary purpose of using a local variable inside a function?
- To share its value with all other functions in the program.
- To retain its value even after the program has finished executing.
- To limit the variable's existence and accessibility to only within that function, preventing unintended side effects. (Correct answer)
- To declare a variable that is accessible from anywhere in the code.
Correct answer: To limit the variable's existence and accessibility to only within that function, preventing unintended side effects.
Local variables are declared within a function and their scope is limited to that function. This is a fundamental principle of encapsulation that helps prevent conflicts with variables of the same name in other functions and avoids unintended modifications to data, making code more modular and easier to debug.
Which of the following statements accurately describes the primary characteristic of a Stack data structure?