Problem 3 – Multiple Choice from T-Square which of the following is the value of vec after the following code is run? a= 1:3:7 b= 2:2:6 c= mod(a,2) vec= zeros(1,7) vec([a b])= [c a(b/2)] A. [0 2 0 4 0 6 0] B. [1 1 0 4 0 7 1] C. [1 2 1 4 1 6 1] D. [1 2 3 4 0 6 7] E. [0 1 0 4 0 7 0] If there is a vector, vec=[1 2 3 4 5] which of the following will NOT output the value 5? A. length(vec(end:-1:1)) B. vec(max(vec)) C. vec(vec(1:4)=[]) D. vec(~[true true true true false]) E. vec(mod(vec,2)~=0 & mod(vec,5)<=0) Which of the follwing will give a vector of evenly spaced integers from 1 to 7? A. 1:7 B. linspace(1,7) C. sort(rand(1,7)) D. mod(9:1:15,8) E. Two answers are correct If vec= [1 4 3 7 2 9] which of the following will remove the odd indices from vec? A. vec([1 3 4 6])=[ ] B. vec([1 3 2])=[ ] C. mod(vec,2)=[ ] D. vec(1:2:6)=[ ] E. Two answers are correct Which of the following will output a value of 1, if vec= [ 1 5 4] ? A. vec(~(~[false true])) B. vec(vec<2 | mod(vec,2)~=0) C. vec(logical(-1)) D. vec(~[false true true true true]) E. Two answers are correct vec = [ 2,6,7,3] b= vec([ 1 0 1 1]) What is the error MATLAB gives for this statement? A. Error: Matrix Dimensions must Agree B. Error : Subscript indices must either be real positive integers or logicals. C. Error : Index exceeds matrix dimensions D. Error: Undefined function or variable a E. Error : The input character is not valid in MATLAB statements or expressions. vec = [ 2,6,7,3] b= vec(vec) What is the error MATLAB gives for this statement? A. Error: Matrix Dimensions must Agree B. Error : Subscript indices must either be real positive integers or logicals. C. Error : Index exceeds matrix dimensions D. Error: Undefined function or variable a E. Error : The input character is not valid in MATLAB statements or expressions. vec1 = [ 2, 6, 7, 3]; vec2 = [3, 4]; vec4 = vec1.*vec2; What is the error MATLAB gives for this statement? A. Error: Matrix Dimensions must Agree B. Error : Subscript indices must either be real positive integers or logicals. C. Error : Index exceeds matrix dimensions D. Error: Undefined function or variable a E. Error : The input character is not valid in MATLAB statements or expressions. Which of the following concatenations will return the string 'cs1371'? A. ['cs' char(1371)] B. ['cs' char(1) char(3) char(7) char(1)] C. ['cs' str2num(1371)] D. ['br'+1 '1371'] E. None of the above Which of the following will NOT return a string with the alphabet in capital letters? A. linspace('A','Z',26) B. 'A':'Z' C. ('a':'z')+'A'-'a' D. char( double('A'):('A' + 25)) E. None of the above A=[1 9 3 527 4 2 6] Which of the following will remove the 7 from A? A. A(2,3)= [ ] B. A(3,2)= [ ] C. A(A~=7) D. A=A(mod(A,7)>=0) E. None of the above If str1 is a word or phrase with no punctuation, which of the following does NOT find whether str1 is a palindrome? A. all(str1 == str1(end:-1:1)) B. strcmpi(str1, str1(end:-1:1)) C. strcmp(str1(1:end/2), str1(end:-1:end/2+1)) D. all(str1./ str1(end:-1:1) == 1) E. None of the above Which of the following are NOT valid conditionals for an if statement? (Which one will allow NOT the statement to run) A. 'true or false' B. strcmp(str1, str2) C. [1 2 3] D. double(false) E. two of the above are incorrect if a>3 if a< 10 a=2*a-5; elseif mod(a,2)==1 a=a-2; end else a=a-1; end If a=6 before the above code is run, what will be its value afterwards? A. a= 2 B. a= 4 C. a= 5 D. a= 7 E. None of the above switch str case {'a' 'b' 'c'} out=str(1:end-1) case double('abc') out=str(1:end-2) case {'abc'} out=str(1:end-3) end If str= 'abc' before the above code is run, what is the value of out afterwards? A. out= 'ab' B. out= 'a' C. out= '' D. out= 'abc' E. Error Which of the following is a variable that cannot be safely run through a switch statement? A. linspace( 'A', 'Z', 26) B. [65 97 65] C. 659765 D. 'A' E. None of the above Which of the follow evaluates to true if a=true and b=false? A. (~a | (a & b)) & ((~b & a) | (b & a)) B. ~ (a & b) & (b | (b & ~a)) C. ((a & b) | (b & (b | a))) | (~a | (a & ~b)) D. ~ ((b & (a | b)) | (a & (a | b))) E. None of the above a=[]; for i=length(z):-1:1 if mod(i,2)==0 a=[z(i) a]; else z(i)=[]; end end If z is a vector of doubles and the above code is run, how will a and z relate? A. a has the even values from z and is reversed B. a and z are the same vectors C. a has the same number of values as z but is reversed D. a has half the values of z and is reversed E. None of the above are correct or the code errors Problem 3 – Multiple Choice [50 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 What is the class of output? A. char B. double C. logical D. cell E. Error 4. 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 solid red line connecting the given x and y values B. A plot of red circular points with the given x and y values C. A curved fit for the given x and y values D. Error E. None of the above 5. 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 6. Which of the following is a valid function header? (mark all that apply) A. function = fx(input) B. function ret1, ret2 = fx(input) C . function ret = fx(34) D. function fx E. [ret1, ret2] = fx(input1, input2) 7. 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)=[] 8. 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))) 9. 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 10. 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 Problem 3 – Multiple Choice [50 Points] 1. B 2. D 3. A 4. B 5. D 6. D 7. E 8. B 9. A 10. A Problem 7 – Miscellaneous [20 Points] 1. You are given the following information: A = true B = false Evaluate: i) (A && B) || (A && B) ________________________________________________ ii) (A || B) && (A || B) ________________________________________________ iii) (~(A || B)) || (A && B) ________________________________________________ 2. Ackermann's Function is "a function to end all functions." The work done by the function ack grows much faster than polynomials or exponentials. Given the following recursive method: function ret = ack(x,y) if (x == 0) ans = y + 1; elseif (y == 0) ans = ack(x - 1, 1); else ans = ack(x - 1, ack(x, y - 1)); end What is the result of ack(1, 4)? 3. Given a vector of coefficients a, mark the code fragments below that will evaluate the following function for a single value of x (not a vector): y = a(1) + a(2) * x + a(3) * x2 + ... a(n) * xn-1. A. polyval(x, a) B. n = 1:length(a) a(n) * x.^(n-1)' C. polyval(a, x) D. polyval(a(end:-1:1), x) 4. Which of the following correctly solves for X in the equation AX = B, knowing that A is a 4x4 matrix of known values, B is a 4x1 column vector of known values, and X is a 4x1 column vector of unknown values? A. B. C. D. X X X X = = = = inv(B)*A B\A inv(A)*B A/B Problem 7 – Miscellaneous [20 Points] 1. (i) False (ii) True (iii) False 2. 6 3. C 4. C Criteria - 5 points each (All or nothing) Problem 6 – Miscellaneous [20 Points] 1. Read the following code and answer the question that follows: ch = 'Ann'; ch = ch + 'a'; ch = [ch 'na']; len = length(ch); What is the value of len at the end? A. 3 B. 4 C. 5 D. 6 2. Read the following code and answer the question that follows: A a b c d = = = = = {'cat', [7 6 9], {'dog'}} A{3} A{1}(1) A{2} A(2) After the code above is executed, what is the data type of the following variables: A = ____________________________________________________________________ a = ____________________________________________________________________ b = ____________________________________________________________________ c = ____________________________________________________________________ d = ____________________________________________________________________ 3. Read the following code and answer the question that follows: x = [9 3 0 6 3] y = mod((sqrt(length(((x+5).*[1 2 3 4 5]))*5)),3) What is the value of y? A. 0 B. 3 C. 2 D. 5 2. You are given the following equations: 3x + 2y – z = 5 6x + z = 7 5x - 2y + z = 12 Write a script in MATLAB to solve the values of x, y, and z. Problem 6 – Miscellaneous [20 Points] 1. C 2. A = { 'cat' [7 6 9] a= 'dog' b= c c= [7 6 d= {[7 6 9]} 9] 3. C 4. A = [3 2 -1; 6 0 1; 5 -2 1] {‘dog’} B = [5; 7; 12] C = inv(A)*B x = C(1); y = C(2); z = C(3); %If they don’t split C into x y and z separately, ignore it Criteria - 5 points each (All or nothing) Problem 7 – Miscellaneous [20 Points] 1. List the three conditions required for a function be recursive: 1. 2. 3. 2. Which one(s) evaluate to a logical(boolean) 'true' ? I. ( 5 > 4 ) & ( ( 8 + 4 ) < 11 ) II. ~( ( ( 6 + 4 * 3 ) > 20 ) ) III. ~( ( 4 ~= 4 ) | (~( 6 < ( 4 * 2 / 8 + 4 ) ) ) ) A. I only B. II only C. III only D. I and III E. II and III 3. If the following command is typed in the Matlab command window, what is returned? >> 4%2 A. B. C. D. error 2 4 0 4. You have x-data and y-data points which best fit a 4th order polynomial. You now want to evaluate the same polynomial at different x-data points. Which function(s) will you need to obtain the above? Problem 7 – Miscellaneous [20 Points] 1. Call to clone, terminating condition, move towards termination 2. B 3. C 4. polyfit(), polyval() Criteria: - 5 points each for complete correct answer for each part 10. Multiple Choice (20 points) A) For the answers below, choose the best answer from the following possibilities: F: filter; I: insert; L: fold; M: map; R: sort; S: search; T: traverse __ raising all employees’ salaries by 2% __ looking for a file in a file drawer __ finding all the vehicles traveling faster than the speed limit __ adding a note to a music score __ finding the heaviest package on a truck __ reading all the data from an Excel spread sheet into Matlab B) Consider the following code: (x & (~y | (x & ~y))) What values for x and y will make the above expression evaluate TRUE? a) x = 0; y = 0 b) x = 0; y = 1 c) x = 1; y = 0 d) x = 1; y = 1 C) True or false: a) True False b) True False c) True False d) True False Functions must return a result To create a 3D plot, the function mesh must be used A parent class cannot be a child class A Queue is a First-In-First-Out (FIFO) data structure D) Your job is to create a sine wave in MATLAB. Given the four lines of code below that create your x-values, what command will give the most accurate representation of the curve? x1 x2 x3 x4 = = = = linspace(0, 2*pi); linspace(0, 2*pi, 10); [0:0.1:2*pi]; [0:1:2*pi]; a) plot(x1, sin(x1)) b) plot(x2, sin(x2)) c) plot(x3, sin(x3)) d) plot(x4, sin(x4)) Problem 10 A) M, S, F, I, L, T - 6 pts, 1 for each B) c C) a) false, b) false, c) false, d) true D) a - 5 pts - 4 pts, 1 for each - 5 pts 10. Multiple Choice (20 points) A) For the answers below, choose the best answer from the following possibilities: F: filter; I: insert; L: fold; M: map; R: sort; S: search; T: traverse __ the annual process of changing all employee records to update their age and time of employment __ moving all the cars off a ferry __ looking up a telephone number __ the IRS setting aside all the tax returns containing checks __ returning a book to the library __ finding the median income of the population of a country B) Consider the following code: (~x & (~y | (x & y))) What values for x and y will make the above expression evaluate TRUE? a) x = 0; y = 0 b) x = 0; y = 1 c) x = 1; y = 0 d) x = 1; y = 1 C) Which of the following is NOT a characteristic of a recursive function? a) Calls itself recursively b) Has a terminating condition c) Calls a helper function d) Moves solution closer to the termination condition D) Your job is to create a sine wave in MATLAB. Given the four lines of code below that create your x-values, what command will give the most accurate representation of the curve? x1 x2 x3 x4 = = = = linspace(0, 2*pi); linspace(0, 2*pi, 50); [0:0.2:2*pi]; [0:1:2*pi]; a) plot(x1, sin(x1)) b) plot(x2, sin(x2)) c) plot(x3, sin(x3)) d) plot(x4, sin(x4)) Problem 10 A) M, T, S, F, I, L B) a C) c D) a - 6 pts, 1 for each - 5 pts - 4 pts - 5 pts 10. Multiple Choice (20 points) A) For the answers below, choose the best answer from the following possibilities: F: filter; I: insert; L: fold; M: map; R: sort; S: search; T: traverse __ finding the oldest player on the team __ selecting the team MVP __ finding all the players who have scored more than 3 goals __ adding a player to the team roster __ updating all the players’ statistics after a game __ saving all your player info to a disk file B) Consider the following code: (x & (y | (x & y))) What values for x and y will make the above expression evaluate TRUE? a) x = 0; y = 0 b) x = 0; y = 1 c) x = 1; y = 0 d) x = 1; y = 1 C) True or false: a) True False b) True False c) True False d) True False Functions must have an input variable To create a 3D plot, the function mesh must be used Object Oriented Programming can be done in MATLAB A Stack is a First-In-First-Out (FIFO) data structure D) Your job is to create a sine wave in MATLAB. Given the four lines of code below that create your x-values, what command will give the most accurate representation of the curve? x1 x2 x3 x4 = = = = linspace(0, 2*pi); linspace(0, 2*pi, 500); [0:0.1:2*pi]; [0:1:2*pi]; a) plot(x1, sin(x1)) b) plot(x2, sin(x2)) c) plot(x3, sin(x3)) d) plot(x4, sin(x4)) Problem 10 A) L, L, F, I, M, T - 6 pts, 1 for each B) d C) a) false, b) false, c) true, d) false D) b - 5 pts - 4 pts, 1 for each - 5 pts 10. Multiple Choice (20 points) A) For the answers below, choose the best answer from the following possibilities: F: filter; I: insert; L: fold; M: map; R: sort; S: search; T: traverse __ finding the largest number in a matrix __ looking for a Ford Thunderbird on autotrader.com __ finding all the employees under 30 years old __ entering a new friend in your address book __ adding 5% to the price of all items under $25 in a store (leaving the other prices unchanged) __ copying all the Students in a Class to a data file B) Consider the following code: ( ~x & (y | (x & y))) What values for x and y will make the above expression evaluate TRUE? a) x = 0; y = 0 b) x = 0; y = 1 c) x = 1; y = 0 d) x = 1; y = 1 C) Which of the following is NOT a characteristic of a recursive function? a) Calls itself recursively b) Has a terminating condition c) Calls a helper function d) Moves solution closer to the termination condition D) Your job is to create a sine wave in MATLAB. Given the four lines of code below that create your x-values, what command will give the most accurate representation of the curve? x1 x2 x3 x4 = = = = linspace(0, 2*pi); linspace(0, 2*pi, 50); [0:0.001:2*pi]; [0:1:2*pi]; a) plot(x1, sin(x1)) b) plot(x2, sin(x2)) c) plot(x3, sin(x3)) d) plot(x4, sin(x4)) Problem 10 A) L, S, F, I, M, T B) b C) c D) c - 6 pts, 1 for each - 5 pts - 4 pts - 5 pts Problem 6 – Miscellaneous [30 Points] I. You are given the following linear equations: x - 3y = 1 3x - y = 2 Now let’s say you wanted to solve the equations simultaneously using MATLAB and store the solution vector in the variable named solution using the following code: solution = A1\A2; How must the variables A1 and A2 be defined in order to get the correct solution? A. B. C. D. E. A1 A1 A1 A1 A1 = = = = = [1 -3; 3 -1]; [2;1]; [3 -1; 1 -3]; [1;2]; [1 -3; 3 -1]; A2 A2 A2 A2 A2 = = = = = [2;1]; [1 -3; 3 1]; [2;1]; [1 -3; 3 -1]; [1 2]; II. Given the following code: function output = rec(x, y) if(x == 0 || y == 0) output = x + y; else output = rec(x-1,y) + rec(x, y-2) end answer = rec(2,2) What is the value of answer? A. 2 B. 4 C. 5 D. 8 E. None of the above III. You are given the following code: [snd, Fst] = wavread('Sound.wav'); Which of the following lines of code will play an amplified version of ‘Sound.wav’? A. B. C. D. sound(snd, Fst*3); sound(snd(round(1:half^2:end)), Fst) sound(snd*3, Fst); z= fft(Snd) z(1:floor(length(z)/2)) = 255; sound(ifft(z), Fst); IV. Given the following piece of code: a.price = 10; b = a.price; What is the data type of the variable b? A. B. C. D. E. struct logical cell char double V. The following code is executed in MATLAB: ABCD = 0; testV = [ 3 -5 6 -8 9 11] for i=1:length(testV) if testV(i) > 0 ABCD = ABCD + mod(testV(i),2); else ABCD = ABCD + 5; end end What is the final value of the variable ABCD? A. 4 B. 24.5 C. 13 D. 14 E. None of the above VI. Given: A = [3 7 5 2 6 1] Circle each of the following choices that would produce an error when executed (circle all that apply). A. A(A) B. A(10) = [] C. A([true false true]) D. A(0) = 3 E. A(linspace(2,6,5)) = 0 Problem 6 – Miscellaneous [30 Points] I. C. II. C. III. C. IV. E. V. C VI. A, B, D Criteria - 5 points each - All or nothing except for VI: -2 for each missing answer or extra answer circled Problem 3 – Vector Manipulation [20 Points] I. Read the code below and answer the questions that follow a = [3:7]; b = a>5; a(3:4) = 0; c = find(a > 2); d = sum(ones(size(a))); e = mod(c,(max(a)-d)); What is the value of the following variables when the code above is executed? b = _________________________________________________________ c = _________________________________________________________ e = _________________________________________________________ II. Which of the following are valid function headers? (Circle all that apply) A. function B. function C. function D. function E. function ret = myFunction(X) myFunction (X) ret = myFunction() = myFunction (X) myFunction (234) Problem 3 – Vector Manipulation [20 Points] I. b = [0 c = [1 e = [1 0 2 0 0 1 5] 1] 1] Criteria: - 5 points each (All or nothing) - If one of them is wrong, but the remaining are correct with erroneous vector carried forward, give them credit for the rest II. A, B, C Criteria: - 5 points (All or nothing) Part 2 – Multiple Choice [20 points] For each of the following problems, write the letter of the best answer choice in the box adjacent to the problem statement. 1. Given: s= struct('Name',{'Bobby', 'Susan', 'Elaine'}, 'Age', [19 21 17],… 'Year',{ 'Jr', 'Sr', 'Fr'}) fld = 'Age'; avg = mean(s(2).(fld)); Which one of the following is the value of avg? A. 17 B. 19 C. 21 D. [] E. Error 2. Given: [num txt raw] = xlsread('Excel.xls') Which one of the following is a true statement? A. The dimensions of num are always equal to the dimensions of raw B. The dimensions of txt are always equal to the dimensions of num C. The dimensions of num are always equal to the dimensions of txt D. txt has a data type of char E. None of the above 3. Which one of the following is NOT a valid function header? A. function A B. function [out1, out2] = A(in) C. function A(in) D. function (out1, out2) = A(in) E. function out = A 4. Given the following line of code: [data, fs] = wavread('sound.wav'); Which one of the following represents the time between consecutive samples? A. inv(data) B. 1/fs C. fs D. length(data)/fs E. data(1:end/2) / length(data) For each of the following terms below, select the short definition on the right that best describes the term. Write the corresponding letter in the box to the left of the question. Only answers in the boxes will be graded. A. a method for solving simultaneous linear equations 5. use of the AND (&&) logical operator 6. the global operator B. method of drawing a sphere with a map image C. allows recursive computation to wrap itself on the stack D. proper way to store towels in a linen closet 7. matrix multiplication E. a way of storing homogenous data accessed by field names F. can be used to rotate individual objects for plotting 8. folding G. extracting a single result from a collection 9. wrapper function H. a way of storing homogeneous data accessed by indexing I. can be used to check for bad inputs to a recursive function 10. cell arrays J. a way of storing heterogeneous data accessed by indexing K. to check if all of the chained logical expressions are true L. method for avoiding passing large data collections into a function M. to check if any of the chained logical expressions are true Part 2 – Multiple Choice [20 points] 1. B. 19 2. E. None of the above 3. D. function (out1, out2) = A(in) - round not square on output vars 4. B. 1/fs 5. K. to check if all of the chained logical expressions are true 6. L. method for avoiding passing large data collections into a function 7. F. can be used to rotate individual objects for plotting 8. G. extracting a single result from a collection 9. I. can be used to check for bad inputs to a recursive function 10 J. a way of storing heterogeneous data accessed by indexing