Assignment: Loops in Python

# Assignment: Loops in Python - Basics of Python - Software Development

 Table of contents Multiple Choice Questions (MCQs) High Order Thinking Questions (HOTS) Fill in the Blanks True or False Hands-On Questions ## Multiple Choice Questions (MCQs)

Q.1. Which loop statement in Python is used to iterate over a sequence of elements?
(a) if statement
(b) for statement
(c) while statement
(d) switch statement

Ans. (b)

Q.2. What is the output of the following code snippet?
numbers = [1, 2, 3, 4, 5]
total = 0
for num in numbers:
total += num
print(total)
(a) 1
(b) 2
(c) 3
(d) 15

Ans. (d)

Q.3. Which loop statement in Python is used to execute a block of code as long as a certain condition is true
(a) if statement
(b) for statement
(c) while statement
(d) switch statement

Ans. (c)

Q.4. What is the output of the following code snippet?
count = 0
while count < 5:
print(count)
count += 1
(a) 0 1 2 3 4
(b) 1 2 3 4 5
(c) 0 1 2 3 4 5
(d) 1 2 3 4

Ans. (a)

Q.5. Which loop statement in Python is used to repeat a block of code for a fixed number of times?
(a) if statement
(b) for statement
(c) while statement
(d) switch statement

Ans. (c)

## High Order Thinking Questions (HOTS)

Q.1. Write a Python program using a for loop to print the first 10 even numbers.

for i in range(1, 21):

if i % 2 == 0:

print(i)

Q.2. Write a Python program using a while loop to find the sum of digits of a given number.

num = int(input("Enter a number: "))

sum_of_digits = 0

while num > 0:

digit = num % 10

sum_of_digits += digit

num //= 10

print("Sum of digits:", sum_of_digits)

Q.3. Write a Python program using a loop to check if a given string is a palindrome or not. A palindrome is a word or phrase that is the same when read backward.

word = input("Enter a word: ")

reversed_word = word[::-1]

if word == reversed_word:

print("Palindrome")

else:

print("Not a palindrome")

Q.4. Write a Python program to generate the Fibonacci sequence using a loop. The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones.

n = int(input("Enter the number of terms: "))

fib_sequence = [0, 1]

for i in range(2, n):

next_term = fib_sequence[i-1] + fib_sequence[i-2]

fib_sequence.append(next_term)

print(fib_sequence)

Q.5. Write a Python program to print the multiplication table of a given number using a loop.

num = int(input("Enter a number: "))

for i in range(1, 11):

product = num * i

print(f"{num} x {i} = {product}")

## Fill in the Blanks

Q.1. In Python, the range() function is commonly used with the _____ statement to iterate over a sequence of numbers.

Ans. for

Q.2. The _____ loop is used when we want to execute a block of code for a fixed number of times.

Ans. for

Q.3. The _____ statement is used to exit a loop prematurely, bypassing the rest of the statements inside the loop.

Ans. break

Q.4. The _____ statement is used to skip the current iteration and move to the next iteration of a loop.

Ans. continue

Q.5. The _____ loop is used when we want to execute a block of code as long as a certain condition is true.

Ans. while

## True or False

Q.1. In Python, a for loop can only iterate over a list of elements.

Ans. False

Q.2. The continue statement can be used in both for and while loops to skip the rest of the statements inside the loop and move to the next iteration.

Ans. True

Q.3. The break statement is used to exit a loop prematurely, regardless of the loop condition.

Ans. True

Q.4. The while loop is guaranteed to execute at least once, even if the condition is initially False.

Ans. False

Q.5. The range() function generates a sequence of numbers starting from 1 by default.

Ans. False

## Hands-On Questions

Q.1. Write a Python program using a loop to find the factorial of a given number. The factorial of a number is the product of all positive integers less than or equal to that number.

num = int(input("Enter a number: "))

factorial = 1

for i in range(1, num + 1):

factorial *= i

print("Factorial:", factorial)

Q.2. Write a Python program using a loop to check if a given number is prime or not. A prime number is a number greater than 1 that has no positive divisors other than 1 and itself.

num = int(input("Enter a number: "))

is_prime = True

if num > 1:

for i in range(2, int(num ** 0.5) + 1):

if num % i == 0:

is_prime = False

break

else:

is_prime = False

if is_prime:

print("Prime")

else:

print("Not prime")

Q.3. Write a Python program using a loop to find the sum of all the prime numbers between 1 and a given number.

limit = int(input("Enter a number: "))

sum_of_primes = 0

for num in range(2, limit + 1):

is_prime = True

for i in range(2, int(num ** 0.5) + 1):

if num % i == 0:

is_prime = False

break

if is_prime:

sum_of_primes += num

print("Sum of prime numbers:", sum_of_primes)

Q.4. Write a Python program using a loop to reverse a given string.

word = input("Enter a string: ")

reversed_word = ""

for char in word:

reversed_word = char + reversed_word

print("Reversed string:", reversed_word)

Q.5. Write a Python program using a loop to count the number of vowels in a given string.

word = input("Enter a string: ")

vowel_count = 0

for char in word:

if char.lower() in ['a', 'e', 'i', 'o', 'u']:

vowel_count += 1

print("Number of vowels:", vowel_count)

The document Assignment: Loops in Python | Basics of Python - Software Development is a part of the Software Development Course Basics of Python.
All you need of Software Development at this link: Software Development

## Basics of Python

49 videos|38 docs|18 tests

## Basics of Python

49 videos|38 docs|18 tests Explore Courses for Software Development exam Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Track your progress, build streaks, highlight & save important lessons and more!
Related Searches

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

;