Exam Qs Flashcards

1
Q

Exercise 1
Write a single script which prints out the results of the following sums. Place the values into variables before printing them rather than just passing the results to the print function (you will need this for questions g and h).

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Exercise 2
Write some code which calculates the area of a rectangle using the formula area = width * height

Do this by setting up a variable for width and height, then calculate the area and store it in the area variable. Then print out “Area: x, Height: y, Width: z”.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Exercise 3
Set up a list called my_list containing the numbers 1 to 4 inclusive. Print the first and last entries in the list. Try and find two different ways to print the last entry in the list.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Set up an empty list. Loop around the numbers between 0 and 10 and, on each loop iteration, add the number squared to the end of the list. Print the final list.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Exercise 5
Set up a tuple containing three floating point numbers: 2.0, 4.0, 6.0. Print out the tuple.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Exercise 6
Print every number from 0 to 30 inclusive.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Exercise 7
Use a loop to print every odd number between 0 to 30 inclusive

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Exercise 8
Write python code to create and output a times table (from 1 to 12) of a number which you define as a variable. Your code should start like this

An example output might start:

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Exercise 9
Take your previous example and use another loop to make it produce multiplication tables for numbers from 1 to 12. Put a title before each table.

Your output should start something like:

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Exercise 10
Write a program to output a list of temperatures in Celsius and Fahrenheit running from -10C to 50C.

The equation for calculating Fahrenheit from Celsius is: f=C∗95+32 . italicised text Print the table neatly.

Double-click (or enter) to edit

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is wrong with this line of code

A

The quotes do not match. Either use all single quotes or all double quotes. For example, name=’Alice’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why does this code raise an error? - (2)

A

age is a string variable but you are adding an integer to it.

Variable types have to match when doing arithmetic.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is incorrect with this code

A

The list only has 3 entries but you are trying to access the fourth one.

Python indexing starts at zero.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What will this print, and why might it confuse a beginner?

A

This will concatenate the lists: [1,2,3,4,5,6]

You might be expecting it to add them element by element [5,7,9], or even include list2 as a member of list 1 [1,2,3,[4,5,6]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What concept does this example illustrate, and what is the mistake

A

You cannot change tuples once you make them. Here you are trying to alter the second element in the tupe. You can’t.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Identify and correct the mistake in this loop

A

The variable ‘num’ is not defined. That line should read

print(number

17
Q

What will be the output of this loop, and what common misunderstanding might this clarify

A

0,1,2,3,4

It will demonstrate that python ranges are zero indexed and exclusive (they start at zero and do not include the last number)

18
Q

What will be the output of this nested loop, and what concept does it illustrate

A

0 0

0 1

1 0

1 1

2 0

2 1

Ranges start at zero and exclude the highest element. Also loop blocks are defined by indentation. And nested loops are evaluated from the deepest loop out.

19
Q

Why doesn’t this code snippet print “Hello, John!”?

A

The variable ‘name’ and the word ‘name’ inside the string are different things. You could write
print(“Hello”,name)

To get the right answer.

20
Q

What error will this code produce and why?

A

hat the function looks like it takes two arguments (x and y) but you are only providing one argument when you call it (2)

21
Q

Explain what each of these ‘magic’ / or ‘shell’ commands does.

A

pwd = print the working directory

cd = change directory (in this case to the one above)

ls = list the contents of the current directory