In Python, which of the following is the correct code to open a file named "results.txt" for the purpose of adding new data to the end of the existing content without overwriting it?
GCE O-Level · Computing (7155)
Python Code: Practice Questions
5 multiple-choice questions marked as you go, and 2 written questions with worked solutions. All on Python Code.
A programmer wants to generate a random integer between 1 and 10 (inclusive). Which Python command should be used after importing the random module?
What is the output of the following Python code?data = [2, 4, 6]
result = 0
for i in range(len(data)):
result += data[i] * i
print(result)
Which of the following is the correct Python expression to calculate the remainder when 25 is divided by 4?
What is the output of the following Python code segment?x = 17
y = 5
print(x // y, x % y)
A student wrote a Python script to calculate the average score of a class. The script asks for the number of students, then prompts for each student's score one by one.
(a) Write a Python code snippet using a for loop to input 5 student scores and store them in a list named scores.
(b) Explain the difference between using a for loop and a while loop in this context.
Write your answer out first, then check it against the worked solution.
Consider a scenario where you need to validate user passwords. A valid password must be at least 8 characters long and must not contain any spaces.
(a) Define a function validate_password(pw) that takes a string as a parameter. It should return True if the password meets both criteria and False otherwise.
(b) Inside the function, use len() and the not in operator or a string method to perform the checks.
(c) Demonstrate how to call this function with an input string provided by the user.
Write your answer out first, then check it against the worked solution.
* The content provided by thinka is generated by AI and may not always be accurate or up-to-date. Please use it as a supplementary resource and verify with official materials.
You've seen the model answer. Now get yours marked.
This page can show you how a good answer looks. It cannot tell you what your answer was missing. thinka marks your written work against the real mark scheme in about 15 seconds.
Want more questions like these? Get a fresh set on this topic, graded as you go.
Practice More