final exam review
MATLAB's for loop construct has the following form: for var = start:step:stop statement end Which of the following are true about MATLAB for loops? a. For loops cannot be nested in MATLAB b. The step expression specifies the number of times the loop will execute c. At the beginning of the first iteration var has a value equal to start d. The value of start can be negative e. The value of step must be an integer
d, c
How many times will the following MATLAB script print 'Hello, world!'? for i = 3:2:11 fprintf('Hello, world!\n'); end
5
What is the median of the following array? [2 6 4 7 2 8]
5
What is the output of >> c = 1;
c. output suppressed
What will be the result of evaluating the following MATLAB expression? char('Jgnnq'-2)
'Hello'
What is the result of evaluating the below MATLAB expression? char('RnldSdws'+1)
'SomeText'
>> a = 4; >> b = 5; >> c = true; What does the expression below evaluate to? >> c && ~ (b >= a)
0
>> x = 10 == 9; What is the value of x?
0
How many times will the following MATLAB script print 'Hello, world!'? x = 10; while x < 10 fprintf('Hello world\n'); x = x + 2; end
0
Analyze the following script x = 3; y = 1; if x > 3 y = x; end What is the value of y once the script finishes?
1
Identify the following statistical functions as measures of central tendency, statistical dispersion, or bivariate relation 1. mean 2 standard deviation 3 regression coefficients 4 median 5 mode 6 range
1. central tendency 2. statistical dispersion 3. bivariate relation 4. central tendency 5. central tendency 6. statistical dispersion
Consider the below MATLAB script: a = [2 4 6 -7 9 2 3 4 9 6]; b = a(3:6); x = 0; for i = 1:1:4 x = x + b(i); end What is the value of x when it finishes executing?
10
Consider the below MATLAB script: x = [1 2 3 4; 4 1 2 3; 3 4 1 2; 2 3 4 1]; y = 0; for i = 1:1:4 y = y + x(i, 2); end What is the value of y when it finishes executing?
10
Consider the below MATLAB script: y = -999; stayInLoop = true while stayInLoop == true x = input('Enter a positive integer, or -1 to quit'); if x == -1 stayInLoop = false; elseif x > y y = x; end end What will the value of y be when it finishes executing if the user makes the following inputs in order: 3, 10, 5, -1?
10
You have the following script: x = 7 y = 3 if x < 7 y = 17 elseif x == 23 y = 9 else y = 10 x = 5 end What is the value of y after the script completes?
10
How many times will the following MATLAB script print 'Hello, world!'? for i = 1:0.2:3 fprintf('Hello, world!\n'); end
11
Consider the below MATLAB script: x = [1 2 3 5; 6 1 2 3; 3 7 1 2; 2 3 8 1]; y = 0; for i = 1:1:4 y = y + x(2, i); end What is the value of y when it finishes executing?
12
Consider the following MATLAB script: a = [1 2 3 4 5]; b = [3 9 8 7 2]; c = a(3) + b(2); What is the value of c when it finishes executing?
12
Consider the below MATLAB script: hours = input('Please enter your hours worked per week: '); if hours >= 15 if hours >= 36 fprintf('You are eligible for vacation time and overtime pay\n') else fprintf('You are eligible for vacation time but not overtime pay\n'); end elseif hours > 0 fprintf('You are eligible for neither vacation time nor overtime pay\n'); else fprintf('You are eligible for $0\n'); end From the choices below, select the input that will result in the script printing: You are eligible for neither vacation time nor overtime pay
14
How many times will the following MATLAB script print 'Hello, world!'? for i = 1:1:5 fprint('Hello, world!\n'); for j = 1:1:5 if rem(j,2) == 0 fprintf('Hello, world\n'); end end end
15
How many integer numbers can be represented with 4 bit pattern?
16
Consider the below MATLAB script: x = [1 2 3 4 5]; y = 0; for i = 1:2:length(x) y = y + (2 * x(i)); end What is the value of y when the script finishes executing
18
Analyze the following script number = -10; value = 2; x = 0; if number > 0 if value == 2; x = 1; end else if value == 2; x = 2; end end What is the value of x once the script finishes?
2
Consider the below MATLAB script: a = [5 4 3 2 1]; b = [-1 -1 2 -1 -1]; c = a .* b; d = c(2) + c(3); What is the value of d when it finishes executing
2
Consider the below MATLAB script: arr = [1 10 -2 1 5 6 1 10]; x = input('Enter a number: '); z = 0 for i = 1:1:length(arr) if x < arr(i) && arr(i) ~= 10 z = z + 1; end end What will the value of z be when the loop finishes executing if the user inputs 1 at the prompt?
2
Consider the below MATLAB script: y = 999999999; stayInLoop = true; while stayInLoop == true x = input('Enter a positive integer, or -1 to quit: '); if x == -1 stayInLoop = false; elseif x < y y = x; end end What will the value of y be when it finishes executing if the user makes the following inputs in order: 6, 3, 5, 2, -1?
2
Consider the following MATLAB script: d = 0 for i = 1:1:12 if i > 10 d = d + 1; end end What is the value of d when it finishes executing?
2
a = [16 19 21 11 24 15 21 22 13 21]; for i = 1:length(a) if a(i) > 21 fprintf('Busted!\n'); elseif a(i) == 21 fprintf('Blackjack!\n'); end end When this is executed, how many times will 'Busted!' be printed to the screen?
2
i = 1; tot = 0; for a = 0:0.2:0.4 for j = 1:1:2 tot = i*j; end end The value of tot at the end of the script is?
2
Consider the following MATLAB script: a = [10, 2, -6, 9, 5]; b = length(a); c = 0; for i = 1:1:b c = c + a(i); end What is the value of c when it finishes executing?
20
What will be the value of x after the following MATLAB script finishes executing? a = 3; b = 4; c = a + 2; x = b * c;
20
Consider the below MATLAB script: a = [1 2 3 4]; b = [2 0 2 0]; c = [3 3 3 3]; d = a .* b + c; x = d(2); What is the value of x when it finishes executing?
3
Consider the below MATLAB script: arr1 = [1 2 3 4]; arr2 = 3 * arr1; for i = 1:1:arr2(1) fprintf('Hello, world!\n'); end How many times will this script print Hello, world!
3
Consider the following MATLAB script: a = -1; b = 3; while a > 0 b = b * 2; end What is the value of b when it finishes executing?
3
How many times will the following MATLAB script print "Hello, world!"? x = 2; while x < 7 fprintf('Hello, world!\n'); x = x + 2; end
3
What will be the value of j after the following script finishes executing? j = 1 for i = 1:1:10 j = i * 3; end
30
Consider the below MATLAB script: arr = [5 2 9 8 7]; i = 1; while arr(i) ~= 8 i = i + 1; end What is the value of i when it finishes executing?
4
Consider the following MATLAB script: a = [1 2 3 4]; b = [2 2 2 3]; c = a .* b; d = c(2); What value will d have when it finishes executing?
4
How many times will the following MATLAB script print 'Hello, world!'? a = 1 while a < 3 fprintf('Hello, world\n'); a = a + 0.5; end
4
What is the value of x when the following MATLAB script finishes executing? a = 5; b = 2; c = a - 3; x = b * c;
4
i = 1; j = 2; matrix = [1 2 3; 4 5 6; 7 8 9]; a = matrix(j,i); What is the value of a?
4
Consider the below MATLAB script: i = 1; tot = 0; for b = 0:0.2:0.4 for j = 0:2:4 tot = j + b end end What is the value of tot when it finishes executing?
4.4
You have the following script: x = 17 y = 45 z = 9 if x > 9 && z == 9 y = 23 else y = 45 end if y > 29 z = 10 else x = 33 end y = z + x What is the value of y after the script completes?
42
Consider the below MATLAB script (assume load_some_kind_of_data is a valid and properly defined MATLAB function): [X, Y, Z] = load_some_kind_of_data() v = -2:1:2; contour(X, Y, Z, v, 'k', 'Fill', 'off'); Assuming the range of values in Z vary between -3 and 3, how many different contour levels will be plotted?
5
Consider the following MATLAB script: y = 0 for i = 1:1:10 for j = 2:2:10 if i == j y = y + 1 end end end What is the value of y when the script finishes executing?
5
How many times will the following MATLAB script print 'Hello, world!'? for i = 2:2:10 for j = 1:1:5 if j == 2 fprintf('Hello, world!\n'); end end end
5
Consider the following MATLAB function: function x = afun(y) x = 2 * y + 1; end Now, further consider the following short script that invokes the above function: clear all; b = afun(3) + afun(-1); What is the value of b when this script finishes executing?
6
Consider the following MATLAB script: for i = 0:3:15 fprintf('Hello world!\n'); end How many times will "Hello world!" be printed when it is executed?
6
Consider the following MATLAB script: n = 24; c = 0; for i = 2:1:12 if rem(n, i) == 0 c = c + 1; end end What will be the value of c when it finishes executing?
6
How many times will the following script print "Hello, world!"? for i = 1:3:16 fprintf('Hello, world!'); end
6
for i = 0:4:20 j = 1; end How many iterations does the code perform?
6
What is the value of z when the following MATLAB script finishes executing? a = 3; b = 4; z = a + b; if b < 4 z = 5; end
7
Consider the below MATLAB script: x = [2 -6 4 5 7 -8]; y = 0; for i = 1:1:length(x) y = y + (x(i) * 2); end What is the value of y when it finishes executing?
8
Consider the following MATLAB script: a = 5; b = 2; for i = 1:2:a b = b + 2; end What is the value of b when it finishes executing?
8
Consider the following MATLAB script: x = 5; y = 7; if y <= 7 x = x + 3; end What is the value of x when it finishes executing?
8
You have the following MATLAB script: a = 1 b = 1 n = 6 for i = 3:1:n temp = a; a = a + b; b = temp; end What will be the value of a after it completes?
8
Consider the below MATLAB function: function [x] = fooBarBat(y) x = 3 * y; end Now, consider the below MATLAB script: a = 1; b = 2; c = fooBarBat(a) + fooBarBat(b); What is the value of c when this script finishes executing?
9
Consider the below MATLAB script (assume load_some_kind_of_data is a valid and properly defined MATLAB function): [X, Y, Z] = load_some_kind_of_data() v = -2:0.5:2; contour(X, Y, Z, v, 'k', 'Fill', 'off'); Assuming the range of values in Z vary between -3 and 3, how many different contour levels will be plotted?
9
Consider the below MATLAB script: x = [10 15 5 20 3]; y = [7 3 5 10 2]; z = x(4) * 0.1 + y(1); What is the value of z when it finishes executing?
9
Consider the following short MATLAB script: a = [10 20 30; 3 7 9; 8 6 12]; b = a(2,3); What is the value of b when it finishes executing?
9
>> whos x; What does this command do?
The whos command provides a text output in the command window that lists the name, size, and class (data-type) of the variable
What is the output of the following MATLAB command? >> a = 3 + 5
a = 8
Consider the following short MATLAB script: a = [1 2 3; 4 5 6; 7 8 9]; b = a(1:end, 3); What will be the value of b when it finishes executing?
[3; 6; 9]
Consider the following MATLAB script: a = [2 4 6 8 9 23 17]; b = a(3:5); What is the value of b when it finishes executing?
[6 8 9]
Consider the below MATLAB function: function [result] = lazy_cube(a) result = a * a * a; end Now consider the following script that invokes the function: clear all; x = lazy_cube(17, 3); disp(x); This script will fail with an error message. Why? a. lazy_cube() expects 1 input argument but the script passes 2. b. The script references a variable that does not exist in the workspace. c. lazy_cube returns 2 output arguments, but the script only expects 1. d. lazy_cube is not a valid name for a MATLAB function. e. lazy_cube() contains an invalid MATLAB statement.
a
Assume you have the following two MATLAB arrays: a = [1 2 3 4 5]; b = [1 2 3 NaN 4 5]; For which of these two arrays (when passed as an argument) will the MATLAB mean() and nanmean() functions return the same value?
a only
Which of the following statements is true for MATLAB scripts (a) Scripts are text files with .m extensions (b) Executing a script is identical to manually copying the content of the script to the command window line by line. (c) Scripts automatically execute when opened with the MATLAB editor. (d) Variables defined during script execution are available in the Workspace window.
a, b, d are correct
Which of the following statements about for and while loops are correct? a. Some while loops never terminate. b. The number of iterations of a for loop is known at the entry point of the loop. c. The number of iterations of a while loop is known at the entry point of the loop. d. Any for loop can be expressed as a while loop. e. A mathematical sum can be computed with a for loop. f. Some for loops never terminate.
a, b, d, e
Which of the below operators perform elementwise operations on matrices? That is, when given two matrices (ie. two-dimensional arrays) having the same number of rows and columns as operands, each element of the resulting matrix has the value resulting from applying the operation to the corresponding elements of the operand matrices. An example elementwise addition is given below for reference: >> [1 2 3; 4 5 6] + [2 2 2; 3 3 3] ans = [3 4 5 7 8 9 a. .* b. ./ c. $ d. / e. * f. -
a, b, f
Which of the following are true about elseif blocks in MATLAB's if elseif else construct? a. You can nest another if statement within an elseif block b. If an if elseif else construct includes an elseif block an else block must also be included c. Any elseif blocks must be after the if block and before the else block, if present d. An if elseif else construct may include either 0 or 1 elseif blocks, but cannot include 2 or more e. An if elseif else construct may have any number of associated elseif blocks, including 0
a, e, c
a = [-1 1 -2 2 -3 3 -4 4]; x = 0; for i = 1:1:length(a) x = x + a(i); disp(a(i)); end At the end of the script, the following statements are correct. a. x is the sum of all elements in the array b. The script will throw an error. c. x = 0 d. The scrip evaluates the sum from 1 to 8 (corresponding to the number of elements in the array) e. Matlab will print the following in the command prompt -1 1 -2 2 -3 3 -4 4 f. x = 36
a, e, c
Which of the following are true about variables in MATLAB? a. Variables can store numeric, logical, or character values b. The name of a MATLAB variable can contain underscore characters c. Variables are set via statements of the form varname == value d. Any variable can be used before it has been assigned a value. e. Variables are set via statements of the form: varname = value
a,b,e
Assume you have a script with the following statement defining a matrix (2D array) and assigning it to a variable: a = [1 2 3 4 5 6; 7 8 9 10 11 12]; Which of the below statements are true? a. a(1,2) + a(2,1) == 9 b. a(2,1) == 2 c. a(2,1) == 7 d. size(a) returns [2 6] e. size(a) returns with an error message
a,c,d
Which of the following are pre-defined variables in MATLAB? Select one or more: a. inf b. kappa c. pi d. x e. nan f. e
a,c,e
What is the output of the following? >> a = 5 + 3;
a. output suppressed
Consider the below MATLAB function function [result] = add_stuff(a, b, c, d) result = a + b + c + d; end Now consider the following script that invokes the function: clear all; [x] = add_stuff(10, 2, 0) fprintf('The sum is %f\n', x); This script will fail with an error message. Why?
add_stuff() expects 4 input arguments, but the script only passes 3.
Which of the following are true about MATLAB scripts? a. Scripts have a .s file extension. b. Scripts can have at most ten lines of text. c. You can execute a script by clicking the "Run" button on the top ribbon in MATLAB. d. Scripts provide a mechanism for executing multiple statements at once. e. Variables previously defined in the workspace cannot be referenced by scripts.
c,d
Which of the following are NOT true about MATLAB scripts? (a) They can contain multiple statements (b) All statements must be terminated with a semicolon (c) They must contain exactly one fprintf statement (d) They can include variable assignment statements
b and c are true
Which of the following are valid MATLAB conditional expressions? a. a ~< 22 b. a == 3 c. a = 22 d. (a == 3) || (b <= 26) e. a ~= 22
b, d, e
Consider the following MATLAB script: x = 1889; sum = 0 while x ~= -1 x = input('Enter a number'); sum = sum + x; end Which of the following are true about this script? a. The loop will never terminate b. The loop will terminate at the start of the next iteration after the user inputs -1 c. The loop will never execute d. Running this code will result in a MATLAB error e. sum and x are both updated once per iteration
b, e
Consider the following short MATLAB script including a while loop: a = 5; b = 0; while expression if rem(a, 2) == 0 b = b + 1; end a = a + 2; end For which of the below values of expression will the loop never terminate? a. true b. a > 4 c. a ~= 6 d. b < 5 e. a ~= 7
b,c,d
Which of the following is true about variables? a. Variables are not case sensitive. b. Each variable has a unique data type. c. Variables are containers that holds either text or a number. d. Variables must be defined by the user prior to use, except for the built in variables pi, inf, and nan.
b,c,d
Which of the following are true about scripts in MATLAB? a. They can only use variables already defined in the Workspace. b. They can contain multiple statements. c. They require a different syntax than when entering the same statement in the Command Window. d. They are text files with a .m extension. e. They cannot use variables already defined in the Workspace. f. They can have names that include the # sign.
b,d
MATLAB's for construct has the following form: for i = start:step:stop # Do stuff end Which of the following statements about MATLAB's for construct are true? a. The loop can only execute a maximum of 100 iterations. b. The value of i will always be less than or equal to that of stop during loop execution. c. It is possible to set start and stop such that the loop never terminates. d. i will have a value less than that of start during the first iteration of the loop. e. 1.5 is a legal value for step.
b,e
A user is entering a command that includes a variable that has not been defined. What will MATLAB do? a. MATLAB will generate a log file 'messages.txt'. The log file contains a detailed description of the error. b. MATLAB will ignore the command and return to the input prompt. c. MATLAB will print an error message in red 'Undefined function or variable' and return to the input prompt. d. MATLAB will exit.
c
For which of the below possible values of expression will this while loop never terminate? a = 10; b = 3; while expression a = a + 1; end a. a <= 13 b. b == 3 c. 1 d. b < 3 e. a > 5
c, b, e
The MATLAB if elseif else construct is if expression statements elseif expression statements else statements end Which of the following statements is true about the if then else construct in MATLAB? a. There can only be one line of code between the each of the blocks. b. The else ... end block is mandatory. c. The expression is one that reduces to a logical value (true/false). d. If statements can be nested. e. The else ... end block is optional.
e, c, d
matrix = [-10 -1 3 4;2 3 0 -10; 1 2 3 4]; Which of these expressions evaluate to row vector (e.g. [2, 3, 0]) a. >> matrix([1,3,4]) + matrix(3,1:3) b. >> matrix(2:3, 1) c. >> matrix(3,4) .* matrix(3,: ) d. >> matrix(1,1:2) ./ matrix(1,2:3) e. >> matrix(:,1:2).*matrix(:,2:3) f. >> matrix(1,1:2) * matrix(1,2:3) g. >> matrix(2,: ) h. >> matrix(4, 1:3)
g, f, c, a
What is the value of f? >> x = 4; >> f = (x + 4)/0;
inf
What will the following MATLAB script print to the screen? temperature = 72; if temperature >= 90 fprintf('It is hot outside\n'); elseif temperature >= 70 && temperature < 90 fprintf('It is warm outside\n'); elseif temperature >= 50 && temperature < 70 fprintf('It is mild outside\n'); else fprintf('It is cold outside\n'); end
it Is warm outside
What is the output of the following set of statements? >> a = 2.45; >> b = 2 * (a - 1.20); >> fprintf('Measured rainfall was %4.2f inches\n', b);
measured rainfall was 2.50 inches
What is the output of >> my_string = 'Hello World'
my_string = Hello World
Analyze the following script. cloud_cover = -0.1; if cloud_cover > 0 && cloud_cover <= 0.3 fprintf('Sky condition is sunny\n'); elseif cloud_cover > 0.3 && cloud_cover <= 0.7 fprintf('Sky condition is partly cloudy\n'); elseif cloud_cover > 0.7 && cloud_cover <= 1 fprintf('Sky condition is cloudy\n'); end What is the output of the script? a. Sky condition is sunny b. Sky condition is partly cloudy c. Sky condition is cloudy d. None of the above
none of the above
Consider the following matrix: x = [3 3 3; 4 5 6]; Which of the below expressions utilizing matrix x evaluates to 2?
size(x,1)
You are generating a multipanel figure with 2 rows and 3 columns. You wish to visualize a function in the bottom left panel. Which of the below invocations of subplot() should immediately precede your visualization code?
subplot(2,3,4);
Consider the following MATLAB script: j = 0; for i = 1:1:5 j = i - 1; end sum = 0; for k = 0:1:j sum = sum + k; end fprintf('The sum is %i\n', sum); What will this script print to the screen?
the sum is 10
What is the output of the following set of expressions? >> c = 4; >> d = 5; >> clear d; >> c*d
undefined function or variable 'd'
You have the following script a = 8; b = 2; if a > b y = a*b; elseif a == b y = a; else y = b; end y = a; What is the value of y once the script finishes?
y=8