Solutions** 11 December 2008 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? b. x = 4; y = 5; 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? b. 360/(2*pi) 3. Consider the row of numbers generated by the command >> x = 1:1:11; % How many numbers are in the list? c. 11 4. Consider the row of numbers generated by the command >> x = 1:2:11; % How many numbers are in the list? b. 6 5. Plot the sine of x from 0 to pi. To do this, which command do you enter? b. x = 0:2*pi/100:pi; plot(x,sin(x)) 6. Which of the following names are valid, variable names? c. watt 7. Does MATLAB recognize the difference between a and A? a. Yes 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; b. 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? b. 2 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 11. If a = 1 2 3 4 5 6 then which of the following is the result of a' ? 1 4 2 5 3 6 12. If a = 1 2 3 4 5 6 then what command extracts the second column of matrix a? a(:,2) 13. What does the command [a, b] = max(x) do? Finds the maximum value and its position in vector x. 14. Which of the following blocks of code does not plot y=10x^2 and y = x^3 on the same figure? figure (1) plot( x, 10*x.^2); figure(2) plot(x, x.^3); 15. Which of the following lines of code plots y versus x with a red line? plot(x, y, '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'); 17. The hold on command allows the user to plot more than one set of data on a single figure. True 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,9) 19. The command pie(z) creates a pie chart of z. True 21. Which of the following commands plots r = sin(theta) in polar coordinates? polar(theta,r); 24. Any variable in the workspace can be accessed by a user-defined function. False 25. Which of the following is NOT a valid name for a function? 52 Card Pickup *** update from lecture *** 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) 27. A function must return numerical output. False 28. In the command fprintf within the string statement what operator executes a linefeed? \n 29. To display formatted output in the command window, we used the plot command. False 30. Which one of the following is a valid placeholder in an fprintf statement %d 31. The input command can only take text as input. False 32. Which of the following commands will ask the user to input a radius and store it in a variable called radius? 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 b. (a+b)< (b+a) % False c. ~c&b % False d. (b-a)==d % True e. abs(b-a) == abs(a-b) % True f. isnumeric(c) % False g. isnumeric(d) % True h. isempty(a) % False i. c == d % True j. (a|b)&c % True 34. Loops are MATLAB constructs that allow a set of commands to be executed multiple times. True 35. A for loop repeats: A set number of times known before the loop is executed 36. The vector may be used to increment a for loop by using the following construct: first : increment : last 37. How many times will a for loop starting with for = 1:3:19 execute (or repeat)? 7 38. A for loop construct must end with an end statement. True 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] 40. While loops repeat a block of commands as long as a logical expression is false. False 41. While loops can be used to ensure that the user inputs the proper type of data. True 42. Once the logical expression at the start of a while loop is false, the statements after the end statement are executed. True 43. How many times will the following loop repeat? i = 2; while i <= 16; i = i+2; end 8 times 44. The following loop while text(x); x = x+1; end will repeat until forever until test(x) is false 45. The following loop will repeat how many times? n = 1; while n < 5 n = n - 1; end infinite.