Docsity

Python Programming Practice Exam: Interactive Programming Part 1, Exams of Technology

Toggle sidebar
bg1
An Introduction to Interactive Programming in
Python Part 1 Certificate Practice Exam
**Question 1.** Which of the following statements correctly creates a comment in Python?
A) `/* This is a comment */`
B) `# This is a comment`
C) `<!-- This is a comment -->`
D) `// This is a comment`
Answer: B
Explanation: In Python, the hash symbol (`#`) begins a single‑line comment.
**Question 2.** What is the result of the expression `5 // 2` in Python?
A) `2.5`
B) `2`
C) `3`
D) `0`
Answer: B
Explanation: `//` is floor division; it returns the integer part of the quotient, 2.
**Question 3.** Which data type is immutable in Python?
A) list
B) dict
C) set
D) tuple
Answer: D
Explanation: Tuples cannot be altered after creation, unlike lists, dictionaries, and sets.
**Question 4.** What will be printed by the following code?
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Python Programming Practice Exam: Interactive Programming Part 1 and more Exams Technology in PDF only on Docsity!

Python Part 1 Certificate Practice Exam

Question 1. Which of the following statements correctly creates a comment in Python? A) /* This is a comment */ B) # This is a comment C) <!-- This is a comment --> D) // This is a comment Answer: B Explanation: In Python, the hash symbol (#) begins a single‑line comment. Question 2. What is the result of the expression 5 // 2 in Python? A) 2.5 B) 2 C) 3 D) 0 Answer: B Explanation: // is floor division; it returns the integer part of the quotient, 2. Question 3. Which data type is immutable in Python? A) list B) dict C) set D) tuple Answer: D Explanation: Tuples cannot be altered after creation, unlike lists, dictionaries, and sets. Question 4. What will be printed by the following code?

Python Part 1 Certificate Practice Exam

x = 3 y = 4 print(x + y * 2) 

A) 14 B) 11 C) 10 D) 7 Answer: B Explanation: Multiplication has higher precedence, so y*2 is 8; then x+8 = 11. Question 5. Which keyword is used to define a function in Python? A) function B) def C) fun D) lambda Answer: B Explanation: The def statement introduces a function definition. Question 6. In the function definition def foo(a, b=5):, what is b? A) A required positional argument B) A keyword‑only argument C) A default argument D) A variable‑length argument

Python Part 1 Certificate Practice Exam

Explanation: != is the inequality operator in Python. Question 10. Which logical operator has the highest precedence? A) or B) and C) not D) All have equal precedence Answer: C Explanation: not is evaluated before and and or. Question 11. What will be the output of the following code?

for i in range(0, 5, 2): print(i, end=' ') 

A) 0 1 2 3 4 B) 0 2 4 C) 2 4 D) 0 2 Answer: B Explanation: range(0,5,2) generates 0,2,4; they are printed separated by spaces. Question 12. Which statement correctly creates an empty list? A) list = () B) list = []

Python Part 1 Certificate Practice Exam

C) list = {} D) list = <> Answer: B Explanation: Square brackets denote a list; parentheses create a tuple, braces a dict. Question 13. After executing my_list = [1, 2, 3]; my_list[1] = 10, what is my_list? A) [1, 2, 3] B) [1, 10, 3] C) [10, 2, 3] D) [1, 2, 10] Answer: B Explanation: Index 1 (second element) is replaced with 10. Question 14. Which method removes and returns the last element of a list? A) pop() B) remove() C) del() D) clear() Answer: A Explanation: list.pop() without an index removes the last item and returns it. Question 15. What does the expression 'Hello'.find('e') return? A) -1 B) 1 C) 2

Python Part 1 Certificate Practice Exam

Answer: A Explanation: str() returns the string representation of its argument. Question 19. Which operator performs exponentiation in Python? A) ^ B) ** C) ^^ D) exp() Answer: B Explanation: ** raises the left operand to the power of the right operand. Question 20. What will be printed by the following code?

a = 10 b = a a = 5 print(b) 

A) 5 B) 10 C) None D) 0 Answer: B Explanation: b was assigned the value of a before a changed; integers are immutable.

Python Part 1 Certificate Practice Exam

Question 21. Which of the following is a correct way to import the SimpleGUI module in CodeSkulptor? A) import simplegui B) from simplegui import * C) import SimpleGUI as sg D) Both A and B are valid in CodeSkulptor Answer: D Explanation: CodeSkulptor supports both import simplegui and from simplegui import *. Question 22. In SimpleGUI, which method starts the event loop and displays the frame? A) frame.run() B) frame.start() C) frame.show() D) frame.open() Answer: B Explanation: frame.start() begins handling events and draws the UI. Question 23. Which SimpleGUI function creates a timer that calls a handler every 100 milliseconds? A) simplegui.create_timer(0.1, handler) B) simplegui.create_timer(100, handler) C) simplegui.create_timer(0.1, handler).start() D) simplegui.create_timer(100, handler).start() Answer: B Explanation: The interval is given in milliseconds; 100 ms equals 0.1 seconds.

Python Part 1 Certificate Practice Exam

Question 27. Which canvas method draws a filled circle? A) canvas.draw_circle(center, radius, line_width, line_color, fill_color) B) canvas.draw_filled_circle(...) C) canvas.circle(center, radius, fill_color) D) canvas.draw_oval(...) Answer: A Explanation: draw_circle with a non‑empty fill_color draws a filled circle. Question 28. What does the following code draw?

canvas.draw_line((0,0), (100,100), 2, "Red") 

A) A red line from the top‑left to (100,100) B) A red vertical line C) A red horizontal line D) Nothing; missing draw handler Answer: A Explanation: draw_line draws a line between the two points with width 2 and color red. Question 29. Which method draws text on the canvas? A) canvas.write_text() B) canvas.draw_text() C) canvas.text() D) canvas.render_text() Answer: B

Python Part 1 Certificate Practice Exam

Explanation: draw_text renders a string at a given position with size and color. Question 30. In a SimpleGUI program, where should you place the call to frame.start()? A) Inside a button handler B) At the very end of the script after all definitions C) Inside the draw handler D) Inside the timer handler Answer: B Explanation: frame.start() should be called after defining all UI elements and handlers. Question 31. Which Python operator has higher precedence? A) or B) and C) == D) + Answer: D Explanation: Arithmetic operators (+, -, *, /) are evaluated before comparison (==) and logical operators. Question 32. What does the expression bool(0) evaluate to? A) True B) False C) 0 D) Raises an error Answer: B

Python Part 1 Certificate Practice Exam

Explanation: int() cannot convert a string containing a decimal point; it raises ValueError. Question 36. Which of the following is a correct way to generate a random integer between 1 and 10 inclusive? A) random.randint(1,10) B) random.random(1,10) C) randint(1,10) D) random.uniform(1,10) Answer: A Explanation: randint(a,b) returns an integer N such that a ≤ N ≤ b. Question 37. In Python, what is the output of print(2 ** 3 ** 2)? A) 64 B) 512 C) 256 D) 8 Answer: B Explanation: Exponentiation is right‑associative: 3**2 = 9; then 2**9 = 512. Question 38. Which statement about the break keyword inside a while loop is true? A) It skips the current iteration and continues with the next. B) It terminates the loop immediately. C) It causes a syntax error. D) It only works in for loops. Answer: B

Python Part 1 Certificate Practice Exam

Explanation: break exits the nearest enclosing loop. Question 39. Which of the following correctly checks whether a list lst is empty? A) if lst == []: B) if len(lst) == 0: C) if not lst: D) All of the above Answer: D Explanation: All three expressions evaluate to True when lst has no elements. Question 40. What does the continue statement do inside a loop? A) Ends the loop entirely. B) Restarts the loop from the beginning. C) Skips the remainder of the current iteration and proceeds to the next. D) Raises an exception. Answer: C Explanation: continue jumps to the loop’s next iteration. Question 41. Which of the following is a correct way to define a function that returns the square of its argument? A) def square(x): return x * x B) def square(x) => x * x C) function square(x) { return x * x; } D) square = lambda x: x ** 2 (as a statement) Answer: A

Python Part 1 Certificate Practice Exam

Question 44. In SimpleGUI, which method registers a handler for keyboard input? A) frame.add_keydown_handler() B) frame.set_keydown_handler() C) frame.add_input() D) SimpleGUI does not support keyboard events. Answer: A Explanation: add_keydown_handler attaches a function that receives key codes. Question 45. What does the expression len("Hello World") return? A) 10 B) 11 C) 12 D) 0 Answer: B Explanation: The string contains 11 characters, including the space. Question 46. Which of the following statements correctly swaps the values of variables a and b in Python? A) temp = a; a = b; b = temp B) a, b = b, a C) Both A and B are correct D) Neither; Python does not allow swapping. Answer: C Explanation: Both the classic three‑line method and tuple unpacking swap values.

Python Part 1 Certificate Practice Exam

Question 47. Which operator is used for integer division that discards the remainder? A) / B) // C) % D) ** Answer: B Explanation: // performs floor division, returning the integer quotient. Question 48. What is the output of the following code?

s = "abcd" print(s[-2]) 

A) c B) d C) b D) a Answer: A Explanation: Negative indices count from the end; -1 is d, -2 is c. Question 49. Which list method removes the first occurrence of a specified value? A) pop(value) B) remove(value) C) del value D) discard(value)

Python Part 1 Certificate Practice Exam

Explanation: The f prefix allows inline expressions and format specifiers. Question 53. What will be the value of x after executing x = 5; x += 3? A) 5 B) 8 C) 3 D) None Answer: B Explanation: += adds the right‑hand side to the variable. Question 54. Which of the following statements creates a dictionary with keys 'a' and 'b' mapping to 1 and 2 respectively? A) {'a':1, 'b':2} B) {'a'=1, 'b'=2} C) {'a'->1, 'b'->2} D) {'a' 1, 'b' 2} Answer: A Explanation: Colon separates key and value in a dict literal. Question 55. In a SimpleGUI timer, which method stops the timer from generating further events? A) timer.pause() B) timer.stop() C) timer.halt() D) timer.disable() Answer: B

Python Part 1 Certificate Practice Exam

Explanation: timer.stop() halts the timer. Question 56. Which of the following statements about the global keyword is correct? A) It must be placed inside a function to modify a global variable. B) It can be used anywhere in the script. C) It creates a new global variable. D) It only works with immutable types. Answer: A Explanation: global tells the function to use the module‑level variable. Question 57. What is the output of the following code?

for i in range(3): print(i, end=' ') 

A) 0 1 2 B) 1 2 3 C) 0 1 2 3 D) 1 2 Answer: A Explanation: range(3) yields 0,1,2. Question 58. Which of the following correctly creates a tuple containing a single element 5? A) (5)