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

Programming Holiday Questions

use ful for a level CompSci learners

Uploaded by

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

Programming Holiday Questions

use ful for a level CompSci learners

Uploaded by

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

Revision Questions

1) List 5 programming languages.


2) Define the following:
i. Variable
ii. Int
iii. Float
iv. String
v. Boolean
vi. Function
vii. Procedure
viii. Concatenation
ix. Operator
x. Operand
xi. Overloaded operator
xii. Assignment Operator
xiii. Quotient
xiv. Floor division
xv. Modulus
xvi. Delimiter
xvii. Array / List
xviii. Tuple
xix. Dictionary
xx. Real
xxi. Set
3) Give the names of the following operators and explain their functions:
* // ** / %
4) What two purposes does the ‘+’ operator serve?
5) Outline the syntax of
a) a basic if-statement (with else)
b) elif
c) multiple nested ifs
d) a basic for-loop
e) for-loop with else
f) a basic while-loop
g) while-loop with else
6) Redo Q5 as flowcharts
Simplification Delimiter: characters used to define boundaries between different types of data (e.g. full stop in English, a
semi-colon in SQL, Java etc)
y=y+5 y=y–5 Concatenation: putting together two strings (see brown box below titled ‘Strings’), ‘+’ is used for this purpose.
y += 5 y–=5 print(): outputs a message to the screen based on what is in the brackets e.g. (x) will output the value of ‘x’,
(“H”) will output the letter ‘H’.
y=y/5 y=y*5
y/=5 y *= 5
Arrays / Lists
y=y+1 y=y–1 Ø used for storing multiple items
y++ y-- e.g. letters = [a, b, c] num = [1, 2, 3, 4]
Ø can also have multi-dimensional / nested lists:
e.g. x = [hello, 5, [s, 11], true]
for-loop prices = [ [bulb, 2] , [screw, 0.5], [pen, 1] ]
Ø each item is called using 0-based indexing
Ø Simple for-loop
e.g. letters[0]à a x[3] à true
for(item in array):
do this prices[1] à [screw, 0.5] prices[1,0] à screw
e.g.
list=[bburn, bolton, burnley]
for(a in list):
print(a)
num=[1, 12, 43,98]
for(x in num):
x = x*2
Ø for / else
num=[1, 12, 43,98]
for(x in num):
x = x*2
else:
print(“end”)
if-statement
Ø Simple if/else
if(this is true): if
do this then
else: else
do this
Ø If there is no ‘else’ part (and the ‘if’ is not true) then the
computer does NOTHING (i.e. goes to the next line of code)
e.g. if(age<17):
print (“17”)
If age is 17 or more nothing happens

while(this is true):
Ø There can be multiple ‘if’s (i.e. multiple conditions to
check):

print(“end”)
while(age<10):

while(age<10):
while-loop

print(age)

print(age)
if(age<16):
if(hifz==true)
Ø Simple while

do this

Ø While / else
print(“lunchtime at 12”)

age++

age++
Only prints ‘lunctime…’ if both conditions are true, and if first age=0

age=0

else:
is false then it doesn’t bother checking second
Ø Multiple ‘if’s can also be achieved with ‘and’ ‘or’
e.g.

if(age<16 and hifz==true):


print(“lunchtime at 12”)
Ø Multiple ‘if’s inside the ‘else’ part
if(hifz==true):
print(“lunch at 12”)
else:
if(age<16):
print(“lunch at 12”)
else
print(“lunch at 12.30”)
Ø Else-if can be also be written as ‘elif’
if(hifz==true):
print(“lunch at 12”)
elif(age<16):
print(“lunch at 12”)
else:
print(“lunch at 12.30”)
Practice Questions
1) Simplify the following
value = value + 5
noStudents = noStudents * 6
hellow = hellow / 12
number = number –10

2) Write the final value of ‘y’ at the end of each line:


y=5 hello = 22 noStudents = 14
L1: y=y+2 à
L2: y = y + hello à
L3: hello = y + noStudents à
L4: y = hello à
L5: y = noStudents à
L6: y = hello – y à

3) Write the final value of each variable at each line:


y=5 hello = 22 noStudents = 14 twenty = 0
L1: twenty = y – 2
L2: y = y + hello
L3: hello = y – noStudents
L4: twenty = hello * 3

Line y hello noStudents twenty


0
1
2
3
4
4) Perform the following arithmetic operations:
a. 100 / 5
b. 100 // 9
c. 100 % 9
d. 32 * 4
e. 8*9
f. 10 ** 3
g. 2 ** 6
h. 64 ** (1/2)
i. 125 / 25
j. 32 % 30
k. 32 // 9
5) Show the final output to the screen:
a. x = “hellow”
print(x*3)
b. y = “7”
print(y*3)
c. z = 12
q = str(z) + “7”
r = int(q) + 15
print(str(r))

6) Print the second item in the fruits list.


fruits = ["apple", "banana", "cherry"]
print( )
7) Change the value from "apple" to "kiwi", in the fruits list.
fruits = ["apple", "banana", "cherry"]
=
8) Use the append method to add "orange" to the fruits list.
fruits = ["apple", "banana", "cherry"]

9) Use the insert method to add "lemon" as the second item in the fruits list.
fruits = ["apple", "banana", "cherry"]
"lemon")
10) Use the remove method to remove "banana" from the fruits list.
fruits = ["apple", "banana", "cherry"]

11) Print "Hello World" if a is greater than b.


a = 50
b = 10
a b
print("Hello World")
12) Print "Hello World" if a is not equal to b.
a = 50
b = 10
if(a!=b)
print("Hello World")
13) Print "Yes" if a is equal to b, otherwise print "No".
a = 50
b = 10
a b
print("Yes")

print("No")
14) Print "1" if a is equal to b, print "2" if a is greater than b, otherwise print "3".
a = 50
b = 10
if(a==b):
print("1")
elif(a>b):
print("2")
else:
print("3")
15) Print "Hello" if a is equal to b, and c is equal to d.
if a == b and c == d:
print("Hello")
16) Print "Hello" if either a is equal to b, or c is equal to d.
if a == b or c == d:
print("Hello")
17) Print i as long as i is less than 6.
i=1
while(i < 6):
print(i)
i += 1
18) Stop the loop if i is 3.
i=1
while(i < 6):
if(i == 3):

i += 1
19) Loop through the items in the fruits list.
fruits = ["apple", "banana", "cherry"]
for x in fruits
print(x)
20) In the loop, when the item value is "banana", jump directly to the next item.
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":

21) Use the range function to loop through a code set 6 times.

for x in :
print(x)

22) What output is given for each block of code:

age = int(input("Enter your age: "))


if age >= 13:
print("You are old enough to have a paper round")
else:
print("You are too young to have a paper round")

dogs = ['willie', 'hootz']


if len(dogs) > 3:
print("Wow, we have a lot of dogs here!")
else:
print("Okay, this is a reasonable number of dogs.")

dogs = ['willie', 'hootz', 'peso', 'monty', 'juno', 'turkey']


if len(dogs) >= 5:
print("Holy mackerel, we might as well start a dog hostel")
elif len(dogs) >= 3:
print("Wow, we have a lot of dogs here!")
else:
print("Okay, this is a reasonable number of dogs.")

dogs = ['willie', 'hootz', 'peso', 'monty']


if len(dogs) >= 5:
print("Holy mackerel, we might as well start a dog hostel")
elif len(dogs) >= 3:
print("Wow, we have a lot of dogs here!")
else:
print("Okay, this is a reasonable number of dogs.")

dogs = ['willie', 'hootz']


if len(dogs) >= 5:
print("Holy mackerel, we might as well start a dog hostel!")
elif len(dogs) >= 3:
print("Wow, we have a lot of dogs here!")
else:
print("Okay, this is a reasonable number of dogs.")

dogs = []
if len(dogs) >= 5:
print("Holy mackerel, we might as well start a dog hostel!")
elif len(dogs) >= 3:
print("Wow, we have a lot of dogs here!")
else:
print("Okay, this is a reasonable number of dogs.")

dogs = []
if len(dogs) >= 5:
print("Holy mackerel, we might as well start a dog hostel!")
elif len(dogs) >= 3:
print("Wow, we have a lot of dogs here!")
elif len(dogs) >= 1:
print("Okay, this is a reasonable number of dogs.")

dogs = []
if len(dogs) >= 5:
print("Holy mackerel, we might as well start a dog hostel!")
elif len(dogs) >= 3:
print("Wow, we have a lot of dogs here!")
elif len(dogs) >= 1:
print("Okay, this is a reasonable number of dogs.")
else:
print("I wish we had a dog here.")

dogsWeKnow = ['willie', 'hootz', 'peso', 'monty', 'juno', 'turkey']


dogsPresent = ['willie', 'hootz']
for dog in dogsPresent:
if dog in dogsWeKnow:
print("Hello, %s!" % dog.title())

23) Write code to check if number inputted by user is odd or even. Output appropriate messages to
reflect this.
24) Write a function that asks the user how old they are. Tell them if they are old enough to
vote. Then extend the program tell them how many years it is until they can retire (assume at age
65).
25) Write a function that asks the user to input a number between 1 and 20. Give a response which
indicates if the number is either within the range, too high or too low.
26) Complete the following task

Three is a Crowd
• Make a list of names that includes at least four people.
• Write an if test that prints a message about the room being crowded, if there are more than three
people in your list.
• Modify your list so that there are only two people in it. Use one of the methods for removing people
from the list, don't just redefine the list.
• Run your if test again. There should be no output this time, because there are less than three people in
the list.
• Add an else statement to your if tests. If the else statement is run, have it print a message that the room
is not very crowded.

27) Complete the following task

Six is a Mob
• Save your program from Three is a Crowd - Part 2 under a new name.
• Add some names to your list, so that there are at least six people in the list.
• Modify your tests so that
Ø If there are more than 5 people, a message is printed about there being a mob in the room.
Ø If there are 3-5 people, a message is printed about the room being crowded.
Ø If there are 1 or 2 people, a message is printed about the room not being crowded.
Ø If there are no people in the room, a message is printed abou the room being empty.

28) Write down the output of the following code:

29) What is the output of the following code:


power = 5
while power > 0:
print("You are still playing, because your power is " + power)
power = power – 1
else
print("\nOh no, your power dropped to 0! Game Over.")

30) Write a python program to print the square of all numbers from 0 to 10
31) Write a python program to find the sum of all even numbers from 0 to 10
32) Write a python program to read three numbers (a,b,c) and check how many numbers between ‘a’
and ‘b’ are divisible by ‘c’
33) Write code for the following task:

Growing Strength
• Make a variable called strength, and set its initial value to 5.
• Print a message reporting the player's strength.
• Set up a while loop that runs until the player's strength increases to a value such as 10.
• Inside the while loop, print a message that reports the player's current strength.
• Inside the while loop, write a statement that increases the player's strength.
• Outside the while loop, print a message reporting that the player has grown too strong, and that they
have moved up to a new level of the game.

34) What is the output of each block of code:


fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)

fruits = ["apple", "banana", "cherry"]


for x in fruits:
print(x)
if x == "banana":
break

fruits = ["apple", "banana", "cherry"]


for x in fruits:
if x == "banana":
break
print(x)

fruits = ["apple", "banana", "cherry"]


for x in fruits:
if x == "banana":
continue
print(x)

for x in range(6):
print(x)
else:
print("Finally finished!")

for x in range(5):
print(x)

for x in range(3, 6):


print(x)

for x in range(3, 8, 2):


print(x)

for x in range(10):
if x % 2 == 0:
continue
print(x)

numbers = [1, 5, 12, 91, 102]


for i in numbers:
print(i * i)

numbers = [1, 5, 12, 91, 102]


for i in numbers:
print(i * i)

my_list = range(0,10)
for i in my_list:
print(i)

adj = ["red", "big", "tasty"]


fruits = ["apple", "banana", "cherry"]
for x in adj:
for y in fruits:
print(x, y)

my_list = [1, 5, 12, 91, 102]


my_list_length = len(my_list)
for i in range(0,my_list_length):
print(i, my_list[i] * my_list[i])

dog = ['Freddie', 9, True, 1.1, 2001, ['bone', 'little ball']]


for i in dog:
print(i)

For the following questions, where it asks you to write “pseudocode”:


• AS Students: You may write either pseudocode or Python, whichever you
prefer (you are welcome to try both if you like)
• A2 Students: you must write BOTH pseudocode and Python
Identifier Data Type Description
Following questions are for A2 students (AS students are welcome to attempt
them if they wish)

You might also like