Lists and procedures in AP pseudocode

Computer Science · AP CSP

Question 1 of 10

0

    Here is a program in the AP exam reference language. Remember that lists are indexed from 1. aList ← [19, 5, 8, 5, 4] INSERT(aList, 5, 2) INSERT(aList, 6, 5) DISPLAY(LENGTH(aList)) What value is displayed?

    Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.

    How to type your answer
    • Fractions: 3/4. Mixed numbers: 1 1/2.
    • Decimals with a point or a comma: 0.75 or 0,75.
    • Negative numbers start with a minus: -3/4.
    • Percentages need the % sign: 75%.

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Lists and procedures in AP pseudocode explained

    AP Computer Science Principles pseudocode stores collections of values in lists. The first item of a list is at index 1, not index 0, and the last item is at index LENGTH(list). You read an item with list[i], and you change lists with the procedures APPEND(list, value), INSERT(list, i, value) and REMOVE(list, i). Each of these changes the list in place.

    A procedure is a named block of code with parameters. It is defined with PROCEDURE and END PROCEDURE, and it can be called with values in brackets. A procedure that gives back a value uses RETURN. Because a procedure works on the same list it was given, changes made to that list inside the procedure are visible to the caller afterwards.

    Common mistakes include starting a loop at 0 when the list begins at 1, and stopping one item early by using LENGTH(list) - 1 as the end of a loop without a reason.

    Worked example

    Write a procedure that returns the total of a list of numbers.

    PROCEDURE sumList(numbers) sets total to 0, then runs FOR EACH item IN numbers and adds item to total. It then RETURNs total.

    Call sumList with the list [4, 9, 2]. The loop adds 4, then 9, then 2. The total is 15, so the call returns 15.

    What you will practise

    New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.

    Example questions

    • Easy

      Here is a program in the AP exam reference language. Remember that lists are indexed from 1. nums ← [19, 19, 10, 20, 7] INSERT(nums, 4, 9) INSERT(nums, 3, 18) DISPLAY(LENGTH(nums)) What is displayed when the program runs?

    • Medium

      The code below is written in the AP exam reference language, where the first element of a list is at index 1. scores ← [19, 2, 4, 15] scores[2] ← scores[2] * 2 INSERT(scores, 3, 14) REMOVE(scores, 5) DISPLAY(scores[1] + scores[LENGTH(scores)]) What is displayed when the program runs?

    • Hard

      Here is a program in the AP exam reference language. Remember that lists are indexed from 1. PROCEDURE countAbove(aList, limit) { count ← 0 FOR EACH item IN aList { IF (item > limit) { count ← count + 1 } } RETURN(count) } values ← [15, 7, 2, 15, 20, 15, 14] INSERT(values, 8, 15) REMOVE(values, 4) DISPLAY(countAbove(values, 13)) What number does DISPLAY show?