You are on page 1of 8

Islamic University of Gaza

Computer Programming

ECOM 2401 & EELE 2315


Fall 2011-2010
Date : 9, December, 2010, Time : 120 minutes

Midterm Exam

Student Name:
Student Id: 1 2 0 0 -

Question Points

Question 1

Question 2

Question 3

Question 4

Question 5

Question 6

Total 50

Computer Programming Page 1 of 8


Question 1 : ( 12 Point)

Please use BLUE pen to solve the first question.

Identify and correct the error(s) in each of the following:

a) if ( age >= 65 );
cout << "Age is greater than or equal to 65" << endl;
else
cout << "Age is less than 65 << endl";

b) int x = 1, total;
while ( x <= 10 )
{
total += x;
x++;
}

c) While ( x <= 100 )


total += x;
x++;

d) For ( x = 100, x >= 1, x++ )


cout << x << endl;

e) The following code should print whether integer value is odd or even:
switch ( value % 2 )
{
case 0:
cout << "Even integer" << endl;
case 1:
cout << "Odd integer" << endl;
}

f) The following code should output the even integers from 2 to 100:
counter = 2;
do
{
cout << counter << endl;
counter += 2;
}
While ( counter < 100 );

Computer Programming Page 2 of 8


Question 2 : ( 16 Point)

Part 1

A C++ program contains the following statements:

void plus(int a, int& b); // first statement


int main()
{
int x = 100;
int y = 9999;
plus(x, y);
. . . . . . . . . . .
}
void plus(int a, int& b)
{
. . . . . . . . . . .
}

State whether the following statements are TRUE or FALSE.

a. The first statement is called the function declaration. ___________________________

b. The integer parameter b in function plus can be used to ___________________________


modify the value of argument y in the main function.

c. The value of x can be modified by the plus function ___________________________

d. Call by reference is used to provide a value for parameter a. ___________________________

e. The void return type of the plus function means it can have ___________________________
a return statement that returns a value of any data type.

Part 2
Assuming the value of variable count is 0 and the value of variable limit is 10,
state whether the result of each the following expressions is true or false.

(count == 0) && (limit > 12) ___________________________

(limit > 20) || (count < 5) ___________________________

!(count == 12) ___________________________

Computer Programming Page 3 of 8


Question 3: ( 15 Point)

Write a complete program to display the following format.


(You need to use loop structure). (Note: the ASCII code for A is 97)

A
AB
ABC
ABCD
ABCDE………K

Computer Programming Page 4 of 8


Question 4 : ( 10 Point)

Consider the following program:

#include <iostream.h>

void Do(int a, int b)


{
a = 3;
a = a + b;
b = a + 2;
}
int main()
{
int x = 5;
Do(x, x);
std::cout << x << std::endl;
}

What output does this program produce under each of the following parameter passing
mechanisms? Show your work.

• Call by reference

• Call by value

Computer Programming Page 5 of 8


Question 5: ( 5 Point)

What does the following programs do?


1
2 // What does this program print?
3 #include <iostream>
4 using std::cout;
5 using std::cin;
6 using std::endl;
7
8 int main()
9 {
10 int x; // declare x
11 int y; // declare y
12
13 // prompt user for input
14 cout << "Enter two integers in the range 1-20: ";
15 cin >> x >> y; // read values for x and y
16
17 for ( int i = 1; i <= y; i++ ) // count from 1 to y
18 {
19 for ( int j = 1; j <= x; j++ ) // count from 1 to x
20 cout << '@'; // output @
21
22 cout << endl; // begin new line
23 } // end outer for
24
25 return 0; // indicate successful termination
26 } // end main

Computer Programming Page 6 of 8


Question 6 : ( 17 Point)

Engineers often measure the ratio of two power measurements in decibels, or dB. The
equation for decibels is

where P2 is the power level being measured and P1 is some reference power level.

a. Write the definition of a function named decibel to calculate the decibel level using
this equation. The function should have two power levels corresponding toP1 and P2 as
parameters and should return the decibel level. Use function log10 to calculate the
logarithm. ( 7 point )

Computer Programming Page 7 of 8


b. Assume that the reference level (P1= 1 watt) and write the statements, including a
loop, that uses function decibel to calculate and display on the terminal screen the
decibel levels corresponding to power levels between 1 and 20 watts in 0.5 watt steps.
(10 point)

My best Wishes …
Eng. Wazen M. Shbair

Computer Programming Page 8 of 8

You might also like