You are on page 1of 7

THE UNIVERSITY OF BRITISH COLUMBIA

APSC 160: MIDTERM EXAMINATION


FEBRUARY 21ST, 2005

Name: _______________________

Student #:

_________________

Signature: _____________________

Lab Section: ______

Instructor: _______________

Notes about this examination


1. You have 60 minutes to write this examination.
2. No notes, books, or any type of electronic equipment is allowed including cell phones and
calculators.
3. Good luck!

1.
2.

3.

4.

5.

Rules Governing Formal Examinations


Each candidate must be prepared to produce, upon request, a
Library/AMS card for identification.
Candidates are not permitted to ask questions of the
invigilators, except in cases of supposed errors or
ambiguities in examination questions.
No candidate shall be permitted to enter the examination
room after the expiration of one-half hour from the
scheduled starting time, or to leave during the first half hour
of the examination.
Candidates suspected of any of the following, or similar,
dishonest practices shall be immediately dismissed from the
examination and shall be liable to disciplinary action.
a. Having at the place of writing any books, papers or
memoranda, calculators, computers, audio or video
cassette players or other memory aid devices, other
than those authorized by the examiners.
b. Speaking or communicating with other candidates.
c. Purposely exposing written papers to the view of
other candidates. The plea of accident or
forgetfulness shall not be received.
Candidates must not destroy or mutilate any examination
material; must hand in all examination papers; and must not
take any examination material from the examination room
without permission of the invigilator.

Question

Max
6
5
5
4
20
10

Total

50

1
2
3
4
5

Mark

Name (please print):

Page 2 of 7

Section 1: Multiple Choice


One mark each. For each of the multiple choice questions below, circle the best answer.
Q1A. Abstraction is:
a) anything that is difficult to understand
b) the process of showing essential details while hiding non-essential details
c) the process of converting a high level program to machine code
d) a C data type
Q1B. Which of the following is not an operator in C?
a) +
b) %
c) $
d) *

Q1C. Which of the following is not a mass storage device?


a) hard disk
b) floppy disk
c) flash drive
d) random access memory
Q1D. Which of the following operators has the highest priority?
a) (binary) +
b) (unary)
c) <
d) &&

Q1E. The programming language C is


a) a program translator
b) a high-level language
c) a machine code language
d) an assembly language
Q1F. Which of the following is not a potential benefit of using functions in a program?
a) the program is shorter
b) the program is easier to read
c) you can write a program without needing parameters
d) it's easier to re-use parts of the program

Name (please print):

Page 3 of 7

Section 2: Short Answer Questions


[5] Q2. Consider the following expressions assuming that p and q are Boolean values.
( p && q ) || ( !p && !q )
( p || !q ) && ( p || q )
Complete the truth tables below (one for each expression) to determine if the two expressions are
equivalent.

p
T
T
F
F

q
T
F
T
F

p
T
T
F
F

q
T
F
T
F

Conclusion: Are the two expressions equivalent?

Name (please print):

Page 4 of 7

[5] Q3. Binary / Decimal conversion


a) Convert the following binary number into decimal: 10010110. Show your work.

b) Convert the following decimal number into binary: 42. Show your work.

[4] Q4. Evaluating expressions


Evaluate the expressions below, assuming the following declarations have been made:
int a = 8;
int b = 3;
double c = 6.0;
double d = 0.1;
a) a / b * c
b) c / d a * b
c) a % b + b % a
d) a * d * c b / a

Name (please print):

Page 5 of 7

Section 3: Writing and Understanding C Programs


[20] Q5. Be sure to read the entire question before you write any code!
Write a complete program in C that computes the tension in a cord using the following formula:
Tension = (2m1m2) / (m1+m2) g
where m1 and m2 are two masses (in kilograms) attached to either end of the cord, and specified
by the user, and g is the acceleration of gravity = 9.80665 m/s2
Your program should continually ask the user to enter two masses, compute the tension, and
write the result to an output file until the user enters the value -1 as the first mass. The output file
should contain a heading row at the top that labels each column in the file. Each successive row
should have 3 columns containing the first mass, the second mass, and the tension. The output
file should not contain the sentinel value -1 at the end.
In the interest of time you may omit comment statements from your solution.

/* continue your answer on the next page */

Name (please print):

Page 6 of 7

Name (please print):

Page 7 of 7

[10] Q6. Trace the following program to determine its output


#include<stdlib.h>
#include<stdio.h>
#include<math.h>
int main(void)
{
int a = 6;
double b = 6.1;
double c = 0.0;
double foo( int a, double b );
a = a * b;
printf("a = %i, b = %f, c = %f\n", a, b, c);
c = foo( a, b );
printf("a = %i, b = %f, c = %f\n", a, b, c);
system("PAUSE");
return 0;

Output from program:

}
double foo( int a, double b )
{
printf("a = %i, b = %f\n", a, b);
a += 2;
return b 0.3;
printf("a = %i, b = %f\n", a, b);
}
Trace Table (be sure to trace all variables in the program):

You might also like