Docsity

CS1171 and CS1371 Computing for Engineers Exam Practice Problems, Exams of Computer Science

Toggle sidebar
bg1
CS1171 - Computing for Engineers Practice Problems
Page 1 of 54
Reference Section:
diag(m) – returns the diagonal elements of the matrix m
factorial(n) – returns n!
find(m) – returns the indices of the true elements of m
image(x) - display the image from the matrix x.
imread(filename) - returns a matrix representation of an image
newy = interp1(x,y,newx) - Interpolates to find newy, the values of the underlying
function Y at the points in newx.
iscell(a) – checks if a is of class cell (a cell array)
ischar(a) – checks if a is of class char (a string)
isempty(here) – checks if here is null (usually represented by [], the empty vector
(x/y/z)label(str) – labels the plot axes with the given string
length(a) – largest dimension of a
length(a) – largest dimension of a
magic(n) – builds a n * n magic square
max(a) – value and index of the max value in a
mesh(x, y, z) – plot the surface defined by the x, y and z arrays with colored lines and
white faces
[xx, yy] = meshgrid(x, y) – compute the plaid from the x and y vectors
min(a) – value and index of the min value in a
mod(a, b) – the remainder when a is divided by b
mod(a, b) – the remainder when a is divided by b
ones(rows, cols) – generate a matrix filled with 1
p = polyfit(x,y,n) - Finds the coefficients of a polynomial P(X) of degree N that fits the
data
y = polyval(p,x) - Evaluates the polynomial p, at all points in x
prod(v) – compute the product of all the elements in a vector v
sin(th) – sin of the angle in radians
size(a) – all the dimensions of a
sort(v) – arranges the vector v in ascending num erical order
newy = spline(x,y,newx) - Performs cubic spline interpolation to find newy, the values of
the underlying function Y at the points in newx.
sum(v) – total all the elements in the vector v
surf(x, y, z) – plot the surface defined by the x, y and z arrays with colored faces and
black lines
title(str) – titles the plot with the given string
[x, fs] = wavread(file) – gives the waveform and sampling frequency for a .wav file
[x, y, z] = xlsread(filename) - Returns the numeric, text and raw data respectively
from an .xls file
xlswrite(filename, array) - Writes the array to the .xls file
zeros(rows, cols) – generate a matrix filled with 0
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36

Partial preview of the text

Download CS1171 and CS1371 Computing for Engineers Exam Practice Problems and more Exams Computer Science in PDF only on Docsity!

CS1171 - Computing for Engineers Practice Problems

Reference Section: diag(m) – returns the diagonal elements of the matrix m factorial(n) – returns n! find(m) – returns the indices of the true elements of m image(x) - display the image from the matrix x. imread(filename) - returns a matrix representation of an image newy = interp1(x,y,newx) - Interpolates to find newy, the values of the underlying function Y at the points in newx. iscell(a) – checks if a is of class cell (a cell array) ischar(a) – checks if a is of class char (a string) isempty(here) – checks if here is null (usually represented by [], the empty vector (x/y/z)label(str) – labels the plot axes with the given string length(a) – largest dimension of a length(a) – largest dimension of a magic(n) – builds a n * n magic square max(a) – value and index of the max value in a mesh(x, y, z) – plot the surface defined by the x, y and z arrays with colored lines and white faces [xx, yy] = meshgrid(x, y) – compute the plaid from the x and y vectors min(a) – value and index of the min value in a mod(a, b) – the remainder when a is divided by b mod(a, b) – the remainder when a is divided by b ones(rows, cols) – generate a matrix filled with 1 p = polyfit(x,y,n) - Finds the coefficients of a polynomial P(X) of degree N that fits the data y = polyval(p,x) - Evaluates the polynomial p, at all points in x prod(v) – compute the product of all the elements in a vector v sin(th) – sin of the angle in radians size(a) – all the dimensions of a sort(v) – arranges the vector v in ascending numerical order newy = spline(x,y,newx) - Performs cubic spline interpolation to find newy, the values of the underlying function Y at the points in newx. sum(v) – total all the elements in the vector v surf(x, y, z) – plot the surface defined by the x, y and z arrays with colored faces and black lines title(str) – titles the plot with the given string [x, fs] = wavread(file) – gives the waveform and sampling frequency for a .wav file [x, y, z] = xlsread(filename) - Returns the numeric, text and raw data respectively from an .xls file xlswrite(filename, array) - Writes the array to the .xls file zeros(rows, cols) – generate a matrix filled with 0

CS1171 - Computing for Engineers Practice Problems

Problem 1 – Sorting [20 Points]

I. What does the Big-O of any algorithm represent?



II. Write out the Big-O of the following sorting algorithms:

a. Insertion Sort: _____________________________________________ b. Merge Sort: _____________________________________________ c. Quick Sort: _____________________________________________ d. Bubble Sort: _____________________________________________

III. Sort the following vector using Insertion Sort:

[9 1 18 20 5 8 2]

CS1171 - Computing for Engineers Practice Problems

Problem 3 – Numerical Methods [20 Points]

I. Which of the following functions can be used for spline interpolation over a given set of data points? Circle all that apply.

A. newY = polyfit(x, y, newX, ‘spline’) B. newY = spline(x, y, newX) C. newY = interp1(x, y, newX, ‘spline’) D. newY = splinefit(x, y, newX)

II. What are the values of a and b after the following code is run?

x = 1:

a = diff(x) a =

b = cumsum(x) b =

III. You have two vectors, time and distance, containing X and Y values respectively. Use this data to find the coefficients of a 5th^ order polynomial. Store the coefficients in a variable called coeffs. Also, plot the polynomial obtained for X values ranging from 1 to 10 seconds.

IV. When plotting 3-D figures to rotate a curve about an axis, what happens if you use a large increment values in your theta vector?

A. The built-in plot functions for 3-D figures will produce an error saying there are insufficient points. B. MATLAB will automatically add evenly spaced points to your theta vector. C. The figure will be angular instead of having a circular shape. D. The number of points in the theta vector does not matter, as long as it first and last value are a distance of a multiple of 2*pi apart.

CS1171 - Computing for Engineers Practice Problems

Problem 4 – Plotting [20 Points]

I. Circle all of the following functions that can be used to plot a 3-dimensional surface in MATLAB.

A. plot3(xx,yy,zz) B. meshgrid(xx,yy,zz) C. surf(xx,yy,zz) D. mesh(xx,yy,zz)

II. You want to plot a surface of rotation for z = f(x) around the x and z axes. Given the following commands:

v = linspace(1,10,50); th = linspace(0,2pi,36); [vv tth] = meshgrid(v,th); rr = vv; xx = rr.cos(tth); yy = rr.*sin(tth); zz = f(x); surf(xx,yy,zz);

III since it does not make sense to rotate it about the y axis, which of the following code blocks rotates f(x) about the other axis (circle the right answer)?

A: xx = vv; rr = f(vv); yy = rr .* cos(tth); zz = rr .* sin(tth); surf(xx, yy, zz)

B: xx = f(vv); rr = vv; yy = rr .* cos(tth); zz = rr .* sin(tth); surf(zz, yy, xx)

C: xx = vv; rr = f(vv); yy = rr .* cos(tth); zz = rr .* sin(tth); surf(zz, yy, xx)

D: xx = f(vv); rr = vv; yy = rr .* cos(tth); zz = rr .* sin(tth); surf(xx, yy, zz)

IV. Write the proper commands to make the plot above smooth and add appropriate titles and labels.

CS1371 - Computing for Engineers Final Exam Version D May 3rd, 2007

III. “In order to increase the pitch of a sound by n half steps, you would stretch/shrink the sound vector to have 1/(2^(n/12)) times the number of original points.”

True or False? If False, Why?

IV. Given an image file called ‘american_flag.jpg’ in which the colors are only pure red, pure white, and pure blue:

af = imread('american_flag.jpg'); [r1,c1] = find(af(:,:,1) == 255 ... && af(:,:,2) == 0 ... && af(:,:,3) == 0); [r2,c2] = find(af(:,:,1) == 0 ... && af(:,:,2) == 0 ... && af(:,:,3) == 255); [r3,c3] = find(af(:,:,1) == 255 ... && af(:,:,2) == 255 ... && af(:,:,3) = = 255); af(r1,c1,:) = 255; af(r2,c2,1) = 255; af(r2,c2,3) = 0; af(r3,c3,1:2) = 0; image(af)

What happens in the resulting image?

A. The white sections become red, the red sections become blue, and the blue sections become white. B. The blue sections become red, the red sections become blue, and the white sections become black. C. The red sections become white, the blue sections become red, and the white sections become blue. D. The blue sections become white, the red sections become black, and the white sections become red.

CS1371 - Computing for Engineers Final Exam Version D May 3rd, 2007

Problem 6 – Miscellaneous [20 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 = unknown1\unknown2;

How must the variables unknown1 and unknown2 be defined in order to get the correct solution?

A. unknown1 = [1 -3; 3 -1]; unknown2 = [2;1]; B. unknown1 = [2;1]; unknown2 = [1 -3; 3 1]; C. unknown1 = [3 -1; 1 -3]; unknown2 = [2 1]; D. unknown1 = [1;2]; unknown2 = [1 -3; 3 -1]; E. unknown1 = [1 -3; 3 -1]; unknown2 = [1; 2];

II. 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 - testV(i); end end

What is the final value of the variable ABCD?


CS1371 - Computing for Engineers Final Exam Version D May 3rd, 2007

Problem 7 – Structure Arrays [20 Points]

I. Which of the following are valid function headers? (Circle all that apply)

A. function ret = myFunction(X) B. function myFunction (234) C. function myFunction (X) D. function = myFunction (X) E. function ret = myFunction()

II. Read the code below and answer the questions that follow:

value = struct('band', 'nickelback', 'album', {'all the right reasons', 'curb'}, 'song', {'animals', 'fly'}, 'genre' , 'rock'); value(3).band = 'U2'; value(3) = setfield(value(3), 'song', 'sweetest thing'); value(5) = value(1); value = rmfield(value, 'genre');

A = isstruct(value)

B = isfield(value(1),'genre')

C = getfield(value(2), 'album')

D = value(4).band

E = fieldnames(value(5))

What are the values of the following variables?

A:

B:

C:

D:

E:

CS1371 - Computing for Engineers Final Exam Version D May 3rd, 2007

Problem 8 – Sorting [20 Points]

III. What does the Big-O of any algorithm represent?



IV. Write out the Big-O of the following sorting algorithms:

a. Insertion Sort: _____________________________________________ b. Merge Sort: _____________________________________________ c. Quick Sort: _____________________________________________ d. Bubble Sort: _____________________________________________

III. Sort the following vector using Merge Sort:

[9 1 18 20 5 8 2]

CS1371 - Computing for Engineers Final Exam Version D May 3rd, 2007

Problem 10 – Plotting [20 Points]

I. Circle all of the following functions that can be used to plot a 3-dimensional surface in MATLAB.

E. plot3(xx,yy,zz) F. meshgrid(xx,yy,zz) G. surf(xx,yy,zz) H. mesh(xx,yy,zz)

II. You want to plot a surface of rotation for z = f(x) around the x and z axes. Given the following commands:

v = linspace(1,10,50); th = linspace(0,2pi,36); [vv tth] = meshgrid(v,th); xx = vv; rr = f(x); yy = rr.cos(tth); zz = rr.*sin(tth); surf(xx,yy,zz);

About which axis does this code make a surface of rotation? _______________

III since it does not make sense to rotate it about the y axis, which of the following code blocks rotates f(x) about the other axis (circle the right answer)?

A: xx = f(vv); rr = vv; yy = rr .* cos(tth); zz = rr .* sin(tth); surf(zz, yy, xx)

B: xx = vv; rr = f(vv); yy = rr .* cos(tth); zz = rr .* sin(tth); surf(zz, yy, xx)

C: xx = f(vv); rr = vv; yy = rr .* cos(tth); zz = rr .* sin(tth); surf(xx, yy, zz)

D: xx = vv; rr = f(vv); yy = rr .* cos(tth); zz = rr .* sin(tth); surf(xx, yy, zz)

IV. Write the proper commands to make the plot above smooth and add appropriate titles and labels.

CS1371 - Computing for Engineers Final Exam Version G May 1st, 2007

Problem 11 – Images/Sounds[20 Points]

I. Consider the following "image" (assume it is a perfect square), saved under the file 'mysquare.jpg':

And the following code:

b = imread('mysquare.jpg'); [n,m,l] = size(b); a = b(1:end, 1:n/2, :); c = b(1:end, (n/2 + 1):end, :); b = [c; a]; image(b);

Which of these will the picture shown on the last line most resemble?

A. B. C. D.

II. Given the file ‘soundtest.wav’, the following commands are executed in MATLAB:

[x fs] = wavread(‘soundtest.wav’); L = length(x);

The time duration in seconds of ‘soundtest.wav’ is:

A. fsL/(L+1) B. fs/L C. L/fs D. Lfs E. fs*L/(fs+1)

1 2 3 4 1

3 4 1 2 1

2 1 4 3 1

1

4

3

2 4

2

3

1

CS1371 - Computing for Engineers Final Exam Version G May 1st, 2007

Problem 12 – Vector Manipulations/Functions [20 Points]

Given the following function:

1 function [A B] = blackbox(input) 2 L = length(input) 3 A = [] 4 B = 0 5 for ind = 1:L 6 if ischar(input{ind}) 7 A = [A input(ind)] 8 elseif isnumeric(input{ind}) 9 B = 0 + input{ind} 10 end 11 end

The following code is executed in MATLAB:

TEST1 = {'ES&T', [98 1 27], 'Woodruff', 'Smith', 1000, [-2]} [A B] = blackbox(TEST1)

I. What is stored in the variable A after the code has been run?


II. What data type is A?


III. What is stored in B after the code has been run?


IV. If line 7 were changed to the following, what now would be stored in A? A = [A input{ind}];

________________________________________________________________________

V. What is the data type of A now (after the change in line 7)?

________________________________________________________________________

CS1371 - Computing for Engineers Final Exam Version G May 1st, 2007

Problem 13 – Structure Arrays [20 Points]

I. Which of the following are valid function headers? (Circle all that apply)

A. function ret = myFunction(X) B. function myFunction (X) C. function = myFunction (X) D. function myFunction (234) E. function ret = myFunction()

III. Read the code below and answer the questions that follow:

TA_List(1) = struct( 'Name', 'Shardul', 'Major', 'EcE', ... 'Experience', 6) TA_List(2) = setfield( TA_List(1), 'Experience', 4) TA_List(2).Name = 'Ankit' TA_List(3) = TA_List(1) setfield( TA_List(3), 'Major', 'ME') setfield( TA_List(3), 'Name', 'Tauhira') rmfield( TA_List, 'Major') TA_List(3).Age = 20 A = isfield( TA_List(1), 'Age') [B, C] = size (getfield (TA_List(2), 'Name')) D = TA_List(3).Name E = fieldnames(TA_List(1))

What are the values of the following variables?

A:

B:

C:

D:

E:

CS1371 - Computing for Engineers Final Exam Version G May 1st, 2007

III. Given:

x = {2, 4, 6, 8, 10}; final = 0 for index = 1:length(x)- end

Which of the following lines of code will have final equal to 80 after the code above is executed in MATLAB?

A. final = final + (x(index) * x(index+1)) B. final = final + (x{index} * x{index+1}) C. final = x(index) * x(index+1) D. final = x{index} * x{index+1} E. None of the above

IV. Which of the following correctly define(s) the time domain axis for the following imported sound file?

[data fs] = wavread(‘foo.wav’); n = length(data);

A. timedomain = 0:1/n:n/fs; B. timedomain = 0:1/fs:n; C. timedomain = 1/fs:1/fs:n/fs; D. timedomain = 0:n*fs; E. timedomain = 0:n/fs:n;

CS1371 - Computing for Engineers Final Exam Version G May 1st, 2007

Problem 15 – Sorting [20 Points]

V. What does the Big-O of any algorithm represent?



VI. Write out the Big-O of the following sorting algorithms:

a. Insertion Sort: _____________________________________________ b. Merge Sort: _____________________________________________ c. Quick Sort: _____________________________________________ d. Bubble Sort: _____________________________________________

III. Sort the following vector using Quick Sort:

[9 1 18 20 5 8 2]