0% found this document useful (0 votes)
13 views4 pages

1 - 2 Memo - PRG100S - MainTest - Nov2022 - 2

Uploaded by

ndivhuwotroye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

1 - 2 Memo - PRG100S - MainTest - Nov2022 - 2

Uploaded by

ndivhuwotroye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SECTION A 60 Marks

QUESTION 1 – MULTIPLE CHOICE

1.1. D 1.10. D 1.19. C


1.2. C 1.11. C 1.20. B
1.3. A 1.12. A 1.21. A
1.4. C 1.13. C 1.22. C
1.5. D 1.14. D 1.23. A
1.6. C 1.15. C 1.24. C
1.7. A 1.16. B 1.25. D
1.8. B 1.17. B
1.9. D 1.18. C

QU EST ION 2 – FILL IN T HE BLAN KS

2.1. Increment
2.2. do while
2.3. altered / changed
2.4. Validation

2.5. Loops

2.6. index.
2.7. group
2.8. flag
2.9. constants
2.10. subscript

QU EST ION 3 – SHORT AN SWERS


3.1. What are common mistakes made by programmers in coding loops?
Failing to initialize the loop control variable
Neglecting to alter the loop control variable
Using the wrong comparison with the loop control variable
Including statements inside the loop that belong outside the loop

3.2. What are the three steps that should occur in every loop?
1. The loop control variable is initialized before entering the loop.
2. The loop control variable is tested, and, if the result is true, the loop body is
entered.
3. The loop control variable is altered within the body of the loop so that the while
expression eventually evaluates as false.
3.3. When are you required to use a for statement, and what are the advantages
of for statements?

You never are required to use a for statement for any loop. A while loop can
always be used instead of a for loop, but when a loop’s execution is based on a
loop control variable progressing from a known starting value to a known ending
value in equal steps, the for loop provides a convenient shorthand. It is easy for
others to read, and because the loop control variable’s initialization, testing, and
alteration are all performed in one location, you are less likely to leave out one of
these crucial elements.

3.4. What is an accumulator?


An accumulator is a variable that you use to gather or accumulate values and is
very similar to a counter that you use to count loop iterations.
Usually, however, you add just one to a counter, whereas you add some other value
to an accumulator.

3.5. Explain when it would be appropriate to write an indefinite loop.


Often, the value of a loop control variable is not altered by arithmetic, but instead
is altered by user input. For example, perhaps you want to continue performing
some task while the user indicates a desire to continue. In that case, you do not
know when you write the program whether the loop will be executed two times, 200
times, or not at all. This type of loop is an indefinite loop.

3.6. What happens when a beginning programmer forgets that array subscripts
start with 0?
A common error by beginning programmers is to forget that array subscripts start
with 0. If you assume that an array’s first subscript is 1, you will always be “off by
one” in your array manipulation.

3.7. What is the advantage of using an array to match a value in a range of


values?
When using an array to store range limits, you use a loop to make a series of
comparisons that would otherwise require many separate decisions. The program
that determines customer discount rates requires fewer instructions than one that
does not use an array, and modifications to the program will be easier to make in
the future.

3.8. Describe an array and provide an example of a real-life equivalent to an array.


An array is a series or list of variables in computer memory, all of which have the
same name and data type but are differentiated with special numbers called
subscripts. Usually, all the values in an array have something in common; for
example, they might represent a list of employee ID numbers or prices for items
sold in a store.

3.9. How do you reference the elements in an array?


Each array element is differentiated from the others with a unique subscript, also
called an index, which is a number that indicates the position of a particular item
within an array. All array elements have the same group name, but each individual
element also has a unique subscript indicating how far away it is from the first
element. For example, a five-element array uses subscripts 0 through 4, and a ten-
element array uses subscripts 0 through 9. In all languages, subscript values must
be sequential integers (whole numbers)

3.10. What are the steps to verify that an item number exists in an array? Provide
an example.
 Set a flag variable named foundIt to "N". A flag is a variable that is set to
indicate whether some event has occurred. In this example, N indicates that
the item number has not yet been found in the list.
 A subscript, sub, is set to 0. This subscript will be used to access each
element in the array.
 A loop executes, varying sub from 0 through one less than the size of the
array. Within the loop, the customer’s ordered item number is compared to
each item number in the array. If the customer ordered item matches any
item in the array, the flag variable is assigned "Y". After all six valid item
numbers have been compared to the ordered item, if the customer item
matches none of them, then the flag variable foundIt will still hold the
value "N".
 If the flag variable’s value is "Y" after the entire list has been searched, it
means that the item is valid and an appropriate message is displayed, but if
the flag has not been assigned "Y", the item was not found in the array of
valid items. In this case, an error message is output and 1 is added to a
count of bad item numbers.
SECTION B 40 Marks

QUESTION 4 [10]

4.1. for (let i = 0; i < Prices.length; i++) { 3


4.2. text += Prices[i] + "<br>"; 2
4.3. total += Prices[i] ; 2
4.4. average = total / Prices.length; 3

QUESTION 5 [10]

5.1. for (var i=1; i <= row; i++) { //outer loop to process the rows 3
5.2. for(var j=1; j <= col; j++){ // inner loop to process the columns 3
5.3. if(value < 10) //check if value is less than 10 2
5.4. text += value + 1; //increment value by 1 2

QUESTION 6 [20]

6.1. var average = 0; 2


6.2. var total = 0; 2
6.3. total += Marks[i]; 2
6.4. average = total / 5; 2
6.5. if(average < 50) { 2
6.6. else if(average > 49 && average < 61) { 2
6.7. else if(average >= 60 && average < 70){ 2
6.8. else if(average >= 70 && average < 80) { 2
6.9. else if(average >= 80 && average < 90){ 2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy