Anda di halaman 1dari 11

# CS1371 - Computing for Engineers Final Exam Version G1

May 1, 2008

## Part 1 Multiple Choice [60 Points]

1. What is the effect of the function imager given the image matrix img?
function [ret] = imager(img)
img = double(img);
val = uint8((img(:,:,1)+img(:,:,2)+img(:,:,3))/3);
ret(:,:,1) = val;
ret(:,:,2) = val;
ret(:,:,3) = val;

A.
B.
C.
D.
E.

## ret is a black and white image

ret is a grayscale image
ret is twice as large as img
ret is the same as img

## None of the above

2. Given the following lines of code, which of the following operations would produce the same
sound?
[x, fs] = wavread('mysound.wav')
sound(x, fs)

A.
B.
C.
D.
E.

## x = x(round(linspace(1, length(x), 2*length(x))))

sound(x, fs/2)
x = 2*x
sound(x, fs/2)
x = sound(x, fs*2)
sound(x, fs/2)
x = x(round(linspace(1, length(x), 2*length(x))))
sound(x, fs*2)
x = x/2
sound(x, fs/2)

## 3. Given the following Matlab code:

A = true;
B = false;
C = (A || B) && ~(B && B)
if C
output = 'almost there'
else
output = 0
end

A. Char
B. Double
C. Logical
D. Cell
E. Error
Page 1 of 11

## CS1371 - Computing for Engineers Final Exam Version G1

May 1, 2008

4. Which of the following becomes much less efficient if the input vector is previously sorted?
A. Bubble sort
B. Insertion sort
C. Merge sort
D. Quick sort
E. Both C and D

x = [0 1 2 3 4]
y = [0 2 4 6 8]
plot(x, y, 'ro')

## What would the plot look like?

A. A plot of red circular points with the given x and y values
B. A solid red line connecting the given x and y values
C. A curved fit for the given x and y values
D. Error
E. None of the above

## 6. Convert the following while loop to a for loop.

vec = [1 2 3 4];
sum = 0;
while length(vec)>0
sum = sum + vec(1);
vec(1) = [];
end

A.

for vec
sum = sum + vec(i);
end

B.

for i = vec
sum = sum + vec(i);
end

C.

## for i < length(vec)

sum = sum + vec(i);
i = i + 1;
end

D.

for i = vec
sum = sum + i;
end

E.

for i = length(vec)
sum = sum+vec(i);
end

Page 2 of 11

May 1, 2008

## 7. Which of the following is a valid function header?

A. function = fx(input)
B. function ret1, ret2 = fx(input)
C . function ret = fx(34)
D. function fx
E. [ret1, ret2] = fx(input1, input2)

## 8. A structure array is defined as follows:

Person(1).name = 'Bob'
Person(2).name = 'Joe'
Person(1).dad = Person(2)

## Which of the following are valid ways to obtain 'Joe'?

I. Person(2).name
II. Person(1).dad.name
III. getfield(Person(1).dad)
A. II & III only
B. I & III only
C. I only
D. I, II, & III
E. I & II only

9. How would you lower a note three half-steps in Matlab given the following code?
[a d] = wavread('c-note.wav');
b = length(a);
half=2^(1/12);

A. a(floor(linspace(1,b,b/(half^3))))
B. a(floor(linspace(1,b,b/(half/3))))
C. a(floor(linspace(1,b,b/(half*3))))
D. a(floor(linspace(1,b,b/half^(-3))))
E. a(floor(linspace(1,b,b*half*3)))

## 10. Which of the following have best case N*logN big O?

I. Bubble Sort
II. Merge Sort
III. Quick Sort
IV. Insertion Sort
A. I and II
B. II and III
C. II and IV
D. III and IV
E. I, II, and III

Page 3 of 11

## CS1371 - Computing for Engineers Final Exam Version G1

May 1, 2008

11. Given:
vec = [5 9 6 3 6 8]

## Which of the following will NOT return the vector v2 = [5 6 3 6 8]?

A. v2 = vec(vec < 9)
B. v2 = vec([1 3:6])
C. v2 = vec;
v2(vec == max(vec))=[]
D. v2 = vec(vec ~= 9)
E. v2 = vec;
v2(vec > 9)=[]

12. What is the difference between M and N where M = length(diff([5 4 3 2 1])) and
N = length(cumsum([5 4 3 2 1]))?
A. M = N
B. M > N
C. M < N
D. Their difference cannot be determined
E. None of the above
13. Given :
x = linspace(1,10,100)
y= x.^3
y1 = diff(y)./diff(x)

## Which of the following will plot the second derivative?

A. plot(x(2:end), diff(y1)./diff(x(2:end)))
B. plot(x(3:end), diff(y1)./diff(x(2:end)))
C. plot(x(2:end), diff(y1)./diff(x))
D. plot(x(2:end), diff(y1(2:end))./diff(x(2:end)))
E. plot(x, diff(y1)./diff(x(2:end)))
14. Given an excel file (test.xls) containing the information below:

The command [a,b,c] = xlsread('test.xls'); is run in Matlab. What is the correct order
of a, b, and c in order of increasing number of elements (i.e. number of rows multiplied by
number of columns for each)?
A. c, b, a
B. a, b, c
C. a == b, c
D. b, c, a
E. a, b == c
Page 4 of 11

## CS1371 - Computing for Engineers Final Exam Version G1

15. Which choice below is NOT true about the following line of code?
f1 = fopen('index.txt', 'w')

## A. The file handle for index.txt is stored in the variable f1

B. The text from index.txt is stored in the cell array f1
C. The file has been opened with read only permission
D. Both A and C
E. Both B and C

## 16. Given the following code:

vec
vec
vec
vec

= [4 234 -5 8 4 23 5 -94]
=vec( vec<20)
= vec'
= abs(vec)

## What is the final output of vec?

A. [1 0 1 1 1 0 1 1]
B. [1; 0; 1; 1; 1; 0; 1; 1]
C. [4; 5; 8; 4; 5; 94]
D. [4 5 8 4 5 94]
E. None of the above

17. Given:
prof = struct('name','Cedric','classes',4);

## Which of the following modifies the original structure, prof?

A. prof = setfield(prof,'name','Stallworth');
B. rmfield(prof,'classes');
C. cedric.age = 'old';
D. getfield(prof, 'name');
E. All of the above

Page 5 of 11

May 1, 2008

May 1, 2008

## 18. Let the following code be executed in Matlab.

K = ones(100, 100);
L = ones(50, 100) .*7;
M = zeros(100, 20);

Which of the following lines of code will NOT produce an error in Matlab?
I. K*L;
II. inv(L);
III. L(20:30, 40:50) = [];
IV. M\K;
V. K(50,100) = L;
A. I and III
B. II, IV and V
C. IV and V
D. II only
E. IV only

19. Using what you know about your homework problem recursiveSum, recursion, and coding
in general, which of the following lines of code is incorrect?
1
2
3
4
5
6

## function ret = myRecursiveSum(vector)

%% Takes in a vector and returns its sum
if length(vector) > 0
ret = ret + myRecursiveSum(vector(2:end))
else
ret = 0;
end

A.
B.
C.
D.
E.

Line 1
Line 2
Line 3
Line 5
Everything is correct in the above code

20. Given:
cell = {'cool', 1:4, false}
cell{2} = [];

## What is the final value of cell?

A. {'cool', [], false}
B. {'cool', false}
C. {'cool', {}, false}
D. {'cool', [1 3 4], false}
E. Error

Page 6 of 11

May 1, 2008

## Part 2 Tracing Questions [30 Points]

A. Given the following function:
function [A, B]= trfa(x,y)
if strcmp(x,y) && strcmpi(x,y)
A = true;
B = true;
elseif ~strcmp(x,y) && strcmpi(x,y)
A = false;
B = true;
elseif strcmp(x,y) || strcmpi(x,y)
A = true;
B = false;
else
A = (x==x);
B = (x==y);
end

The function is called using the test cases below. Please choose the correct outputs.
21. [A, B] = trfa('ta', 'TA')
A. A = 0; B = 0

B. A = 0; B = 1

D. A = 1; B = 1

E. Error

C. A = 1; B = 0

A. A = 0; B = 0

B. A = 0; B = 1

D. A = 1; B = 1

E. Error

C. A = 1; B = 0

A. A = 0; B = 0

B. A = 0; B = 1

D. A = 1; B = 1

E. Error

C. A = 1; B = 0

A. A = 0; B = 0

B. A = 0; B = 1

D. A = 1; B = 1

E. Error

C. A = 1; B = 0

A. A = 0; B = 0

B. A = 0; B = 1

D. A = 1; B = 1

E. Error

Page 7 of 11

C. A = 1; B = 0

May 1, 2008

## B. The following script is executed in Matlab:

A = struct('Name',{'Plant','Pond Snail','Fish','Bird','Human'}, ...
'Prey',{'Sun','Plant','Pond Snail','Fish','Fish'});
setfield(A(1),'Prey','None');
B = [];
C = [];
for index = 1:length(A)
v = getfield(A(index),'Prey');
i = find(strcmp(B,v));
if length(i) == 0
B = [B {v}];
C = [C 1];
else
C(i) = C(i) + 1;
end
end
D = A(1:end-1);
E = [B {C}];
F = fieldnames(rmfield(A,'Name'));

## 26. What are the values stored in variable B?

A. 'None', 'Plant', 'Pond Snail', 'Fish', 'Fish'
B. 'Sun', 'Plant', 'Pond Snail', 'Fish', 'Fish'
C. 'Plant', 'Pond Snail', 'Fish', 'Bird', 'Human'
D. 'None', 'Plant', 'Pond Snail', 'Fish'
E. 'Sun', 'Plant', 'Pond Snail', 'Fish'
27. What are the values stored in variable C?
A. [1 1 1 1 1 ]

B. [1 1 1 2]

C. [2 2 2 1]

D. [1 1 1 2 2]

A. struct

B. cell

C. array

D. double

E. char

D. double

E. char

D. double

E. char

A. struct

B. cell

C. array

A. struct

B. cell

C. array

Page 8 of 11

E. [1 1 1 1 2]

May 1, 2008

1
2
3
4
5
6
7
8
9
10
11
12

## function ret = fitPolys(x, mpoly, n)

[r c] =
for i =
y =
for

size(mpoly);
1:r
zeros(1,length(x));
k = 1:c
y = y + mpoly(i,k)*x.^(c-k);

end
coeff(i,:) = polyfit(x,y,n);
newy(i,:) = polyval(coeff(i,:),x);
end
ret = newy;

## Answer the following questions based on the following function call:

ret = fitPolys(-20:20, [5 6 1 5; 0 4 2 5; 0 0 3 1], 3)

31. What are the dimensions (rows by columns) of the output variable ret?
A. 1 x 41

B. 3 x 40

C. 40 x 3

D. 3 x 41

E. 41 x 3

32. The x and y data for which polynomial is passed into polyfit during the second iteration of
the outermost for loop?
A. 5x4 + 6x3 + x2 + 5x

B. 5x3 + 6x2 + x + 5

D. 4x2 + 2x + 5

E. 3x + 1

## C. 4x4 + 2x3 + 5x2

33. What is the order of the fitted polynomial during the second iteration of the outermost for
loop?
A. 1

B. 2

C. 3

D. 4

E. 5

34. What would happen if you were to replace line 5 with: y = [];
A.
B.
C.
D.
E.

Nothing would change. The function would produce the same results.
MATLAB would generate an error: Matrix dimensions must agree
MATLAB would generate an error: Index exceeds matrix dimensions
MATLAB would generate an error: Undefined function or variable y
The function would not cause an error, but it would produce different results.

35. What would happen if you were to replace line 6 with: for k = c:-1:1
A. Nothing would change. The function would produce the same results.
B. MATLAB would generate an error: Matrix dimensions must agree
C. MATLAB would generate an error: Index exceeds matrix dimensions
D. MATLAB would generate an error: Undefined function or variable y
E. The function would not cause an error, but it would produce different results.
Page 9 of 11

May 1, 2008

## Part 4 Coding Problems [40 Points]

You must complete ALL of the following coding questions. Your code should not exceed 7
lines for any individual question. These are NOT functions. A function header is NOT
necessary. Please copy your answers to the given answer sheet.
38. Given vectors x and y of equal length containing x and y data values, please do the
following:
- Compute the cumulative integral of y and store in the variable integ
- Fit a second order line to the integral and store in fit
- Evaluate the fit with the original x values and store in newInteg
- Plot integ and newInteg against x (in the same plot)

39. You are given a sound called 'sound.wav'. Determination the duration of the sound (how
many seconds it plays for) and store this is duration. Then, plot the amplitude of the sound
against time.

40. Write the code to plot the rotation the function f(x) = x2 around the x axis. The first few
lines have been done for you.
u = linspace(0, 5);
v = u.^2;
th = linspace(0, 2*pi);
[uu, tth] = meshgrid(u, th);
[vv, tth] = meshgrid(v, th);

41. Given a vector v, write code that creates the following items (no hard coding!):
- vecA, which contains all the elements of v which are less than 9
- vecB which is v in reverse order
- vecC, which contains only the elements of v which are at odd indices
- D, which is the average of the values in v
- vecE which is a vector of length 5 with random values between 5 and 7.

## Part 4 Criteria (each worth 10 points, 40 points total)

38. Suggested solution:
Page 10 of 11

## CS1371 - Computing for Engineers Final Exam Version G1

May 1, 2008

integ = cumtrapz(x,y);
fit = polyfit(x, integ, 2);
newInteg = polyval(fit, x);
plot(x, integ, x, newInteg)

## - 3 points each for correctly defining integ/fit/newInteg

- 1 point for plotting correctly
- 1 point if concept not completely correct (except for the plotting)
39. Suggested solution:
[data, Fs] = wavread('cNote.wav');
N = length(data);
duration = N/Fs;
dt = 1/Fs;
t = dt:dt:tnote
plot(t, data)

## - 2 points each for:

- Correctly reading in sound
- Correctly defining a time vector/dt
- 3 points each for:
- Correctly defining duration
- Plotting t/amplitude
- only 1 point for each of above if not completely correct
40. Suggested solution:
rr = vv;
xx = uu;
yy = rr.*sin(tth);
zz = rr.*cos(tth);
surf(xx,yy,zz) or mesh(xx,yy,zz)

Requirements:
- Correctly defines rr/xx/yy/zz (these count as 4 different requirements)
- Plots the surface using surf or mesh
- 2 points for each of the above requirements if completely correct
- 1 point if it is almost correct (i.e. student left out dot in the multiplication, etc.)
39. Suggested solution
vecA = v(v<9);
vecB = v(end:-1:1);
vecC = v(1:2:end);
D = mean(v);
vecE = 2*rand(1,5)+5;

- 2 points each for correctly defining vecA, vecB, vecC, D, and vecE
- 1 point if almost correct

Page 11 of 11