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 2023/2024

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 [40 Marks]

Answer all questions in this section

Question One: [10 Marks]

Select the letter with the most correct answer [1 Mark Each]

I. By default how the value are passed in C++?
a. call by value
b. call by reference
c. call by pointer
d. call by object

II. A function that is called by itself, either directly or indirectly is called as
a. Super function
b. Recursive function
c. Main function
d. None of the above

III. Iteration uses a repetition structure whereas recursion uses
a. Sorting structure
b. Selection structure
c. Controlling structure
d. All of the above

IV. A graphical representation of an algorithm is called as
a. Pseudocode
b. Flow chart
c. Flow lines
d. Flow structure

V. Which one is not a valid identifier?
a. rdd2
b. x (5)
c. DATE
d. A3O

VI. Where does the return statement returns the execution of the program?
a. main function
b. caller function
c. same function
d. block function

VII. In recursion, the condition for which the function will stop calling itself is ______
a. Best case
b. General case
c. Base case
d. There is no such condition

VIII. What is the correct way to assign the pointer p to the address of x?
a. *p=&x
b. p=%x
c. p=&x
d. None of the above

IX. Which looping process is best used when the number of iterations is known?
a. for
b. while
c. do … while
d. all looping processes require that the iterations be known

X. Which of these best describes an array?
a. A data structure that shows a hierarchical behavior
b. Container of objects of similar types
c. Arrays are immutable once initialized
d. Array is not a data structure


Question Two: [10 Marks]

Carefully read the following and write TRUE for the correct sentence and FALSE for the incorrect sentence. [1 Mark Each]

I. There is no stand-alone else statement in C++. Every else has a related if.

II. C++ data types fall into the following three categories: simple, structured, and pointers.

III. The preprocessor command # include <iostream> instructs the preprocessor to include the header file iostream in the program.

IV. Including a sēmicolon before statement1 in a two-way selection creates a syntax error.

V. It is possible that the body of a do … while loop may not execute at all.

VI. If a C++ function does not use parameters, parentheses around the empty parameter list are not required.

VII. A function can return a value of type array.

VIII. Parameters allow you to use different values each time the function is called.

IX. Arrays can be passed as parameters to a function either by value or by reference.

X. In C++, pointer variables are declared using the word pointer.


Question Three: [20 Marks]

I. A program runs without any errors being reported but outputs results that are wrong, what type of error is likely to have caused this?
(2 Marks)

II. What is the major difference between a while statement and a do-while statement?
(4 Marks)

III. Write C++ statements to do the following:
(2 Marks Each)

a. Declare an array alpha of 15 components of type int.

b. Set the value of the fifth component of the array alpha to 35.

c. Set the value of the ninth component of the array alpha to the sum of the sixth and thirteenth components of the array alpha.

d. Set the value of the fourth component of the array alpha to three times the value of the eighth component minus 57.

IV. Write a program that prompts the user to input three numbers. The program should then output the largest number.
(6 Marks)


SECTION B [60 Marks]

Answer any THREE questions in this section

Question Four: [20 Marks]

I. Rewrite the following as a for loop.
(5 Marks)

int i = 0, value = 0;
while (i <= 20)
{
    if (i % 2 == 0 && i <= 10)
        value = value + i * i;
    else if (i % 2 == 0 && i > 10)
        value = value + i;
    else
        value = value - i;
    i = i + 1;
}
cout << "value = " << value << endl;

II. Rewrite the following as a do … while loop.
(5 Marks)

int i = 0, value = 0;
while (i <= 20)
{
    if (i % 2 == 0 && i <= 10)
        value = value + i * i;
    else if (i % 2 == 0 && i > 10)
        value = value + i;
    else
        value = value - i;
    i = i + 1;
}
cout << "value = " << value << endl;

III. Use the conditional operator to rewrite the following if … else statement.
(5 Marks)

if (a < b)
{
    a = b;
}
else
{
    a = -b;
}

IV. Use iterative method to rewrite the following recursive function.
(5 Marks)

int factorial(int n)
{
    if (n > 1)
        return n * factorial(n - 1);
    else
        return 1;
}

Question Five: [20 Marks]

What is the output of the following programs/ code fragments?
[4 Marks Each]

I.

#include <iostream>
#include <conio>
void main()
{
    int list[10] = {1,2,3,4,5,6,7,8,9,10};
    cout << "list[6] = " << list[6] << endl;
    cout << "list[0] + list[9] = " << (list[0] + list[9]) << endl;
    cout << "list[7] - list[3] = " << (list[7] - list[3]) << endl;
    cout << "list[4] * list[1] = " << (list[4] * list[1]) << endl;
    getch();
}

II.

#include <iostream>
#include <conio>
void main()
{
    int *p;
    int x = 37;
    cout << "x = " << x << endl;
    p = &x;
    cout << "*p = " << *p << ", x = " << x << endl;
    *p = 58;
    cout << "*p = " << *p << ", x = " << x << endl;
    cout << "Value of x = " << x << endl;
    getch();
}

III.

#include <iostream>
#include <conio>
void funOne(int a, int& b, char V);
void funTwo(int& x, int y, char& w);
 
void main()
{
    int num1, num2;
    char ch;
    num1 = 10;
    num2 = 15;
    ch = 'A';
    cout << "Inside main: num1 = " << num1 << ", num2 = " << num2 << ", and ch = " << ch << endl;
    funOne(num1, num2, ch);
    cout << "After funOne: num1 = " << num1 << ", num2 = " << num2 << ", and ch = " << ch << endl;
    funTwo(num2, 25, ch);
    cout << "After funTwo: num1 = " << num1 << ", num2 = " << num2 << ", and ch = " << ch << endl;
    getch();
}
 
void funOne(int a, int& b, char v)
{
    int one;
    one = a;
    a++;
    b = b * 2;
    v = 'B';
    cout << "Inside funOne: a = " << a << ", b = " << b << ", v = " << v << ", and one = " << one << endl;
}
 
void funTwo(int& x, int y, char& w)
{
    x++;
    y = y * 2;
    w = 'G';
    cout << "Inside funTwo: x = " << x << ", y = " << y << ", and w = " << w << endl;
}

IV.

#include <iostream>
#include <math>
#include <conio>
void main()
{
    cout << "floor(-37.4) = " << floor(-37.4) << endl;
    cout << "ceil(-37.4) = " << ceil(-37.4) << endl;
    cout << "pow(2,4) = " << pow(2,4) << endl;
    cout << "abs(-17) = " << abs(-17) << endl;
    getch();
}

V.

#include <iostream>
#include <conio>
int fib(int fTerm, int sTerm, int pos)
{
    if(pos == 1)
        return fTerm;
    else if(pos == 2)
        return sTerm;
    else
        return fib(fTerm, sTerm, pos - 1) + fib(fTerm, sTerm, pos - 2);
}
 
void main()
{
    cout << "The fibonacci number at position 4 is " << fib(3, 7, 4) << endl;
    getch();
}

Question Six: [20 Marks]

What is wrong with the following programs/ code fragments?
[5 Marks Each]

I.

include <iostream>
void main () {
    int a, b;
    bool found;
    cout << "Enter two integers: ";
    cin >> a >> b;
    if a > a * b && 10 < b
    Found = 2 * a > b;
    else
    {
    found = 2 * a < b;
    if (found)
    a = 3;
    if (true)
    {
    b = 0;
    a = 1;
    c = 2;
    }
    }
    getch();
}

II.

#include <iostream>
#include <conio>
const int N = 2,137;
void main
{
    int a, b, c, d;
    a = 3;
    b = 5;
    c = c + d;
    N = a + c;
    For (i = 3; i <= N; i++)
    {
        cout << i;
        i = i + 1;
    }
    getch();
}

III.

#include <iostream>
#include <conio>
void main()
{
    int count = 1, total;
    While (count <= 10);
    {
        total += count;
        Count++;
    }
    cout << "total = " << total << endl;
    getch();
}

IV.

# include<iostream>
void main()
{
    int SomeVariable = 5;
    cout << "SomeVariable: " << SomeVariable << "\n";
    int *pVar = & SomeVariable;
    pVar = 9;
    cout << "SomeVariable: " << *pVar << "\n";
}

Question Seven: [20 Marks]

I. Write a program that prompts the user to input three numbers. The program should then output the numbers in ascending order.
(6 Marks)

II. By using a recursive function write a program that computes the sum of numbers from 1 to n.
(6 Marks)

III. Write a C++ program that prompts the user to input the elapsed time for an event in seconds. The program then outputs the elapsed time in hours, minutes, and seconds.
(8 Marks)


END OF EXAMINATION PAPER