ELEC1032y 1 2011 2
ELEC1032y 1 2011 2
FACULTY OF ENGINEERING
MAY 2011
DATE Monday
16 May 2011 MODULE CODE ELEC1032Y(1)
INSTRUCTIONS TO CANDIDATES
Answer Question 1 on the Answer Sheet attached on the last page of the question
paper.
Multiple Choice
Identify the letter of the choice that best completes the statement or answers the
question.
1. A ____ declares the function to the compiler—it tells the compiler the name of the
function, the data type of the value that the function will return (the keyword void
indicates that the function will not be returning any value), and the data types of each
argument that the function expects to receive when it is called.
A. function prototype C. function declarator
B. function body D. function header
2. In a(n) ____ loop the condition is used to keep track of the number of repetitions that
have occurred.
A. Posttest C. pretest
B. fixed-count D. condition-controlled
4. In a function prototype that has a two-dimensional argument, the ____ size is optional.
A. Column C. array
B. Row D. subscript
Page 1 of 7
BASICS OF COMPUTER PROGRAMMING – ELEC 1032Y(1)
8. In the following statements, the printf statement will be executed ____ times.
count = 10;
while (count <= 10)
printf("%d ",count);
A. 0 C. 10
B. 1 D. an infinite number of
9. Consider the following declarations of a function that receives an array of integers and
finds the element with the maximum value:
12. In C, the array name and index of the desired element are combined by listing the index
in ____ after the array name.
A. square braces C. curly braces
B. Parentheses D. dashes
13. The ____ statement is similar to the break statement but applies only to loops created
with while, do-while, and for statements.
A. Continue C. stop
B. Loop D. exit
14. Omitting the ____ expression in a for statement results in an infinite loop.
A. Altering C. initializing
B. Tested D. break
15. The method for adjusting the random numbers produced by a random-number generator
to reside within a specified range is called ____.
A. Scaling C. stubbing
B. Converting D. prototyping
16. The purpose of a ____ is to operate on the passed data and return, at most, one value
directly back to the calling function.
A. function body C. function header
B. function declaratory D. prototype
Page 2 of 7
BASICS OF COMPUTER PROGRAMMING – ELEC 1032Y(1)
17. char codes[] = "sample"; sets aside ____ elements in the codes array.
A. 5 C. 7
B. 6 D. 8
18. The header line ____ declares calc to be a pointer to a function that returns an integer.
A. int (*calc)() C. int *calc()
B. int &calc() D. int calc(*)
20. The need to initialize variables or make some other evaluations prior to entering a
repetition loop is so common that the ____ statement allows all the initializing
statements to be grouped together as the first set of items within the statement’s
parentheses.
A. For C. do-while
B. While D. switch
22. When a function simply receives copies of the values of the arguments and must
determine where to store these values before it does anything else, this is known as a
____.
A. pass by value C. pass by reference
B. Stub D. function declarator
24. The function ____ returns the common logarithm of its argument.
A. double log(double) C. double exp(double)
B. double log10(double) D. double fmod(double)
26. Assuming grade is an array of ten integers, the statement ____ is invalid.
A. grade = &grade[2]; C. *grade = *(grade + 2);
B. *grade = *grade + 2; D. *grade = *(&grade[2]) + 2;
Page 3 of 7
BASICS OF COMPUTER PROGRAMMING – ELEC 1032Y(1)
28. Adding ____ to a pointer causes the pointer to point to the next element of the original
data type being pointed to.
A. 1
B. 1 * sizeof(data type being pointed to)
C. 2
D. 2 * sizeof(data type being pointed to)
29. When an array is created, the compiler automatically creates an internal ____ for it and
stores the base address of the array in it.
A. Location C. pointer
B. pointer constant D. symbolic constant
30. The ____ statement literally loops back on itself to recheck the expression until it
evaluates to 0 (becomes false).
A. For C. while
B. do-while D. switch
31. If numPtr is declared as a pointer variable, the expression ____ can also be written as
numPtr[i].
A. *numPtr C. *numPtr + i
B. *(numPtr + i) D. (numPtr + i)
33. If we store the address of grade[0] in a pointer named gPtr (using the assignment
statement gPtr = &grade[0];), then, the expression ____ references grade[0].
A. *gPtr C. &gPtr
B. gPtr(0) D. gPtr
int nums[100];
int *nPtr;
36. When a function invokes itself, the process is called ____ recursion.
A. Indirect C. mutual
B. self-referential D. direct
Page 4 of 7
BASICS OF COMPUTER PROGRAMMING – ELEC 1032Y(1)
37. In performing ____ on pointers, we must be careful to produce addresses that point to
something meaningful.
A. subscript operations C. duplication
B. Arithmetic D. comparisons
38. All C compilers provide ____ function(s) for creating random numbers, defined in the
stdlib.h header file.
A. One C. three
B. Two D. four
39. Coding a function prototype as ____ makes sense when the function is used by a number
of other functions in a source code file.
A. Void C. global
B. Private D. local
Page 5 of 7
BASICS OF COMPUTER PROGRAMMING – ELEC 1032Y(1)
Question 2
(c) Write a C function that exchanges (swaps) the values in two single-precision
variables of its called function. Make use of pointers.
[10 marks]
Question 3
(a) Give the general format of a ‘for’ loop and explain how it operates.
[4 marks]
Page 6 of 7
BASICS OF COMPUTER PROGRAMMING – ELEC 1032Y(1)
Question 4
(b) Give the general format of the switch statement, and briefly explain how it
operates. [7 marks]
(d) Write a C program that outputs the following. Use recursion. [6 marks]
Question 5
Page 7 of 7
BASICS OF COMPUTER PROGRAMMING – ELEC 1032Y(1)
Student ID:
Programme:
Year:
1 21
2 22
3 23
4 24
5 25
6 26
7 27
8 28
9 29
10 30
11 31
12 32
13 33
14 34
15 35
16 36
17 37
18 38
19 39
20 40
Page 8 of 7