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 2021/2022

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


Question 2

Match the item in Column A with its proper explanation in Column B.
(0.5 Marks Each)

Column AColumn B
i. Insertion operator
ii. int *px;
iii. switch
iv. void
v. cout<<*px;
vi. continue
vii. int add(int &num);
viii. sizeof
ix. int a = 4; record(&a);
x. \t

Column B Options: A. Backspace.
B. <<.
C. Calculate size of array.
D. Asterisk is used as a pointer operator.
E. Transfers control to one of the several statements, depending on the value of a condition.
F. call by reference
G. Causes the remaining portion of the enclosing loops body to be skipped.
H. continue at the end of line
I. Type with an empty set of values.
J. Allocate dynamic memory for a new variable.
K. Asterisk used as dereferencing operator.
L. Vertical tab.
M. Pass by pointer.
N. >>
O. Call by pointer.
P. Indicated in curly brackets
Q. Horizontal tab.
R. Declare a local variable
S. Pass by reference.


Question 3

a) Suppose an array is declared as double marks [29]; Answer the following questions. (0.5 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?

b) Compare and contrast between while loop and for loop statement. (2 Marks)

c) Study the program below and complete the assignment statement so that it computes the sum of all the numbers in the array. (1 Mark)

#include <iostream>
using namespace std;
int main()
{
    int values[] = {20, 1, 99, 3, 9906};
    sum =
    cout<<"Sum of all numbers = "<<sum ;
}

SECTION B: (45 MARKS)

Question 4

a) 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 the line(s) correctly or any other associated line(s) which leads to the identified error. (3 Marks Each)

i.

/*
A cpp program to print the sum of two numbers
*/
#include<iostream>
using namespace std;
int main()
{
    int a, b, c;
    a = 7;
    b = 8;
    a + b = c;
    cout<<c;
    return 0;
}

ii.

/*
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";
        cout<<endl;
    }
    return 0;
}

iii.

#include<iostream>
using namespace std;
int main()
{
    int x = 4;
    int y = 0;
    Int count = x/y;
    while(count<10)
    {
        cout<<"I'm a programmer \n";
    }
    cout<<count;
}

b) Consider that the following programs are error free. What is the output of each of the given program? (2 Marks Each)

i.

#include<iostream>
using namespace std;
int main()
{
    int x = 10;
    while(x > 0)
    {
        x = x - 2;
        cout<<x;
    }
    return 0;
}

ii.

#include<iostream>
using namespace std;
int main()
{
    int a = 4;
    int b = 2;
    cout<<+a<<", "<<a;
    cout<<endl;
    cout<<b++<<", "<<b;
    cout<<"\n";
    cout<<-a<<", "<<a;
    cout<<"\n";
    cout<<b--<<", "<<b;
    return 0;
}

iii.

#include<iostream>
using namespace std;
int main()
{
    int x = 2;
    cout<<(x != 4)<<endl;
    cout<<(x == 4)<<endl;
    cout<<(x > 3)<<endl;
    cout<<(x < 4)<<endl;
    cout<<(x = 0)<<endl;
    cout<<(x > 0)<<endl;
    cout<<(x && 4)<<endl;
    cout<<(x || 4)<<endl;
    cout<<(!x)<<endl;
    return 0;
}

Question 5

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. (15 Marks)


Question 6

Ms. Wandiba is class teacher at Jitambue Secondary School at Ulanga district. She is used to have monthly examination on 28th of every month. In this digital era, she wants a simple computer program that will be able to store permanently and display the report of her students. For each student, the report will have the following information: all scores, total scores, average score, and GPA. The student must attempt 9 examinations to have a complete report. Suppose that the class has 50 students, write a C++ program that will:

a) Accept scores of all students and store them into array name scoredmark. (2 Marks)

b) Calculate sum and average, and find the grade from average marks. For each student, store all scores from 9 subjects, total scores, average score, and GPA into an array named report. (4 Marks)

c) Export data from array report to a file named report.txt. (5 Marks)

d) Display data from report.txt on a screen. (4 Marks)


Question 7

This is a Guessing Game. Study carefully a program hereunder that randomly generates numbers between 1 and 10 (inclusive).

#include <iostream>
#include <stdlib.h> /* grand, rand */
#include <time.h>/* time */
using namespace std;
int random(int,int);
int main()
{
    int minNumber=1,maxNumber=10;
    cout<<random(minNumber,maxNumber);
}
//function to generate random numbers
int random(int min, int max) //range : [min, max)
{
    static bool first = true;
    if ( first )
    {
        grand(time(NULL)); //seeding for the first time only!
        first = false;
    }
    return min + rand() % ((max + 1) - min);
}

a) Modify the program such that it implements a guessing game where a user is asked to input a number between 1 and 10 inclusive, and then the program checks whether the entered number is equal to the randomly generated one or not. If the two numbers are equal, the user is congratulated, otherwise, he/she is asked to play again. (6 Marks)

b) Modify the program in part a) such that the user is given at most three chances to guess the number. If the difference between entered number and generated number is 3, the user is told “Cold”, meaning he/she is very far from the correct answer. If the difference is 2, display “Warm”. If the difference is 1, display “Hot”. Once again, the user must be congratulated if he/she gets the answer correctly. If he/she fails after three attempts or if the difference between correct answer and the entered number is greater than 3, he/she should be asked to try again. (9 Marks)


END OF EXAMINATION PAPER