0% found this document useful (0 votes)
4 views

Updated CSC209 Midterm 2 exam model Answer

The document is a midterm exam for CSC209, consisting of multiple-choice questions and programming tasks related to MATLAB. It covers topics such as loops, conditional statements, logical operators, and plotting functions. The exam is divided into two parts, with specific instructions and point allocations for each question.

Uploaded by

ggh303995
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Updated CSC209 Midterm 2 exam model Answer

The document is a midterm exam for CSC209, consisting of multiple-choice questions and programming tasks related to MATLAB. It covers topics such as loops, conditional statements, logical operators, and plotting functions. The exam is divided into two parts, with specific instructions and point allocations for each question.

Uploaded by

ggh303995
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CSC209 Midterm -2 Exam (461) Fall 2024- 2025

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]

No The MATLAB Program The output


for i = -1:1 a. [-3 -2 -1]
i = i - 2; b. -3
1.
end c. -2
disp(i) d. -1

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

a=[1 0 0.5 0] a. ans = 0 0 0 0


6. b. ans = 1 0 1 0
logical(a) c. ans = 0 1 0 1
d. ans = 1 1 1 1

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

b. Assume a: eye(2,2),b: [2 -2; -22),c = ones(2,2), and d = l. Evaluate t]re


following expressions in the Truth rables for Logic operators below. (10 pts)

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

PART 2: Answer all questions (70 pts)

Ol:Answ er all q uestions (35 pts) [CLO(s): 2,1], [PI(s): l.l,l.2l


1- Write a MATLAB instruction to solve the following problems using a gg loop to:
a. Display the numbers from I to l0 (S pts)

lori=\;LO 2-
disp(i); 1-
end \

b. Find factoriai ofnumber 10. (10 pts)

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

x = input ('Enter value of x: ');


y = input ('Enter value of y: ');
if x>=o&&y>= o ¤)
o
fun=x +y; @
elseifx>=0&&y<0
;un=x+y^21 @
elseif x<O&& y>=O @)
fun=x^2+y;
else @
fun=x^2 *y"2;@
end O
disp (['The value of the function is ,, num2str(fun)]);

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

num = input(rEnter a number: ' );CD


if num < 0
b
end
if num >= 0
total = total + num; e
count = co + l;S
end

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

c. use MATLAB instruction to reproduce the following subplot according to the


relation between x and y, ifx = l:60, y=sinh) for all the sub figures.
[fS ptsl

-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

You might also like