← All Mettl Flashcard Decks

Coding Fundamentals and Logic Flashcards

7 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 7 Coding Fundamentals and Logic flashcards as text
  1. What is the output of the following code? for i in range(1, 6): if i % 2 == 0: print(i, end=' ')

    Answer: 2 4

    The loop runs i=1 to 5; only even values (2 and 4) satisfy i%2==0.

  2. A variable declared inside a function in most programming languages has _____ scope.

    Answer: Local

    Variables declared inside a function have local scope and are not accessible outside it.

  3. Which of the following is an example of a runtime error?

    Answer: Division by zero

    Division by zero is detected only at runtime when the denominator is evaluated as zero.

  4. What does the following loop print? i = 1 while i < 32: i *= 2 print(i)

    Answer: 32

    i doubles: 1→2→4→8→16→32; the loop exits when i=32, which is then printed.

  5. Which principle states that a class should have only one reason to change?

    Answer: Single Responsibility Principle

    The Single Responsibility Principle (SRP) states each class should have exactly one responsibility.

  6. What is the output of: print(bool(0), bool(''), bool([1]))?

    Answer: False False True

    0 and '' are falsy, while a non-empty list [1] is truthy in Python.

  7. In Big-O notation, which function grows the slowest as n increases?

    Answer: O(log n)

    O(log n) grows slower than O(√n), O(n), and O(n log n) for large n.