Assessment test Answers - Callum & Ryans Web - Home



Assessment test Answers1.What value is assigned to the variable X by each of the following statements?(a) X = 1 + 5/2 3.5[1](b)X = (25 – 4) / 3 7[1](c)X = 5 + 7 MOD 2 6[1](d)X = ((27 + 5) /2) DIV 3 5[1]2.(a)Given that a = 7 and b = 6, state in each case whether x will be set to True or False.(i)if (a <= 7 AND b > 4) thenx = Trueelse x = False[1]x = True(ii)if (a > 7 OR b = 1) thenx = Trueelse x = False[1]x = False(iii)if (NOT (a > b) OR (b <= 7)) thenx = Trueelse x = False[1]x = True(b).Write an IF statement involving a Boolean operator (AND, OR or NOT) which performs the same function as the following nested IF statement:if day = “sunny” thenif temperature > 20 thenpicnic = “Yes”else picnic = “No”endifendif[2]if day = “sunny” AND temperature > 20 thenpicnic = “Yes”elsepicnic = “No”endif3.The following pseudocode uses definite iteration. It is intended to find and print the largest number in an array numbers of ten positive integers.max = numbers[0]for n = 1 to 9if max > numbers[n] thenmax = numbers[n]endifnext nprint (“Maximum is”, max)(a)The pseudocode contains an error and does not work correctly. Correct the error.[1]if max < numbers[n] then… or, if numbers[n] > max then…(b) Write the corrected algorithm using a while… endwhile loop instead of a for … next loop[3]max = numbers[0]n = 1while n < 10if numbers[n]> max thenmax = number[n]endifn = n + 1endwhileprint (“Maximum is”, max)4.Manjeri writes a program to draw circles in random colours, sizes and positions on the screen. She uses a random number generator to generate a colour number which she allocates to red, yellow, green or blue.Forever = Truewhile Forever == Truefor i = 1 to 20Xcoord = random (1, 400)Ycoord = random (1, 300)size = random (20, 70)colourNumber = random (1, 4)if colourNumber == 1 thencolour = “red”else if colourNumber == 2 thencolour = “yellow”else if colourNumber == 3 thencolour = “green”elsecolour = “blue”endifbrushColour = colourFillCircle(XCoord, YCoord, size)next iDelay 200 #delay a few secondsClear Screenendwhile(a)What type of variable is Colour? String[1](b)State what is meant by selection and iteration, using examples from Manjeri’s program.[4]Selection: Selection is choosing a route through the program depending on a condition. The IF statement in the program is an example of selection.Iteration: Iteration is repeating a series of steps. The FOR loop in the program is an example of iteration.(d)What does the statement ColourNumber = random (1, 4) do?[2]Assigns a random number between 1 and 4 to ColourNumber(c)An array colourArray could be used instead of the IF statement to assign colours to the variable colour. The index of the array starts at 0.(i)How many elements would the array contain? 4[1](ii)What would the array contain? Four strings, “Red”, “Yellow” “Green”, “Blue”[1](iii)Write a statement to assign a colour to the variable colour using the array.[1]colour = colourArray [colourNumber - 1](d)Give two reasons why it is important to use meaningful identifier names in a program.[2]You are less likely to make errors in the code It makes the program easier to understand/debug/maintain (e)Add statements to the program to make the routine repeat indefinitely.[3]See lines of code in red above (or a similar indefinite while or repeat loop)5.Tina wants to create a multiple choice test. Each question will have three possible answers.She writes the first question. The correct answer is “2”. Here is the question:What is the world’s largest ocean?1.Atlantic2.Pacific3.IndianAnswer 1, 2 or 3She writes the following pseudocode:print ("What is the world's largest ocean? ")print ("1. Atlantic")print ("2. Pacific")print ("3. Indian")correctAnswer = "2"answer = input ("Answer 1, 2 or 3: ")(a)Write a selection statement to output “Correct!” if the user enters the correct answer, or “No, it’s the Pacific” if the user enters the wrong answer.[2]if answer == correctAnswer thenprint ("Correct!")elseprint ("No; it's the Pacific")endifTina wants to store the test questions and correct answers in a file on disk. She decides that for each question in the test she needs a record in the text file. The record will contain separate fields for the question, each possible answer and the correct answer.She writes the following pseudocode algorithm to ask the test setter to enter the data for a question. The test setter is prompted to enter the three possible answers for each question and the correct answer, to be stored on the file.testfile = openAppend (“geography_test.txt”)question = input (“Enter the first question, and press Enter:”)answer1 = input (“Enter the first answer")answer2 = input (“Enter the second answer")answer3 = input (“Enter the third answer")correctAnswer = input (“Enter the number of the correct answer")(b)What is the function of the first pseudocode statement?[2]It tells the program the name of the test fileand opens the file so that either a new file is created or new records can be written to the end of the file.(c)Write a statement to write the data for a question to the text file.[2]testfile.writeLine (question, “,”, answer1, “,”, answer2, “,”, answer3, “,”, correctAnswer“\n”)Accept:testfile.writeLine (question, answer1, answer2, answer3, correctAnswer)or a reasonable pseudocode answer which includes all the fields to be written in a single record(d)Describe briefly an alternative method of creating a text file.[2]The user could enter the questions in a text editor such as Notepad, with fields separated by commas, and each record starting on a new line.(e)Tina has started to write an algorithm which allows a student to take the test. She has stored 10 questions on the file named “geography_test.txt”.testfile = openRead (“geography_test.txt”)testScore = 0for n = 1 to 10nextQuestion = testfile.readLine()split nextQuestion into separate fields question, answer1, answer2, answer3, correctAnswerprint (question, answer1, answer2, answer3)userAnswer = input (“Enter 1, 2 or 3:”)if userAnswer == correctAnswer thenprint (“Correct!”)testscore = testscore + 1elseprint (“Wrong – bad luck”)endifnext nclose fileprint (“Your score is”, testScore)Add statements to the above pseudocode which will test whether the answer is correct and give an appropriate response, adding 1 to the test score if the answer is correct. At the end of the test, display the total score and close the file.[5](Give marks for comparing user answer with correct answer, adding one to testScore if correct, giving an appropriate response, closing the file, printing the total score)6.The following function calculates a rail fare, based on the distance travelled. A discount of 10% is given for off-peak travel.function RailFare(distance, day, time)ifdistance < 5 thenRailFare = 3.00elseif distance < 12 thenRailFare = 4.00elseRailFare = 5.00endifif (day == "Saturday") or (day == "Sunday") then RailFare = 0.9 * RailFare else if (time >= 10.00) and (time < 16.00) then RailFare = 0.9 * RailFareendifreturn RailFareendfunction What does the subroutine return if it is called with each of the following statements:(a)RailFare (4, “Monday”, 8.00)3.00(b)RailFare (15, “Saturday”, 14.00) 4.50(c)RailFare (8, “Tuesday”, 18.00)4.00[3]7.The following subroutine returns a value to the main programSUBROUTINE checkval (value, low, high)IF value < low THENRETURN “Low” ELSE IF value > highRETURN “High”ELSE RETURN “OK”ENDIFENDSUBROUTINE(a)(i)Name one parameter used by the subroutine.[1]any of value, low, high(ii)State the data type of the return value[1]string(iii)In the main program, the following statements are written.OUTPUT (“Please enter measurement:”)measurement USERINPUTWrite one or more statements which will call the subroutine in order to check whether the measurement is between 18 and 25, and output the result.[2]result checkval (measurement, 18, 25)OUTPUT resultorOUTPUT (checkval (measurement, 18, 25))(b)Give two reasons why programmers use subroutines.[2]A modular program which puts separate subtasks into subroutines is easier to understand and maintainSubroutines can be tested individually so the program is easier to debugA large complex program can be developed more quickly because different programmers can write different subroutines Subroutines can be re-used in several programsA subroutine may be used several times in the same program but the code only has to be written once[Total 50 marks] ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download

To fulfill the demand for quickly locating and searching documents.

It is intelligent file search solution for home and business.

Literature Lottery

Related searches