Apsc 160 (Ubc)
Apsc 160 (Ubc)
Apsc 160 (Ubc)
First Name:
Student #:
SOLUTIONS
Lab Section:
(give date/time if unknown)
Duration: 60 minutes.
Length: 10 pages (including cover page and reference page).
Complete the above fields in ink (pen).
NO AIDS! No papers, notes, books, texts, or any type of electronic equipment is allowed including cell
phones, calculators, or sound/image players/transmitters/receivers (see rules #4a).
No questions except for errors or ambiguities (see rules #2).
Use the backs of facing or same page if you run out of space.
Clearly indicate where the solution is if you use backs of pages.
All questions are independent except where explicitly noted.
Always use good programing practices to get full marks (you may omit comments).
Midterm Examination
Q#
Awarded
Of
12
25
18
14
14
10
Total
100
Marker
99
Page 1 of 10
9001
9000
B) (1 points)
double a = 1 / 2;
a
0.0
C) (2 points)
int a, b;
// note: this is not an error in the question
a = a || !a;
b = b && !b;
a
True (or 1)
False (or 0)
D) (1 point)
int a;
// note: this is not an error in the question
int b = 1 + a;
b
garbage value
E) (1 point)
double a = 1.2, b = 1.8;
double c = a + (int) b;
c
2.2
Midterm Examination
Page 2 of 10
F) (1 point)
int a = 0, b = 1;
double c = b / a;
c
G) (1 point)
int pi[] = { 3, 1, 4, 1, 5, 9 };
int a = pi[4];
a
H) (2 points)
int a = 1, b = 1;
if(a > 0) {
int a = 2;
b = 2;
}
// what are the values here?
a
I)
(1 point)
int inc(int a) {
return a++;
}
...
int a = 0;
inc(a);
// what is the value here?
a
J)
(1 points) [bonus question]
int a = 5;
a = !(--a);
a
False (or 0)
Midterm Examination
Page 3 of 10
Midterm Examination
Page 4 of 10
}
UBC APSC 160 - Fall 2008
Midterm Examination
Page 5 of 10
B) (7 points)
Nanna is concerned that her serving tray is not big enough for all of the sushi rolls which you
prepared. Nanna needs to know the surface area of her rectangular tray, but she only knows the
length of its sides. Nanna trusts that you, her favorite and most gifted grandchild, can help her by
writing a function to compute the area of a rectangle given the length of its sides.
What is the prototype for this function?
double rectArea(double, double);
Midterm Examination
Page 6 of 10
Midterm Examination
Page 7 of 10
Midterm Examination
Page 8 of 10
!(p || q)
compared to
!p && !q
!(p||q)
B)
!(p && q)
compared to
!p && !q
!(p&&q)
!p && !q
Midterm Examination
Page 9 of 10
Reference Sheet
<stdio.h>
int fclose(FILE *);
int feof(FILE *);
FILE* fopen(const char *, const char *);
int fprintf(FILE *, const char *, ...);
int fscanf(FILE *, const char *, ...);
int printf(const char *, ...);
int scanf(const char *, ...);
int sprintf(char *, const char *, ...);
<math.h>
double cos(double);
double exp(double);
double fabs(double);
double log(double);
double log10(double);
double pow(double, double);
double sin(double);
double sqrt(double);
double tan(double);
double hypot(double, double);
printf specifiers
c
Character
d or i
Scientific notation
String
Midterm Examination
Page 10 of 10