Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COMP1010: Introductory Computer Science I

Final Examination

Part 1: Short answer (32 marks)

[2]  1) What will be drawn in the canvas by the program below?

size(600,600);

background(255);

line(200,0,200, 600);

ellipse(400,300,100,100);

rect(300,400,200,100);

[2]  2) What will be printed in the console by the following program?

float x = 9;

int y = 2;

println(x / y);

println(y % 3);

[2]  3) Study the code below.  Then list all the possible widths of the rectangles.

int x = 1;

void draw () {

x = (x % 3) + 10;

rect(mouseX, mouseY, x, x);

}

[2]  4) In the code below, draw an arrow from each use of a variable (the four places are shown in bold) to the declaration of that variable. One arrow is shown for you.

int x = 3;

float y = 2;

void setup(){

float y=2;

fn(x + y);

}

void fn(float x){

y = 0;

float y = 5 + x;

println(y);

}

[2]  5) Write a single IF statement that will match the comment below.

//Reset count to 0 if x is a multiple of 10, or y is a multiple of 10, //or both x and y are multiples of 5.

THE UNIVERSITY Of MANITOBA

COMP1010: Introductory Computer Science I

Final Examination

[2]  6) What will be printed in the console by the following program?

float x = 3.2;

float y = 2.3;

boolean a = x <= y;

boolean b = 2*y > x;

if(a || !b) {

if(b && !a)

println("one");

else

println("two");

}

else

if(b && !a)

println("three");

else

println("four");

[2]  7) When should you use a do-while loop instead of a while loop?

[2]  8) Complete the code to print a random number between 1 and 100 whose last digit is a 5 or an 8.

int n;

int lastDigit;

do {

n = __________________________________________________________________ lastDigit = n % 10;

} while(_________________________________________________________________________ ); println(n + " ends with " + lastDigit);

[2]  9) There is an int variable score in a program. Give the statements that will draw the value of score in the centre of the canvas.

    Use text 24 pixels tall.

    Draw it in red.

    You can use height/2 for the y coordinate, but adjust the x coordinate so that the number

is horizontally centred in the canvas.

[2]  10) Complete the code to print the average word length of the words in the String words. (For the

example below, the average would be 2.8.) The String will only contain letters and commas, with no comma after the last word. It must work for any such String. Do not use a loop.

String words = "the,cat,in,the,hat";

int numCommas = 4;

println ("The average word length is: " +

_____________________________________________________________________________________ );

[2]  11) What will be printed in the console by the following program?

float w = 2.2;

void setup(){

float y = 1.0;

float x = fn(w + y, y);

println(x + "," + y + "," + w);

}

float fn(float x, float y){

w = 0.5;

y = 2.0;

return w + x + y;

}

[1]  12) Why are functions helpful? Circle your answer.

a)        Functions make code more readable.

b)        Functions allow for code re-use.

c)        Functions break a large problem into manageable chunks.

d)        Functions reduce the size of the setup and draw functions.

e)        Functions are helpful for all of the above reasons.

[1]  13) Given the following function header, which of the following function calls would be legal?

String makeStr(char fillChar, int size) { 

a)         double d = makeStr('A', 10);

b)        println(makeStr(' ', 50));

c)         String s = makeStr("FILL!", 10);

d)        char c = makeStr(char fillChar, int size);

e)         String s = makeStr('a', 3.14159);

[2]  14) What will be printed in the console by the following program?

int[] a = {1,2,3};               Console:

void setup(){

test1(a);

println(a[0] + "," + a[1]);

test2(a);

println(a[0] + "," + a[1]);

}

void test1(int[] x){

x = new int[5];

}

void test2(int[] x){

x[0] = 5;

}

[1]  15) What does the array arr contain after running the following code?

int[] arr = {1, 2, 3, 4, 5, 6, 7};

for (int i = 0; i < arr.length / 2; i++) {

int temp = arr[arr.length - 1 - i];

arr[arr.length - 1 - i] = arr[i];

arr[i] = temp;

}

a) {1, 2, 3, 4, 3, 2, 1}

b) {1, 2, 3, 4, 5, 6, 7}

c) {7, 6, 5, 4, 3, 2, 1}

d) {1, 1, 1, 1, 1, 1, 1}

e) {1, 3, 5, 7, 2, 4, 6}

[1]  16) Given the following arrays, which statement will make y a deep copy of x?

int[] x = {1, 2, 3};

int[] y = new int[x.length];

a)        y = x;

b)        y[] = x[];

c)        y = x.copyArray();

d)        for (int i = 0; i < x.length; i++) { y = x; }

e)        for (int i = 0; i < x.length; i++) { y[i] = x[i]; }

[2]  17) Give the main advantage and the main disadvantage of using a binary search instead of a linear

search.

Advantage: _______________________________________________________________________________

Disadvantage: _____________________________________________________________________________

[Questions 18 and 19 have been deleted.]

Part 2: Programming (40 marks)

[5]  20) It’s spring! Complete the code below to draw a flower.                     Use a 400x400 canvas with a white background, and thick

lines (6 pixels wide). [1 mark]

    Set the colours as indicated (red, white, black, green) [1 mark]     The stem should be a green line from the centre of the circle to

the bottom of the canvas [1 mark]

    Draw a red circle with a black border for the flower. Its centre

should be in the centre of the canvas. Its diameter should be ½ the width of the canvas. [1 mark]

    Draw a white square with a black border in the middle of the

red circle, with a width that is half the diameter of the circle. [1 mark]

Red circle

Green stem

[5]  21) With spring come bugs! Write a program to draw the flower from Question 20, with a bug that moves around the flower.

    The bug is a black circle that should rotate around the flower

somewhere in the space between the edge of the red flower and    the edge of the canvas. The bug should not touch the flower or the edge of the canvas. The exact position doesn’t matter.

    The bug should start in the position shown, to the right of the

flower.

    The bug should circle clockwise around the flower, taking 2

seconds to go once around the flower.

    The width and height of the bug should be 1/40th of the width of

the canvas.

    You can assume that your code from Question 20 is in the place

indicated, except for the size command. Put that one in its proper place.

Black Bug

// ASSUME YOUR CODE TO DRAW THE FLOWER IS HERE

// YOU DO NOT NEED TO WRITE IT AGAIN

[5]  22)    Complete the function below which will calculate and return the discount to be given to a      customer, as a value from 0.0 (full price) to 1.0 (free). For example, if the customer should get a 20% discount, this function should return 0.20. The discount is based on the quantity of items    ordered, and whether or not the customer is a “preferred customer” . The rules are given below,  and suitable constants have been defined for you.

    Preferred customers get

o a 25% discount for 100 items or more

o a 10% discount for fewer items.

    Regular customers get

o a 15% discount for 200 items or more

o a 5% discount for 50- 199 items

o no discount at all otherwise.

float discount(boolean preferred, int quantity){

final float PREF_BULK = 0.25;

final float PREF = 0.10;

final float REG_BULK = 0.15;

final float REG_LARGE = 0.05;

[5]  23)    Complete the function below, which will draw a rectangle with the

given top left corner, width, and height, and then fill it with the given

number of rows and columns of ellipses. For example, the function

call texturedRect(50,100,400,200,5,8); should produce the

result shown at right, if the canvas is 500x500.

void texturedRect(int left, int top,

int wide, int high,

int rows, int cols){

stroke(0);

strokeWeight(1);

fill(255);

[5]  24)    Complete the function below, which will accept a String s. If s contains a < character, and then a > character to the right of that, then this function should return a String containing all of the characters that appear between < and >. If there is no <, or no >, or if > appears before <   instead of after it, then the function should return an empty String. You may assume that there are no more than one of each of < and > characters. For example:

grabTag("Test <one> two") should return "one"

grabTag("Test <one two") should return ""

grabTag("Test >one< two") should return ""

In this question you are only allowed to use the basic length, charAt, and + operations on   Strings. You can not use indexOf, substring, or any other String operations, even if you happen to know them.

String grabTag(String s){

[5]  25)    a) Write a function hasComment which will accept a String containing a single line of code    from a Processing program. It should determine whether or not the line contains a // comment. If it does, it should return an int giving the position of the comment (the index of the first of the      two / consecutive characters), otherwise it should return - 1. For example,

hasComment("int noComment=0;") should return  -1

hasComment("int x=width/2; //This is x") should return  15

b) Write a function chopComment which will also accept a String containing a single line of code from a Processing program. It should remove any // comment from the line, if there is one,          returning a String containing the possibly modified line. It should, of course, use the                    hasComment function. For example,

chopComment("int noComment=0;") should return  "int noComment=0;" chopComment("int x=0; //This is x") should return  " int x=0; "

[5]  26)    Mirror writing: Complete the program below that, whenever the mouse button is pressed, will store the path that the mouse is taking by recording the sequence of mouseX and mouseY values in arrays, but will not draw anything. When the mouse button is released the program should            immediately draw the mirror image of whatever the user drew (flipped left-to-right). It should     repeat the process every time the mouse button is pressed.

void setup(){

size(500,500);

}

void draw(){

}//close draw

[5]  27)    Complete the function myBestSearch below which should search myArray for key. If key is    found, the function should return the index (position) where it occurs in myArray. If the key is not found in the array the function should return - 1.

Note: The maximum mark is 5 if you do a binary search, and 3 if you do a linear search.

int myBestSearch (int[] myArray, int key){