Вы находитесь на странице: 1из 3

MAKERERE UNIVERSITY

COLLEGE OF ENGINEERING, DESIGN, ART AND TECHNOLOGY


DEPARTMENT OF MECHANICAL ENGINEERING
MEC2203: COMPUTER PROGRAMMING CAT 1

Date: Friday 7th April 2017 Time: 14:00-15:30 hours

Attempt both problems observing all ANSI/ISO coding standards and conventions.

PROBLEM ONE:

(a) Mark the following statements as true or false. (12 Marks)


(i) An identifier can be any sequence of digits and letters: FALSE
(ii) In C++, there is no difference between a reserved word and a pre-defined identifier:
FALSE
(iii)The operands of the modulus operator must be integers: TRUE
(iv) If a = 4; and b = 3;, then after the statements a = b; the value of b is still 3: TRUE
(v) In the statement cin >> y;, y can only be an int or a double variable: FALSE
(vi) In an output statement, the newline character may be part of the string: TRUE
(vii) The following is a legal C++ program: TRUE
int main( )
{
return 0;
}
(viii) In a mixed expression, all the operands are converted to floating-point numbers:
FALSE
(ix) Suppose x = 5. After the statement y = x++; executes, y is 5 and x is 6: TRUE
(x) The expression ! (x>0) is true only if x is a negative number: TRUE
(xi) The while loop: TRUE
j= 0;
while (j <=10)
j++;
terminates if j > 10
(i) In an infinite while loop, the while expression (the decision maker) is initially false, but
after the first iteration, it is always true. FALSE
(b) What is the output of the following statements? Suppose a and b are int variables, c is
a double variable, and a = 13, b = 5, and c = 17.5. (6 Marks)
(i) cout << a + b – c << endl; 0.5
(ii) cout << 15 / 2 + c << endl; 24.5
(iii) cout << a / static_cast<double> (b) + 2 * c <<endl; 37.6
(iv) cout << 14 % 3 + 6.3 + b / a << endl; 8.3
(v) cout << static_cast<int>(c) % 5 + a – b << endl; 10
(vi) cout << 13.5 / 2 + 4.0 * 3.5 + 18 << endl; 38.75
(c) Using illustrations, explain the difference between a local and global variable (2 Marks)

A global variable is a variable declared in the main body of the source code, outside all
functions, while a local variable is one declared within the body of a function or a block.

1|P ag e
Global variables can be referred from anywhere in the code, even inside functions, whenever it is
after its declaration. The scope of local variables is limited to the block enclosed in braces ({ })
where they are declared. For example, if they are declared at the beginning of the body of a
function (like in function main) their scope is between its declaration point and the end of that
function. In the example above, this means that if another function existed in addition to main,
the local variables declared in main could not be accessed from the other function and vice versa.

PROBLEM TWO

(a) The following program has syntax mistakes. Correct them. On each successive line,
assume that any preceding error has been corrected. (6 Marks)
const char = STAR = ‘*’
const int PRIME = 71;
int main
{
int count, sum;
double x;
count = 1;
sum = count + PRIME;
x := 25.67;
newNum = count + ONE + 2;
sum + count = sum;
x = x + sum * COUNT;
cout << “ count = ” << count << ” , sum = ” << sum
<< ”, PRIME = ” << Prime << endl;
}

2|P ag e
(b) Suppose that x, y, and z are int variables, and x = 10, y = 15, and z = 20. Determine
whether the following expressions evaluate to true or false.(5 Marks)
(i) ! (x > 10) TRUE
(ii) x <= 5 || y < 15 FALSE
(iii) (x !=5 ) && (y !=z) TRUE
(iv) x >= z || (x + y >= z) TRUE
(v) (x <= y -2) && (y >= z) || (z – 2 != 20) FALSE
(c) In regard to formatting numeric output, explain the difference between precision,
width, and justification (3 Marks)

Width describes the number of spaces in which a value is displayed. Precision defines the
number of digits shown after the decimal point. Justification refers to the alignment of data
within a horizontal field.

(d) State what output, if any, results from each of the following statements: (6 Marks)
(i) for (i = 1; i <= 1; i++)
cout << “*”;
cout << endl;
output is *
(ii) for (i = 1; i <=5; i++)
{
cout << “*”;
i = i + 1;
}
cout << endl;
output is ***
(iii) for (i = 0; i<=5; i++)
cout << “*”;
cout << endl;
output is ******

3|P ag e

Вам также может понравиться