Introduction to High Level Programming (CP 123) - Exam

THE UNIVERSITY OF DODOMA

INTRODUCTION TO HIGH LEVEL PROGRAMMING (CP 123) END OF YEAR EXAMINATION ACADEMIC YEAR 2022/2023

INSTRUCTIONS TO CANDIDATES

  1. This examination paper consists of TWO sections: SECTION A and SECTION B.
  2. Answer ALL questions in SECTION A.
  3. In SECTION B, attempt THREE (3) questions out of the FOUR (4) provided.
  4. The total marks for this paper are 100.
  5. All questions must be answered in the answer booklet provided.
  6. Write your Student ID Number clearly on every sheet of your answer booklet.
  7. Silent, non-programmable calculators are permitted.
  8. 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

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;

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

iv. Elements of array during initialization are enclosed into:
A. parentheses ( )
B. square brackets [ ]
C. angled brackets < >
D. curly brackets { }
E. Guillemets << >>

v. This is a stream extraction operator
A. >
B. >>
C. <
D. <<
E. â—‡

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

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

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

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

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


Question Two

Match the item in Column A with its proper explanation in Column B.
(1 Mark Each)

Column AColumn 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

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)

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)

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)

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)

d. Suppose an array is declared as double marks[29];
(2 Marks Each)

i. What is the name of the array?

ii. What type of values can be stored?

iii. What is the greatest index of the array?

iv. What is the maximum number of values that can be stored in the array?

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;
}

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)

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;
}

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

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)

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)


Question Six

a. What is Sentineal loop?
(2 Marks)

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
    }
}

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  

Question Seven

a. What is programming error?
(2 Marks)

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;
}

c. For each of the following instructions, include a simple and readable piece of code to support your answer.
(3 Marks Each)

i. I want to print the first ten digits in descending order.

ii. I want to run an infinite loop.


END OF EXAMINATION PAPER