COS1512 EXAM PACK 2023
COS1511-Exam-pack - Various COS1511 exams to practice on. Computer Systems: Fundamental Concepts (University of South Africa) lOMoARcPSD| SECTION A 20 MARKS (10 MULTIPLE CHOICE QUESTIONS, 2 marks each) Please answer this section on the mark-reading sheet that you received (not in your answer book). Choose one option for every question. QUESTION 1 Suppose the following declarations appear in a C++ program: float amount, forWaiter; char service; If the following function header is given: float tip(float amountP, char serviceP) which of the options below is a correct calling statement of the function tip? 1. forWaiter = tip(float amount, char service); 2. tip(amountP, serviceP); 3. forWaiter = tip(123.66, 'A'); 4. forWaiter = tip(amountP, serviceP); 5. None of the above options is a correct calling statement. QUESTION 2 Suppose the following declarations appear in a C++ program: float afford, amount; int number; string name; If the following function header is given: void supplyInfo(float affordP, int nrP, string & nameP, float & amountP) which of the options below is a correct calling statement of the function supplyInfo? 1. supplyInfo(800, 4, "Beach Hotel", 789); 2. supplyInfo(800, 4, name, amount); 3. supplyInfo(affordP, nrP, nameP, amountP); 4. supplyInfo(afford, number, "Beach Hotel", 789); 5. None of the above options is a correct calling statement. Downloaded by Thomas Mboya () lOMoARcPSD| COS111U May 2009 [TURN OVER] 2 QUESTION 3 Suppose the following declarations appear in a C++ program: int age, days; char grade; float salary; Suppose the following calling statement appears in the program: inputInfo(35, 'B', salary, days); Which of the options below is a correct function header of the function inputInfo? 1. void inputInfo(int ageP, char gradeP, float & salaryP, int & daysP) 2. void inputInfo(35, 'B', float salaryP, int daysP) 3. void inputInfo(int & age, char & grade, float & salary, int & days) 4. void inputInfo(35, 'B', salary, days) 5. All of the above options are correct function headers. Questions 4 and 5 are based on the following C++ program. #include <iostream> #include <string> using namespace std; int main( ) { string favour; int tvHours, favourHours; cout << "How many hours per week do you watch sport on TV, " << endl << "what is your favourite sport, how many hours for that? "; cin >> tvHours >> favour >> favourHours; if (tvHours > 10) if (favour == "soccer") { if (favourHours > tvHours / 2) cout << "Group A" << endl; } else if (favourHours > tvHours / 4) cout << "Group B" << endl; else cout << "Group C" << endl; else if (favour != "rugby") cout << "Group D" << endl; return 0; Downloaded by Thomas Mboya () lOMoARcPSD| COS111U May 2009 [TURN OVER] 3 } QUESTION 4 Suppose the program above is executed with the following input: 11 soccer 5 Which of the options below gives the output of the program? 1. Group A 2. Group B 3. Group C 4. Group D 5. No output QUESTION 5 Suppose the following input is given to the program above: 6 soccer 6 Which of the options below gives the output of the program? 1. Group A 2. Group B 3. Group C 4. Group D 5. No output QUESTION 6 Suppose in a C++ program values are assigned to a string variable name and to a float variable amount. Then a while loop is entered. The loop has to be executed as long as • name is not equal to "NOBODY" and • amount is not less than 10.00. Which of the options below gives a correct condition for the loop? 1. ((name != "NOBODY") && (amount >= 10.00)) 2. ((name != "NOBODY") || (amount >= 10.00)) 3. (!(name == "NOBODY") && (amount < 10.00)) 4. (!((name != "NOBODY") && (amount >= 10.00))) 5. None of the options above is a correct condition. QUESTION 7 Suppose we want to assign the value true to a bool variable success if • the value of the int variable grade is 10 or higher and Downloaded by Thomas Mboya () lOMoARcPSD| COS111U May 2009 [TURN OVER] 4 • the value of the char variable sym is 'A' or 'B' or 'C'. Which of the options below gives a correct assignment statement? 1. success = (grade >= 10) || sym == 'A' || sym == 'B' || sym == 'C'; 2. success = (grade >= 10) || (sym == 'A' || 'B' || 'C'); 3. success = (grade >= 10) && (sym == 'A' || sym == 'B' || sym == 'C'); 4. success = (grade >= 10) && sym == 'A' && sym == 'B' && sym == 'C'; 5. None of the options above is a correct assignment statement. Questions 8, 9 and 10 are based on the following C++ program. Note that the conventions as explained in the Study Guide are used in the variable diagrams. 1 #include <iostream> 2 #include <string> 3 using namespace std; 4 void supplyInfo(string destP, float & feeP, string & timeP) 5 { 6 if (destP != "Cape Town" && feeP != 40) 7 feeP *= 3; 8 else if (destP != "Johannesburg") 9 { 10 destP = "Funny"; 11 feeP = 1; 12 } 13 timeP = "17:15"; 14 if (destP != "Funny") 15 timeP = "10:23"; 16 else 17 timeP = "Fun"; 18 } 19 float amountDue(int nrA, int nrC, float feeP) 20 { 21 float total; 22 total = nrA * feeP; 23 feeP *= 0.80; 24 total += nrC * feeP; 25 return total; 26 } 27 int main( ) 28 { 29 string destination, time; 30 int nrAdult, nrChild; 31 float fee, amount; 32 destination = "P.E."; Downloaded by Thomas Mboya () lOMoARcPSD| COS111U May 2009 [TURN OVER] 5 33 fee = 40; 34 supplyInfo(destination, fee, time); 34 nrAdult = 4; 35 nrChild = 5; 36 amount = amountDue(nrAdult, nrChild, fee); 37 cout << "Time: " << time << " Due: R" << amount << endl; 38 return 0; 39 } QUESTION 8 Which of the options below correctly reflects the situation after Line 33 has been executed? Option 1 Line 33 destination fee time "P.E." 40 ? destP feeP timeP "P.E." 40 ? nrAdult nrChild amount ? ? ? Option 2 Line 33 destination | [destP] fee | [feeP] time | [timeP] "P.E." 40 ? [nrAdult] [nrChild] [amount] ? ? ? Option 3 Line 33 [destination] | destP [fee] | feeP [time] | timeP "P.E." 40 ? [nrAdult] [nrChild] [amount] ? ? ? Option 4 Line 33 destination fee time "P.E." 40 ? nrAdult nrChild amount ? ? ? Option 5 Line 33 None of the above options is correct. Downloaded by Thomas Mboya () lOMoARcPSD| COS111U May 2009 [TURN OVER] 6 QUESTION 9 Which of the options below correctly reflects the situation after Line 24 has been executed? Option 1 Line 24 [destination] [fee] | feeP [time] "Funny" 0.8 "Fun" [nrAdult] | nrA [nrChild] | nrC [amount] | total 4 5 8 Option 2 Line 24 [destination] [fee] [time] "P.E." 1 "Fun" [nrAdult] [nrChild] [amount] 4 5 ? nrA nrC feeP total 4 5 0.8 8 Option 3 Line 24 [destination] [fee] | feeP [time] "P.E." 96 "10:23" [nrAdult] | nrA [nrChild] | nrC [amount] | total 4 5 960 Option 4 Line 24 [destination] [fee] [time] "P.E." 120 "10:23" [nrAdult] [nrChild] [amount] 4 5 ? nrA nrC feeP total 4 5 96 960 Option 5 Line 24 None of the above options is correct. Downloaded by Thomas Mboya () lOMoARcPSD| COS111U May 2009 [TURN OVER] 7 QUESTION 10 Which of the options below correctly reflects the situation after Line 37 has been executed? Option 1 Line 37 destination fee time "P.E." 40 "10:23" nrAdult nrChild amount 4 5 960 Option 2 Line 37 destination fee time "P.E." 96 "10:23" nrAdult nrChild amount 4 5 960 Option 3 Line 37 destination fee time "Funny" 1 "Fun" nrAdult nrChild amount 4 5 8 Option 4 Line 37 destination fee time "Funny" 0.8 "Fun" nrAdult nrChild amount 4 5 8 Option 5 Line 37 None of the above options is correct. Downloaded by Thomas Mboya () lOMoARcPSD| COS111U May 2009 [TURN OVER] 8 SECTION B (80 MARKS) Please answer this section in your answer book. QUESTION 1 [12] A bookshop gives discount to customers as follows: • Students get 10% discount, • book dealers get 12% discount and • pensioners get 15% discount. • All other customers get 10% discount only if their total purchases are more than R200. Write down ONLY the necessary C++ statements to calculate and display the final amount that is due, after discount. Do NOT write a complete program. Use the following variables: float amount; // the amount due before discount char customerType; // the type of customer: 'S' (student) or // 'D' (dealer) or'P' (pensioner) or'O'(other) Assume that values have been assigned to amount and customerTypealready. You may also need the following variables: float discount, finalAmount; QUESTION 2 [10] A bank offers a fixed composite interest rate of 12% per year on money that is invested. Thus if you invest R100, it will be worth R112 at the end of the first year; and then R112 + (12% of R112) at the end of the second year, etc. Complete the C++ program below to determine the number of years needed for an initial investment of R1000 to become worth more than R3000. Use an appropriate loop structure. Do not introduce any additional variables. Write down ONLY the missing statements. Downloaded by Thomas Mboya () lOMoARcPSD| COS111U May 2009 [TURN OVER] 9 #include <iostream> using namespace std; int main( ) { const float START = 1000; // the amount that is invested const float PERC = 0.12; // the interest rate float amount; // worth of investment int years; // number of years // YOUR STATEMENTS SHOULD COME IN HERE cout << "After " << years << " years the investment " << "is worth more than R3000" << endl; return 0; QUESTION 3 [12] In this question you have to write a complete function. Suppose marks have been given to 30 students and that the marks are stored in an int array called marks. You have to write a void function, called findFailAndDistinctto determine the number of students who failed (thus got a mark of less than 50) and the number of students who got a distinction (thus a mark of at least 75). These two values should be returned to the main function. Assume the following: • a declaration of a global constant: const int NUM_MARKS = 30; // number of marks • two declaration statements in the main function: int marks[NUM_MARKS]; // list of marks int nrFail, nrDistinct; • values have been assigned already to all the elements of the array • the function is called in the main function as follows: findFailAndDistinct(marks, nrFail, nrDistinct); Write down ONLY the complete function findFailAndDistinct. QUESTION 4 [16] (a) Declare an integer constant DIM1 equal to 3 and an integer constant DIM2 equal to 5. (2) (b) Declare two int two-dimensional arrays, namely • arrayA with DIM1 rows and DIM2 columns, and • arrayB with DIM2 rows and DIM1 columns. (4) Downloaded by Thomas Mboya () lOMoARcPSD| COS111U May 2009 [TURN OVER] 10 (c) Assume that values have been assigned to all the elements of arrayA and arrayB. Also assume that an int variable funnySum has been declared and intialised to 0. Use nested for loops and write down the necessary C++ instructions to calculate funnySum as follows: each row of arrayA is compared element by element to the corresponding column of arrayB and the value -1, 0, or 1 is added to funnySum according to whether the element of arrayA is less than, equal to, or greater than the element of arrayB. For example, if the given arrays arrayA and arrayB are: 8 12 5 10 3 6 6 5 6 7 4 9 16 and 6 7 9 20 4 4 3 3 6 22 4 5 respectively, then funnySum should be equal to 1+1-1+1-1 +0+0+0+1+1 -1-1+1+1+1 = 4. Display the value of funnySum. Do NOT write a complete program or any functions. Write down ONLY the required statements. (10) QUESTION 5 [14] The Post Office keeps record of all parcels handled by them. They keep the following information for each parcel: • sender (a string, for example "Anna Every") • receiver (a string, for example "Johnny Allbody") • weight (a floating point number, for example 2.25) • postage (a floating point number, for example 23.50) (a) Write down the declarations for a struct for storing the information of one parcel. Give the name Parcel to the struct. (5) (b) Assume that an array Parcel todaysParcels[50] has been declared and that the information for 50 parcels has been stored in the array. The program fragment below determines and displays the average weight and the highest postage of all the parcels handled by the Post Office that day. Now write down ONLY the necessary C++ instructions for line numbers 4, 6, 7 and 9 to complete this program fragment. Write down only the line number and the instruction that should appear next to the line number. (9) 1. float totalWeight, averageWeight, maxPostage; 2. totalWeight = 0; 3. maxPostage = 0; 4. for ( ) //examine all parcels 5. { 6. // determine total weight of today’s parcels 7. // determine maximum postage of today’s parcels 8. } 9. // determine average weight of today’s parcels; Downloaded by Thomas Mboya () lOMoARcPSD| COS111U May 2009 [TURN OVER] 11 10. (ios::fixed); 11. sion(2); cout << "The average weight of today's parcels is " << averageWeight << endl; cout << "The maximum postage for a parcel handled today is " << maxPostage << endl; Do NOT write a complete program. Write down ONLY the required statements. QUESTION 6 [16] In both parts of this question you have to write the body of a function. In both cases the function header looks as follows: string changedSentence(string senP) Hint: Below the question we list a number of string member functions that you may need. (a) The function receives a string of characters, indicated by senP in the function header. The function has to replace all occurrences of the string he with Theo and return the changed string to the main function. Example: If the string When he saw the hen, then and there he heard the noise. is given, the string WTheon Theo saw tTheo Theon, tTheon and tTheore Theo Theoard tTheo noise. should be returned to the main function. You should write the body of the function ONLY. (8) (b) The function receives a string of characters, indicated by senP in the function header. The function has to replace all occurrences of the stand-alone string he with Theo and return the changed string to the main function. You may assume that senP will not start or end with he. Example: If the string When he saw the hen, then and there he heard the noise. is given, the string When Theo saw the hen, then and there Theo heard the noise. should be returned to the main function. You should write the body of the function ONLY. (8) TURN OVER FOR SOME STRING MEMBER FUNCTIONS Downloaded by Thomas Mboya () lOMoARcPSD| COS111U May 2009 [TURN OVER] 12 A number of string member functions to help you StringO( ) StringOr(startPos, length) StringO(substring) StringO(substring, startPos) StringOt(insertPos, substring); StringO(startPos, length); StringOce(startPos, length, substring); where startPos, length and insertPos are of type int, and substring is of type string. © UNISA 2009 Downloaded by Thomas Mboya () lOMoARcPSD| 1 COS111U memorandum May 2009 COS111U MEMORANDUM MAY 2009 SECTION A Question 1: 3 Question 2: 2 Question 3: 1 Question 4: 5 Question 5: 4 Question 6: 1 Question 7: 3 Question 8: 4 Question 9: 2 Question 10: 5 Downloaded by Thomas Mboya () lOMoARcPSD| 2 COS111U memorandum May 2009 SECTION B QUESTION 1 12 marks // switch: switch(customerType) 1 { case 'S': discount = 0.10; break; 2 case 'D': discount = 0.12; break; 2 case 'P': discount = 0.15; break; 2 case 'O': if (amount > 200) discount = 0.10; else discount = 0; 3 } finalAmount = amount * (1.0 - discount); // many other ways 1 cout << finalAmount << endl; 1 OR // nested if: if (customerType == 'S') discount = 0.10; 2 else if (customerType == 'D') discount = 0.12; 2 else if (customerType == 'P') discount = 0.15; 2 else if (amount > 200) //if (customerType == 'O') redundant discount = 0.10; 2 else discount = 0.0; 2 finalAmount = amount * (1.0 - discount); // many other ways 1 cout << finalAmount << endl;
Connected book
Written for
Document information
- Uploaded on
- February 10, 2023
- Number of pages
- 160
- Written in
- 2022/2023
- Type
- Exam (elaborations)
- Contains
- Questions & answers
Subjects
- cos1512
- 2023
- cos1512 exam pack
- cos1512 exam pack
- cos1512 exam pack 2023
- cos1512 exam pack 2023
- cos1512 exam pack 2023
-
exam pack
-
introduction to programming ii
-
cos1512 introduction to programming ii
Document also available in package deal