Intro to Matlab Quizzes
What is the starting index of an array in MATLAB?
✅1
Which of the following if-statement prints the message "YAY!" if x is a number between 1 and 10 inclusive (inclusive means the logical condition must be true if x = 1 or x = 10), prints the message "HOORAY!" if x is a number between 3 and 7 exclusive (exclusive means the logical condition must be false if x = 3 or x = 7), or prints the message "Phooey..." for any other case? Note: If x is a number between 3 and 7, "HOORAY" should be printed instead of "YAY!".
✅ if x > 3 && x < 7 disp("HOORAY ! "); elseif x >= 1 && x <= 10 disp("YAY ! "); else disp("Phooey . . . "); end
Which of the following is a string?
✅"Hello world!"
Construct a statement that will determine whether a value is between 30 and 70, including the value 70 but not 30.
✅(x > 30) & (x <= 70)
Read the names of the variables and functions carefully. You may assume the script and/or functions are in the working directory and visible in the Command Window. performCalculation.m: function ret = performCalculation(a, b, c) ret = (a + b) * c; end
✅-20
Based on MATLAB's precedence rules, what does 3 - 2 * 4^2 + 10 / 5 * (4 - 5) evaluate as?
✅-31
Consider the following code: i = 5; while i < 5 i = i + 1; end How many times does this while-loop iterate?
✅0
Consider the following function: function ret = func3(n) ret = 0; switch n case 1 ret = 10; case 2 ret = 20; end end What is the return value of func3(3)?
✅0
How many output values does disp return?
✅0
Consider the following code: for i = 10:15 disp("In a loop!"); if i == 12 continue; else break; end disp("In a loop!"); end How many times does the message "In a loop!" gets printed?
✅1
How many input arguments does disp take?
✅1
Consider the following code: x = 2; y = 1; while (x - y) > 0.001 x = (x + y)/2; y = 2/x; end The loop eventually terminates when x is an approximation of the square root of 2 within 0.001 accuracy. What is the value of x after just 1 iteration of the loop?
✅1.5
Consider the following function: function ret = func2(n) if n <= 15 ret = n * 2; else ret = n / 2; end end What is the return value of func2(21)?
✅10.5
Consider the following code: for i = 10:15 disp("In a loop!"); if i == 12 continue; end disp("In a loo!"); end How many ties does the message "In a loop!" gets printed?
✅11
What is the value of timeLapsed after executing the following commands? timeLapsed = [15, 18, 13, 20, 14, 20, 21, 22, 17]; timeLapsed(9:12) = 11;
✅15 18 13 20 14 20 21 22 11 11 11 11
Consider the following code: x = 0; for i = 1:5 for j = 1:5 if (i + j) == 3 x = x + 1; end end end What is the value of x after the loop is executed?
✅2
Consider the following function: function ret = func4(n) ret = 0; switch n case 1 ret = 10; case 2 ret = 20; otherwise ret = 30; end end What is the return value of func4(2)?
✅20
Consider the following function: function ret = func1(n) ret = n; if n <= 15 ret = n * 2; end ret = ret + 1 end What is the return value of func1(10)?
✅21
Consider the following function: function ret = func1(n) ret = n; if n <= 15 ret = n * 2; end ret = ret + 1 end What is the return value of func1(21)?
✅22
Consider the following code: x = 0; for i = 1:5 for j = 1:5 x = x + 1; end end What is the value of x after the loop is executed?
✅25
Which of the following MATLAB commands is the correct representation of (3 - 2 * 4^2 + 10) / (5 * (4 - 5))?
✅3 - 2 * 4^2 + 10) / (5 * (4 - 5))
Consider the following function: function ret = func2(n) if n <= 15 ret = n * 2; else ret = n / 2; end end What is the return value of func2(15)?
✅30
Consider the following function: function ret = func4(n) ret = 0; switch n case 1 ret = 10; case 2 ret = 20; otherwise ret = 30; end end What is the return value of func4(3)?
✅30
What are the dimensions of the following array? A = 2 2 5 6 4 3 8 2 4 1 2 6 8 5 3
✅3x5
How many input parameters does the function performCalculation have? function [x, y] = performCalculation(a, b, c, d) x = a + b; y = c + d; end
✅4
Consider the following code: i = 1; while i < 50 i = i * 2; end How many times does this while-loop iterate?
✅6
Given the following code, what is the value of colorPurple? colorRed = 1; colorBlue = colorRed + 4; colorPurple = colorRed + colorBlue
✅6
What is the size of the following array? v = [17; 3; 9; 19; 16; 20; 14]
✅7x1
What is the difference between a function and a script?
✅A function has an independent workspace whereas a script does not.
Which of the following statement is NOT true about a scalar variable?
✅A scalar variable has zero as one of its dimension.
Given theArray = [3, 5, 6, 10, 2, 1], which of the following commands create an array with 3rd and 4th elements of theArray.
✅All of the above
Read the names of the variables and functions carefully. You may assume the script and/or functions are in the working directory and visible in the Command Window. Suppose the function below is the only function defined in my_custom_function.m: function x = performCalculation(a) b = mod(a, 2); x = a + b; mod(m, n) is a built-in function that returns the remainder of the division m/n. What is the result of the function call performCalculation(20)?
✅An error message because the file name does not match the function name
What is the result of the following commands? x = [1:3.4; -6:3.5:0.5]; size(x)
✅Error
Suppose var is a logical variable. When does the following statement evaluate as true? (var & ~var) | xor(var, var)
✅Never
Read the names of the variables and functions carefully. You may assume the script and/or functions are in the working directory and visible in the Command Window. performCalculation.m: function ret = performCalculation(a, b, c) performCalculation = a + b + c; end Command Window: x = 2; y = 4; z = 1; x = x + y; result = performCalculation(x, y, z); What is the value of result?
✅No value is assigned due to an error: the function's output variable is not assigned any value.
Read the names of the variables and functions carefully. You may assume the script and/or functions are in the working directory and visible in the Command Window. scriptA.m: a = 2; b = 4; addTwo.m: function ret = addTwo(a, b) ret = a + b; end Command Window: scriptA; result = addTwo(a, b); ret = result + ret; What is the value of ret?
✅No value is assigned due to error.
Consider the following code: i = 5; while i > 0 i = i + 1; end How many times does this while-loop iterate?
✅This is an infinite loop.
Given an array A = [1 2 3; 4 5 6; 7 8 9], what is the result of the following command A(1:2,1:2)?
✅[1.2;4.5]
What is the value of c after executing the following commands? a = [3 0 8]; b = [5 0 2]; c = a .* b;
✅[15 0 16]
Given originalArray = [1, 10, 20, 40], what is the value of newArray after the following command? newArray = originalArray * 2;
✅[2, 20, 40, 80]
What command will construct an array that decreases by 3 starting from 50 and ending at 32?
✅[50:-3:32]
Read the names of the variables and functions carefully. You may assume the script and/or functions are in the working directory and visible in the Command Window. scriptA.m: a = 5; b = 11; performCalculation.m: function ret = performCalculation(a, b) scriptA; ret = a + b; end Command Window: a = 17; b = 16; a = a + 1; b = b + 1; result = performCalculation(a, b); What is the value of a, b, and result in the base workspace?
✅a = 18, b = 17, result = 16
Given an array a = 1:100, which of the following command adds one to all elements at odd indicies (not divisble by 2, e.g. 1, 3, 5, 7, ...)?
✅a(1:2:end) = a(1:2:100) + 1;
Which of the following examples concatenates column vectors trialOne, trialTwo and trialThree into a new column vector allTrials?
✅allTrials = [trialOne; trialTwo; trialThree]
What is the value of the array anotherArray = [3:2:10]; after executing anotherArray([2:3]) = [-1, -2];
✅anotherArray = 3 -1 -2 9
Consider the following array: dataTrial1 = [28.3, 29.1, 28.4, 29.0, 28.6, 28.7, 28.7, 28.8]; What is the value of the following command? dataTrial1(end-5:end-2)
✅ans = 28.4000 29.0000 28.6000 28.7000
Consider the following array: dataTrial1 = [28.3, 29.1, 28.4, 29.0, 28.6, 28.7, 28.7, 28.8]; What is the value of the following command? dataTrial1([6, 2, 7])
✅ans = 28.7000 29.1000 28.7000
Consider the following array: dataTrial1 = [28.3, 29.1, 28.4, 29.0, 28.6, 28.7, 28.7, 28.8]; What is the value of the following command? dataTrial1(2:4)
✅ans = 29.1000 28.4000 29.0000
sqrt is a built-in function that calculates the square root of its input argument What is the result of the following command? numArray = [4:9] .^2 sqrt(numArray)
✅ans = 4 5 6 7 8 9
What is the class of x after the following statement: x = 20;
✅double
Which of the following is a correctly written for-loop that repeats 5 times?
✅for i = 11:15 end
Which of the following is a built -in constant in MATLAB?
✅inf
What is the value of the array initialArray = [1, 2, 3, 4] after executing i = 7; initialArray(i) = 2;
✅initialArray = 1 2 3 4 0 0 2
Consider the following code: studentScores = [32, 59, 34, 64, 79, 84, 92, 23, 10, 38, 42, 75, 81, 93, 98, 85, 92]; lowScores = studentScores(studentScores < 65) What is the output?
✅lowScores = 32 59 34 64 23 10 38 42
What is the input argument for the function CalcScore? score = CalcScore(pointsEarned)
✅pointsEarned
What statement does NOT transpose the row array pounds = [21, 14, 19]; into a column array?
✅pounds_transpose = pounds"
Which of the following is a valid identifier?
✅sampleOne
Consider the following code: randomNumbers = [1, 12, 3, 8, 16, 4, 9, 5, 17, 22, 46, 8]; selectArray = [false, true, false, true, true, true, false, false, false, true, true, true]; selectNumbers = randomNumbers(selectArray) What is the output?
✅selectNumbers = 12 8 16 4 22 46 8
What is the correct way to access the fourth element of the array thisArray?
✅thisArray(4)
What is the result of the following command? u = 10:1;
✅u becomes an empty vector.
Which of the following is NOT an element-wise operation?
✅x * y
Consider the following code: x = 0; for i = 1:5 for j = 1:5 x = x + 1; end end Which of the following is an equivalent while-loop version of the above?
✅x = 0; i = 1; while i <= 5 j = 1: while j <= 5 x = x + 1; j = j + 1; end i = i + 1; end
Consider the following logical variables: a = true; b = false; c = false; Which of the following expressions evaluate as false?
✅xor(a, b) & c