โ† All Mettl Flashcard Decks

Coding Fundamentals and Logic Flashcards

6 cards from real Mettl practice questions. Tap to flip, then mark Knew It or Still Learning โ€” missed cards come back until you master them.

Read the first 6 Coding Fundamentals and Logic flashcards as text
  1. Which of the following statements accurately describes the primary characteristic of a Stack data structure?

    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).

  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 ```

    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).

  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 ```

    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.

  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?

    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`.

  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 ```

    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.

  6. In the context of programming, what is the primary purpose of using a local variable inside a function?

    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.