[go: up one dir, main page]

0% found this document useful (0 votes)
29 views25 pages

All Grades- Python.docx

Uploaded by

compsci909
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views25 pages

All Grades- Python.docx

Uploaded by

compsci909
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Multiple choice questions about using variables in Python

1. What is a variable in Python?

a) A constant value
b) A reserved word
c) A named storage location for data
d) A mathematical operation

Correct Answer: ✅ c) A named storage location for data


2. Which character is commonly used to assign a value to a variable in Python?

a) =
b) :
c) ->
d) -

Correct Answer: ✅ a) =
3. In Python, variable names are case-sensitive. What does this mean?

a) Variable names must be written in uppercase.


b) Variable names must be written in lowercase.
c) Variable names can have mixed case (e.g., MyVariable and myvariable are different).
d) Variable names cannot contain letters.

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

Correct Answer: ✅ b) my_variable


5. What is the value of a variable after executing the following code: `x = 5`?

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?

Follow us on Instagram for additional resources: zero1.education


a) Integer
b) String
c) Float
d) Dynamic

Correct Answer: ✅ d) Dynamic


7. What is the purpose of the `del` statement in Python?

a) To delete a file
b) To delete a variable
c) To delete a function
d) To delete a comment

Correct Answer: ✅ b) To delete a variable


8. How do you concatenate two strings stored in variables `str1` and `str2`?

a) `str1 + str2`
b) `str1 . str2`
c) `str1 & str2`
d) `str1 : str2`

Correct Answer: ✅ a) `str1 + str2`


9. What is the result of the expression `5 / 2` in Python?

a) 2.5
b) 2
c) 2.0
d) 2.25

Correct Answer: ✅ a) 2.5


10. Which operator is used for exponentiation in Python?

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

Correct Answer: ✅ c) To take user input


Follow us on Instagram for additional resources: zero1.education
12. What is the value of the variable `x` after executing the code `x = x + 1` if `x` initially equals 5?

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

Correct Answer: ✅ b) String


14. What is the output of the following code snippet?

```
x=5
y = "10"
z=x+y
print(z)
```

a) 15
b) 510
c) "510"
d) It will result in an error.

Correct Answer: ✅ d) It will result in an error.

Follow us on Instagram for additional resources: zero1.education


Multiple choice questions about using the `if` statement in Python
1. What is the primary purpose of the `if` statement in Python?

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

Correct Answer: ✅ c) To make decisions and execute code conditionally


2. Which of the following operators is used to compare two values for equality in Python?

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: ✅ a) True


4. In Python, how do you write an `if` statement with multiple conditions, where both conditions
must be true?

a) `if x and y:`


b) `if x || y:`
c) `if x & y:`
d) `if x or y:`

Correct Answer: ✅ a) `if x and y:`


5. What is the purpose of the `elif` keyword in Python?

a) It defines a new function.


b) It is used to handle exceptions.
c) It provides an alternative condition to check if the `if` condition is false.
d) It represents the end of the `if` statement.

Correct Answer: ✅ c) It provides an alternative condition to check if the `if` condition is false.

Follow us on Instagram for additional resources: zero1.education


6. What will be the output of the following code:

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

Correct Answer: ✅ a) x is greater than 5


7. How can you check if a variable `x` is not equal to 7 in Python?

a) `if x == 7:`
b) `if x = 7:`
c) `if x != 7:`
d) `if x <> 7:`

Correct Answer: ✅ c) `if x != 7:`


8. What is the purpose of the `else` statement in an `if-else` construct?

a) It defines a new condition to check.


b) It is executed if the `if` condition is true.
c) It is executed if none of the previous conditions are true.
d) It is not used in Python.

Correct Answer: ✅ c) It is executed if none of the previous conditions are true.


9. What is the indentation level used for code inside an `if` block in Python?

a) 2 spaces
b) 4 spaces
c) 8 spaces
d) Tab character

Correct Answer: ✅ b) 4 spaces


10. Which of the following code snippets will print "Even" if the variable `x` contains an even
number?

a)

if x % 2 == 0:
print("Even")

Follow us on Instagram for additional resources: zero1.education


b)

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

11. What does the `not` keyword do in Python?

a) It negates a boolean expression.


b) It checks if a variable is not defined.
c) It reverses the order of elements in a list.
d) It defines a new variable.

Correct Answer: ✅ a) It negates a boolean expression.


12. In Python, what will the `if` statement evaluate to if the condition inside it is an empty string
(`""`)?

a) True
b) False
c) None
d) Error

Correct Answer: ✅ b) False


13. Which operator is used to check if a value is present in a list or other iterable in Python?

a) `in`
b) `not`
c) `and`
d) `or`

Correct Answer: ✅ a) `in`

Follow us on Instagram for additional resources: zero1.education


14. How can you create a nested `if` statement in Python?

a) By using the `elif` keyword within an `if` block.


b) By using the `and` operator to combine conditions.
c) By placing one `if` statement inside another.
d) By using the `then` keyword.

Correct Answer: ✅ c) By placing one `if` statement inside another.


15. What will be the output of the following code?

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) You are an adult.


b) You are a teenager.
c) You are a child.
d) No output will be printed.

Correct Answer: ✅ b) You are a teenager.

Follow us on Instagram for additional resources: zero1.education


Multiple choice questions about using the `while` loop in Python.
1. What is the primary purpose of the `while` loop in Python?

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

Correct Answer: ✅ c) To iterate over a sequence of items


2. How do you exit a `while` loop prematurely in Python?

a) Using the `break` statement


b) Using the `continue` statement
c) By changing the loop variable
d) By using the `exit` keyword

Correct Answer: ✅ a) Using the `break` statement


3. What is the result of the following code?

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?

a) A loop that runs for a fixed number of iterations.


b) A loop that runs indefinitely until it's manually terminated.
c) A loop that prints infinitely long sequences.
d) A loop that calculates infinite decimal places.

Correct Answer: ✅ b) A loop that runs indefinitely until it's manually terminated.
5. What does the `continue` statement do in a `while` loop?

a) It exits the loop.


b) It restarts the loop from the beginning.
c) It skips the current iteration and continues with the next.
d) It prints the current iteration value.

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) It defines an alternative condition to check.


b) It is executed if the `while` condition is false.
c) It is executed after every iteration of the loop.
d) It is not used in `while` loops.

Correct Answer: ✅ b) It is executed if the `while` condition is false.


7. How can you create an infinite loop intentionally in Python?

a) `while True:`
b) `while False:`
c) `while 1 == 1:`
d) `while count < 5:`

Correct Answer: ✅ a) `while True:`


8. What is the purpose of the `pass` statement in Python?

a) It defines a new variable.


b) It exits the loop.
c) It skips the current iteration and continues with the next.
d) It acts as a placeholder for future code.

Correct Answer: ✅ d) It acts as a placeholder for future code.


9. What does the `+=` operator do in the context of a `while` loop?

a) It adds two numbers together.


b) It multiplies two numbers.
c) It increments a variable by a specified value.
d) It performs string concatenation.

Correct Answer: ✅ c) It increments a variable by a specified value.


10. How do you avoid an infinite loop in Python?

a) By using the `break` statement.


b) By using the `pass` statement.
c) By ensuring the `while` condition eventually becomes false.
d) By using the `continue` statement.

Correct Answer: ✅ c) By ensuring the `while` condition eventually becomes false.

Follow us on Instagram for additional resources: zero1.education


11. What will be the output of the following code?

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?

a) It increments a loop counter.


b) It prints text to the console.
c) It reads user input and allows interactive input during the loop.
d) It defines a new variable.

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?

a) The current loop iteration number.


b) Whether the loop is running.
c) Whether a specified condition is true or false.
d) The time elapsed since the loop started.

Correct Answer: ✅ c) Whether a specified condition is true or false.


14. What will be the output of the following code?

x=0
while x < 5:
x += 2
print(x)

a) 2
b) 4
c) 5
d) 6

Correct Answer: ✅ d) 6

Follow us on Instagram for additional resources: zero1.education


15. Which statement can be used to skip the rest of the current iteration and move to the next one in
a `while` loop?

a) `skip`
b) `next`
c) `continue`
d) `break`

Correct Answer: ✅ c) `continue`

Follow us on Instagram for additional resources: zero1.education


Multiple choice questions about using the `for` loop in Python.
1. What is the primary purpose of the `for` loop in Python?

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

Correct Answer: ✅ c) To iterate over a sequence of items


2. In Python, what does the `range()` function return?

a) A random number
b) A sequence of integers
c) A string
d) A floating-point number

Correct Answer: ✅ b) A sequence of integers


3. How do you exit a `for` loop prematurely in Python?

a) Using the `break` statement


b) Using the `continue` statement
c) By changing the loop variable
d) By using the `exit` keyword

Correct Answer: ✅ a) Using the `break` statement


4. What is the result of the following code?

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?

fruits = ["apple", "banana", "cherry"]


for fruit in fruits:
print(fruit)

a) "apple" "banana" "cherry"


b) apple
banana

Follow us on Instagram for additional resources: zero1.education


cherry
c) ['apple', 'banana', 'cherry']
d) apple, banana, cherry

Correct Answer:
banana
✅ b) apple
cherry

6. What does the `continue` statement do in a `for` loop?

a) It exits the loop.


b) It restarts the loop from the beginning.
c) It skips the current iteration and continues with the next.
d) It prints the current iteration value.

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?

a) It defines an alternative condition to check.


b) It is executed if the `for` loop completes without encountering a `break` statement.
c) It is executed after every iteration of the loop.
d) It is not used in `for` loops.

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?

a) By using the `elif` keyword within a `for` block.


b) By placing one `for` loop inside another.
c) By using the `and` operator to combine conditions.
d) By using the `then` keyword.

Correct Answer: ✅ b) By placing one `for` loop inside another.


9. What will be the output of the following code?

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

Follow us on Instagram for additional resources: zero1.education


10. Which statement can be used to skip the rest of the current iteration and move to the next one in
a `for` loop?

a) `skip`
b) `next`
c) `continue`
d) `break`

Correct Answer: ✅ c) `continue`

Follow us on Instagram for additional resources: zero1.education


1. What is a variable in Python?
a) A constant value
b) A reserved keyword
c) A named location in memory to store data ✅
d) A function that returns a value

2. Which of the following is a valid variable name in Python?


a) 123variable
b) my-variable
c) _variable ✅
d) variable@

3. What is the purpose of assigning a value to a variable?


a) To make the code more readable
b) To reserve memory for the variable
c) To store and manipulate data ✅
d) To create a new variable

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

6. What is the correct way to declare a variable in Python?


a) var = 5
b) variable = 5 ✅
c) 5 = variable

7. Which of the following is a valid variable name in Python?


a) my_variable
b) _variable ✅
c) 123variable

8. What is the scope of a variable in Python?


a) The area of code where the variable can be accessed ✅
b) The size of the variable's memory allocation
c) The number of times the variable can be reassigned

Follow us on Instagram for additional resources: zero1.education


9. 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 ✅
10. Which of the following is NOT a valid data type for a variable in Python?
a) Integer
b) String
c) Boolean
d) Character ✅
11. What is the result of the following code snippet?
x=5
y=3
z=x+y
print(z)
a) 8✅
b) 15
c) Error

12. What is the purpose of the "print" function in Python?


a) To assign a value to a variable
b) To perform mathematical calculations
c) To display output ✅
13. Which of the following is a valid way to comment a line of code in Python?
a) // Comment
b) /* Comment */
c) # Comment ✅
14. What is the output of the following code snippet?
x = 10
y = "20"
z=x+y
print(z)
a) 30
b) 1020 ✅
c) Error

15. What is the purpose of the "input" function in Python?


a) To display output
b) To read user input ✅
c) To perform mathematical calculations

Follow us on Instagram for additional resources: zero1.education


16. Which of the following is a valid way to convert a string to an integer in Python?
a) str()
b) int()✅
c) float()

17. What is the output of the following code snippet?


x = "5"
y=3
z=x*y
print(z)
a) 15
b) "555"✅
c) Error

Follow us on Instagram for additional resources: zero1.education


Fill-in-the-blank: while loops in Python.

1. A while loop is used to repeatedly execute a block of code as long as a ______



condition is true. boolean

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

4. The ______ statement is used to prematurely end a while loop. ✅ break


5. The ______ statement is used to skip the current iteration and move to the next

iteration of a while loop. continue

6. The ______ function can be used to generate a sequence of numbers to control


the execution of a while loop. ✅
range

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

Follow us on Instagram for additional resources: zero1.education


15. The ______ function can be used to generate a sequence of numbers to control
the execution of a while loop. ✅
range

Follow us on Instagram for additional resources: zero1.education


Short Python programs to help you practice and master the use of the "if"
statement:

Program 1:
Program to check if a number is positive or negative

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


if num > 0:
print("The number is positive.")
else:
print("The number is negative.")

Program 2:
Program to check if a number is even or odd

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


if num % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")

Program 3:
Program to check if a student has passed or failed

marks = int(input("Enter the marks: "))


passing_marks = 40
if marks >= passing_marks:
print("Congratulations! You have passed.")
else:
print("Sorry, you have failed.")

Program 4:
Program to check if a year is a leap year

year = int(input("Enter a year: "))


if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
print("The year is a leap year.")
else:

Follow us on Instagram for additional resources: zero1.education


print("The year is not 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.")

Follow us on Instagram for additional resources: zero1.education


Short Python programs to help you practice and master the use of the
"while" loop:

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

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


factorial = 1
while num > 0:
factorial *= num
num -= 1
print("The factorial is:", factorial)

Program 5:
Program to check if a number is prime using a while loop
num = int(input("Enter a number: "))

Follow us on Instagram for additional resources: zero1.education


is_prime = True
i=2
while i <= num // 2:
if num % i == 0:
is_prime = False
break
i += 1
if is_prime:
print("The number is prime.")
else:
print("The number is not prime.")

Program 6:
Program to reverse a number using a while loop

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


reversed_num = 0
while num > 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10
print("The reversed number is:", reversed_num)

Program 7:
Program to find the sum of digits in a number using a while loop

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


sum = 0
while num > 0:
digit = num % 10
sum += digit
num //= 10
print("The sum of digits is:", sum)

Program 8:
Program to print a pattern using a while loop

rows = int(input("Enter the number of rows: "))


i=1
while i <= rows:
print("*" * i)
i += 1

Follow us on Instagram for additional resources: zero1.education


True or False questions in Python

1. Python is a high-level programming language. (True)

2. Python uses indentation to define code blocks. (True)

3. Python is a statically typed language. (False)

4. The "print()" function is used to display output in Python. (True)

5. Python is an interpreted language. (True)

6. Variables in Python must be declared before they can be used. (False)

7. Python supports both single-line and multi-line comments. (True)

8. The "input()" function is used to get user input in Python. (True)

9. Python is an object-oriented programming language. (True)

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)

13. Python supports automatic type conversion. (True)

14. The "range()" function generates a sequence of numbers 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)

17. Python supports operator overloading. (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)

Follow us on Instagram for additional resources: zero1.education


Follow us on Instagram for additional resources: zero1.education

You might also like