Docsity

CSE 131 Spring 2007 Final Exam (Blue Version) - Prof. Jon Sticklen, Exams of Computer Science

Partial preview of the text

Download CSE 131 Spring 2007 Final Exam (Blue Version) - Prof. Jon Sticklen and more Exams Computer Science in PDF only on Docsity!

BLUE VERSION (with answers)

Spr 2007

Section A: Array FETCH Operations

Question 1: After the following code fragment is run, what is the value of y?

x = [1,2,3; 4,5,6; 7,8,9]; y = x([1,2], end-1:end)

a) [2,3,5,6] b) [1,2;3,4] c) [2,3; 5,6] d) [1,2,3; 4,5,6]

Question 2: After the following code fragment is run, what is the value of y?

z = [1,2; 2,1]; y = [z(1,:); z(:,2)']

a) [1,2; 2,1] b) [1,2,2,1] c) [1,2,1,2; 2,1,2,1] d) [2,1; 1,2]

Question 3: After the following code fragment is run, what is the value of p?

x = [7,2,3; 0,4,8; 5,6,1]; p = x(1,2)*x(2,:) + x(end,:)

a) [0,8,16] b) [0,8,17] c) [5,14,25] d) [5,14,17]

Question 4: After the following code fragment is run, what is the value of z?

x = [10,100;50,100]; y = x'; z = [x(:,end)'; y(end,:)]'

a) [100,100; 100,100] b) [10,50; 10,50] c) [100,100,100,100] d) [100,100; 100,10]

Question 5: After the following code fragment is run, what is the value of x?

y = 'Ashley Ohio is my home.'; x = y(1, 8:11)

a) Ashley b) Ohio c) home d) none of the above

Question 10: After the following code fragment is run, what is the value of y?

x = [2, 4, 6, 7, 5, 8, 10]; y = x(find ( 2 < sqrt(x) & sqrt(x) < 3))

a) [6, 7, 5, 8] b) [2, 3, 4, 5, 6] c) [3, 4, 5, 6] d) [2, 4, 10]

Question 11: After the following code fragment is run, what is the value of a?

a = [2, 4, 6, 7, 5, 8, 10]; y = find(a > 8 | a < 4); a(y) = a(y) + 10 % be careful with this one!

a) [12, 20] b) [12, 4, 6, 7, 5, 8, 20] c) [22, 14, 16, 17, 15, 18, 30] d) [1, 7]

Question 12: After the following code fragment is run, what is the value of b?

a = [2, 4, 6, 7, 5, 8, 10]; y = find(a < 4 | a = = 4); b = [a(y); a(y)-1]^

a) [4, 16; 1, 9] b) [8, 20; 5, 13] c) [4, 16, 36, 49, 25, 64, 100] d) none of the above

Section D: Array SET Operations

Question 13: After the following code fragment is run, what is the value of x?

x = [1, 2, 3; 4, 5, 6; 7, 8, 9]; y = [4, 5, 6; 7, 8, 9; 1, 2, 3]; x(end, :) = y(:, end)'

a) [1, 2, 3; 4, 5, 6; 1, 2, 3] b) [1, 2, 3; 4, 5, 6; 7, 8, 9] c) [1, 2, 3; 4, 5, 6; 6, 9, 3] d) [1, 2, 6; 4, 5, 9; 7, 8, 3]

Question 14: After the following code fragment is run, what is the value of y?

x = [1, 2; 3, 4]; y = [1, 2, 3; 4, 5, 6; 7, 8, 9]; y(x(1, end), end) = 50

a) [1, 2, 3; 4, 5, 6; 7, 8, 50] b) [1, 2, 3; 4, 5, 50; 7, 8, 9] c) [1, 2, 50; 4, 5, 6; 7, 8, 9] d) none of the above

Question 15: After the following code fragment is run, what is the value of A?

A = ones(4,4); B = zeros(2,2); A(1:end-2, 3:end) = B

a) [0,0,1,1; 0,0,1,1; 1,1,1,1; 1,1,1,1] b) [1,1,0,0; 1,1,0,0; 1,1,1,1; 1,1,1,1] c) [1,1,1,1; 1,1,1,1; 0,0,1,1; 0,0,1,1] d) [1,1,1,1; 1,1,1,1; 1,1,0,0; 1,1,0,0]

Question 16: After the following code fragment is run, what is the value of x?

a = 2; b = 4; x = [1,2,3,4; 5,6,7,8; 9,10,11,12]; x(:,end) = 100

a) [1,2,3,100; 5,6,7,8; 9,10,11,100] b) [1,2,3,100; 5,6,7,100; 9,10,11,100] c) [1,2,3,4; 5,6,7,8; 9,10,11,100] d) [1,2,3,4; 5,6,7,8; 100,100,100,100]

Question 17: After the following code fragment is run, what is the value of x?

a = 2; b = 4; x = [1,2,3,4; 5,6,7,8; 9,10,11,12]; x(:,end) = (1:2:6)'

a) [1,2,3,4; 3,6,7,8; 5,10,11,12] b) [1,2,3,1; 5,6,7,3; 9,10,11,5] c) [1,2,3,4; 5,6,7,8; 9,10,11,12] d) [1,2,3,4; 3,6,7,3; 5,10,11,12]

Section E: Matrix Operations

Question 18: Which of the following will NOT generate an error for arrays A, B, and C defined as

A = [1,2,3; 4,5,6]; B = [3,6; 9,12; 15, 18]; C = [2,4; 6,8];

a) C * B b) A * C c) C * A d) A * C’

Question 19: After the following code fragment is run, what is the value of x?

A = [2 3; 5 -1]; y = [5; 4]; x = A \ y

a) [5; 4] b) [1; 1] c) [2/5, 3/5; 5/4, -1/4] d) none of the above

Question 23: After the following code fragment is run, what is the value of output?

a = 10; b = 3; c = 2; d = 5; output = 35; if ((a < b) & (c < d)) | (c < d) output = 100 else output = 0 end

a) 0 b) 100 c) [100, 0] d) 35

Section G: FOR Construct

Question 24: After the following code fragment is run, what is the value of total?

total(1) = 1; total(2) = 2; for count = 3: total(count-1) = total(count-2)^ end

a) [1, 2, 3, 4] b) 1 c) [1, 1, 1] d) [1, 1]

Question 25: After the following code fragment is run, what is the value of myVar?

data = [1:3; 4:6; 7:9]; myVar = 1; for x=1: for y=1: if x == y myVar = myVar * data(x,y) end end end

a) 45 b) 15 c) 22 d) 5

Question 26: After the following code fragment is run, what is the value of theAns?

data = [1:3; 4:6; 7:9]; count = 0; for j = [2, 3] count = count + 1; theAns(count) = data(j-1,j) + data(j, j-1); end

a) 5 b) [6, 14] c) 4: d) none of the above

Question 27: After the following code fragment is run, what is the value of mySum?

mySum = 0; for i = 1: for j = 1: for k = 1: mySum = mySum + k; end end end

a) 6 b) 8 c) 12 d) 3

Question 28: After the following code fragment is run, what is the value of mySum?

mySum = 0; for i = 1 for j = 1: for k = 1: mySum = mySum + ijk end end end

a) 3 b) 9 c) 18 d) 27

End of Exam

If you have comments about 131, or questions about your final exam,

stop by Dr. Sticklen’s office hours next term. Hours for next term will be

posted at the beginning of the term on the main class website:

http://cse131.cse.msu.edu

Have a good summer!

Do not open

this test booklet

until you are

instructed to do so.

This is the BLUE exam.