2013F002/MidtermSample1
From CDS 130
Sample Midterm 1
Contents 
1. Binarytodecimal
Convert 00111
to decimal
Answer 

7 
2. Decimaltobinary
Convert 49 to binary
Answer  

Start out by guessing
etc. In table form, it is
where in the last row a 
3. Bytes per dollar
If a 300 GB hard drive costs $100 dollars and a 4.7 GB DVD disk costs $2 dollars, which is a better deal in terms of bytes per dollar? Show your work.
4. Logic Gates
In the image below, four NANDS are connected and three of the inputs are set to 1
. What are the values ofZ
and A
if B = 1 and Output = 1? For reference, the logic table associated with a NAND gate is shown.
 Z = 1, A = 0
 Z = 0, A = 0
 Z = 1, A = 1
 Z = 0, A = 1

Answer 

The correct answer is E. None of the above! To see this, try

5. Array Syntax
Which of the following (circle each one that applies)

is the same as entering A(1) = 1; A(2) = 1; A(3) = 3; 
Answer 

This A(1) = 1; A(2) = 1; A(3) = 3; is the same as A = [1,1,3] so

6. Matrix Syntax
Write a set of commands that will create the following matrix
1 2 3 1 2 3 1 3 3
Answer 

M = [1, 2, 3;1, 2, 3;1 3 3] or M = [1,2,3]; M = [M;M]; M = [M;1,3,3]; 
7. Iteration
clear; A(9) = 13; for i = [10,11,12] A(i) = A(i1)+37; end
What is A(12)
and A(1)
?
Answer 

clear; A(9) = 13; i = 10; A(i) = A(i1) + 37; i = 11; A(i) = A(i1) + 37; i = 12; A(i) = A(i1) + 37; Rewrite with numbers clear; A(9) = 13; i = 10; A(10) = A(9) + 37; i = 11; A(11) = A(10) + 37; i = 12; A(12) = A(11) + 37; or clear; A(9) = 13; i = 10; A(10) = 13 + 37; % Which is 50 i = 11; A(11) = A(10) + 37; % Which is 50 + 37 = 87 i = 12; A(12) = A(11) + 37; % Which is 87 + 37 = 124 note that we have only specified values for the elements 912 of the array. MATLAB assumes that elements 18 are zero by default. So, 
8. Interpreting a for loop
Which, if any, nested for loops will produce the matrix M
?
 A.
 B.
 C.
 D.
 All of the above.
 None of the above.
M = 1 1 1 2 2 2 3 3 3
A. for j = [1:3] for i = [1:3] M(i,j) = i; end end 
B. for i = [1:3] for j = [1:3] M(i,j) = i; end end 
C. for i = [0:2] for j = [0:2] M(i+1,j+1) = i+1; end end 
D. for j = [0:2] for i = [0:2] M(i+1,j+1) = i+1; end end 
Answer 


9. Nested for loops
for i = [1:3] for j = [1:3] Z(i,j) = 1.0; end end 
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3

1 2 3 1 2 3 1 2 3

1 2 3 2 4 6 3 6 9

10. Colormap I
Answer 

B. The second row of the colormap matrix 
Suppose that you have the following colormap matrix:
C = [1.0, 0.0, 0.0 ; 0.0, 1.0, 0.0 ; 0.0, 0.0, 1.0];
Which commands could be used to remove the color green
and replace it with the color represented by mixing128
drops of Red, Green, and Blue? Circle the best answer.
A. C(2,1) = 128; C(2,2) = 128; C(2,3) = 128; 
B. C(2,1) = 0.50; C(2,2) = 0.50; C(2,3) = 0.50; 
C. C(1,1) = 0.50; C(2,1) = 0.50; C(3,1) = 0.50; 
D. C(1,1) = 128; C(2,1) = 128; C(3,1) = 128; 
11. Colormap II
Suppose that you entered the following commands:
clear; counter = 1; for i = [1:10] for j = [1:10] M(i,j) = counter; counter = counter+1; end end for j = [1:100] C(j,1) = j/100; C(j,2) = 0; C(j,3) = 0; end imagesc(M) colormap(C)
Which is the correct image?
 A
 B
 C
 D
12. Colormap III
Comment 

The correct answer is A. (You may notice that if you enter the commands in MATLAB, the first and last squares won't be cut off as they happen to be in the image shown.) 
clear; C = [1 1 1;0 0 0]; for i = [1:10] for j = [1:10] M(i,i) = 1; % Option A. M(i,j) = 1; % Option B. M(i,1) = 1; % Option C. M(i,2) = 1; % Option C. M(1,j) = 1; % Option D. M(2,j) = 1; % Option D. end end imagesc(M) colormap(C) axis square; colorbar;
To produce the image shown, which line or lines should be kept in the program?

13. Replacement in an array
Given an array with 5
elements that are either 0
or 1
, write a program that implements the algorithm
Replace any0
with a1
.
Answer 

% Solution should work for any combination of 1s and 0s in this array. clear; A = [0,0,1,1,0] for i = [1:5] if (A(i) == 0) A(i) = 1; end end A 
14. Replacement in a matrix
Given a matrix with 5
rows and 5
columns, with elements that are either 0
or1
, write a program that implements the algorithm
Replace any0
with a1
.
Answer 

clear; A = [0,0,1,1,0]; A = [A;A;A;A;A] for j = [1:5] for i = [1:5] if (A(i,j) == 0) A(i,j) = 1; end end end A 