All Grades- Python.docx
All Grades- Python.docx
a) A constant value
b) A reserved word
c) A named storage location for data
d) A mathematical operation
a) =
b) :
c) ->
d) -
Correct Answer: ✅ a) =
3. In Python, variable names are case-sensitive. What does this mean?
Correct Answer:
different).
✅ c) Variable names can have mixed case (e.g., MyVariable and myvariable are
4. Which of the following is a valid Python variable name?
a) 123variable
b) my_variable
c) $variable
d) variable-1
a) 0
b) 1
c) 5
d) "x"
Correct Answer: ✅ c) 5
6. Which data type is automatically assigned to a variable when you don't specify a type explicitly in
Python?
a) To delete a file
b) To delete a variable
c) To delete a function
d) To delete a comment
a) `str1 + str2`
b) `str1 . str2`
c) `str1 & str2`
d) `str1 : str2`
a) 2.5
b) 2
c) 2.0
d) 2.25
a) ^
b) **
c) //
d) %
Correct Answer: ✅ b) **
11. What is the purpose of the `input()` function in Python?
a) To display output
b) To convert data types
c) To take user input
d) To declare variables
a) 4
b) 5
c) 6
d) It will result in an error.
Correct Answer: ✅ c) 6
13. Which data type is used to store a sequence of characters in Python?
a) Integer
b) String
c) Boolean
d) Float
```
x=5
y = "10"
z=x+y
print(z)
```
a) 15
b) 510
c) "510"
d) It will result in an error.
a) To define a function
b) To create a loop
c) To make decisions and execute code conditionally
d) To print text to the console
a) ==
b) =
c) !=
d) >
Correct Answer: ✅ a) ==
3. What is the result of the expression `5 > 3`?
a) True
b) False
c) 8
d) Error
Correct Answer: ✅ c) It provides an alternative condition to check if the `if` condition is false.
x = 10
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")
a) x is greater than 5
b) x is less than 5
c) x is less than or equal to 5
d) x is equal to 5
a) `if x == 7:`
b) `if x = 7:`
c) `if x != 7:`
d) `if x <> 7:`
a) 2 spaces
b) 4 spaces
c) 8 spaces
d) Tab character
a)
if x % 2 == 0:
print("Even")
if x is even:
print("Even")
c)
if x % 2:
print("Even")
d)
if x == even:
print("Even")
Correct Answer: ✅ a)
if x % 2 == 0:
print("Even")
a) True
b) False
c) None
d) Error
a) `in`
b) `not`
c) `and`
d) `or`
age = 16
if age >= 18:
print("You are an adult.")
elif age >= 13:
print("You are a teenager.")
else:
print("You are a child.")
a) To define a function
b) To create a conditional statement
c) To iterate over a sequence of items
d) To print text to the console
count = 0
while count < 5:
print(count)
count += 1
a) 0
b) 1
c) 2
d) 4
Correct Answer: ✅ d) 4
4. What is an infinite loop in Python?
Correct Answer: ✅ b) A loop that runs indefinitely until it's manually terminated.
5. What does the `continue` statement do in a `while` loop?
Correct Answer: ✅ c) It skips the current iteration and continues with the next.
Follow us on Instagram for additional resources: zero1.education
6. What is the purpose of the `else` block in a `while` loop?
a) `while True:`
b) `while False:`
c) `while 1 == 1:`
d) `while count < 5:`
i=5
while i > 0:
print(i)
i -= 1
a) 0
b) 5
c) 1
d) 4
Correct Answer: ✅ c) 1
12. In Python, what is the purpose of the `input()` function in the context of a `while` loop?
Correct Answer: ✅ c) It reads user input and allows interactive input during the loop.
13. What does the `while` loop condition check in each iteration?
x=0
while x < 5:
x += 2
print(x)
a) 2
b) 4
c) 5
d) 6
Correct Answer: ✅ d) 6
a) `skip`
b) `next`
c) `continue`
d) `break`
a) To define a function
b) To create a conditional statement
c) To iterate over a sequence of items
d) To print text to the console
a) A random number
b) A sequence of integers
c) A string
d) A floating-point number
for i in range(3):
print(i)
a) 0
b) 1
c) 2
d) 3
Correct Answer: ✅ c) 2
5. What will be the output of the following code?
Correct Answer:
banana
✅ b) apple
cherry
Correct Answer: ✅ c) It skips the current iteration and continues with the next.
7. What is the purpose of the `else` block in a `for` loop?
Correct Answer:
statement.
✅ b) It is executed if the `for` loop completes without encountering a `break`
8. How can you create a nested `for` loop in Python?
for i in range(5):
if i == 2:
continue
print(i)
a) 0 1 2 3 4
b) 0 1 3 4
c) 0 1 2 3
d) 0 3 4
Correct Answer: ✅ b) 0 1 3 4
a) `skip`
b) `next`
c) `continue`
d) `break`
4. Which of the following is NOT a valid data type for a variable in Python?
a) Integer
b) String
c) Boolean
d) Character ✅
5. What happens if you try to use a variable that has not been assigned a value?
a) The program crashes
b) Python automatically assigns a default value
c) An error occurs ✅
d) The variable is ignored
2. The ______ statement is used within a while loop to update the condition and
prevent an infinite loop.✅increment
3. The syntax of a while loop in Python consists of the keyword ______, followed by a
condition, and a colon. ✅ while
7. The ______ statement is used to initialize the variable before entering the while
loop.✅ initialization
8. The ______ statement is used to update the variable within the while loop. ✅
update
9. In a while loop, the condition is checked ______ the execution of the loop block. ✅
before
10. The ______ statement is used to prevent the loop from executing any further and
move to the next iteration. ✅
continue
11. The ______ statement is used to exit the loop completely. ✅ break
12. The ______ function can be used to iterate over a sequence until a certain
condition is met. ✅
while
13. The ______ statement is used to increment or decrement the loop control
variable.✅ update
14. The ______ statement is used to initialize the loop control variable. ✅
initialization
Program 1:
Program to check if a number is positive or negative
Program 2:
Program to check if a number is even or odd
Program 3:
Program to check if a student has passed or failed
Program 4:
Program to check if a year is a leap year
Program 5:
Program to check if a character is a vowel or consonant
char = input("Enter a character: ")
if char.lower() in ['a', 'e', 'i', 'o', 'u']:
print("The character is a vowel.")
else:
print("The character is a consonant.")
Program 6:
Program to check if a number is positive, negative, or zero
num = int(input("Enter a number: "))
if num > 0:
print("The number is positive.")
elif num < 0:
print("The number is negative.")
else:
print("The number is zero.")
Program 7:
Program to check if a number is divisible by both 2 and 3
num = int(input("Enter a number: "))
if num % 2 == 0 and num % 3 == 0:
print("The number is divisible by both 2 and 3.")
else:
print("The number is not divisible by both 2 and 3.")
Program 8:
Program to check if a person is eligible to vote
age = int(input("Enter your age: "))
voting_age = 18
if age >= voting_age:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
Program 1:
Program to print numbers from 1 to 5 using a while loop
num = 1
while num <= 5:
print(num)
num += 1
Program 2:
Program to calculate the sum of numbers from 1 to 10 using a while loop
num = 1
sum = 0
while num <= 10:
sum += num
num += 1
print("The sum is:", sum)
Program 3:
Program to print even numbers from 2 to 10 using a while loop
num = 2
while num <= 10:
print(num)
num += 2
Program 4:
Program to find the factorial of a number using a while loop
Program 5:
Program to check if a number is prime using a while loop
num = int(input("Enter a number: "))
Program 6:
Program to reverse a number using a while loop
Program 7:
Program to find the sum of digits in a number using a while loop
Program 8:
Program to print a pattern using a while loop
10. The "len()" function is used to get the length of a string or a list in Python. (True)
11. Python has a built-in module for working with regular expressions. (True)
12. The "if" statement is used for conditional execution in Python. (True)
15. Python has a built-in module for working with dates and times. (True)
16. The "for" loop is used to iterate over a sequence of elements in Python. (True)
18. The "random" module in Python is used for generating random numbers. (True)
19. Python has a built-in module for working with files. (True)
20. The "while" loop is used for repetitive execution in Python. (True)