Docsity

CS 1371 Practice Test Fall 2007, Exams of Computer Science

Toggle sidebar
bg1
CS 1371 Practice Test – Fall 2007
Problem 1 – Vector Manipulation
A. Consider the following code :
A = 1:2:20;
B = linspace(1,10,10);
B((mod(B, 3) == 0)||((mod(B,5)==0)) = [];
C = [A(5:end) A(1:5)];
[M in] = max(C);
A(in) = -1;
D = B([3 2 6]).*2;
A(2:6)
What are the values of the variables after the code is run?
A: ___________________________________________________________________________
B: ___________________________________________________________________________
C: ___________________________________________________________________________
D: ___________________________________________________________________________
B. For each of the following, only a line or two of code is required.
1. Create a vector, vec1, that has 20 values between 89 and 101.
2. Swap the first and second halves of vec1 and store in vec2.
3. Find the smallest value in vec2 and its location. Store the value as M and the location as L.
C. Evaluate the following logical expressions as true or false:
A = true
B = false
1. any([A B]) : _____________________________________________________________
2. (A || B) && B : __________________________________________________________
3. A || (B && A) : __________________________________________________________
pf3
pf4
pf5

Partial preview of the text

Download CS 1371 Practice Test Fall 2007 and more Exams Computer Science in PDF only on Docsity!

CS 1371 Practice Test – Fall 2007

Problem 1 – Vector Manipulation

A. Consider the following code :

A = 1:2:20; B = linspace(1,10,10); B((mod(B, 3) == 0)||((mod(B,5)==0)) = []; C = [A(5:end) A(1:5)]; [M in] = max(C); A(in) = -1; D = B([3 2 6]).*2; A(2:6)

What are the values of the variables after the code is run?

A: ___________________________________________________________________________

B: ___________________________________________________________________________

C: ___________________________________________________________________________

D: ___________________________________________________________________________

B. For each of the following, only a line or two of code is required.

  1. Create a vector, vec1, that has 20 values between 89 and 101.
  2. Swap the first and second halves of vec1 and store in vec2.
  3. Find the smallest value in vec2 and its location. Store the value as M and the location as L.

C. Evaluate the following logical expressions as true or false:

A = true B = false

  1. any([A B]) : _____________________________________________________________
  2. (A || B) && B : __________________________________________________________
  3. A || (B && A) : __________________________________________________________

Problem 2 – String Manipulation

A. Anagrams are words which contain the same letters, except in a different order. For example, “egret” and “greet” are anagrams.

1.) Write a function called isAnagram that consumes two strings and returns a Boolean: true if the strings are anagrams or false if they are not.

Note: you may assume that the strings will all be lowercase letters, with no other characters Hint: Use built-in MATLAB functions.

Examples: A = isAnagram('tears', 'stare') A = 1

B = isAnagram ('good', 'dog') B = 0

2.) Now you decide you want a little more sophistication. For example, “butterfly” and “flutter by” are actually anagrams. Give a string A, write the code to remove all the spaces from A.

B. Consider the following function:

function ret = sillyString(X)

for ind = 1:length(X) if strcmpi(X(ind), 't') ret(ind-1:ind) = 'CS'; elseif X(ind) >= 'A' && X(ind) <= 'Z' ret(ind) = 'z'; else ret(ind) = X(ind); end end

1.) What would be the result of the following lines of code? If the code would produce an error, briefly explain why.

a.) a1 = sillyString('I love CS1371!') a1 = _________________________________________________________

b.) a2 = sillyString('This test is HARD') a2 = _________________________________________________________

Problem 3 - Conditionals

A. Your friend keeps all of his expenses for a checking account in a vector called budget. Deposits are positive numbers, and money spent is negative. He writes a piece of MATLAB code to determine his financial situation:

1 if sum(budget) < 10 2 if sum(budget) < 0 3 status = 'in debt'; 4 else 5 status = 'broke'; 6 elseif all(budget > 0) 7 status = 'frugal'; 8 elseif abs(min(budget)) > 500 9 status = 'big spender'; 10 else 11 status = 'fine'; 12 end

  1. Your friend is not a very good programmer. The first time he runs the code, there is an error. What is the error? (Assume that budget has been defined before the code above is executed)
  2. You fix the code for your friend. Now, consider the following cases, and write the final value of budget after the code above has been executed.

i.) budget = [10 200 53]

status = ____________________________________________________

ii.) budget = [-27 1000 -84 -720]

status = ____________________________________________________

iii.) budget = []

status = ____________________________________________________

B. Now, your friend has nice parents willing to help him out. Write a function called parentalAid which takes in one of the statuses from above and returns the amount of money his parents are sending. This should be based on the following:

If status is:

  • ‘broke’ or ‘in debt’, the amount will be 200 dollars
  • ‘fine’ the amount will be 50 dollars
  • all others, the amount will be 0

Note: You must use a switch-case statement for this question

Problem 5 – Cell Arrays

A. Consider the following function, which takes in a cell array:

function [out1, out2] = spiffy(ca)

out1 = ca; out2 = {}; for X = 1:length(ca) if ischar(ca{X}) for Y = 1:length(ca{X}) out1{X}(Y) = char(ca{X}(Y)+1); end elseif isnumeric(ca{X}) out2 = [out2 ca(X)]; end end

Now, consider the following cases:

  1. [A, B] = spiffy({'cool', [1 3 7], [false false false], 'sweet'})

a.) What is the value of A{1}? ____________________________________________________ b.) What is the class of A{1}? ____________________________________________________ c.) What is the value of B? _______________________________________________________ d.) What is the class of B? ________________________________________________________

  1. [C, D] = spiffy({[2 5 0], true, [33 3]})

a.) What is the value of length(C)? _____________________________________________ b.) What is the value of C? _______________________________________________________ c.) What is the value of D? _______________________________________________________

B. Write a function called longV that takes in a cell array and concatenates all the vectors into one long vector. The function outputs the one vector and the average length of the vectors in the cell array. Note: scalars count as a vector of length 1. There may be other items, such as strings, in the cell array.

Example: A = longV({1:3, 'gah', 5, [3 7]}) A = [1 2 3 5 3 7]

Thanks to Jacey Planteen for her help on the test. Last Modified: 3:10PM September 16 th^2007 by Shardul Bhatia