Here are 10 question-answer pairs on the basics of Python, covering conditional statements, loops, and input/output functions, suitable for Class 8:
Questions and Answers
-
Q: What is the purpose of the
if
statement in Python?
A: Theif
statement is used to execute a block of code only if a specified condition is true. -
Q: Write a Python statement to check if a number is greater than 10.
A:if number > 10: print("The number is greater than 10")
-
Q: What is the difference between the
if
andelse
statements?
A: Theif
statement executes a block of code if the condition is true, while theelse
statement executes a block of code if the condition is false. -
Q: Name the two types of loops in Python.
A: The two types of loops in Python are for loops and while loops. -
Q: Write an example of a
for
loop that prints numbers from 1 to 5.
A:for i in range(1, 6): print(i)
-
Q: How does the
input()
function work in Python?
A: Theinput()
function is used to take input from the user as a string. -
Q: What will be the output of the following code?
for i in range(3): print("Hello")
A:
Hello Hello Hello
-
Q: What is the syntax of the
while
loop in Python?
A:while condition: # Code block to execute
-
Q: Write an example of an
if-else
statement to check if a number is even or odd.
A:if number % 2 == 0: print("Even") else: print("Odd")
-
Q: What is the purpose of the
print()
function in Python?
A: Theprint()
function is used to display output on the screen.
These questions cover fundamental concepts in an easy-to-understand way.
Here’s a set of 20 Python MCQs designed for Class VIII students. The questions range from basic to slightly advanced levels, suitable for a 4th periodic test (PT).
1. What is Python?
a) A snake
b) A programming language
c) A game
d) An operating system
Answer: b) A programming language
2. Who developed Python?
a) Dennis Ritchie
b) Guido van Rossum
c) James Gosling
d) Bjarne Stroustrup
Answer: b) Guido van Rossum
3. Which of the following is a valid Python keyword?
a) if
b) var
c) loop
d) define
Answer: a) if
4. What is the extension of a Python file?
a) .txt
b) .py
c) .java
d) .cpp
Answer: b) .py
5. How do you output text in Python?
a) cout
b) console.log
c) print
d) System.out.println
Answer: c) print
6. What will be the output of this code?
print(5 + 3 * 2)
a) 16
b) 11
c) 13
d) 10
Answer: b) 11
7. How can you take input from the user in Python?
a) input()
b) scan()
c) get()
d) cin
Answer: a) input()
8. What is the correct way to start a loop in Python?
a) for i in range(5):
b) loop i in 5:
c) while i range(5):
d) repeat i from 1 to 5:
Answer: a) for i in range(5):
9. Which symbol is used for comments in Python?
a) //
b) /* */
c) #
d) <!-- -->
Answer: c) #
10. What is the output of this code?
x = 10
y = 5
print(x == y)
a) True
b) False
c) Error
d) None
Answer: b) False
11. Which function is used to find the length of a string?
a) size()
b) count()
c) length()
d) len()
Answer: d) len()
12. What does this code do?
x = "Python"
print(x[1:4])
a) Outputs Pyt
b) Outputs ytho
c) Outputs yth
d) Outputs tho
Answer: c) Outputs yth
13. What is the output of this code?
print(5 // 2)
a) 2.5
b) 2
c) 3
d) 2.0
Answer: b) 2
14. Which data type is used to store numbers with decimals?
a) int
b) float
c) str
d) list
Answer: b) float
15. What is the result of this code?
x = [1, 2, 3, 4]
x.append(5)
print(x)
a) [1, 2, 3, 4, 5]
b) [1, 2, 3, 4]
c) [5, 1, 2, 3, 4]
d) Error
Answer: a) [1, 2, 3, 4, 5]
16. Which of the following is NOT a data type in Python?
a) list
b) tuple
c) dictionary
d) number
Answer: d) number
17. How do you declare a variable in Python?
a) int x = 5
b) x = 5
c) declare x = 5
d) var x = 5
Answer: b) x = 5
18. What is the output of this code?
x = True
y = False
print(x and y)
a) True
b) False
c) None
d) Error
Answer: b) False
19. What does the pop()
function do in Python?
a) Removes an element from the start of a list
b) Removes the last element of a list
c) Adds an element to the end of a list
d) Returns the length of a list
Answer: b) Removes the last element of a list
20. Which function is used to display all methods available for an object?
a) dir()
b) help()
c) methods()
d) list()
Answer: a) dir()
Here’s a collection of Python MCQs focusing on if-else
statements, suitable for testing basic and intermediate understanding:
1. What is the correct syntax for an if
statement in Python?
a) if (x > 10):
b) if x > 10:
c) if x > 10 then:
d) if x > 10 {
Answer: b) if x > 10:
2. What is the output of the following code?
x = 5
if x > 3:
print("Hello")
else:
print("Bye")
a) Hello
b) Bye
c) Error
d) None
Answer: a) Hello
3. What does this code output?
x = 10
if x % 2 == 0:
print("Even")
else:
print("Odd")
a) Even
b) Odd
c) Error
d) None
Answer: a) Even
4. What is the purpose of the else
block in an if-else
statement?
a) To execute a block of code only when the if
condition is false.
b) To terminate the program.
c) To handle errors in the code.
d) To run code only when if
is true.
Answer: a) To execute a block of code only when the if
condition is false.
5. What is the output of this code?
x = 7
if x > 10:
print("Greater")
else:
print("Smaller or Equal")
a) Greater
b) Smaller or Equal
c) Error
d) None
Answer: b) Smaller or Equal
6. How do you write an if-else
statement that checks multiple conditions in Python?
a) Using elif
b) Using else-if
c) Using elseif
d) Using ifthen
Answer: a) Using elif
7. What is the output of this code?
x = 15
if x < 10:
print("Less than 10")
elif x == 15:
print("Equal to 15")
else:
print("Greater than 10 but not 15")
a) Less than 10
b) Equal to 15
c) Greater than 10 but not 15
d) None
Answer: b) Equal to 15
8. What happens if you write an else
block without an if
statement?
a) The code runs normally.
b) The program throws an error.
c) The else
block is ignored.
d) It prints else
by default.
Answer: b) The program throws an error.
9. What is the output of this code?
x = 5
if x > 3:
if x < 10:
print("Yes")
else:
print("No")
a) Yes
b) No
c) Error
d) None
Answer: a) Yes
10. What is the output of this code?
x = 10
if x > 20:
print("Large")
elif x == 10:
print("Equal")
else:
print("Small")
a) Large
b) Equal
c) Small
d) None
Answer: b) Equal
11. Can an if
statement exist without an else
block?
a) Yes
b) No
c) Only in Python 3
d) Only if using elif
Answer: a) Yes
12. What is the output of this code?
if False:
print("This will never print")
else:
print("This will always print")
a) This will never print
b) This will always print
c) Error
d) None
Answer: b) This will always print
13. Which operator is used to combine conditions in an if
statement?
a) &&
b) ||
c) and
d) or
Answer: c) and
14. What is the output of this code?
x = 8
if x > 5 and x < 10:
print("Within range")
else:
print("Out of range")
a) Within range
b) Out of range
c) Error
d) None
Answer: a) Within range
15. What is the output of this code?
x = 0
if x:
print("True")
else:
print("False")
a) True
b) False
c) Error
d) None
Answer: b) False
16. What is the correct syntax for a one-liner if-else
statement in Python?
a) if x > 5: print("Yes") else: print("No")
b) print("Yes") if x > 5 else print("No")
c) print("Yes" if x > 5 else "No")
d) Both b and c
Answer: d) Both b and c
17. What is the output of this code?
x = 5
y = 10
if x > y:
print("x is greater")
elif x == y:
print("x is equal to y")
else:
print("x is smaller")
a) x is greater
b) x is equal to y
c) x is smaller
d) Error
Answer: c) x is smaller
18. How do you check for equality in Python?
a) =
b) ==
c) !=
d) equals
Answer: b) ==
19. What is the output of this code?
x = 5
if x != 5:
print("Not equal")
else:
print("Equal")
a) Not equal
b) Equal
c) Error
d) None
Answer: b) Equal
20. What does this code do?
x = 5
if x > 0:
print("Positive")
if x % 2 == 0:
print("Even")
else:
print("Odd")
else:
print("Negative")
a) Prints "Positive" and "Even"
b) Prints "Positive" and "Odd"
c) Prints "Negative"
d) None
Answer: b) Prints "Positive" and "Odd"
0 Comments
कमेंट केवल पोस्ट से रिलेटेड करें