python

Introduction to Python: Class 9 AI

Multiple Choice Questions

1. What is Python?

a) A type of hardware
b) A programming language
c) A database
d) A search engine
Answer: b) A programming language

2. Python is a ___________ language.

a) Compiled
b) Interpreted
c) Assembly
d) Machine
Answer: b) Interpreted

3. Which symbol is used for comments in Python?

a) %
b) #
c) $
d) &
Answer: b) #

4. What is the correct file extension for Python files?

a) .pt
b) .py
c) .python
d) .p
Answer: b) .py

5. Which keyword is used to define a function in Python?

a) func
b) def
c) define
d) function
Answer: b) def

6. What data type is the value 10.5 in Python?

a) int
b) float
c) str
d) bool
Answer: b) float

7. Which of the following is a mutable data type in Python?

a) Tuple
b) String
c) List
d) Integer
Answer: c) List

8. What does the following code output? print(2 ** 3)

a) 6
b) 8
c) 9
d) 16
Answer: b) 8

9. Which of the following is NOT a Python keyword?

a) while
b) if
c) switch
d) else
Answer: c) switch

10. How do you convert a string “123” to an integer in Python?

a) int(“123”)
b) str(“123”)
c) list(“123”)
d) float(“123”)
Answer: a) int(“123”)

11. What will print(type(5)) output?

a) <class ‘int’>
b) <class ‘float’>
c) <class ‘str’>
d) <class ‘bool’>
Answer: a) <class ‘int’>

12. What is the output of len(“Hello”)?

a) 4
b) 5
c) 6
d) 7
Answer: b) 5

13. Which of these is used to input data from the user in Python?

a) get()
b) input()
c) scan()
d) read()
Answer: b) input()

14. Which function is used to get the length of a list in Python?

a) size()
b) count()
c) len()
d) length()
Answer: c) len()

15. Which of the following will create an empty set in Python?

a) {}
b) set()
c) []
d) ()
Answer: b) set()

16. What will print(10 % 3) output?

a) 3
b) 1
c) 0
d) 10
Answer: b) 1

17. Which operator is used for exponentiation in Python?

a) *
b) **
c) ^
d) %
Answer: b) **

18. What will bool(0) output?

a) True
b) False
c) Error
d) None of the above
Answer: b) False

19. What will print(5 == 5) output?

a) True
b) False
c) Error
d) None of the above
Answer: a) True

20. What is the output of print(4 / 2)?

a) 2
b) 2.0
c) 4
d) Error
Answer: b) 2.0

21. How do you open a file for reading in Python?

a) open(filename, “r”)
b) open(filename, “w”)
c) open(filename, “rb”)
d) open(filename, “rw”)
Answer: a) open(filename, “r”)

22. What does the is keyword do in Python?

a) Tests if two variables refer to the same object
b) Compares values of two variables
c) Checks type of variable
d) None of the above
Answer: a) Tests if two variables refer to the same object

23. What is the output of print(‘Python'[0:3])?

a) Py
b) Pyt
c) Pyth
d) None
Answer: b) Pyt

24. Which of the following is a control structure in Python?

a) Function
b) List
c) Loop
d) String
Answer: c) Loop
25. What is the use of the if statement in Python?

a) Looping
b) Conditional execution
c) Function definition
d) Error handling
Answer: b) Conditional execution
26. Which token type is a while loop in Python?

a) Operator
b) Keyword
c) Identifier
d) Constant
Answer: b) Keyword
27. What does the else statement in Python indicate?

a) Start of a loop
b) End of a loop
c) The block of code that runs if the if condition is false
d) End of a function
Answer: c) The block of code that runs if the if condition is false
28. Which of the following is NOT a control structure?

a) if-else
b) for
c) while
d) def
Answer: d) def
29. An algorithm is a ___________.

a) Step-by-step procedure for solving a problem
b) Flowchart
c) Program
d) Syntax error
Answer: a) Step-by-step procedure for solving a problem
30. What does a flowchart represent?

a) A structured programming language
b) A visual representation of an algorithm
c) A computer system
d) A set of inputs
Answer: b) A visual representation of an algorithm
31. Which symbol in a flowchart represents a decision?

a) Circle
b) Square
c) Diamond
d) Arrow
Answer: c) Diamond
32. Which of the following is NOT a type of token in Python?

a) Keyword
b) Identifier
c) Constant
d) Diagram
Answer: d) Diagram
33. What type of token is a + in Python?

a) Identifier
b) Operator
c) Constant
d) Keyword
Answer: b) Operator
34. What is source code?

a) Executable program
b) Human-readable instructions written by a programmer
c) Binary code
d) Machine code
Answer: b) Human-readable instructions written by a programmer
35. Which of the following control structures is used to repeat a block of code multiple times?

a) if-else
b) for
c) def
d) try
Answer: b) for
36. Which flowchart symbol is used for input/output operations?

a) Rectangle
b) Parallelogram
c) Diamond
d) Circle
Answer: b) Parallelogram
37. Which Python token represents a reserved word that has a special meaning?

a) Identifier
b) Keyword
c) Constant
d) Operator
Answer: b) Keyword
38. What is the output of an algorithm?

a) Input
b) Output
c) Both input and output
d) None of the above
Answer: b) Output
39. In a flowchart, what shape represents a process or an operation?

a) Diamond
b) Rectangle
c) Parallelogram
d) Circle
Answer: b) Rectangle
40. Which Python loop ensures that the loop runs at least once?

a) while
b) for
c) if
d) No such loop in Python
Answer: d) No such loop in Python
41. What is the purpose of a flowchart?

a) To write code
b) To visually plan an algorithm
c) To debug a program
d) To execute a program
Answer: b) To visually plan an algorithm
42. Which of the following is NOT a valid Python keyword?

a) True
b) False
c) elif
d) switch
Answer: d) switch
43. What is the purpose of the continue statement in Python?

a) To stop the loop
b) To skip the rest of the current iteration and proceed to the next iteration
c) To exit the program
d) To skip the entire loop
Answer: b) To skip the rest of the current iteration and proceed to the next iteration
44. Which type of token is a variable name in Python?

a) Operator
b) Identifier
c) Keyword
d) Constant
Answer: b) Identifier
45. Which control structure can be used to handle exceptions in Python?

a) try-except
b) if-else
c) for
d) while
Answer: a) try-except
46. What is the first step in solving a problem using an algorithm?

a) Coding
b) Debugging
c) Understanding the problem
d) Testing
Answer: c) Understanding the problem
47. In Python, which statement allows you to exit a loop?

a) continue
b) exit
c) break
d) stop
Answer: c) break
48. What is the role of a flowchart in programming?

a) To write code
b) To design and visualize the structure of an algorithm
c) To correct errors
d) To write comments
Answer: b) To design and visualize the structure of an algorithm
49. Which operator is used to assign a value to a variable in Python?

a) ==
b) =
c) !=
d) :=
Answer: b) =
50. What symbol is used to indicate the start or end in a flowchart?

a) Rectangle
b) Oval
c) Diamond
d) Arrow
Answer: b) Oval
51. Which of the following is NOT a type of loop in Python?

a) for
b) while
c) do-while
d) Nested loop
Answer: c) do-while
52. What is the term used for the human-readable version of a program?

a) Machine code
b) Source code
c) Bytecode
d) Object code
Answer: b) Source code

53. What is an algorithm?
a) A step-by-step procedure to solve a problem
b) A programming language
c) A diagram representing the flow of data
d) A Python function

Answer: a) A step-by-step procedure to solve a problem

54. What is the main difference between an algorithm and a flowchart?
a) Algorithms use diagrams, flowcharts do not
b) Flowcharts are visual representations, algorithms are written steps
c) Algorithms are used in AI, flowcharts are not
d) Flowcharts represent data, algorithms represent programs

Answer: b) Flowcharts are visual representations, algorithms are written steps

55. How does Python play a role in AI?
a) It is a hardware component
b) It is widely used for developing AI applications
c) It cannot be used for AI
d) It replaces human intelligence

Answer: b) It is widely used for developing AI applications

56. What is a character set in Python?
a) A set of commands
b) A set of characters that Python can recognize
c) A set of data types
d) A set of functions

Answer: b) A set of characters that Python can recognize

57. Which of the following is NOT a token in Python?
a) Keywords
b) Variables
c) Operators
d) Flowchart

Answer: d) Flowchart

58. What is the purpose of comments in Python?
a) To execute additional code
b) To make code harder to read
c) To explain and document code
d) To increase code runtime

Answer: c) To explain and document code

59. Which symbol is used for a single-line comment in Python?
a) //
b) #
c) /* */
d) <!– –>

Answer: b) #

60. What does the input() function in Python do?
a) It processes data
b) It takes input from the user
c) It prints data on the screen
d) It converts data into integers

Answer: b) It takes input from the user

61. Which of the following is a control structure in Python?
a) If-else
b) Variables
c) Lists
d) Tokens

Answer: a) If-else

62. What is the purpose of the selection structure in Python?
a) To execute code sequentially
b) To make decisions and select between different code blocks
c) To repeat a set of instructions
d) To store multiple values

Answer: b) To make decisions and select between different code blocks

63. Which of the following is a list in Python?
a) [1, 2, 3]
b) {1, 2, 3}
c) (1, 2, 3)
d) “123”

Answer: a) [1, 2, 3]

64. What is one of the main uses of Python?
a) Web development
b) Game development
c) Data analysis
d) All of the above

Answer: d) All of the above

65. Which of the following is NOT a feature of Python?
a) Easy to learn
b) Case-sensitive
c) Slow to execute
d) Platform-independent

Answer: c) Slow to execute

66. Which of the following is a valid statement in Python?
a) x = 5
b) if(x>5)
c) 5 = x
d) while x < 5:

Answer: a) x = 5

67. What is operator precedence?
a) The order in which operations are performed in an expression
b) The importance of variables
c) The type of data used in operations
d) The way comments are used in code

Answer: a) The order in which operations are performed in an expression

68. What does the print() function do in Python?
a) It stores data
b) It prints the output to the screen
c) It stops the program
d) It converts numbers to strings

Answer: b) It prints the output to the screen

69. What is a syntax error in Python?
a) An error in the logical flow of a program
b) An error in the structure or grammar of the code
c) An error in data storage
d) An error in the operating system

Answer: b) An error in the structure or grammar of the code

70. Which control structure is used to execute a block of code repeatedly?
a) If-else
b) While loop
c) Break
d) Sequential structure

Answer: b) While loop

71. What is the sequential structure in Python?
a) Execution of code in a specific order, one after another
b) Execution of code in a random order
c) Repeated execution of a block of code
d) Execution of code based on a condition

Answer: a) Execution of code in a specific order, one after another

72. Which of the following is a feature of Python?
a) Interpreted language
b) Platform-dependent
c) Complex to understand
d) Low-level language

Answer: a) Interpreted language

73. Which of the following is an error that occurs during runtime in Python?
a) Logical error
b) Runtime error
c) Compilation error
d) Typing error

Answer: b) Runtime error

74. Which operator has the highest precedence in Python?
a) + (Addition)
b) * (Multiplication)
c) – (Subtraction)
d) ** (Exponentiation)

Answer: d) ** (Exponentiation)

75. How are multi-line comments written in Python?
a) Using multiple # symbols
b) Using ”’ or “””
c) Using //
d) Using /* */

Answer: b) Using ”’ or “””

76. Which function is used to display text or data in Python?
a) show()
b) print()
c) display()
d) output()

Answer: b) print()

77. What is the correct syntax for defining a list in Python?
a) list = {1, 2, 3}
b) list = [1, 2, 3]
c) list = (1, 2, 3)
d) list = <1, 2, 3>

Answer: b) list = [1, 2, 3]

78. Which of the following is NOT a valid identifier in Python?
a) my_variable
b) _myVariable
c) 1myVariable
d) myVariable

Answer: c) 1myVariable

79. What type of error occurs when a variable is used without being defined in Python?
a) Syntax error
b) Name error
c) Logical error
d) Index error

Answer: b) Name error

Short Questions and Answers

1. What is Python used for?
Python is used for web development, data analysis, artificial intelligence, automation, and more.

2. Is Python case-sensitive?
Yes, Python is a case-sensitive language.

3. How do you declare a variable in Python?
You can declare a variable by simply assigning a value, like x = 10.

4. What is a list in Python?
A list is a collection of items that is ordered and changeable.

5. How do you add an element to a list in Python?
You can use the append() method to add an element to a list.

6. How do you create a function in Python?
A function is created using the def keyword.

7. What does the len() function do in Python?
The len() function returns the number of items in an object.

8. What is a string in Python?
A string is a sequence of characters.

9. How do you concatenate two strings in Python?
You can concatenate two strings using the + operator.

10. What is the purpose of the for loop in Python?
The for loop is used to iterate over a sequence (e.g., list, tuple, string).

11. How do you comment in Python?
Comments in Python are marked with #.

12. What does int() do in Python?
The int() function converts a value to an integer.

13. How do you convert an integer to a string in Python?
You use the str() function to convert an integer to a string.

14. What is a dictionary in Python?
A dictionary is an unordered collection of key-value pairs.

15. How do you check the data type of a variable in Python?
You use the type() function to check the data type of a variable.

16. What is a set in Python?
A set is an unordered collection of unique items.

17. How do you remove an item from a list in Python?
You can use the remove() or pop() method.

18. How do you write an infinite loop in Python?
Use a while True: loop to create an infinite loop.

19. What is the difference between == and is in Python?
== compares values, while is checks if two variables refer to the same object.

20. What does None represent in Python?
None represents the absence of a value or a null value.

21. What is the purpose of lambda in Python?
lambda is used to create small anonymous functions.

22. How do you open a file in Python?
Use the open() function to open a file.

23. What is a control structure?
A control structure is a block of code that determines the flow of control in a program.

24. What are loops used for in Python?
Loops are used to execute a block of code repeatedly.

25. What is an if statement in Python?
An if statement is used to make decisions based on conditions.

26. What is the purpose of an algorithm?
An algorithm is a step-by-step procedure to solve a problem.

27. What does a flowchart represent?
A flowchart visually represents the sequence of steps in an algorithm.

28. What is a Python token?
A token is the smallest unit of a program, such as a keyword, operator, or identifier.

29. What is an identifier in Python?
An identifier is a name used to identify a variable, function, class, or other objects.

30. What is a keyword in Python?
Keywords are reserved words that have a predefined meaning in Python.

31. What is source code?
Source code is the human-readable form of a program written by a programmer.

32. What does the while loop do in Python?
The while loop repeatedly executes a block of code as long as the condition is True.

33. What is a flowchart symbol for a process?
A rectangle represents a process in a flowchart.

34. What is the difference between if and elif in Python?
if starts a conditional block, while elif is used to check additional conditions.

35. What is a constant in Python?
A constant is a value that does not change during the execution of a program.

36. What does the continue statement do in a loop?
It skips the rest of the code inside the loop for the current iteration.

37. What is an operator in Python?
Operators are symbols used to perform operations on variables and values.

38. What does a diamond symbol represent in a flowchart?
A diamond represents a decision or condition in a flowchart.

39. What are nested loops?
Nested loops are loops inside other loops.

40. What is the function of the break statement in Python?
The break statement is used to exit a loop prematurely.

41. What is machine code?
Machine code is the low-level code that a computer can execute directly.

42. What is pseudocode?
Pseudocode is a plain language description of the steps in an algorithm.

43. What does the term ‘control flow’ mean?
Control flow refers to the order in which individual statements are executed.

44. What are relational operators?
Relational operators compare values and return True or False.

45. What is a for loop in Python?
A for loop iterates over a sequence like a list or a range.

46. What does the term ‘conditional execution’ mean?
Conditional execution refers to executing code only if a certain condition is true.

47. What is the purpose of an oval symbol in a flowchart?
An oval represents the start or end of a flowchart.

48. What is indentation used for in Python?
Indentation defines the blocks of code in Python.

49. What is a flowchart?
A flowchart is a diagram that represents a process or algorithm.

50. What is the difference between = and == in Python?
= is an assignment operator, while == is a comparison operator.

51. What is the purpose of an else statement?
The else statement runs code if the condition in the if statement is False.

52. What is a nested if statement?
A nested if statement is an if statement inside another if statement.

Long Questions Answers

1. Explain how Python supports object-oriented programming.
Python supports object-oriented programming (OOP) through the use of classes and objects. OOP allows for encapsulation, inheritance, and polymorphism. A class is a blueprint for creating objects (instances), and Python allows defining methods (functions within classes) and attributes (variables within classes) to model real-world entities.

2. Explain the difference between if, elif, and else in Python, with an example.
The if statement checks a condition; if it’s true, the block of code under it runs. If the if condition is false, the elif condition is checked. If both if and elif are false, the code in the else block executes. Example:
python
x = 10
if x > 20:
print(“x is greater than 20”)
elif x == 10:
print(“x is equal to 10”)
else:
print(“x is less than 10”)
3. Describe the structure of a flowchart and its key components.
A flowchart is a diagram representing a process or algorithm. The key components include:
• Oval: Represents the start and end of the process.
• Rectangle: Represents a process or task.
• Diamond: Represents a decision point.
• Parallelogram: Represents input/output operations.
• Arrows: Indicate the flow of control.
4. What are the different types of tokens in Python? Provide examples of each.
The types of tokens in Python are:
• Keywords: Reserved words like if, else, while.
• Identifiers: Names used for variables, functions, etc., like x, my_function.
• Literals: Constant values like numbers (5, 3.14) and strings (“Hello”).
• Operators: Symbols that perform operations like +, -, *, /.
• Punctuators: Symbols used in syntax like ,, :, {, }.
5. Write a Python program using a while loop and explain how it works.
The while loop runs as long as its condition is true. Example:
python
count = 0
while count < 5:
print(count)
count += 1
In this example, the loop prints the value of count until it reaches 
6. What is an algorithm, and how is it different from a flowchart?
An algorithm is a step-by-step method to solve a problem. A flowchart is a graphical representation of an algorithm. While an algorithm is written in text or pseudocode, a flowchart uses symbols and diagrams to show the logical flow of the process.
7. Explain the difference between the for and while loops in Python. Provide examples.
A for loop iterates over a sequence (e.g., a list or a range), while a while loop runs as long as its condition is true. Example of a for loop:
python
for i in range(5):
print(i)
Example of a while loop:
python
count = 0
while count < 5:
print(count)
count += 1
8. Describe how control structures affect the flow of a program.
Control structures like if, elif, else, for, and while change the flow of execution in a program. They enable conditional execution, loops, and decision-making, allowing the program to respond dynamically based on conditions and inputs.
9. What are nested loops, and how are they useful? Provide an example.
Nested loops are loops within loops. They are useful when working with multi-dimensional data. Example:
python
for i in range(3):
for j in range(2):
print(i, j)
10. Write a Python program that calculates the factorial of a number using a while loop.
Example:
python
num = int(input(“Enter a number: “))
factorial = 1
while num > 0:
factorial *= num
num -= 1
print(f”Factorial is {factorial}”)
This program calculates the factorial by multiplying the number by each decrementing value.
12. What are relational operators in Python, and how do they work? Provide examples.
Relational operators compare values and return True or False. Examples:
• > (greater than): 5 > 3 is True.
• < (less than): 2 < 4 is True.
• == (equal to): 3 == 3 is True.
13. How is source code converted into machine code in Python? Explain the process.
Python source code is first compiled into bytecode, a lower-level, platform-independent representation. This bytecode is then interpreted by the Python interpreter into machine code, which is executed by the computer.
14. Explain the steps involved in writing an algorithm to solve a problem.
Writing an algorithm involves:
• Understanding the problem.
• Identifying the input and output.
• Breaking down the problem into smaller steps.
• Arranging the steps logically.
• Testing the algorithm to ensure it solves the problem.
15. Write a Python program using a for loop to print the multiplication table of a number.
Example:
python
num = int(input(“Enter a number: “))
for i in range(1, 11):
print(f”{num} x {i} = {num*i}”)
16. How can flowcharts help in debugging a program?
Flowcharts provide a visual representation of the algorithm, making it easier to trace the logic and identify errors. By following the flow of the process step-by-step, a programmer can see where the logic might be failing and debug accordingly.

**NOTE**    refer your school textbook – Remaining questions from Control Structures (Sequential , Selection and Iteration / repetition /looping) – Algorithm , flowchart and source code.

Leave a Comment

Your email address will not be published. Required fields are marked *