Introduction to High Level Programming (CP 123) - Exam
THE UNIVERSITY OF DODOMA
INTRODUCTION TO HIGH LEVEL PROGRAMMING (CP 123)END OF YEAR EXAMINATIONACADEMIC YEAR 2022/2023
INSTRUCTIONS TO CANDIDATES
This examination paper consists of TWO sections: SECTION A and SECTION B.
Answer ALL questions in SECTION A.
In SECTION B, attempt THREE (3) questions out of the FOUR (4) provided.
The total marks for this paper are 100.
All questions must be answered in the answer booklet provided.
Write your Student ID Number clearly on every sheet of your answer booklet.
Silent, non-programmable calculators are permitted.
No communication devices are allowed.
TIME ALLOWED: THREE (3) HOURS
SECTION A: (15 MARKS)
Question One
Read each question carefully and choose the most correct response.(0.5 Marks Each)
i. If a repetition has to be done until user enters “stop”. What is the best flow of control to implement this loop?
A. for statement
B. if statement
C. if-else statement
D. while statement
E. switch case statement
Answer (Click to show)
D. while statement
ii. If isFull = true; then what is the proper declaration for isFull variable.
A. bol isFull;
B. bull isFull;
C. bool isFull;
D. bul isFull;
E. boolean isFull;
Answer (Click to show)
C. bool isFull;
iii. If x = &y; is a correct statement, then x is:
A. a copy of y
B. a datatype of y
C. a pointer to y
D. an address of y
E. a value of y
Answer (Click to show)
C. a pointer to y
iv. Elements of array during initialization are enclosed into:
A. parentheses ( )
B. square brackets [ ]
C. angled brackets < >
D. curly brackets { }
E. Guillemets << >>
Answer (Click to show)
D. curly brackets { }
v. This is a stream extraction operator
A. >
B. >>
C. <
D. <<
E. â—‡
Answer (Click to show)
B. >>
vi. If a statement void recordMarks(string &myname, int &mynumber), then arguments are passed by:
A. value
B. reference
C. pointer
D. name
E. parameter
Answer (Click to show)
B. reference
vii. The directive “using namespace std” is used to:
A. avoid syntax errors
B. avoid logical errors
C. allow the use of short form like i++
D. avoid name collision
E. allow space in variable name
Answer (Click to show)
D. avoid name collision
viii. The ofstream is a datatype whose object is used to:
A. read from a file
B. link two files
C. load files into memory
D. write into a file
E. delete files
Answer (Click to show)
D. write into a file
ix. The loop is infinity if there is:
A. no update statement
B. no initialization statement
C. condition statement
D. no body of loop
E. no keyword for, while, and do while
Answer (Click to show)
A. no update statement
x. It combines the object files, generated by compiler/assembler, and other pieces of codes to originate an executable file.
A. Compiler
B. Linker
C. Loader
D. Interpreter
E. Assembler
Answer (Click to show)
B. Linker
Question Two
Match the item in Column A with its proper explanation in Column B. (1 Mark Each)
Column A
Column B
i. They begin with a hash symbol.
A) Semicolon (;)
ii. They store multiple values of the same type.
B) /n
iii. It is an iteration process which checks the test condition at the end of the iteration.
C) end()
iv. It aborts the program.
D) \n
v. Inserting a new line in C++ program.
E) Double colon(:)
vi. The index number of the last element of an array with 10 elements.
F) do while
vii. Marks the end of any statement in C++ program.
G) exit()
viii. Is where the program execution starts.
H) function main
ix. Is among the mandatory part of function declaration.
I) Processed by a processor
x. A software which is used for writing C++ source code.
J) 9
K) while
L) 10
M) Text Editor
N) Return type
O) Array
P) Parameters
Q) Compiler
R) Interpreter
Answer (Click to show)
i - I ii - O iii - F iv - G v - D vi - J vii - A viii - H ix - N x - M
Question Three
a. What is the value of y after execution of the following lines of codes? int x = 4; y = -x+x!-+x++/x--+2*x++; (2 Marks)
Answer (Click to show)
The expression is ambiguous and contains invalid operators (!-). In proper C++, this would not compile. Assuming a corrected, standard expression was intended and based on operator precedence, with x=4, a typical evaluation might yield y = 10, but the original expression is syntactically incorrect.
b. What are the values of g and z when the following lines of codes are executed? int z = 90; z++; z++; int g = ++z; (2 Marks)
Answer (Click to show)
z = 93 g = 93
c. Study the following piece of code: int u = 20; int y = u++%10;
i. What is the value of u and y if the code is executed? (2 Marks)
Answer (Click to show)
u = 21 y = 0
ii. What are the values of u and y if instead of using the postfix increment operator (u++), you use the prefix version (++u)? int u = 20; int y = ++u%10; (2 Marks)
Answer (Click to show)
u = 21 y = 1
d. Suppose an array is declared as double marks[29]; (2 Marks Each)
i. What is the name of the array?
Answer (Click to show)
marks
ii. What type of values can be stored?
Answer (Click to show)
double (floating-point numbers)
iii. What is the greatest index of the array?
Answer (Click to show)
28
iv. What is the maximum number of values that can be stored in the array?
Answer (Click to show)
29
e. Study the program below and complete the assignment statement so that it computes the sum of all the numbers in the array. (2 Marks)
#include <iostream>using namespace std;int main(){ int values[] = {20, 1, 99, 3, 9906}; // ... sum calculation ... cout<< "Sum of all numbers = " << sum;}
Answer (Click to show)
int sum = 0;for(int i = 0; i < 5; i++) { sum = sum + values[i];}
SECTION B: (60 MARKS)
Attempt THREE (3) out of FOUR (4) questions provided.
Question Four
a. Differentiate between return type and data type. (2 Marks)
Answer (Click to show)
Return type specifies the type of value a function returns to its caller. Data type defines the type of data a variable can hold (e.g., int, float, char).
b. Careful study a piece of program below. What does the program prints? (3 Marks)
#include<iostream>using namespace std;int main(){ int x = 10; while(x > 0) { x = x - 2; cout<<x; } return 0;}
Answer (Click to show)
86420
c. Sanga’s Used Spare Parts Shop charges the following prices: 500/- per bolt, 300/- per nut, and 100/- per washer. Write a program that asks the user for the number of bolts, nuts, and washers in their purchase and then calculates and prints out the total. As an added feature, the program checks the order. A correct order must have at least as many nuts as bolts and at least twice as many washers as bolts, otherwise the order has an error. For an error the program writes out “Check the Order: too few nuts” or “Check the Order: too few washers” as appropriate. Both error messages are written if the order has both errors. If there are no errors the program writes out “Order is OK”. In all cases the total price (of the specified number of items) is written out.
Note: Use const keyword for the unit cost of each item. In other words, declare constants such as: const int BOLTPRICE = 500;(15 Marks)
Example Input/Output:
Number of bolts: 12
Number of nuts: 8
Number of washers: 24
Check the Order: too few nuts
Total cost: 10800
Answer (Click to show)
#include <iostream>using namespace std;int main() { const int BOLTPRICE = 500; const int NUTPRICE = 300; const int WASHERPRICE = 100; int bolts, nuts, washers; cout << "Number of bolts: "; cin >> bolts; cout << "Number of nuts: "; cin >> nuts; cout << "Number of washers: "; cin >> washers; bool validOrder = true; if (nuts < bolts) { cout << "Check the Order: too few nuts" << endl; validOrder = false; } if (washers < 2 * bolts) { cout << "Check the Order: too few washers" << endl; validOrder = false; } if (validOrder) { cout << "Order is OK" << endl; } int totalCost = (bolts * BOLTPRICE) + (nuts * NUTPRICE) + (washers * WASHERPRICE); cout << "Total cost: " << totalCost << endl; return 0;}
Question Five
a. Write a program that accepts two integers through variables created in the pokea() function. The modulus of the first number against the second number has to be calculated in the calculate() function and then return the answer to the main function. The display() function will finally display the answer to the user. (10 Marks)
Answer (Click to show)
#include <iostream>using namespace std;void pokea(int &a, int &b);int calculate(int a, int b);void display(int result);int main() { int num1, num2; pokea(num1, num2); int result = calculate(num1, num2); display(result); return 0;}void pokea(int &a, int &b) { cout << "Enter first number: "; cin >> a; cout << "Enter second number: "; cin >> b;}int calculate(int a, int b) { return a % b;}void display(int result) { cout << "The modulus is: " << result << endl;}
b. Write a program that checks if a password is strong enough for security purposes. A strong password must contain at least one digit, at least one lowercase character and at least one uppercase character. Moreover, all characters in total should be at least 8. (10 Marks)
Answer (Click to show)
#include <iostream>#include <string>#include <cctype>using namespace std;int main() { string password; cout << "Enter password: "; cin >> password; bool hasDigit = false, hasLower = false, hasUpper = false; if (password.length() < 8) { cout << "Password is weak: Too short" << endl; return 0; } for (char c : password) { if (isdigit(c)) hasDigit = true; if (islower(c)) hasLower = true; if (isupper(c)) hasUpper = true; } if (hasDigit && hasLower && hasUpper) { cout << "Password is strong." << endl; } else { cout << "Password is weak." << endl; if (!hasDigit) cout << " - Missing a digit" << endl; if (!hasLower) cout << " - Missing a lowercase letter" << endl; if (!hasUpper) cout << " - Missing an uppercase letter" << endl; } return 0;}
Question Six
a. What is Sentineal loop? (2 Marks)
Answer (Click to show)
A sentinel loop is a loop that continues to process data until a special value, called a sentinel value, is encountered. This special value signifies the end of data input.
b. Careful study a piece of program below then writes the missing lines of codes. (3 Marks)
#include<iostream>using namespace std;int main(){ bool repeat = true; string choice = "\0"; while(repeat) { repeat = false; cout<<"\nIt is the UDOM only!\n\n"; cout<<"Do you want to hear it again? "; cout<<"Enter 'yes' or 'no' to agree or deny "; //missing line if(choice == "yes") //missing line else //missing line }}
Answer (Click to show)
#include<iostream>using namespace std;int main(){ bool repeat = true; string choice = "\0"; while(repeat) { repeat = false; cout<<"\nIt is the UDOM only!\n\n"; cout<<"Do you want to hear it again? "; cout<<"Enter 'yes' or 'no' to agree or deny "; //missing line cin >> choice; if(choice == "yes") //missing line repeat = true; else //missing line repeat = false; }}
c. Study carefully the kgtpesa menu shown in illustration 1. The customer can navigate deeper into the menu and back to main menu/previous step as many times as he/she want without running a program afresh. Use one/more C++ flow of controls as needed to implement this menu. (15 Marks)
Operation: When a user run a program, the program will prompt the user to select a digit for a particular service/action. For example; when user select 2, the inner menu will pop up which again prompt the user to select the service/action and so forth.
kgtpesa
0. Ongezu Pesa
1. Tuma Pesa
2. Vocha na Bando
1. Lipa kwa phone
2. Chagua Kampuni
3. Malipo ya serikali
4. Bahati Nasibu
5. Rudi Nyuma
6. Rudi Menyu kuu
7. Lipia Bill
8. Akaumi yangu
Answer (Click to show)
#include <iostream>using namespace std;void mainMenu();void vochaMenu();int main() { mainMenu(); return 0;}void mainMenu() { int choice; do { cout << "kgtpesa\n\n"; cout << "0. Ongezu Pesa\n"; cout << "1. Tuma Pesa\n"; cout << "2. Vocha na Bando\n"; cout << "Enter your choice (0-2, -1 to exit): "; cin >> choice; switch(choice) { case 0: cout << "Ongezu Pesa selected.\n"; break; case 1: cout << "Tuma Pesa selected.\n"; break; case 2: vochaMenu(); break; case -1: cout << "Exiting...\n"; break; default: cout << "Invalid choice!\n"; } } while(choice != -1);}void vochaMenu() { int choice; do { cout << "\nVocha na Bando\n"; cout << "1. Lipa kwa phone\n"; cout << "2. Chagua Kampuni\n"; cout << "3. Malipo ya serikali\n"; cout << "4. Bahati Nasibu\n"; cout << "5. Rudi Nyuma\n"; cout << "6. Rudi Menyu kuu\n"; cout << "7. Lipia Bill\n"; cout << "8. Akaumi yangu\n"; cout << "Enter your choice (1-8): "; cin >> choice; switch(choice) { case 1: cout << "Lipa kwa phone selected.\n"; break; case 2: cout << "Chagua Kampuni selected.\n"; break; case 3: cout << "Malipo ya serikali selected.\n"; break; case 4: cout << "Bahati Nasibu selected.\n"; break; case 5: cout << "Returning to previous menu...\n"; return; case 6: cout << "Returning to main menu...\n"; return; case 7: cout << "Lipia Bill selected.\n"; break; case 8: cout << "Akaumi yangu selected.\n"; break; default: cout << "Invalid choice!\n"; } } while(choice != 5 && choice != 6);}
Question Seven
a. What is programming error? (2 Marks)
Answer (Click to show)
A programming error is a mistake in a program’s source code that causes it to behave unexpectedly, produce incorrect results, or fail to run. They are broadly classified into Syntax Errors, Runtime Errors, and Logical Errors.
b. Careful study a piece of program below. Copy into your booklet the line(s) which is incorrect (expected to produce error). State the type of an identified error, then propose a solution by re-writing correctly the line(s) or any other associated line(s) which leads to the identified error. (3 Marks)
/*A CPP program to print the word CPP three times*/#include<iostream>using namespace std;int main(){ for(int a = 0; a<3; a++) ; { cout<<"CPP";- Did not clode--" cout<<endl; } return 0;}