Python Class 7 Practice Questions SET 3 – If Condition, Loops, Range & Programming Exercises 

MULTIPLE CHOICE QUESTIONS

  1. What will be the output of the following code? for i in range(1, 6, 2): print(i, end=" ") a. 1 2 3 4 5
    b. 1 3 5
    c. 2 4
    d. 1 5
  2. Which of the following can create an infinite loop if used incorrectly?
    a. for with range()
    b. while with condition always True
    c. for with step size 2
    d. range(0)
  3. In nested loops, if the outer loop runs 4 times and the inner loop runs 3 times, the total iterations are:
    a. 7
    b. 12
    c. 81
    d. 64
  4. Which statement is correct about the else clause in loops?
    a. It executes only when break is used
    b. It executes only when loop ends normally
    c. It executes only in while loop
    d. It never executes
  5. Which of the following programs will print numbers 1, 2, 3 repeatedly forever?
    a. for i in range(1,4): print(i)
    b. while True: for i in range(1,4): print(i)
    c. for i in range(1,4): while True: print(i)
    d. Both b and c

FILL IN THE BLANKS

  1. In Python, loops inside other loops are called ______ loops.
  2. A loop with multiple conditions joined by and/or operators is called a ______ condition loop.
  3. The statement that immediately stops execution of the current loop is ______.
  4. If the step value is negative, the range function generates numbers in ______ order.
  5. The output of range(10, 2, -2) is ______.

DESCRIPTIVE QUESTIONS

  1. Explain how nested loops can be used to display patterns with an example.
  2. Differentiate between break, continue, and pass with small code snippets.
  3. Why is it dangerous to write a while loop without updating the loop variable? Give an example.
  4. Describe how logical operators (and/or) can be combined with if statements in loops.
  5. Write the dry run (step-by-step execution) of the following code: for i in range(2, 5): for j in range(1, 3): print(i * j)

PROGRAMMING QUESTIONS

  1. Write a program to print the following pattern using nested loops: * * * * * * * * * *
  2. Write a program to calculate the sum of all prime numbers between 1 and 50.
  3. Write a program that asks the user for a number and checks if it is an Armstrong number (e.g., 153 = 1³+5³+3³).
  4. Write a program that prints all numbers between 1 and 100 which are divisible by both 3 and 5.
  5. Write a program using a while loop that keeps asking for input until the user enters “exit”.
Answer Key

MULTIPLE CHOICE QUESTIONS

  1. b. 1 3 5
  2. b. while with condition always True
  3. b. 12
  4. b. It executes only when loop ends normally
  5. b. while True: for i in range(1,4): print(i)

FILL IN THE BLANKS

  1. nested
  2. compound (or multiple)
  3. break
  4. reverse / descending
  5. [10, 8, 6, 4]

DESCRIPTIVE QUESTIONS

  1. Nested loop for patterns
    Example – printing a right triangle pattern: for i in range(1, 5): for j in range(i): print("*", end=" ") print() Output: * * * * * * * * * *
  2. Break, Continue, Pass
    • break: exits the loop completely.
    • continue: skips current iteration.
    • pass: does nothing, acts as placeholder.
    for i in range(1,6): if i == 2: continue # skips 2 if i == 4: break # exits loop if i == 3: pass # does nothing print(i) # Output: 1, 3
  3. Danger of not updating loop variable
    If loop variable never changes, condition remains True → infinite loop. i = 1 while i < 5: # Infinite loop print(i) # i not updated
  4. Using logical operators with if in loops for i in range(1, 11): if i % 2 == 0 and i % 3 == 0: print(i, "divisible by 2 and 3")
  5. Dry run of given codefor i in range(2, 5): # i = 2, 3, 4 for j in range(1, 3): # j = 1, 2 print(i * j) Steps:
    • i=2 → j=1 → 2, j=2 → 4
    • i=3 → j=1 → 3, j=2 → 6
    • i=4 → j=1 → 4, j=2 → 8
      Output: 2, 4, 3, 6, 4, 8

PROGRAMMING QUESTIONS

Loop until “exit” entered while True: text = input("Enter text (type 'exit' to stop): ") if text.lower() == "exit": break print("You typed:", text)

Pattern printing for i in range(1, 5): for j in range(i): print("*", end=" ") print()

Sum of prime numbers (1–50) total = 0 for num in range(2, 51): for i in range(2, num): if num % i == 0: break else: total += num print("Sum of primes from 1 to 50:", total)

Armstrong number check num = int(input("Enter a number: ")) temp = num power = len(str(num)) total = 0 while temp > 0: digit = temp % 10 total += digit ** power temp //= 10 if total == num: print("Armstrong number") else: print("Not Armstrong")

Numbers divisible by 3 and 5 (1–100) for i in range(1, 101): if i % 3 == 0 and i % 5 == 0: print(i)

Leave a Reply

Discover more from Abhyas

Subscribe now to keep reading and get access to the full archive.

Continue reading