19
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. ret is a black and white image B. ret is a grayscale image C. ret is twice as large as img D. ret is the same as img E. 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. x = x(round(linspace(1, length(x), 2*length(x)))) sound(x, fs/2) B. x = 2*x sound(x, fs/2) C. x = sound(x, fs*2) sound(x, fs/2) D. x = x(round(linspace(1, length(x), 2*length(x)))) sound(x, fs*2) E. 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 Page 1 of 19

CS1371 2008 Spring Questions G

Embed Size (px)

DESCRIPTION

CS1371 2008 Spring Questions G

Citation preview

Page 1: CS1371 2008 Spring Questions G

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. ret is a black and white imageB. ret is a grayscale imageC. ret is twice as large as imgD. ret is the same as imgE. 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. x = x(round(linspace(1, length(x), 2*length(x))))sound(x, fs/2)

B. x = 2*xsound(x, fs/2)

C. x = sound(x, fs*2)sound(x, fs/2)

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

E. x = x/2sound(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 = 0end

What is the class of output?A. CharB. DoubleC. LogicalD. Cell E. Error

Page 1 of 11

Page 2: CS1371 2008 Spring Questions G

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 sortB. Insertion sortC. Merge sortD. Quick sortE. Both C and D

5. Given the following: 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 valuesB. A solid red line connecting the given x and y valuesC. A curved fit for the given x and y valuesD. ErrorE. 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

Page 3: CS1371 2008 Spring Questions G

CS1371 - Computing for Engineers Final Exam Version G1 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 fxE. [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).nameII. Person(1).dad.nameIII. getfield(Person(1).dad)

A. II & III onlyB. I & III onlyC. I onlyD. I, II, & IIIE. 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 SortII. Merge SortIII. Quick SortIV. Insertion Sort

A. I and II B. II and III C. II and IV D. III and IVE. I, II, and III

Page 3 of 11

Page 4: CS1371 2008 Spring Questions G

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 = NB. M > NC. M < ND. Their difference cannot be determinedE. None of the above

13. Given :

x = linspace(1,10,100)y= x.^3y1 = 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, aB. a, b, cC. a == b, cD. b, c, aE. a, b == c

Page 4 of 11

Page 5: CS1371 2008 Spring Questions G

CS1371 - Computing for Engineers Final Exam Version G1 May 1, 2008

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 f1B. The text from ‘index.txt’ is stored in the cell array f1C. The file has been opened with read only permissionD. Both A and CE. Both B and C

16. Given the following code:

vec = [4 234 -5 8 4 23 5 -94]vec =vec( vec<20)vec = vec'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

Page 6: CS1371 2008 Spring Questions G

CS1371 - Computing for Engineers Final Exam Version G1 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 IIIB. II, IV and VC. IV and VD. II onlyE. 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 function ret = myRecursiveSum(vector)%% Takes in a vector and returns its sum

2 if length(vector) > 03 ret = ret + myRecursiveSum(vector(2:end))4 else5 ret = 0;6 end

A. Line 1B. Line 2C. Line 3D. Line 5E. 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

Page 7: CS1371 2008 Spring Questions G

CS1371 - Computing for Engineers Final Exam Version G1 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 C. A = 1; B = 0

D. A = 1; B = 1 E. Error

22. [A, B]= trfa('prof','prof')

A. A = 0; B = 0 B. A = 0; B = 1 C. A = 1; B = 0

D. A = 1; B = 1 E. Error

23. [A, B] = trfa('student','students')

A. A = 0; B = 0 B. A = 0; B = 1 C. A = 1; B = 0

D. A = 1; B = 1 E. Error

24. [A, B] = trfa('C','D')

A. A = 0; B = 0 B. A = 0; B = 1 C. A = 1; B = 0

D. A = 1; B = 1 E. Error

25. [A, B,] = trfa(8,8)

A. A = 0; B = 0 B. A = 0; B = 1 C. A = 1; B = 0

D. A = 1; B = 1 E. Error

Page 7 of 11

Page 8: CS1371 2008 Spring Questions G

CS1371 - Computing for Engineers Final Exam Version G1 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; endend 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] E. [1 1 1 1 2]

28. What is the class of variable D?

A. struct B. cell C. array D. double E. char

29. What is the class of variable E?

A. struct B. cell C. array D. double E. char

30. What is the class of variable F?

A. struct B. cell C. array D. double E. char

Page 8 of 11

Page 9: CS1371 2008 Spring Questions G

CS1371 - Computing for Engineers Final Exam Version G1 May 1, 2008

C. The following function fitPolys has been written in MATLAB:

1 function ret = fitPolys(x, mpoly, n)23 [r c] = size(mpoly);4 for i = 1:r5 y = zeros(1,length(x));6 for k = 1:c7 y = y + mpoly(i,k)*x.^(c-k);8 end9 coeff(i,:) = polyfit(x,y,n);10 newy(i,:) = polyval(coeff(i,:),x);11 end12 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 C. 4x4 + 2x3 + 5x2

D. 4x2 + 2x + 5 E. 3x + 1

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. Nothing would change. The function would produce the same results.B. MATLAB would generate an error: Matrix dimensions must agreeC. MATLAB would generate an error: Index exceeds matrix dimensionsD. MATLAB would generate an error: Undefined function or variable “y”E. 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 agreeC. MATLAB would generate an error: Index exceeds matrix dimensionsD. 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

Page 10: CS1371 2008 Spring Questions G

CS1371 - Computing for Engineers Final Exam Version G1 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

Page 11: CS1371 2008 Spring Questions G

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:tnoteplot(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 solutionvecA = 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