Quiz 4 Solutions

Wed Apr 11

You have up to 25 minutes. You may use a standard calculator if necessary, but no text book, notes, or computer.

1. What is the output of the following Python program? Remember that variable assignments and updates do not produce output – only print statements do. You may want to make some notes to keep track of those variables, so clearly label what is scratch work and what is output.
user = "Inara"
k1 = 3*2
print("Hi", user)
print("Level", k1)
k1 = k1 + 2
print("k1")

Output:

Hi Inara
Level 6
k1

1. The following Python program uses conditional statements (if/else). What is its output? Again, clearly label what is scratch work and what is output.
xy = 5
zq = 3
print(xy*2)
if xy > 7:
print("yes")
else:
print("no")
print(xy + zq)

Output:

10
no
8

1. This question is about scheduling tasks on the CPU in an operating system. We’ll use batch processing, which means that once we start a task, we will run it to completion without interruption. There are two ways to select the next task to run: First-come, first-served (FCFS) and Shortest job first (SJF).

Calculate the average turnaround time for scheduling the following jobs using both FCFS and SJF.

  Job    Time
J1      3
J2      8
J3      2
J4      4

Solution:

The timeline for FCFS looks like this:

    J1    J2         J3   J4
|-----|----------|----|------|
0     3         11   13     17

So the turnaround time for each job is:

  J1   3 - 0 =  3
J2  11 - 0 = 11
J3  13 - 0 = 13
J4  17 - 0 = 17

The average is (3+11+13+17)/4 = 44/4 = 11.0.

Now for SJF, the timeline is:

    J3   J1    J4     J2
|----|-----|------|----------|
0    2     5      9         17

So the turnaround times are:

  J1   5 - 0 =  5
J2  17 - 0 = 17
J3   2 - 0 =  2
J4   9 - 0 =  9

And that average is (5+17+2+9)/4 = 33/4 = 8.25.