APCSP EXAM QUESTIONS AND
ANSWERS
Consider the code fragment shown below.
From temp to x, then x to y.
y ← temperature
Based on the code snippet, how would you best describe what the program is
doing with variables x and y? - ANSWER The program swaps the values of x
and y.
Consider the code fragment shown below.
DISPLAY: "What is your name?" name ↑ INPUT () DISPLAY ("Hello").
Display (name)
What happens if the user types "Karel" into the program? - ANSWER What's
your name? Hello, Karel.
Consider the code fragment shown below.
PROCEDURE: Mystery (number) { RETURN ((number MOD 2) = 0) }
Which of the following most accurately defines the Mystery PROCEDURE's
behaviour? - ANSWER Returns whether an integer is even.
The starting values of a and b in the program below are 0 and 1, respectively.
IF (b = 0)
{ IF (b = 1) { DISPLAY ("Karel") } `
ELSE
{ IF (a = 1) { DISPLAY ("Tracy") }
ELSE
{ DISPLAY ("Dog") } DISPLAY ("Turtle")
What appears as a result of executing this program? - ANSWER: Dog Turtle
, Consider the code fragment shown below.
PROCEDURE Mystery (number) { DISPLAY ("WOW").
REPEAT NUMBER OF TIMES { DISPLAY ("!") } `
What happens when you call the Mystery procedure with an input of 3? -
ANSWER: WOW!!
Consider the code fragment shown below.
X = RANDOM (1, 10)
Y = RANDOM (10, 20)
DISPLAY (x=y)
Which of the following most accurately reflects the code's behavior? -
ANSWER The software may display either true or false.
Consider the code fragment shown below.
PROCEDURE: Mystery (word, list).
{ For each item in the list.
{ IF (item = word) { RETURN (true) } } RETURN (false) `
Which of the following most accurately defines the Mystery PROCEDURE's
behavior? - ANSWER Return whether a word is in the list.
Consider the code fragment shown below.
PROCEDURE: Mystery (word, list).
{` x ← 0 y ← 0 FOR EACH ITEM IN LIST { IF (item = word) { x ← x + 1 }
ELSE { x ← 0 }
IF (x > y)
{y←x}}
RETURN (y);
What is the value of?
Mystery("karel"); ["karel", "karel", "tracy", "karel", "karel", "karel", "tracy"] -
Answer three.
Consider the code fragment shown below.
ANSWERS
Consider the code fragment shown below.
From temp to x, then x to y.
y ← temperature
Based on the code snippet, how would you best describe what the program is
doing with variables x and y? - ANSWER The program swaps the values of x
and y.
Consider the code fragment shown below.
DISPLAY: "What is your name?" name ↑ INPUT () DISPLAY ("Hello").
Display (name)
What happens if the user types "Karel" into the program? - ANSWER What's
your name? Hello, Karel.
Consider the code fragment shown below.
PROCEDURE: Mystery (number) { RETURN ((number MOD 2) = 0) }
Which of the following most accurately defines the Mystery PROCEDURE's
behaviour? - ANSWER Returns whether an integer is even.
The starting values of a and b in the program below are 0 and 1, respectively.
IF (b = 0)
{ IF (b = 1) { DISPLAY ("Karel") } `
ELSE
{ IF (a = 1) { DISPLAY ("Tracy") }
ELSE
{ DISPLAY ("Dog") } DISPLAY ("Turtle")
What appears as a result of executing this program? - ANSWER: Dog Turtle
, Consider the code fragment shown below.
PROCEDURE Mystery (number) { DISPLAY ("WOW").
REPEAT NUMBER OF TIMES { DISPLAY ("!") } `
What happens when you call the Mystery procedure with an input of 3? -
ANSWER: WOW!!
Consider the code fragment shown below.
X = RANDOM (1, 10)
Y = RANDOM (10, 20)
DISPLAY (x=y)
Which of the following most accurately reflects the code's behavior? -
ANSWER The software may display either true or false.
Consider the code fragment shown below.
PROCEDURE: Mystery (word, list).
{ For each item in the list.
{ IF (item = word) { RETURN (true) } } RETURN (false) `
Which of the following most accurately defines the Mystery PROCEDURE's
behavior? - ANSWER Return whether a word is in the list.
Consider the code fragment shown below.
PROCEDURE: Mystery (word, list).
{` x ← 0 y ← 0 FOR EACH ITEM IN LIST { IF (item = word) { x ← x + 1 }
ELSE { x ← 0 }
IF (x > y)
{y←x}}
RETURN (y);
What is the value of?
Mystery("karel"); ["karel", "karel", "tracy", "karel", "karel", "karel", "tracy"] -
Answer three.
Consider the code fragment shown below.