Pseudocode: presenting query results

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Pseudocode: presenting query results

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A query returns 5 student records. Where should the heading be printed?
A Before the loop, so it prints once
B Inside the loop, so it prints for every record
2. Which loop prints each returned record as one line?
A print results.name for each record in results
B for each record in results print record.name
3. Where should a count of the records be printed?
A After the loop finishes
B At the start of each loop pass
4. Which line builds a formatted output line for one record?
A print record
B print record.name + " - " + record.mark
5. A query returns zero records. What should the program do?
A Print a no results message
B Print the heading and a total of nothing with no explanation

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1print "Name | Mark" Aif count = 0 then print "No results found"
2for each record in results Bprint "Records found: " + count
3When the query returns no records, the program prints a no results message instead of an empty list. CThe fields have no separator, so the output runs together, such as Ava88. It needs a separator between the fields.
4After the loop, a summary line reports how many records were displayed. DRepeats the loop body once for every record the query returned.
5for each record in results\n print "Name | Mark"\n print record.name + " | " + record.mark EThe heading is printed inside the loop, so it repeats above every record. It should be printed once before the loop.
6print record.name + record.mark FPrints a heading row once, before the loop, so each column of the results is labelled.

Definitions

for each record in results

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
_____ "Name | Points" for each record in results print record.name + " | " + record.points
2.
print "Name | Points" for each record in _______ print record.name + " | " + record.points
3.
for each record in results print record.name _ " - " + record.points end for
4.
count ← count + 1 end for print "Total records: " + _____
5.
if count = _ then print "No results found" end if

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A print "Top students"
B print r.name, r.mark
C for each r in results
D print r.name, r.mark
E end for
Question 2
A print "Name Points"
B for each r in results then
C print r.name, r.points
D end for
Question 3
A print "Name Points"
B for each r in results
C print r.name, r.points
D end if
Question 4
A print ID, Name, Mark
B for each r in results
C print r.id, r.name, r.mark
D end for
Question 5
A count ← length(results)
B if count = 0 then
C print No results found
D end if

Answer Key

Pseudocode: presenting query results
Section A: Quick Questions
Q1
A — Before the loop, so it prints once
Q2
B — for each record in results print record.name
Q3
A — After the loop finishes
Q4
B — print record.name + " - " + record.mark
Q5
A — Print a no results message
Section B: Match the Pairs
Q1
print "Name | Mark" → Prints a heading row once, before the loop, so each column of the results is labelled.
Q2
for each record in results → Repeats the loop body once for every record the query returned.
Q3
When the query returns no records, the program prints a no results message instead of an empty list. → if count = 0 then print "No results found"
Q4
After the loop, a summary line reports how many records were displayed. → print "Records found: " + count
Q5
for each record in results\n print "Name | Mark"\n print record.name + " | " + record.mark → The heading is printed inside the loop, so it repeats above every record. It should be printed once before the loop.
Q6
print record.name + record.mark → The fields have no separator, so the output runs together, such as Ava88. It needs a separator between the fields.
Section C: Definitions
Q1
for each record in results — Loops through the returned records, printing each as a formatted line under a heading, then prints a summary such as a count or total. If there are none, print a no results message.
Section D: Fill in the Blanks
Q1
print "Name | Points" for each record in results print record.name + " | " + record.points
Q2
print "Name | Points" for each record in results print record.name + " | " + record.points
Q3
for each record in results print record.name + " - " + record.points end for
Q4
count ← count + 1 end for print "Total records: " + count
Q5
if count = 0 then print "No results found" end if
Section E: Spot the Error
Q1
B — The heading line must be a fixed text string. This line uses r.name and r.mark before the loop has created r, so it would fail. It should be print "Name Mark".
Q2
B — A for each loop header does not use the word then, because then belongs to an if statement. It should be for each r in results.
Q3
D — The loop was opened with for each, so it must be closed with end for. It should be end for, not end if.
Q4
A — The heading is text, so it needs quotation marks. Without them ID, Name and Mark are treated as undefined variables. It should be print "ID Name Mark".
Q5
C — The no results message is a text string and must be in quotation marks. It should be print "No results found".