Docsity

Exam Solutions for CMSC 212, Spring 2009 - Part 1 - Prof. Laurence Alan Herman, Exams of Computer Science

Toggle sidebar
bg1
CMSC 212 Exam #1 solutions (Version #1) Spring 2009
1. [18 pts.]
a. [4 pts.] Here the integer the pointer points to is constant, but the pointer is not.
invalid assignment: *p= b;
valid assignment: p= &b;
b. [5 pts.] The fact that parameters are passed by value in C means that a function parameter is a
copy of the argument which is used in the call. The behavior that results is that a function can
change the value of a parameter, and it has no effect upon the argument that was used in the call.
(Note: the reason that array parameters act differently has now been explained in class, but had
not been emphasized as of the exam date.)
c. [4 pts.] Portability refers to the property that a program has when it’s able to work when compiled
on a different machine, or using a different compiler, and work right without modification.
C has more issues with portability than Java because the C language standard leaves a lot more
decisions up to the compiler writer (primarily for efficiency purposes), such as sizes of data types,
evaluation order of expressions, etc., whereas Java mandates specific choices for such issues.
d. [5 pts.] A side effect occurs when, in the process of evaluating an expression, something is changed
as a result– such as a variable’s value being modified, or something being read or printed.
(Note in particular that expressions, not statements, have side effects.)
Assignments, the assignment operators, and the unary increment and decrement operators all
produce values in C, so they’re expressions but cause side effects, so any example using them
would illustrate a side effect, such as a= 3,b += 4, or c++, or d= e++.
Also note: side effects are not in and of themselves inadvertent as some answers said, in fact,
unless the programmer made a mistake, they are entirely intentional. If a programmer wrote the
statement a= 3 or d= e++, then he or she intended a’s value, or d’s and e’s values, to be modified
in the process of evaluating the expressions.
2. [30 pts.] Each part is 4 pts.
a. 8 1
b. 7 1
c. 1 0
d. 5 6 24
e. 3
f. -11 15 3.750000
Note: although some answers said that C
has no unary negation operator, note that it
was mentioned and illustrated in class (see
slide 45 from Lecture #06), and it’s mentioned
in Chapter 5 of the text,
g. 2.250000 4
1
pf2

Partial preview of the text

Download Exam Solutions for CMSC 212, Spring 2009 - Part 1 - Prof. Laurence Alan Herman and more Exams Computer Science in PDF only on Docsity!

CMSC 212 Exam #1 solutions (Version #1) Spring 2009

  1. [18 pts.] a. [4 pts.] Here the integer the pointer points to is constant, but the pointer is not. invalid assignment: *p= b; valid assignment: p= &b;

b. [5 pts.] The fact that parameters are passed by value in C means that a function parameter is a copy of the argument which is used in the call. The behavior that results is that a function can change the value of a parameter, and it has no effect upon the argument that was used in the call. (Note: the reason that array parameters act differently has now been explained in class, but had not been emphasized as of the exam date.)

c. [4 pts.] Portability refers to the property that a program has when it’s able to work when compiled on a different machine, or using a different compiler, and work right without modification. C has more issues with portability than Java because the C language standard leaves a lot more decisions up to the compiler writer (primarily for efficiency purposes), such as sizes of data types, evaluation order of expressions, etc., whereas Java mandates specific choices for such issues.

d. [5 pts.] A side effect occurs when, in the process of evaluating an expression, something is changed as a result– such as a variable’s value being modified, or something being read or printed. (Note in particular that expressions, not statements, have side effects.) Assignments, the assignment operators, and the unary increment and decrement operators all produce values in C, so they’re expressions but cause side effects, so any example using them would illustrate a side effect, such as a= 3, b += 4, or c++, or d= e++. Also note: side effects are not in and of themselves inadvertent as some answers said, in fact, unless the programmer made a mistake, they are entirely intentional. If a programmer wrote the statement a= 3 or d= e++, then he or she intended a’s value, or d’s and e’s values, to be modified in the process of evaluating the expressions.

  1. [30 pts.] Each part is 4 pts.

a. 8 1

b. 7 1

c. 1 0

d. 5 6 24

e. 3

f. -11 15 3. Note: although some answers said that C has no unary negation operator, note that it was mentioned and illustrated in class (see slide 45 from Lecture #06), and it’s mentioned in Chapter 5 of the text,

g. 2.250000 4

  1. [27 pts.] Answer removed.
  2. [25 pts.] Different solutions are possible; here are two versions. The first one starts by declaring an unsigned long variable that has all 1 bits and shifts them right until its value becomes 0, counting the shifts necessary for that to occur. The second one starts stores 1 in an unsigned long variable, then shifts it left until its value becomes 0, counting the shifts necessary. int sizeof_long(void) { unsigned long ul= ~0; int size= 0;

while (ul > 0) { ul >>= 1; size++; }

return size / 8; }

int sizeof_long(void) { unsigned long ul= 1; int size= 0;

while (ul != 0) { ul <<= 1; size++; }

return size / 8; }

Note that the bitwise shift operators << and >> do not change their operands’ values. As explained during lecture, only a few operators in C modify any of their operands, and the bitwise shift operators are not among them.