Sample Questions for CS205 Final Exam. No programs are to be written, but you are asked to recognize valid code. You are also asked to select the valid output from code fragments. 1. Create two variables by assigning 4 and 5 to x and y. Assign the value of x + y to the variable z. What two statements do this correctly? a. 4 = x; 5 = y; x+y = z b. x = 4; y = 5; z = x+y c. x = 4; 5 = y; z = x+y 2. To convert angles in radians to angles in degrees, which factor do you need to multiply radians to covert to degrees correctly? a. 2*pi/360 b. 360/(2*pi) c. 180/(2*pi) 3. Consider the row of numbers generated by the command >> x = 1:1:11; % How many numbers are in the list? a. 10 b. 12 c. 11 4. Consider the row of numbers generated by the command >> x = 1:2:11; % How many numbers are in the list? a. 5 b. 6 c. 7 5. Plot the sine of x from 0 to pi. To do this, which command do you enter? a. x = 0:2*pi/100:2*pi; plot(x,sin(x)) b. x = 0:2*pi/100:pi; plot(x,sin(x)) c. x = pi:2*pi/100:2*pi; plot(x,sin(x)) 6. Which of the following names are valid, variable names? a. pay-plan b. 22d c. watt 7. Does MATLAB recognize the difference between a and A? a. Yes b. No 8. If you clear and assign the variables as shown and then execute who, what variables are in the work space? >> clear; a = 5; Bab = 3; a. a ans Bab b. a Bab c. A Bab 9. Create an array by executing the following command: >> A = [ [1;2;3] , [4;5;6] , [7;8;9]] >> A(2) % is equal to what number in the array? a. 4 b. 2 c. 5 10. Consider the array created by the following command: >> A = [ [1;2;3] , [4;5;6] , [7;8;9]] >> A = A' >> A(2) % is equal to what number in the array? a. 4 b. 2 11. If a = 1 2 3 4 5 6 then which of the following is the result of a' ? 6 5 4 3 2 1 6 5 4 3 2 1 1 4 2 5 3 6 4 1 5 2 6 3 12. If a = 1 2 3 4 5 6 then what command extracts the second column of matrix a? a(:,2) a(3,2) a(2,:) a(;,2) a(end,:) a(2,;) 13. What does the command [a, b] = max(x) do? Finds the maximum value in vectors a and b. Finds the maximum value and its position in vector x. Finds the position of the maximum value in vectors a and b. Finds the maximum value and minimum value. Forms an a by b matrix of maximum values. 14. Which of the following blocks of code does not plot y=10x^2 and y = x^3 on the same figure? plot(x, 10.^2, x, x.^3); hold on; plot(x, 10*x.^2); plot(x, x.^3); hold off; figure (1) plot( x, 10*x.^2); figure(2) plot(x, x.^3); figure(1) plot(x, 10*x.^2, x, x.^3); 15. Which of the following lines of code plots y versus x with a red line? plot('r', x, y); plot(x, 'r', y); plot(x, y, 'r'); plot(y, x, 'r'); 16. Which of the following commands plots y = x^5 with a dashed line, triangle right markers, and in green? plot(x, x.^5, '-->g'); plot(x, x.^5, 'drtg'); plot(x, x.^5, 'dash r-tri green'); plot(x, x.^5, '-g>'); 17. The hold on command allows the user to plot more than one set of data on a single figure. True or False. 18. Which one of the following commands plots a graph in the lower right corners of a 3 x 3 grid of graphs? subplot(3,3,1) subplot(3,3,3) subplot(3,3,7) subplot(3,3,9) 19. The command pie(z) creates a pie chart of z. True of False 20. The built-in command logxlogy creates a plot with logarithmic scaling on both the x and the y axes. True or False 21. Which of the following commands plots r = sin(theta) in polar coordinates? polar(theta,r); polar(r,theta) plot(theta,r) plot(r,theta) 22. The polyfit function with degree 3 selected, produces a cubic model for the data x =[1,4,6,9,11,14,15] , y = [-2,175,613,2110,3878,8045,9910]. It is given by the formula y = 3x^3 - x^2 + x - 5. True or False 23. To evaluate the function y = 3x^3 - x^2 + x - 5 over a range of x = 1:100, with coef = [3, -1, -5], which command can you use? polyval(coef, x) model(coef, x) polyfit(coef, x) linereg(coef, x) 24. Any variable in the workspace can be accessed by a user-defined function. True or False 25. Which of the following is NOT a valid name for a function? Poker Blackjack Solitare 52 Card Pickup 26. The first line of a function that takes as input mass and velocity and outputs kinetic energy would resemble which of the following: function energy = kinetic_energy(mass,velocity) function[mass,velocity] = kinetic_energy(energy) kinetic_energy = function(mass,velocity) [mass,velocity] = function(kinetic_energy) 27. A function must return numerical output. True or False 28. In the command fprintf within the string statement what operator executes a linefeed? \n \l \nl \new \new_line 29. To display formatted output in the command window, we used the plot command. True or False 30. Which one of the following is a valid placeholder in an fprintf statement %d %n %p %r %v 31. The input command can only take text as input. True or False 32. Which of the following commands will ask the user to input a radius and store it in a variable called radius? input=radius('Enter a radius: ') radius=('Enter a radius') radius=input('Enter a radius') input(radius)=('Enter a radius: ') input=('Enter a radius: ') 33. Consider the following assignment of variables: a=3; b=4; c=true; d=1; If the following commands are executed after the assignment of variables what are their truth values? In other words are they True(1) or False(0)? a. a|b % True or False b. (a+b)< (b+a) % True or False c. ~c&b % True or False d. (b-a)==d % True or False e. abs(b-a) == abs(a-b) % True or False f. isnumeric(c) % True or False g. isnumeric(d) % True or False h. isempty(a) % True or False i. c == d % True or False j. (a|b)&c % True or False 34. Loops are MATLAB constructs that allow a set of commands to be executed multiple times. True or False 35. A for loop repeats: Forever Until the user stops the loop A set number of times known before the loop is executed An unknown number of times A set number of times unknown before the loop is executed 36. The vector may be used to increment a for loop by using the following construct: first : increment : last increment : first : last last : increment : first increment : last : first first : last : increment 37. How many times will a for loop starting with for = 1:3:19 execute (or repeat)? 0 3 6 7 19 38. A for loop construct must end with an end statement. True or False 39. After the execution of the following loop, what will be the values of the elements of vector b? a=[3 7 1 9 11]; for i=1:1:5 b(i)=a(i)^2 + i; end b=[10 51 4 85 126] b=[9 49 1 81 121] b=[3 7 1 9 11] b=[1 2 3 4 5] b=[4 9 4 13 16] 40. While loops repeat a block of commands as long as a logical expression is false. True or False 41. While loops can be used to ensure that the user inputs the proper type of data. True or False 42. Once the logical expression at the start of a while loop is false, the statements after the end statement are executed. True or False 43. How many times will the following loop repeat? i = 2; while i <= 16; i = i+2; end 0 times 2 times 8 times 16 times infinite 44. The following loop while text(x); x = x+1; end will repeat until forever until test(x) is false until test(x) is true 0 times It is impossible to tell. 45. The following loop will repeat how many times? n = 1; while n < 5 n = n - 1; end Pick an answer: 0, 1, 4, 5, or infinite.