Updated CSC209 Midterm 2 exam model Answer
Updated CSC209 Midterm 2 exam model Answer
PART 1: Answer all questions (30 pts) [CLO(s): 1.3], [PI(s): 1.1]
P1. Select the best choice out of the options in the output of the program and fill
your correct answer in table 1. [20 Pts]
a. b=1
a=true b. b=2
2.
b=a+true c. b = true
d. b = error
k=0;i=0;
while(k<1 && i<1) a. i = 1 , k=1
k=k+1 b. i = 0 , k = 0
3.
i=i+1; c. i = 0 , k = 1
i=i-k d. i = 1 , k = 0
end
a. 0
b. 1
4. 'A' == 'a'
c. Inf
d. error
x=3;y=0.5;
if x >= 0 && y >= 0 a. fun = 3.5
b. fun = 3.500
5. fun = x + y c. fun = 3.5000
elseif x >= 0 && y < 0 d. error
fun = x + y^2
Page 1 of 6
/
CSC209 Midterm-2 Exam (461) Fall 2024- 2025
x='a';
switch (x)
a. c=5
case 'a' b. c = 44
7. c=44 c. c=x
otherwise d. error
c=5
end
x=0:3 a. a=0 3 0 9
y=x.^2 b. a=0 1 4 9
8.
plot(x,y) c. a=0 9 0 3
d. not exist
a= axis
x=3;
n=1;
y = 0;
if n==1 a. y=0
9. if x == 3 b. y=2
y = 4; c. y=4
end d. y=7
end
disp(y)
B = '123' a. C=1
10. b. C=0
C = ischar(B) c. C=0 0 0
d. C=1 1 1
Table 1: The Answer of the multiple choices (1 - 10) Each with 2 Marks
1 2 3 4 5
d b c a d
6 7 8 9 10
b b a c a
Page 2 of 6
/
CSC209 Midterm-2 Exam (461) Fall2024-2025
I 0 1 ,)-
a<c
1 0
2 b&a 10 2-
01
t a>=b&b>=a 0 0
0 0
4. c ll a > c error 2-
1 0
5 -d<=b 2-.
0 1
lori=\;LO 2-
disp(i); 1-
end \
1; )-
fact_of_10 =
fori=1:10 2-
fact_of_l0 = fact_of_l0 * i; L
end '2-
disp(['The factorial of 10 is: ', num2str(fact_of_i0)]) .L
Page 3 of 7
CSC2O9 Midterm-2 Exam (461) Fall}024-2025
c Write program with if statements to implement the function f(x,y) for user input
a
values ofx and y. t20 ptsl
xa,lt x>0andy>Q
/'(x, y) =
x+f x>0andy(0
l+v x(0andy>0
f+v' .r ( 0andy q Q
02: A nswer all o uestions (35 pts) [CLO(s): 2.2], [PI(s): t.t,t.z,l
a. use while and if statement to implement a MATLAB program that evaluate and
display the average of certain data entered by the user. [15 ptsl
Conditions:
l. The data should be entered by the user one by one
2. The entry data should be positive number only
3. The program will end the process if the user entered a negative number
total = 0; ,, - j
count = 0;- 61Q
disp('Ente r positive numbers (enter a negative number to finish
while true
):'); o
Page 4 of 7
CSC2O9 Midterm-2 Exam (461) Fall2024-202s
if count > 0 C
average = total I count; Q
disp(['Average of the numbers entered:' num2str(average)]);
else C
disp(rNo positive numbers were entered.r);
end
{t
b. Examine the following program and find the errors
[s pts]
x:3: v:5:
ifx<=a;'@
ifP:g' @
fun= x+y p
elseif
fun= x+y^2
end
a Missing end 2
O No condition stated for the elsi
Page 5 of 7
CSC209 Midterm-2 Exam (461) Fall2OZ4-2025
-1
0 20 40 60
x x
x x
20 40 0 20 40 60
x x
Clear
(,
clc
x=1:60;0
y=sin(x);e
subplot(3,2,1)
plot(x,y);
xlabel('x');
ylabel('y')
subplot(3,2,2) ca'
plot(x,y);
xlabel('x'); ->
abel('y')
subplot(3,2,3)
plot(x,y);
@
xlabel('x'); (9-
Page 6 of 7
CSC209 Midterm-2 Exam (461) Fall2024-2025
ylabel('y') o-
/')-
subplot(3,2,4) e >l
plot(x,y); A:>)
,t==
xlabel('x!); @
ylabe!('y') 67e,
subplot(3,2,5) o-\)
---:-
plot(x,y);
@
xlabel('x'); @
ylabel('y') 6e
subplot(3,2,6) 6\
plot(x,y); .6-.---.q
xlabel('x');
ylabel('y')
@
PaEe 7 of 7