Python Exam with Model Answers (Up to
Functions)
1. What is Python and why is it widely used?
Answer: Python is a high-level, interpreted programming language known for its simplicity and
readability. It is widely used because of its large community, libraries, and versatility.
2. Write the correct syntax to print 'Hello World' in Python.
Answer: print('Hello World')
3. What is the difference between a variable and a constant?
Answer: A variable can change its value, while a constant should remain unchanged throughout the
program.
4. Define a variable 'x' and assign it the value 10.
Answer: x = 10
5. What data type is the result of: 5 / 2 ?
Answer: float (2.5)
6. What operator is used for integer division in Python?
Answer: //
7. What is the output of: 3 ** 2 ?
Answer: 9
8. List three basic data types in Python.
Answer: int, float, str
9. What is the difference between =, == and != ?
Answer: = assigns a value, == checks equality, != checks inequality.
10. What is the type of the value: True?
Answer: bool
11. Write an if statement to check if a number is positive.
Answer: if num > 0: print('Positive')
12. Write an if-else statement to check if a number is even or odd.
Answer: if num % 2 == 0: print('Even') else: print('Odd')
13. Write an if-elif-else statement to grade scores.
Answer: if score >= 90: grade = 'A' elif score >= 75: grade = 'B' else: grade = 'C'
14. What keyword is used to end an if block?
Answer: Indentation (no keyword, just indentation ends the block).
15. What is the output of: if 0: print('yes') else: print('no')?
Answer: no
16. Write a for loop to print numbers from 1 to 5.
Answer: for i in range(1,6): print(i)
17. Write a while loop to print numbers from 1 to 5.
Answer: i = 1 while i <= 5: print(i) i += 1
18. What is the difference between a for loop and a while loop?
Answer: for is used when the number of iterations is known, while is used when the condition is
evaluated until false.
19. What keyword is used to skip an iteration in a loop?
Answer: continue
20. What keyword is used to stop a loop?
Answer: break
21. Create a list of 5 integers.
Answer: my_list = [1, 2, 3, 4, 5]
22. Access the first element of a list named my_list.
Answer: my_list[0]
23. Change the second element of my_list to 100.
Answer: my_list[1] = 100
24. Append the number 50 to my_list.
Answer: my_list.append(50)
25. Remove the last element of my_list.
Answer: my_list.pop()
26. What is the difference between append() and extend()?
Answer: append() adds a single element, extend() adds multiple elements from another list.
27. What is list slicing? Give an example.
Answer: Extracting a portion of a list. Example: my_list[1:4]
28. How do you find the length of a list?
Answer: len(my_list)
29. What is the output of: len([])?
Answer: 0
30. What is the output of: [1,2] + [3,4]?
Answer: [1, 2, 3, 4]
31. What keyword is used to define a function?
Answer: def
32. Write a function that prints 'Hello'.
Answer: def say_hello(): print('Hello')
33. Write a function that takes a name as a parameter and prints 'Hello, name'.
Answer: def greet(name): print('Hello,', name)
34. What is the difference between parameters and arguments?
Answer: Parameters are placeholders in function definition, arguments are actual values passed.
35. What is the output of add(2,3)?
Answer: 5
36. Can a function return multiple values? Explain with example.
Answer: Yes, by returning a tuple. Example: return x, y
37. What is the default return value of a function without return statement?
Answer: None
38. Write a function to calculate the square of a number.
Answer: def square(n): return n * n
39. Write a function to find the maximum of two numbers.
Answer: def maximum(a, b): return a if a > b else b
40. Explain local and global variables.
Answer: Local variables exist inside functions, global variables are accessible throughout the
program.
41. What is indentation in Python and why is it important?
Answer: Indentation defines code blocks, without it Python raises errors.
42. What is the difference between single quotes and double quotes in strings?
Answer: No difference, both define strings. Double quotes useful when string contains single
quotes.
43. How do you write a multi-line string in Python?
Answer: Using triple quotes ''' or """.
44. How do you take input from the user?
Answer: input('Enter value: ')
45. What function is used to convert a string to an integer?
Answer: int()
46. What function is used to convert an integer to a string?
Answer: str()
47. What is the difference between input() and print()?
Answer: input() takes user input, print() displays output.
48. What does the type() function do?
Answer: Returns the data type of a value.
49. How do you add comments in Python?
Answer: Using # for single line, or triple quotes for multi-line.
50. Explain the concept of dynamic typing in Python.
Answer: Variables can change type at runtime depending on the assigned value.