Download (UCB) E7 Computer Programming Final Exam Guide 2024 and more Exams Programming Methodologies in PDF only on Docsity!
E7 Computer
Programming
Final Exam Guide
- What is the color of the resulting image if the following function is applied to all the pixels in the image, assuming pixel is a 3x1 array: function [pixel_new] = changeimage(pixel) pixel_new = [0 0 0; 0 1 0; 0 0 0] * pixel; end (a) red (b) green (c) blue (d) grey (e) None of these
- The MATLAB function eps returns the difference between the input number and the next closest number that can be exactly represented by floating point notation. Given that MATLAB’s default data type for numbers is a double precision float, what is eps(1.0)? TIP: Double precision variables use 1 bit for the sign, 52 bits for the fractional significand, and 11 bits for the exponent. (a) 2 −^10 (b) 2 −^11 (c) 2 −^51 (d) 2 −^52 (e) 2 −^1074
- What will the expression B = mytest(2,3,4) yield given the following function? fu nction [ A ] = m yte st(a ,b ,c , va ra rgin ) A = a + b; if ise m pty ( va ra rgin ) A = A * c; end end (a) B = 5 (b) B = 8 (c) B = 20 (d) MATLAB will throw an error because the variable name B and the output name A are incon- sistent. (e) MATLAB will throw an error because there are not enough inputs.
- You write the following code in order to grayscale an image stored in the variable img. When comparing with a classmate, you notice that your code takes much longer to run than theirs for a sample 800x600 resolution image. What is the time complexity for your algorithm? Note that functions called within the code may contribute to the time complexity. [M,N,~] = size(img); for k = 1: for i = 1:M for j = 1:N img(:,:,k) = mean(img,3); end end end (a) O(M^2 N^2 ) (b) O(M^2 ) (c) O(N^2 ) (d) O(MN) (e) O(kMN)
- The following code is executed successively in the command window: S ( 1 ) .N am e = ' UC B '; S ( 1 ) .Pop = 35700 ; S ( 2 ) .N am e = ' UM ich '; S ( 2 ) .Pop = 43400 ; S ( 3 ) .N am e = ' G Tech '; S ( 3 ) .Pop = 21500 ; What is the result when size([S.Name]) is now executed? (a) ans = [3 1] (b) ans = [1 13] (c) ans = [1 3] (d) ans = [1 19] (e) An error would be thrown.
- What is the value of myFn(5)? function out = myFn(n) if n > 2 out = myFn(n-2) + 2 * myFn(n-1); else out = 1; end end (a) ans = 5 (b) ans = 17 (c) ans = 21 (d) ans = 41 (e) ans = 99
- After writing the following code to make a simple plot, you now want to modify it to store a figure handle to this figure you are creating. Where in this code could you add the line, fig = figure;, in order to do this? plot ( - 10 : 10 ,( - 10 : 10 ). ^ 2 ); title (' Basic Q ua d ratic '); xla b el('x '); yla b el('y '); (a) Before the call to plot (b) Between the call to plot and the call to title (c) After all of these function calls (d) B or C (e) A, B, or C
- Consider the following lines of the code: x = - pi : pi/ 8 : pi; y 1 = sin ( x); y2 = cos( x); y3 = tan ( x) +1; figu re ( 1 ) plot(x , y 1 ,'r') plot(x , y 2 ,'g-- o '); hold on ; plot(x , y 3 ,'y* - '); After executing the code above, what does the final plot in figure 1 look like? (a) 3 lines (red solid line for y1, green dashed line with circle marker for y2 and yellow solid line with star marker for y3) (b) 2 lines (green dashed line with circle marker for y2 and yellow star markers for y3 without a line) (c) 2 lines (red solid line for y1 and green dashed line with circle marker for y2) (d) 2 lines (green dashed line with circle marker for y2 and yellow line with star markers for y3) (e) 1 line (yellow star markers for y3 with solid line)
- You are given a matrix A of size m-by-n and array b of size m-by-1. Given that rank([A,b]) == rank(A), what condition must be met so that there exists only one solution of x, where x is the solution to Ax = b? (a) rank(A) = m (b) rank(A) = n (c) rank(b) = m (d) rank(b) = n (e) None of the above
- How many iterations of Newton’s Method will it take to converge to within 0.0001 given f ( x ) = 4_._ 71 x − 6_._ 32 with initial guess x 0 = 1? (a) 1 (b) 2 (c) 3 (d) 4 (e) > 4
- You are given a set of data points of length n , separated into two variables, x and y. Both vectors have size 1 - by-n. How do you set up matrix A and vector beta such that running coeff = A \ beta will give you the coefficients a , b and c of a linear regression that fits the following equation: y = a + b sin( x )+ c exp( x )? (a) A=[1,x,y]; beta=y (b) A=[ones(length(x),1),sin(x),exp(x)]; beta=y (c) A=[ones(length(x),1),sin(x),y]; beta=exp(x) (d) A=[ones(length(x),1),(sin(x))',(exp(x))']; beta=y' (e) A=[1,sin(x),exp(x)]; beta=y'
- Regression problems are defined by linear systems that: (a) Have infinitely many solutions (b) Have no solution (c) Are overdetermined (d) A and C (e) B and C
- Consider the code below: f = @(x) exp(x); a = fzero(f,0); which returns a = - 926. Is the variable, a, a true root of the function f? (a) No, there are no real roots to the given function (b) No, exp(a) approaches infinity (c) Yes, exp(a) approaches infinity (d) Yes, exp(a) approaches 0 (e) There is not enough information available
- Which of the following statements about matrix inverse is FALSE? (a) A singular matrix has no inverse (b) If a system of linear equations does not have a unique solution or has many solutions, pinv(A) can be used and it returns the pseudoinverse of matrix A. (c) A matrix is invertible if its determinant is not zero (d) x = A\b is always computed the same as x = inv(A)*b (e) If A is invertible, inv(A) will not throw an error
- Recall the Maclaurin series for five common functions: 1 1 − x = 1 + x + x^2 + x^3 + x^4 + ... x^2 ln(1 + x ) = x − 2! x^3 x^4
x^2 exp( x ) = 1 + x + 2! x^3 x^4
3! 4!
x^2 x^4 cos( x ) = 1 − + 2! 4! x^3 x^5 sin( x ) = x − + 3! 5! x^6 x^8 − + 6! 8! x^7 x^9 − + 7! 9!
Which function does the following code approximate? f = 0; for i = 0:N f = f + (-1)^i * x^(2i+1) / factorial(2i+1); end (a) f ( x ) = 1 / (1 − x ) (b) f ( x ) = ln( x ) (c) f ( x ) = exp( x ) (d) f ( x ) = cos( x ) (e) f ( x ) = sin( x )
- In addition to passing through each data point, what additional conditions are applied when setting up a cubic spline problem? (a) 1st and 2nd derivative matches with adjacent splines at each interior data point, as well as 1 additional condition for the system. (b) 1st and 2nd derivative matches with adjacent splines at each interior data point, as well as 2 additional conditions for the system. (c) 1st and 2nd derivative matches with adjacent splines at each interior data point, as well as 4 additional conditions for the system. (d) 1st, 2nd, and 3rd derivative matches with adjacent splines at each interior data point, as well as 2 additional conditions for the system. (e) 1st, 2nd, and 3rd derivative matches with adjacent splines at each interior data point, as well as 4 additional conditions for the system.
. . .
- Given the data x = [0 , 1 , 2] and y = [3 , 1 , 2], what is the Lagrange interpolation at x = 1_._ 5? (a) 0. (b) 1. (c) 1. (d) 1. (e) 2
- Which of the following is NOT true about Taylor series? (a) An infinite number of terms is always required to completely describe a given function. (b) Taylor series can be used to describe periodic functions like sine and cosine. (c) A linear approximation is the first two terms of a function’s Taylor series expansion. (d) A and B (e) A, B, and C
- Consider the following function f. What is the value of f ′(2) as estimated by first-order forward difference with spacing ∆ x = 1? f = @(x) x^2 - 2*x + 1; (a) 3 (b) 1 (c) 4 (d) 2 (e) 2.
- Consider the three following finite difference schemes, and assume uniform grid spacing (i.e. xi − xi − 1 = xi +1 − xi = ∆ x ) (note: the first expression is a fourth-order central scheme): . df.^ = dx. xk . df.^ = dx. xk . df.^ = dx. xk f ( xk − 2 ) − 8 f ( xk − 1 ) + 8 f ( xk +1) − f ( xk +2) 12( xk +1 − xk ) f ( xk +1) − f ( xk )) xk +1 − xk f ( xk +1) − f ( xk − 1 )) . xk +1 − xk − 1 In the log-log plot of the numerical error associated to those schemes vs. grid spacing ∆ x , what would be the approximate slope for the first, second and third scheme, respectively? (a) 1, 2, 3 (b) 4, 1, 2 (c) 4, 1, 1 (d) 4, 2, 1 (e) There wouldn’t be any linear trends.
- Which of the following statements regarding numerical differentiation is FALSE? (a) Central difference has a higher order of accuracy than forward or backward difference (b) Finite difference schemes contain numerical errors due to the approximation of derivatives (c) The central difference scheme is called “second order” because it has O ( h^2 ) time complexity (d) The difference between the finite difference and exact solutions changes with the size of the discretization step (e) Taylor series can be used to derive high-order finite difference formulas.
- Assume that at a specific time, a car has a velocity v ( t ), where t is time in seconds and ∆ t is the time interval. We want to calculate the acceleration a of the car using the car’s velocity. Which of the formulas below is a second-order approximation? (a) a ( t ) = [ v ( ti +1) − 2 v ( ti ) + v ( ti − 1 )] / ∆ t^2 (b) a ( t ) = [ v ( ti +1) − v ( ti )] / ∆ t (c) a ( t ) = [ v ( ti +1) − v ( ti − 1 )] / (2∆ t ) (d) a ( t ) = [ v ( ti − 2 ) − 8 v ( ti − 1 ) + 8 v ( ti +1) − v ( ti +2)] / (12∆ t ) (e) a ( t ) = [ v ( ti +1) − v ( ti − 1 )] / ∆ t
- Consider the following function for the next question: 1 + x f ( x ) = (1 + sin( xπ/ 2))^2
What is the trapezoidal approximation of the integral of f ( x ) from 0 to 2 with a step of ∆ x = 1? (a) 1 (b) 1. (c) 2 (d) 2. (e) 5
n +
- The area under a function, f , can be approximated using Simpson’s Rule: " h f ( x 0 ) + 4 3 n Σ− 1 i = 1 i odd ! f ( xi ) n Σ− 2
- 2 i = 2 i even ! f ( xi )
- f ( xn ) (3) Given the following code, what would you insert in place of ‘?’ below to solve for the area under the function? x=[-pi:pi/8:pi]; y=sin(x)+(2pi); h=pi/8; Area=h/3(y(?)+sum(4y(?))+sum(2y(?))+y(?)) (a) 0, 1:2:end-1, 1:2:end-2, end (b) 0, 1:2:end-1, 3:2:end-2, end (c) 1, 1:2:end-1, 1:2:end-2, end (d) 1, 2:2:end-1, 3:2:end-2, end (e) None of the above
- To numerically solve a generic ODE written as dy/dt = f ( t, y ), a 2nd-order Runge-Kutta predictor- corrector model can be used: Step 1 : y ∗^ = yn + f ( tn, yn )∆ t (^1) ∗ Step 2 : yn +1 = yn + ( f^ ( tn,^ yn )^ +^ f^ ( tn +1 ,^ yn +1))∆ t 2 This scheme is used on the equation dy = 2 y + 1_._ (4) dt What would be the value of y 2 , using ∆ t = 1, and the initial condition y 0 = 0? (a) 2 (b) 7 (c) 5 (d) 12 (e) 1
2
- Consider the standard decay ODE: dy = − k ∗ y. (5) dt When an explicit Euler’s method is applied to this ODE with time step ∆ t , and k > 0, what conditions will always lead to a numerically stable solution? (a) When k = 2 / ∆ t (b) When | 1 + k ∆ t | > 1 (c) When | 1 − k ∆ t | > 1 (d) When | 1 − k ∆ t | < 1 (e) None of these
- Based on the following figure, what is the midpoint rule approximation of ∫ (^10) F ( x ) dx? You can take ∆ x = 2. (a) 12 (b) 20 (c) 21 (d) 23 (e) 24
- To compute a left Riemann integral, the following code is written: 1 fu nction [ inte st] = le ftriem a n n ( xvec , yvec ) 2 ste p size = xvec ( 2 )- xvec ( 1 ); 3 % m issin g cod e 4 inte st= su m ( A ); 5 end You can assume that data xvec and yvec are the same length. What is the correct line of code that should appear in line 3? (a) A=yvec(2:end).stepsize (b) A=yvec(1:end-1).stepsize (c) A=(yvec(1:end-1)+yvec(2:end))./2.stepsize (d) A=yvec(k+1).stepsize (e) None of the above
- Consider the organization of data in object-oriented programming. If a button called myPushButton and a text field called ResultEditField were created in MATLAB’s appdesigner, which of the following lines of code can be written in myPushButton’s callback function to display the text 'Pushed' whenever the button is pressed? (a) app.ResultEditField = 'Pushed' (b) ResultEditField = 'Pushed' (c) app.ResultEditField.Value = myPushButton (d) app.ResultEditField.Value = 'Pushed' (e) None of the above
- A climate stabilization wedge, as defined by Pacala and Socolow (2004), is a carbon-mitigation strategy that yields: (a) The reduction of one Gt of carbon from the atmosphere over 50 years. (b) The reduction in emissions of one Gt of carbon/year after 50 years. (c) The reduction of one Gt of all pollutants from the atmosphere over 50 years. (d) The reduction of one Gt of emissions of all ozone depleting substances from the atmosphere over 50 years. (e) The colloquial name for a slice of cake eaten by climate scientists to calm their fears about climate change.
The next 3 questions refer to the following code: Imagine that this summer you have been hired by a structural engineering firm in New York City. Your first task as their esteemed Cal intern is to document all of the historic skyscrapers in down- town. You decide to utilize the skills you learned in E7 to help you, namely your object oriented programming skills. Consider the following class definition for data type: buildings, which can be used to construct a portfolio of the buildings you are scoping out: classdef buildings properties name year floors end methods function obj = buildings(inputArg1,inputArg2,inputArg3) if nargin == 3 obj.name = inputArg1; obj.year = inputArg2; obj.floors = inputArg3; end end function obj = method1(obj,arg) obj.name{end+1} = arg.bldg; obj.year(end+1) = arg.year; obj.floors(end+1) = arg.numfloor; end function obj = method2(obj,bldgArg,entryArg) obj.name{bldgArg} = entryArg.bldg; obj.year(bldgArg) = entryArg.year; obj.floors(bldgArg) = entryArg.numfloor; end function obj = method3(obj,bldgArg) obj.name(bldgArg) = []; obj.year(bldgArg) = []; obj.floors(bldgArg) = []; end end end
- Suppose you have an instance of buildings, called portfolio. How do method1 and method2 change portfolio, respectively? (a) Add an entry into portfolio, replace an entry in portfolio. (b) Replace an entry in portfolio, add an entry in portfolio. (c) Add an entry into portfolio, remove an entry from portfolio. (d) Remove an entry from portfolio, add an entry into portfolio. (e) None of the above
- You execute the following commands: N YC _ p o rtfolio = b uildin gs ({} ,[] ,[]); bld g 1 = struct(' bldg ',' C h rysle r ',' ye ar ' , 1930 ,' n um flo o r ' , 77 ); bld g 2 = struct(' bldg ',' Flatiro n ',' ye ar ' , 1903 ,' n um flo o r ' , 23 ); N YC _ p o rtfolio = N YC _ p o rtfolio.m eth o d 1 ( bldg 1 ); N YC _ p o rtfolio = N YC _ p o rtfolio.m eth o d 1 ( bldg 2 ); How many elements are in NYC portfolio.name? (a) NYC portfolio.name is empty. (b) 1 (c) 2 (d) An error occurs. (e) None of the above
- You execute the following commands, in addition to the commands executed in the previous problem: N YC _ p o rtfolio = N YC _ p o rtfolio .m eth o d 2 ( 1 , bldg 2 ); N YC _ p o rtfolio = N YC _ p o rtfolio .m eth o d 3 ( 1 ); What is the value of NYC portfolio.year? (a) 1903 (b) 1903 1903 (c) 1930 1903 (d) 1930 (e) An error occurs