IB DP1 CS PRACTICE
6. There were 10 gymnasts who took part in a gymnastics competition. The names and the
scores for all competitors were sorted alphabetically and stored in two arrays, NAMES and
SCORES (see Figure 1).
Figure 1: Data held in the NAMES and the SCORES array. (For example, the score awarded to Mary Allen
was 6.5)
(a) State the name of the gymnast whose score is stored in SCORES[5].
[1]
Labar and Tanya.
(b) Construct an algorithm in pseudocode to determine the average score.
[3]
SET total = 0
SET count = LENGTH(scores)
For n FROM 0 TO count – 1
total = total + scores[n]
END FOR
Average = total / count
OUTPUT “Average Score: “, average
To qualify for the next round of competition, a competitor must have a score above the average
score.
(c) Construct an algorithm in pseudocode that will determine and output the number of
gymnasts whose scores are above the average score.
You may assume that the average score is stored in the variable AVERAGE.
[4]
An algorithm is needed that:
•allows input of a gymnast’s name
•searches for this name in the NAMES array using a binary search
•outputs the gymnast’s score. If the inputted name does not occur in the NAMES array, it
outputs an appropriate message.
For example, from the data given in Figure 1:
•if “Allen, Mary” is the input name, then 6.5 should be output
•if “Peterson, Tina” is the input name, then a message saying “this name is not found” should be
output.
(d) Construct an algorithm in pseudocode as described above.
[7]
7. A real estate business maintains an unsorted database of houses and apartments that it tries
to sell for the property owners. The following UML diagram describes the objects in the current
system.
(a) Define the term primitive data type.
[1]
(b) Explain the concept of base case in recursion and why it is essential in recursive algorithms.
[2] (NOT COVERED IN DP1 wait for DP2)
(c.i) Outline the relationship between Owner and Property and give a reason.
[2]
([Link]) Define inheritance and give an example.
[2]
(d) Distinguish between a class and an instantiation in this scenario.
[2]
(e) Outline how the modifier static affects how a variable is used.
[2]
(f) Describe how the modifier static could be used to access the total number of both House
and Apartment objects that have been created in this system.
[2]
The object-oriented software solution that implements this system for the real estate business
allows a customer to select a maximum of 10 houses that he or she is interested in.
These houses are stored in an array wishList of type House.
(g) Construct the code needed to instantiate an array wishList that can store a maximum of
10 House objects.
[3]
8. All the unsorted House objects in the database have been copied to a sufficiently large array
allHouses. This array is not completely filled with House objects.
The array allHouses and all methods in this question are declared in the main program
class. All methods can access the array allHouses directly.
Consider the following method.
public void unknown(String x)
{
for (int i=0; i<[Link]; i++)
{
if (allHouses[i].getCity().equals(x))
{
[Link] (allHouses[i].getAddress());
}
}
}
(a) Define the term method signature.
[2]
(b i) Describe how the original String variable, passed to a method as a parameter, can be
assigned a new value by that method.
[2]
(b ii) Construct the Java code for the method houseSort that will sort the array allHouses
in ascending order of price [4]
(c) State the intended purpose of the method unknown.
[1]
(d.i) Outline the runtime error that is likely to occur if this method is called.
[2]
([Link]) Outline how this error can be corrected.
[2]
A method is needed to select from the original array allHouses the three most expensive
houses below or equal to a given price.
(e) Construct the code for the method selectThree that will take an integer parameter
budget. It must return a sorted array of size 3 that contains the three most expensive House
objects (in ascending order of price) with a price that is less than or equal to budget.
You may assume that the array allHouses is now sorted and contains at least three House
objects with a price less than or equal to budget.
[6]
MARKING SCHEME
6.
There were 10 gymnasts who took part in a gymnastics competition. The names and the scores
for all competitors were sorted in alphabetical order and stored in two arrays, NAMES and
SCORES (see Figure 1).
Figure 1: Data held in the NAMES array and the SCORES array
For example, the score awarded to Mary Allen was 6.5 .
[N/A]
[[N/A]]
(a) State the name of the gymnast whose score is stored in SCORES[5].
[1]
Markscheme
Award [1 max].
Labar, Tanya or Tanya Labar;
(b)
Construct an algorithm in pseudocode to determine the average score.
[3]
Markscheme
Award [3 max].
Award [1] for initializing all the variables used
Award [1] for a correct loop.
Award [1] for calculating sum using correct array index.
Award [1] for calculating the average.
Example 1:
SUM=0
K=0
loop while K <= 9 //Accept [Link]()-1
SUM = SUM + SCORES[K]
K=K+1
end loop
AVERAGE = SUM / 10 //Accept use of [Link]() or a counter variable
Example 2:
S=0
loop K from 0 to 9
S = S + SCORES[K]
end loop
S = S / 10
To qualify for the next round of competition, a competitor must have a score above the average
score.
(c)
Construct an algorithm in pseudocode that will determine and output the number of gymnasts
whose scores are above the average score.
You may assume that the average score is stored in the variable AVERAGE.
[4]
Markscheme
Award [4 max].
Award [1] for initializing and increasing counter if needed
Award [1] for a correct loop
Award [1] for a correct condition in if statement
Award [1] for output
COUNT=0
loop K from 0 to 9
if SCORES[K]>AVERAGE then
COUNT=COUNT+1
end loop
output COUNT
An algorithm is needed that:
•allows input of a gymnast’s name
•searches for this name in the NAMES array using a binary search
•outputs the gymnast’s score. If the inputted name does not occur in the NAMES array, it
outputs an appropriate message.
For example, from the data given in Figure 1:
•if “Allen, Mary” is the input name, then 6.5 should be output
•if “Peterson, Tina” is the input name, then a message saying “this name is not found” should be
output.
(d) Construct an algorithm in pseudocode as described above.
[7]
Markscheme
Award [7 max].
Award [1] for inputting a name and initializing all variables used
Award [1] for the correct loop
Award [1] for using a flag
Award [1] for calculating the middle index(MID) inside the loop
Award [1] for comparing NAMES[MID] with GYMNAST
Award [1] for changing the value of END if needed
Award [1] for changing the value of START if needed
Award [1] for the if statement after the loop
Award [1] for outputting either SCORES[MID] or an appropriate message
input GYMNAST
START = 0
END = 9
FOUND = False
loop while START <= END and NOT FOUND
MID = (START + END) div 2
if NAMES[MID] > GYMNAST then // method compareTo() may be used
END = MID - 1
else
if NAMES[MID] < GYMNAST then
START = MID + 1
else
FOUND=True
end if
end if
end loop
if FOUND then
output (SCORES[MID])
else
output (GYMNAST, ‘this name is not found’)
end if
7.
A real estate business maintains an unsorted database of houses and apartments that it tries to
sell for the property owners.
The following UML diagram describes the objects in the current system.
(a) Define the term primitive data type.
[1]
Markscheme
Award [1 max].
These are data types that are pre-defined / fundamental / basic in the programming
language;
A data type that is always assigned a value (in the memory);
They are the building blocks of the composite data types / classes / objects;
The types that are implemented directly as bit patterns (by a Java compiler);
(b) State an additional attribute in the class Property that would have data type
[[N/A]]
(b.i) Boolean
[1]
Markscheme
Award [1 max] for any suitable example. Allow a description.
For example
hasParking;
hasPool;
hasGarden;
Whether the property has been sold or not;
([Link])
double (or float).
[1]
Markscheme
Award [1 max] for any suitable example. Allow a description.
For example
height;
area;
Price;
(HL B) Explain the concept of base case in recursion and why it is essential in recursive
algorithms.
The base case in recursion acts as the stopping condition that prevents infinite looping by
terminating the recursive process. It provides the algorithm with a definitive endpoint, ensuring it
doesn't endlessly call itself and potentially lead to stack overflow errors.
(c.i) Outline the relationship between Owner and Property and give a reason.
[2]
Markscheme
Award [1 max].
Aggregation (allow Property 'has a' Owner); Because the two instances can exist on
their own.
([Link]) Define inheritance and give an example.
[2]
Markscheme
Award [1 max].
Inheritance: means creating new classes based on existing ones, by inheriting
attributes and methods from one class to another.
Example: Apartment 'is a' Property;
House is a subclass of Property;
House inherits Property;
(d) Distinguish between a class and an instantiation in this scenario.
[2]
Markscheme
Award [2 max].
Award [1] for distinguishing between a class and an instantiation at the definition level;
Award [1] for including an example; A class (e.g. House class) is a blueprint /
definition / specification (that defines all variables and methods that are needed) and
an instantiation of a class is creates a (new) object of that class [1] (e.g. a (new) object
of an actual house giving its actual address etc [1];
(e) Outline how the modifier static affects how a variable is used.
[2]
Markscheme
Award [2 max].
A static variable is used when it is to be a class variable (belongs to the class not to the
instantiation);
It is used when all objects of that class are to have the same value for the static
variable;
It will not be instantiated when a new object is created;
If the value of a static variable is changed, it will be changed in all instances of that
class;
It does not require an object of that class to be accessed;
(f) Describe how the modifier static could be used to access the total number of both House and
Apartment objects that have been created in this system.
[2]
Markscheme
Award [2 max].
Both classes House and Apartment need a static (integer) variable count;
that is incremented whenever a new object of that class is created; A static variable
(e.g total) could be defined in the Property class;
Which is incremented every time a House or Apartment object is instantiated; A static
method in the Property class retrieves the total number of house and apartments;
From static total variables that are in each of the 2 sub-classes;
The object-oriented software solution that implements this system for the real estate business
allows a customer to select a maximum of 10 houses that he or she is interested in.
These houses are stored in an array wishList of type House.
(g) Construct the code needed to instantiate an array wishList that can store a maximum of 10
House objects.
[3]
Markscheme
Award [3 max].
Award [1] for the identifier (left-hand side);
Award [1] for use of new;
Award [1] for House[10];
House[] wishList = new House[10];
8.
All the unsorted House objects in the database have been copied to a sufficiently large
array allHouses. This array is not completely filled with House objects.
The array allHouses and all methods in this question are declared in the main program class. All
methods can access the array allHouses directly.
Consider the following method.
public void unknown(String x)
{
for (int i=0; i<[Link]; i++)
{
if (allHouses[i].getCity().equals(x))
{
[Link] (allHouses[i].getAddress());
}
}
}
(a) Define the term method signature.
[2]
Markscheme
Award [2 max].
The signature defines the parameters (and their types);
The method name / identifier;
Allow either the return type or access modifier for a mark;
(b) Describe how the original String variable, passed to a method as a parameter, can be
assigned a new value by that method.
[2]
Markscheme
Award [2 max].
String is an immutable object / Java uses pass-by-value / does not use pass-by-
reference;
It cannot simply have its value reassigned inside the method;
The method type must be String;
To return the new value for that String variable;
(c) State the intended purpose of the method unknown.
[1]
Markscheme
Award [1 max].
To output the addresses of all houses in a given city; Note to examiners. Must imply all
the houses that match, not just one.
(d.i) Outline the runtime error that is likely to occur if this method is called.
[2]
Markscheme
Award [2 max]
The length of the array may not correspond to the number of houses stored;
Causing a null pointer error/exception;
Since the loop may try to access the getCity method for a null entry;
([Link]) Outline how this error can be corrected.
[2]
Markscheme
Award [2 max.]
Declare a variable count that stores the number of objects in allHouses;
Change loop condition to j<count; Add a test if(allHouses[i]!=null);
Before testing allHouses[i].getCity().equals(x) ; Use a while loop with a double
condition;
(i<[Link])&&(allHouses[i]!=null) ; Initially fill the array with dummy objects;
So that no null entries will be encountered; Change the array to an arrayList;
So that no null entries will be encountered / so that the array has the exact number of
valid objects in it;
A method is needed to select from the original unsorted array allHouses the three
most expensive houses below or equal to a given price.
(e) Construct the code for the method selectThree that will take an integer parameter budget. It
must return a sorted array of size 3 that contains the three most expensive House objects (in
ascending order of price) with a price that is less than or equal to budget.
You may assume that the array allHouses is now sorted and contains at least three House
objects with a price less than or equal to budget.
[6]
Markscheme
Award [6 max].
Award [1] for correct return type and return;
Award [1] for correct parameter, budget (allow allHouses to be passed as well);
Award [1] for correctly instantiating a result array;
Award [1] for while loop with one correct condition; // other loops could be used
Award [1] for while loop with two correct conditions in the right order; // note that some
of these conditions may be implemented as IF statements;
Award [2] for assigning all 3 House objects in the correct order (award [1] for good
attempt at identifying the 3 objects);
Example answer 1
public House[] selectThree(int budget)
{
House[] result = new House[3];
houseSort(); // allow [Link]()
int i=0;
while ((i<[Link])&&(allHouses[i]!=null)
&&(allHouses[i].getPrice()<=budget))
{
i++;
}
i--;
for (int a=0; a<=2; a++)
{
result[a] = allHouses[i-2+a];
}
return result;
}
Example answer 2
public House[] selectThree(int budget)
{
House[] result = new House[3];
houseSort();
int i=0;
while ((i<[Link])&&(allHouses[i]!=null)
&&(allHouses[i].getPrice()<=budget))
{
i++;
}
i--;
result[0] = allHouses[i-2];
result[1] = allHouses[i-1];
result[2] = allHouses[i];
return result;
}
Example answer 3
public House[] selectThree(int budget)
{
House[] result = new House[3];
houseSort();
int index = 2;
for(int i=[Link]-1; i>=0 && index>=0; i--)
{
if(allHouses[i]!=null && allHouses[i].getPrice()<=budget)
result[index--] = allHouses[i];
}
return result;
}
SL 8 b