Download CS1371 Practice Test 2 Solutions - Georgia Tech, Fall 2009 - Prof. David Smith and more Exams Computer Science in PDF only on Docsity!
Georgia Institute of Technology
College of Computing
CS1371 Computing for Engineers
Practice Test 2 Solutions - Fall Semester 2009
Multiple Choice
1. The following MATLAB code is executed:
A = struct('FirstName','George','Democrat',false); B = fieldnames(A); A.(B{2}) = true;
What does the structure A look like after all of the code above has been executed?
A. FirstName: 'George' B. FirstName: 'George' C. FirstName: 'George'
Democrat: 0 Democrat: 1 Democrat: 0 George: 0
D. FirstName: 'George' E. Error
Democrat: 0 George: 1
2. Which choice below is NOT true about the following line of code?
f1 = fopen('recipe.txt', 'r')
A. The file handle for ‘recipe.txt’ is stored in the variable f
B. The text from ‘recipe.txt’ is stored in the cell array f
C. The file has been opened with read only permission
D. Both A and C
E. Both B and C
3. 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
4. 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
5. The following code is executed:
A = {[4 3 2 1] 'abc' [true false]} out1 = class(A{3}(1)); out2 = class(A{2});
What are the values of out1 and out2?
A. out1 = 'cell' out2 = 'char'
B. out1 = 'cell' out2 = 'cell'
C. out1 = 'logical' out2 = 'cell'
D. out1 = 'double' out2 = 'char'
E. out1 = 'logical' out2 = 'char'
6. Consider these three iterative loops:
I.
vec = [3 -2 9] x = 0 for i = vec x = x + i end
II.
vec = [3 -2 9] x = 0 for i = length(vec) x = x + vec(i) end
III.
vec = [3 -2 9] x = 0 while length(vec) > 0 x = x + vec(1) vec(1) = [] end
Which of the following is correct?
A. I and II are equivalent
B. I and III are equivalent
C. II and III are equivalent
D. I, II, and III are all equivalent
E. None of the above are equivalent
Coding
1.Write a function called dropTest that will compute the test score that will be dropped for each
student in a class (the drop test score should be the lowest score for each student). The function
Tracing
main = struct('Name',{'David','Sean','Nathan','Harold'}, 'Status',{1,2,3,4}); len = length(main); for i = 1:len switch main(i).Status case 1 main(i).Duty = 'Garbage'; case 2 main(i).Duty = 'Dishes'; case 3 main(i).Duty = 'Floor'; case 4 main(i).Duty = 'Counter-top'; otherwise main(i).Duty = 'Exempt'; end end
varA = fieldnames(main) rmfield(main, 'Status'); varB = isfield(main, 'Status') main = rmfield(main, 'Status'); varC = isfield(main, 'Status') main(1).Age = 25; main(2).Age = 40; main(end + 1).Age = 30;
varD = [getfield(main(1),'Name') getfield(main(2),'Name')... getfield(main(3),'Name') getfield(main(4),'Name') getfield(main(5),'Name')]
varE = [getfield(main(1),'Duty') getfield(main(2),'Duty')... getfield(main(3),'Duty') getfield(main(4),'Duty') getfield(main(5),'Duty')]
varF = [getfield(main(1),'Status') getfield(main(2),'Status')... getfield(main(3),'Status') getfield(main(4),'Status') getfield(main(5),'Status')]
varG = [getfield(main(1),'Age') getfield(main(2),'Age')... getfield(main(3),'Age') getfield(main(4),'Age') getfield(main(5),'Age')]
What are the final values of the following variables after the code above is executed in
MATLAB?
varA = 'Name' 'Status' 'Duty' varB = 1 varC = 0 varD = DavidSeanNathanHarold varE = GarbageDishesFloorCounter-top
varF = Error
varG = [25 40 30]
Given: data.xls
close all [a b c] = xlsread('data.xls'); s = ':*+o'; c = 'rgyk'; [m n] = size(a); hold on for z=2:n plot(a(:,1)',a(:,z)',[c(z) s(z)]) end title(b{1}) xlabel(b{2,1}) ylabel('Value') axis tight
Complete the blank plot below with the data, title, labels, and axis that the above code produces.
Do not worry about indicating the color of the plot data.