Class 9 Exam  >  Class 9 Notes  >  Artificial Intelligence (AI) for Class 9  >  Practice Questions: Python

Practice Questions: Python | Artificial Intelligence (AI) for Class 9 PDF Download

Q1. What is the output of the following code?

num = 5
if num > 0:

    print("Positive number")

(a) Negative number
(b) Positive number
(c) 0
(d) Error

Ans: (b)
The condition num > 0 is true, so "Positive number" is printed.

Q2. Which of the following keywords is used to start a conditional block in Python?
(a) for
(b) while
(c) if
(d) def

Ans: (c)
The if keyword is used to begin a conditional statement.

Q3. What is the output of the following code?

num = -1

if num > 0:

    print("Positive")

else:

    print("Not positive")

(a) Positive
(b) Error
(c) Not positive
(d) 0

Ans: (c)
Since -1 is not greater than 0, the else block executes.

Q4. What will be the result of the following program?

age = 16

if age >= 18:

    print("You can vote")

else:

    print("You cannot vote")

(a) You can vote
(b) You cannot vote
(c) Error in code
(d) Nothing prints

Ans: (b)
Since age is less than 18, the else block runs.

Q5. Which of the following is the correct syntax for an if-elif-else ladder?
(a) if... then... else
(b) if... elif... else
(c) if... elseif... endif
(d) if... else if... then

Ans: (b)
Python uses elif for else-if conditions in a ladder structure.

Q6. What is the output of the following code?

marks = 85

if marks > 90:

    print("Grade A")

elif marks > 80:

    print("Grade B")

else:

    print("Grade C")

(a) Grade A
(b) Grade B
(c) Grade C
(d) No output

Ans: (b)
85 is greater than 80 but not greater than 90, so the second condition matches.

Q7. Which loop is best suited when the number of iterations is known in advance?
(a) while loop
(b) if loop
(c) for loop
(d) repeat loop

Ans: (c)
for loops are ideal when you know the number of iterations beforehand.

Q8. What does this code print?

numbers = [1, 2, 3]

sum = 0

for val in numbers:

    sum += val

print(sum)

(a) 6
(b) 123
(c) Error
(d) 0

Ans: (a)
The code adds 1 + 2 + 3 and prints 6.

Q9. What is the correct way to check if a number is even in Python?
(a) if num / 2 == 0:
(b) if num % 2 == 0:
(c) if num == even:
(d) if num % 2 = 0:

Ans: (b)
Using modulus operator to check for remainder when divided by 2 is the correct method.

Q10. What is the output of the following while loop?

i = 1

sum = 0

while i <= 3:

    sum += i

    i += 1

print(sum)

(a) 6
(b) 3
(c) 0
(d) 1

Ans: (a)
The loop adds 1 + 2 + 3 and prints the result, which is 6.

The document Practice Questions: Python | Artificial Intelligence (AI) for Class 9 is a part of the Class 9 Course Artificial Intelligence (AI) for Class 9.
All you need of Class 9 at this link: Class 9
32 videos|57 docs
Related Searches

pdf

,

ppt

,

Sample Paper

,

video lectures

,

Practice Questions: Python | Artificial Intelligence (AI) for Class 9

,

Extra Questions

,

Summary

,

Practice Questions: Python | Artificial Intelligence (AI) for Class 9

,

shortcuts and tricks

,

Exam

,

Practice Questions: Python | Artificial Intelligence (AI) for Class 9

,

practice quizzes

,

Free

,

past year papers

,

Semester Notes

,

Objective type Questions

,

MCQs

,

mock tests for examination

,

study material

,

Viva Questions

,

Previous Year Questions with Solutions

,

Important questions

;