UNIVERSITY OF CAPE COAST
COLLEGE OF AGRICULTURAL AND NATURAL SCTENCES
SCHOOL OF PHYSICAL SCIENCES
DEPATUIMENT OF COMPUTER SCIENCE AND INFORMATION TECHNOTOGY.
END OF FrRST SEMESTEF-aOL9/2O2O
PROGRAMMTNG AND PROBLEM ANALYSTS (CSC 203)
TIME: 2 HRS
SECTION A: Instruction: This exam is a closed textbook and a closed note. Use of any
electronic device (e.g. for computing and/or communicating) is NOT permitted. Select the
most appropriate answer (s).
1. What is wrong with the following function body? 6. A two dimension array can also be thought of as
void calculate (int count, float (u) a table
price , float& cost ) (b) an array of arrays
t (") none of the above
if (count < O)
cost =0 . 0; (d) A and B
else
cost=count*pri-ce; 7. A definition of a variable outside any function is called
return;
)
(u) local function definition
(u) void functions may not have a return statement. (b) global variable definition
(b) void fimctions must return a value (") giobal function header
(") lothing (d) global tunction definition
(d) cannot mix reference and value parameters If we want a search function to search an array for some
initialize an integer array of size 10, but value and return either the index where the value was
2. 1f you declare and
found, or -1 if not found, which of the following proto-
only list 5 values, what values are stored in the remaining
types would be appropriate?
5 indexed variables?
(u) o
(u) void search(const int array, int target, int numEle-
ments);
(b) garbage
(b) void search(const int array, lnt target);
(") 0.0
(") int search(const int array[], int numElements);
(d) '0'
(d) int search(const int arrayfi, int target, lnt numEle-
3. Which of the following loop statements is guaranteed to ments);
iterate the body of the loop at least once?
The foliowing are true about the expression left &&
(") while(control)body; right.
(b) do body while(control);
(u) The expression is false when left is false and right
(.) for (initialize; test; update) borty; is false
(d) none of the above (b) The expression is true when left ls true and right
is false
4. Given the array declaratiou, irrt a[20]; The last (legal) el-
ement is written as:
(") The expression is false when left is false and right
is 1;rue
(u) al2l (d) The expression is true when left is true and right
(b) a[0] is true
(") a[20]
10. What is the value of x after the following statements?
(d) a[1e]
double x;
What is wrong with the following code? x = O;
float scores [10], totai; x += 3.0 * 4.0;
x -= 2.O;
(u) Cannot deciare regular and array variables to-
gether.
(b) Arrays must be integers (u) 22.0
(c) The i0 should be replaced with a variable name, (b) 12"0
whose vaiue isinput from the user (.) 10.0
(d) Nothing. (d) t4"o
11. What is the value of the rouJwingr 16. You should make a para,meter a referetce parameter
sqrt(sqrt(pow (2,a))) ; if:
(") 1 (u) You need the function to chauge th:e value of the
(b) 2
argument passed to the function.
(") 4
(b) you need to be able to change the value of the pa-
rameter in the function, but not th-e value of the
(d) 16
argument.
12. What is y after the following switch statement is executed? (") Always.
intx=3;iaty=41 (d) If any of the other parameters are reference pa-
slritch (x + 31 1 rameters.
case 6: y = 0; 17. Which of the following declare an a.rray of 5 characters,
caseT: y=1; and initializes them to some known values?
default: Y +=. 1'
) (") char array[5]:{'a','b','c','d','e'};
(b) chararray[4]: {'a','b','c','d','e'};
(o) 1 (") char array[]:{'a','b','d','e'};
(b) 2 (d) A and C
(") 3
r8. Which of these array definitions will set all the indexed
(d) 4 variables to 0?
(") o (r) int array[5];
13. If you were to write a functiorr for displaying the cost of (b) int arrayfS] : iOh
an item to the screen, which function prototype would be
(.) int array[5] : i0,1,2,3,4];
most appropiiate?
(d) int array[5] : {1,0,0};
(u) void displayQ;
(b) void display(float myCost); 19. What is the size of an integer type iu C++?
(") int display (float myCost); (r) 1 byte
(d) float display0; (b) 4 byte
14. The statements int x : 1; int y; y : x++; (.) 2byte
(u) Assign y the value 2; (d) 8 byte
(b) Change the value of x to 2. 20. If a function needs to modify rrrr:re than one variable, it
must
(.) Assign y the value 0;
(d) Assign y the value 1; (") be pass by value
(") B and D (b) be a void function
15" Given the function definition, which of the fol-
(") return all values needed
lowing are correct? With arguments Tand 2.0 (d) be a call by reference function
iat func(int n, double d)
t
int ; = o'
doubl-e sum = 0;
while( 3 >= 0)
t
sum += d;
--j;
)
return sum;
)
(u) returns 7*2
(b) It compiles but computes none of these.
(") returns 7*2
(d) is a syntax error in the program so it wont
lhere
SECTION B (10 marks each)
Answer any four questious in as detail as possible on the provided answer booklet. Be sure to justify all your €l,nswers.
1. [C++ Application] Write a C++ application that inputs one number consisting of four digits from the user. You will need
to separate the number into its individual digits and prints the digits separated from one another by two (2 ) spaces each.
For example, if the user types in the number 4655. The output should display 4 6 5 5. [10 marks]
2. Write a C*+ function that counts the number of times the item x appears among the first n elements of -the array and
returns that count as the frequency of x irr the array. [10 marks]
3. IFunctions]
(a) Write a C++ function to deterrnine min function that returns the srnallest of four given integers: [5 marks]
(b) Write a function Area. The two parameters represent the length and width of a rectangle. Have the function return
an area of a rectangle. [5 marks]
4. fArray] Write a function that will convert an array of ints of size l0 into an array with allelements havirrg the largest
element in the array. Each element of the final array must have the value of the largest element in the original array.
[10 marks]
5. fstructures] Assume the following structure definition [10 marks]
struct Fu].lNane
{
string lastilane;
String niddlel{ane ;
striDg firstNane;
);
Write statements that
(a) define a FullName structure variable narred info [3 marks]
(b) assign your la-st, middle and first name to the members of the info variable. [3 marks]
(c) display the contents of the members of the info variable [4 marks]
6. fl,oops ] t1o marksl
(a) What is the final value of x in this code fragment? Justify your answer [5 rnarks]
int x = 0;
int i;
for (i = 0; i < 1O; i++)
{
if(iz 4==0)
x += 50;
else i,f (1 7 3 == 1)
x += 30;
)
x += i;
(b) What is the final value of x in this code fragment? You must show your work to earn some points [5 marks]
intx=0;
int Y = 5'
vhile (y <= 50)
{
' Y +- x;
x++ i
)