0% found this document useful (0 votes)
243 views

Computer Science Exam Answer (Python Programming With Sequences of Data)

Uploaded by

hello.hy123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
243 views

Computer Science Exam Answer (Python Programming With Sequences of Data)

Uploaded by

hello.hy123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Year 9 – Python programming with sequences of data Assessment – Answers

Assessment answers

Task .
The partial program below creates a list of the days of the week and assigns it to days.

1 days = ["Monday", "Tuesday", "Wednesday",


2 "Thursday", "Friday",
3 "Saturday", "Sunday"]

Question 1: If the lines below were added to the program, what would the program output be?

4 item = days[3]
5 print(item)

Answer Thursday

Question 2: If the lines below were added to the program, what would the program output be?

4 index = 1
5 item = days[index+1]
6 print(index, item)

Answer 1 Wednesday

Teacher note: The value of index is also printed to make sure the learners don’t think that index has
not been incremented.

Page 1 Last updated: 12-05-2021


Year 9 – Python programming with sequences of data Assessment – Answers

Question 3: Fill in the gaps in the code below, so that the items in the list of days are displayed
on the screen:

4 for day in days :


5 print( day )

Task .
Read the program below:

1 planets = ["Mercury", "Venus", "Earth",


2 "Mars", "Jupiter", "Saturn",
3 "Uranus", "Neptune"]
4 index = int(input())
5 planet = planets[index]
6 if index < 3:
7 print("It’s a rocky planet")
8 elif planet[1] in [‘a’, ‘e’]:
9 print("The second letter is", planet[1])

Question 1: If the user types 1 on the keyboard, what will be the output of this program when it is
executed?

Answer It’s a rocky planet

Teacher note: The objective is not to assess knowledge of zero-based indices again. The answer will
be the same regardless of whether the learner thinks that the planet is Mercury or Venus.

Question 2: If the user types 3 on the keyboard, what will be the output of this program when it is
executed?

Answer The second letter is a

Teacher note: The objective is not to assess knowledge of zero-based indices again. The answer will
be the same regardless of whether the learner thinks that the planet is Earth or Mars.

Page 2 12-05-2021
Year 9 – Python programming with sequences of data Assessment – Answers

Question 3: Suppose that the elif in line 8 is modified to an if. If the user types 1 on the keyboard,
what will be the output of this program when it is executed?

Answer It’s a rocky planet


The second letter is e

Question 4: The ‘rocky planets’ are the first four planets of the solar system. How would you
modify the program so that all planets are correctly classified (i.e. the message “It’s a rocky planet” is
displayed for all four of them).

Answer Change the condition in line 6 to index < 4 or index <= 3.

Task .
Read the program below and answer the questions. You can use the reference table at the bottom of
the page to see how the index method works.

1 alphabet = 'abcdefghijklmnopqrstuvwxyz'
2 letter = 'c'
3 position = alphabet.index(letter)
4 print(position)

Question: What will be the output of this program?

A. 2
B. 3
C. d
D. ‘c’ + 1 Answer A

Question: The program below is an extension of the previous one. Fill in the gaps, so that the
program prompts the user to enter a word and then iterates over each letter in the word to display its
position in the alphabet.

1 alphabet = 'abcdefghijklmnopqrstuvwxyz'
2 print("Enter a word:")
3 word = input()
Page 3 12-05-2021
Year 9 – Python programming with sequences of data Assessment – Answers

4 for letter in word :


5 position = alphabet.index(letter)
6 print(position)

Reference table

string.index(substring) Search in a string for the first


occurrence of a substring and
e.g. word.index("intro") return its (zero-based) index.
e.g. name.index("A")

Task .
The program below converts a binary number to its decimal equivalent. The questions that follow do
not require you to understand how it works.

1 binary = input()
2 decimal = 0
3 weight = 1
4 for digit in reversed(binary):
5 if digit == '1':
6 decimal = decimal + weight
7 weight = 2 * weight
8 print(binary, "=", decimal)

Question 1: Locate all the variables in the program.

Variables binary, decimal, weight, digit

Question 2: Locate the line of code where program execution is suspended until the user enters a
value on the keyboard.

Page 4 12-05-2021
Year 9 – Python programming with sequences of data Assessment – Answers

Line 1 (due to input)

Question 3: Locate the line of code that contains a condition (a logical expression).

Line 5 (the condition is: digit == ‘1’)

Question 4: Locate the line of code that doubles the value of a variable.

Line 7 (the assignment is weight = 2 * weight)

Question 5: Locate the lines of code that may be executed more than once.

Lines 5 to 7 (they are included in a for-loop)

Question 6: Locate the line of code that may never be executed.

Line 6 (it lies within an if, so if the condition is never True, it will never be executed)

Task .
Read the program below and answer the questions. The reference table after the questions shows you
how the append and pop methods work.

1 days = []
2 while len(days) < 12:
3 days.append(31)
4 days.append(30)
5 days[1] = 28
6 days.pop()
7 days.insert(7, 31)

Question 1: What will be the contents of the days list, after lines 1 to 4 of the program have been
Page 5 12-05-2021
Year 9 – Python programming with sequences of data Assessment – Answers

executed?

Answer [31, 30, 31, 30, 31, 30, 31, 30, 31, 30, 31, 30]

Question 2: What will be the contents of the days list, after line 5 of the program has been
executed?
Write the items of the list, or describe how the list will change with respect to your previous answer.

Answer [31, 28, 31, 30, 31, 30, 31, 30, 31, 30, 31, 30]
The value of the second item becomes 28.

Question 3: What will be the contents of the days list, after line 6 of the program has been
executed?
Write the items of the list, or describe how the list will change with respect to your previous answer.

Answer [31, 28, 31, 30, 31, 30, 31, 30, 31, 30, 31]
The last item (30) has been removed from the list.

Question 4: What will be the contents of the days list, after line 7 of the program has been
executed?
Write the items of the list, or describe how the list will change with respect to your previous answer.

Answer [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
A new item (31) has been inserted as the 8th item in the list (with index 7).

Teacher note: The objective is to assess understanding of the insert method, not to assess knowledge
of zero-based indices again. The answer will be the same regardless of whether the learner inserts at
position 7 or 8.

Reference table

list.append(item) Add an item to the end of the list

e.g. numbers.append(42)

list.insert(index, item) Insert an item at a given position

e.g. cities.insert(2, "Oslo")

Page 6 12-05-2021
Year 9 – Python programming with sequences of data Assessment – Answers

Resources are updated regularly — the latest version is available at: ncce.io/tcc.

This resource is licensed under the Open Government Licence, version 3. For more information on this licence, see
ncce.io/ogl.

Page 7 12-05-2021

You might also like