Docsity

Practice Test II MCQs for Computing for Engineers | CS 1371, Exams of Computer Science

Toggle sidebar
bg1
Page 1 of 8
CS 1371 โ€“ Spring 2008
Practice Test 2
Part 1 โ€“ Multiple Choice [30 Points]
1. Which of the following should NEVER be used within a recursive function?
I. Terminating Condition
II. Calling a clone of the function
III. Iteration
IV. Infinite Loop
V. Matrix Manipulation
A. I only
B. III only
C. IV only
D. I and II
E. III, IV, and V
2. Which of the following is a structure array most suited to represent?
A. A long list of numbers
B. Data from a spreadsheet
C. The title of a book
D. A flow chart
E. An address book
3. You want to open 'quote.txt', find all the misspelled words, and replace them with the
correctly spelled equivalent in a new file 'correctquote.txt'. How would you open the original
file?
A.
a = dlmread('quote.txt')
B.
a = dlmread('quote.txt', '/t')
C.
a = fopen('quote.txt')
D.
a = fopen('quote.txt', 'r')
E.
a = fopen('quote.txt', 'w')
4. Consider the following code:
a = [9 8 4; 2 3 6; 5 7 3];
b = a(:, end:-1:1);
a(2:end,:);
c = a + b;
What is the value of
c
?
A.
[8 10 9
B.
[18 16 8
C.
[9 2 5
D.
[13 16 13
E. Error
10 6 13 4 6 12 10 6 13 8 6 8
9 13 6 ] 10 14 6] 9 13 6 ] 8 14 8 ]
pf3
pf4
pf5
pf8

Partial preview of the text

Download Practice Test II MCQs for Computing for Engineers | CS 1371 and more Exams Computer Science in PDF only on Docsity!

CS 1371 โ€“ Spring 2008

Practice Test 2

Part 1 โ€“ Multiple Choice [30 Points]

  1. Which of the following should NEVER be used within a recursive function?

I. Terminating Condition II. Calling a clone of the function III. Iteration IV. Infinite Loop V. Matrix Manipulation A. I only B. III only C. IV only D. I and II E. III, IV, and V

  1. Which of the following is a structure array most suited to represent? A. A long list of numbers B. Data from a spreadsheet C. The title of a book D. A flow chart E. An address book
  2. You want to open 'quote.txt', find all the misspelled words, and replace them with the correctly spelled equivalent in a new file 'correctquote.txt'. How would you open the original file? A. a = dlmread('quote.txt') B. a = dlmread('quote.txt', '/t') C. a = fopen('quote.txt') D. a = fopen('quote.txt', 'r') E. a = fopen('quote.txt', 'w')
  3. Consider the following code: a = [9 8 4; 2 3 6; 5 7 3]; b = a(:, end:-1:1); a(2:end,:); c = a + b;

What is the value of c? A. [8 10 9 B. [18 16 8 C. [9 2 5 D. [13 16 13 E. Error 10 6 13 4 6 12 10 6 13 8 6 8 9 13 6 ] 10 14 6] 9 13 6 ] 8 14 8 ]

  1. Choose the plot created by the following code: x = [0 1 2 0]; y = [0 1 0 0]; plot(x,y,'d-') axis equal

A. B.

C. D.

E. None of the above

  1. Consider the following code: u=0:10; v=u.^2; th=linspace(0,2pi) [uu tth]=meshgrid(u,th); [vv tth]=meshgrid(v,th); rr=vv; xx=uu; yy=rr.sin(tth); zz=rr.*cos(tth); surf(xx,yy,zz);

Based on the code above, in which axis is the function rotated? A. x-axis B. y-axis C. z-axis D. None of the above E. Error

  1. The following code is run in Matlab: H(1).hw={[15,15],{18,18},100}; H.hw(3)=H(1).hw(1); H(1).OGG=H.hw{2}{1};

What is the final value of H? A. hw: {[15 15] {18,18} [15 15]} OGG: 18 B. 1x2 struct array C. hw: {[15 15] {{15,15},18} 100} OGG: {15,15} D. 2x1 struct array E. Error

  1. The following MATLAB code is executed:

G = [9 4 3; 1 4 2; 7 6 3]; H = [G G(end:-1:1,3)]; J = [H; H(1,:)]; K = sum(J);

What values are stored in variable K after all of the code above has been executed? A. [23 13 19 23] B. [26 18 11 11] C. [19 9 19 19] D. [26 18 11 23] E. None of the above

Part 2 โ€“ Tracing Problems [20 Points]

A. Consider the following function aRock:

function [type, group] = aRock(structval) ff = fieldnames(structval); if length(ff) >= 3 type = 1; group = 'coolRock'; else type = 3; group = aRockstar(structval); end

function group = aRockstar(structval) gg = fieldnames(structval); if length(gg) == 3 group = 'madeItAt'; else val = structval(end).(gg{end}); structval(end+1).(val) = 0; group = [aRockstar(structval) gg{end}]; end

The following lines are run in the command window of Matlab:

value = struct('name',{'bob','cod','eric'},'last',{'singh','patel','jiva'}) [second, first] = aRock(value)

  1. What is the value of ff?

A. [name last] B. {'name', 'last'} C. ['name'; 'last']

D. {'name'; 'last'} E. Error

  1. What is the value of second?

A. 1 B. 0 C. 3 D. 2 E. Error

  1. What is the value of gg?

A. {'name' 'last' 'jiva'} B. {'name'; 'last'; 'last'}

C. {'name'; 'last'; 'jiva'} D. ['name'; 'last' ; 'jiva'] E. None of the above

  1. What is the value of first?

A. 'coolRock' B. 'madeItAt' C. 'madeItAtlast' D. 'lastmadeItAt'

E. None of the above

  1. How many times does aRockstar get called?

A. 0 B. 1 C. 2 D. 3 E. 4

Part 3 โ€“ Design Problem [20 Points]

You have created a structure array with information about all the mp3 files in your music folder. Your array has fields containing the following: o the filename (a string) o album (a string) o artist (a string) o song length (in minutes) (a double) o a rating from 1 to 5 (5 being the best) (a double)

The following is a list of possible algorithm steps. For each of the three functions described below, write the letters of the necessary steps in order. Steps will not be used more than once within the same function, but they may be used in more than one function. Not all steps will be used. Note: it may help to determine the necessary steps for each function, and then put them in order.

A. Find structure with maximum song length B. Check value in the proper field against input value C. Find all songs with rating D. Subtract song length from playlist length (if needed) E. Remove structure from input F. Initialize output G. Check artist field with input value

H. Check difference between song length and playlist length I. Find maximum rating J. Concatenate structure to output (if needed) K. Remove field from structure L. Check filename M. Use iteration N. Randomly select one song with rating

Function 1: FilterMusic FilterMusic should take in the music structure array, a field (artist, album, etc.), and a value for that field (specific artist/album, etc). The function should remove any structures in the array which have that value for the input field and then return the modified structure array.

  1. Order of steps for FilterMusic: F M B J

Function 2: TimedPlaylist TimedPlaylist takes in the structure array and the number of minutes you would like your playlist to last. The function will return a structure array of the songs to be played. (Note: the number of minutes the songs play for may not exactly match the input amount. In this case, the playlist should be shorter than the input time, however, this difference should not be greater than the play time of the shortest song. Each song may only be included once.)

  1. Order of steps for TimedPlaylist: F M A H J D E

Function 3: RatedPlaylist The final function takes in the structure array and the number of songs you would like in the playlist. The function will return a structure array of the songs to be played. The function should add songs to the playlist based on rating โ€“ all songs with the highest rating should be added first, then all songs with the next highest rating, and so forth until the input number of songs has been reached. However, the function should randomly choose among songs with the same rating, and each song should only be included once.

  1. Order of steps for RatedPlaylist: F M I C N J E

Part 4 โ€“ Coding Problem [30 Points]

Choose ONE of the following two problems:

Problem A Write a function called RecursiveMin which recursively finds the minimum value in a given vector. You may NOT use the min() function to do this, and you MUST use recursion to receive credit.

function ret = RecursiveMin(vec)

if isempty(vec) ret = []; elseif length(vec) == 1 ret = vec; elseif vec(1) <= vec(end) ret = RecursiveMin(vec(1:end-1)); else ret = RecursiveMin(vec(2:end)); end

Problem B Write a function called xlsStruct that takes in an Excel file and returns a structure array. The first row of the Excel file will contain the title of each column (i.e., Name, Age, Score, etc). Each row beneath that will contain actual data and information. Your function should create a structure array with fieldnames corresponding to the titles in that first row, and the function should then create a structure by filling in the fields for each row of data.

function SA = xlsStruct(filename)

[num txt raw] = xlsread(filename); [row col] = size(raw); for ind1 = 2:row for ind2 = 1:col SA(ind1-1).(raw{1, ind2}) = raw{ind1, ind2}; end end