Internet & Network Review

Pseudocode
Functions & Procedures
Loops
Arrays
100

What is a computer network?

Two or more connected computing devices that can communicate and share resources.

100

What value is printed?

score ← 12 

score ← score + 8 

print(score)

20. The second statement adds 8 to the current value of score.

100

Why would a programmer create a procedure for a task performed several times in a program?


To reuse the same instructions through procedure calls, reduce repeated code, and make updates easier.

100

How many times is "Ready" printed?

REPEAT 4 TIMES

{

    print("Ready")

}

4 times.

100

What value is printed?

colors ← ["red", "blue", "green", "yellow"]

print(colors[2])

"green". The elements have indices 0, 1, 2, and 3.

200

How does the Internet differ from the World Wide Web?

The Internet is a network of interconnected networks. The Web is a system of linked pages and resources accessed using the Internet.

200

What value is printed?

result ← 18 - 3 * 4 + 10 MOD 4 

print(result)

8. Multiplication and MOD occur before addition and subtraction: 18 - 12 + 2 = 8.

200

Identify the parameter and the argument.

PROCEDURE greet(name)

{

    print("Hello, " + name)

}

greet("Maya")

Name is the parameter. "Maya" is the argument supplied in the call.

200

What value is printed?

total ← 2


REPEAT 3 TIMES

{

    total ← total + 4

}


print(total)

14. The successive values are 6, 10, and 14.

200

What value is printed?

scores ← [85, 92, 78, 95] scores[2] ← 80
print(scores[0] + scores[2])

165. The element at index 2 becomes 80, so the calculation is 85 + 80.

300

Two connections have the same bandwidth, but one has much higher latency. Which would generally be better for a live video conversation, and why?

The connection with lower latency because information takes less time to travel, reducing conversational delays.

300

What values are printed, in order?

x ← 4

y ← 9

x ← y

y ← x + 2

print(x)

print(y)

9, then 11. Assigning y to x replaces the original value of x.

300

What value is printed?

PROCEDURE calculateScore(points, bonus)

{

    RETURN(points + bonus * 2)

}

result ← calculateScore(6, 4)

print(result)

14. The procedure calculates 6 + 4 * 2 and returns the result.

300

What values are printed, in order?

number ← 2


REPEAT UNTIL (number > 10)

{

    print(number)

    number ← number + 3

}

2, 5, and 8. The next value is 11, which makes the stopping condition true.

300

What value is printed?

numbers ← [4, 9, 2, 7, 6]

total ← 0


FOR EACH number IN numbers

{

    IF (number > 5)

    {

        total ← total + number

    }

}


print(total)

22. Only 9, 7, and 6 are added. A FOR EACH loop visits the elements without requiring an index.

400

A photo is sent across the Internet in packets. Must all packets travel along the same route and arrive in order? Explain.

No. Packets can travel along different routes and arrive out of order. Protocols such as TCP support putting data back in order and recovering missing data.

400

What is printed?

points ← 75

hasPass ← true

usedHint ← true


IF ((points ≥ 80) OR hasPass)

{

    IF (NOT usedHint)

    {

        print("Gold")

    }

    ELSE

    {

        print("Silver")

    }

}

ELSE

{

    print("Bronze")

}

Silver. The outer condition is true because hasPass is true. The inner condition is false because a hint was used.

400

What value is printed?

PROCEDURE adjust(value, amount)

{

    IF (value < 10)

    {

        RETURN(value + amount)

    }

    ELSE

    {

        RETURN(value - amount)

    }

}

score ← adjust(7, 5)

score ← adjust(score, 3)

print(score)

9. The first call returns 12. The second call receives 12 and returns 12 - 3 = 9.

400

What value is printed?

total ← 0


REPEAT 3 TIMES

{

    REPEAT 2 TIMES

    {

        total ← total + 1

    }


    total ← total + 4

}

print(total)

18. Each outer iteration adds 2 + 4 = 6. Three outer iterations add 18 altogether.

400

What are the final contents of values?

values ← [2, 4, 6, 8]

index ← 1


REPEAT 3 TIMES

{

    values[index] ← values[index] + values[index - 1]

    index ← index + 1

}

[2, 6, 12, 20].

Each calculation uses the most recently updated previous element:

  • Index 1: 4 + 2 = 6
  • Index 2: 6 + 6 = 12
  • Index 3: 8 + 12 = 20
500

A network has these two-way connections: A–B, A–C, B–D, C–D, and D–E. Identify the single connection whose failure would disconnect E from A. Explain why the other connections do not create the same vulnerability.

D–E. Every route to E must use that connection. Alternate routes through B or C allow A to reach D if any one of the other connections fails.

500

A student wants to swap the values of a and b.

What does this program actually print, and how can it be corrected?

a ← 3

b ← 8


a ← b

b ← a


print(a)

print(b)

It prints 8, then 8. The original value of a is overwritten. Store it in a temporary variable:

temp ← a

a ← b

b ← temp

 

500

This procedure prints the correct average, but the main program needs to store that average and use it in a calculation. Identify the problem and correct the procedure.

PROCEDURE average(a, b)

{

    result ← (a + b) / 2

    print(result)

}

grade ← average(80, 100)

print(grade + 5)

Printing a value does not return it to the calling code. Replace print(result) with RETURN(result). The corrected program prints 95.

500

Explain why this loop never stops. Then change only the update statement so the program stops and prints 0.

count ← 9


REPEAT UNTIL (count = 0)

{

    count ← count - 2

}


print(count)

Subtracting 2 from 9 produces 7, 5, 3, 1, -1, ..., so count never equals 0. Change the update to either:

count ← count - 1

OR 

count ← count - 3


500

This program is supposed to find the greatest value in any nonempty array of numbers.

Why does it fail for the array shown? Change only the initialization of greatest so the algorithm works for any nonempty array.

numbers ← [-8, -3, -12, -5]

greatest ← 0


FOR EACH number IN numbers

{

    IF (number > greatest)

    {

        greatest ← number

    }

}


print(greatest)

It incorrectly prints 0 because every array element is less than the initial value. Initialize using the first element:

greatest ← numbers[0]

The corrected program prints -3.

M
e
n
u