Page 1 of 9
Faculty of Engineering
Engineering Software Fundamentals
Mid-Term Examination
Your name (LAST NAME, First Name):
ID No.:
Lab section # (circle one)
51
52
53
54
___________________________________________________________________________
• Time: 90 min
• This is a closed book closed notes exam. Please put away your cell phones.
• There are 25 multiple-choice questions; there are 8 pages in this exam (including this
one).
Page 2 of 9
Part I. Multiple choice questions
Page 3 of 9
1. Which one of the following commands will print as shown below (there is no empty line
before or between the two lines)?
I like
Matlab
a. fprintf('I like'), fprintf(' '), fprintf('Matlab')
b. fprintf('\nI like'), fprintf(' '), fprintf('Matlab\n')
c. disp('I like'), disp('Matlab')
d. disp('I like\nMatlab')
2. Assume the user input is 19. What will be the Matlab screen display after the following
command?
age=input('enter your age:');
a. enter your age:19
b. age =
19
c. enter your age:19
age =
19
d. enter your age:
19
3. function miles=kms_to_miles (kms), which one of the following is correct to call this
function, assume function input is correctly defined
a. y= kms_to_miles (kms)
b. miles= kms_to_miles (kms)
c. y= kms_to_miles (x)
d. all of the above
4. Suppose that an array named sensor contains data collected from 4 sensors, one sensor
in each of the 4 columns. Which one of the following commands is correct to extract the 1st
column into a column vector named s1?
a. s1=sensor(:,1)
b. s1=sensor(1,:)
c. s1=sensor(1)
d. none of the above
5. Which one of the following formats should be used to print an integer (inside fprintf)?
a. %d
b. %int
c. %s
d. %f
6. In terms of input/output,
function [ ] = Print_report_number(n)
means:
a. one input, one output
b. no input, one output
c. one input, no output
d. no input, no output
7. For
function [x y]=a_pm_b (a, b)
Page 4 of 9
which one of the following is the correct call
a. (x, y)=a_pm_b (a, b)
b. [x y]=a_pm_b (a, b)
c. [x y]=a_pm_b (a)
d. [x] =a_pm_b (a, b)
8. For
function [ ] = PrintHeader ( )
which one of the following is the correct call
a. Results= PrintHeader (10)
b. Results= PrintHeader ( )
c. PrintHeader
d. PrintHeader (10)
9. After executing the following code:
clear, clc
a = 2;
x =half1 (a);
a
function x=half1(a)
% this function will calculate a/2
% input: a
% output: x
a=a/2;
x=a;
end
The Matlab screen display will be:
a. a =
1
b. a =
2
c. an error message
d. a =
0
10. After executing the following code:
clear, clc
x = [2, 2, 2];
a = [2:2:6];
x =half2 (a);
a
function a=half2 (x)
% this function will calculate x/2
% input: x
% output: a
a=x/2;
end
Page 5 of 9
The Matlab screen display will be:
a. a =
2 4 6
b. a =
1 2 3
c. a =
2 2 2
d. a =
1 1 1
11. What is the value of x after execution of the following code?
x = 5;
y = 2;
if (x > 0)
x=7
elseif (y < 10)
x=9
end
a. 2
b. 5
c. 7
d. 9
e. 10
12. What is the output of the following program?
x=[1, 3, 9];
y=[2, 6, 8];
if (x<=y)
fprintf ('x<=y');
else
fprintf ('not all elements of x<=y');
end
a. not all elements of x<=y
d. 'not all elements of x<=y'
b. x<=y
e. An error message
c. 'x<=y'
13. What is the value of n after the execution of the following code?
n=0;
for(m=1:2)
for(k=4:-1:2)
n=n+1;
end
end
fprintf ('%d', n);
a. 2
b. 4
c. 6
d. 8
e.12
Page 6 of 9
14. How many lines will be printed after the execution of the following code?
for k=[7 1 6]
fprintf ('%d\n', k);
end
a. 0
b. 1
c. 2
d. 3
e.4
15. How many lines will be printed after the execution of the following code?
for k=[6:1:7]
fprintf ('%d\n', k);
end
a. 0
b. 1
c. 2
d. 3
e.4
16. What is the output of the following code?
for k=[7; 1; 6]
fprintf ('%d\n', k);
fprintf ('inside loop\n');
end
7
a. 1
6
inside loop
b. 7 1 6
c. 7
d. 14
inside loop
inside loop
inside loop
e. none of the above
Page 7 of 9
17. What will be printed after the execution of the following code?
a=1:3;
b=a+2;
c=a+4;
m=[ a' b' c'];
for k=m
disp(k);
end
a. 6
12
18
b. 9
12
15
c. 9 12 15
d. 6 12 18
e. none of the above
18. What is the value of k after the execution of the following code?
k=1;
p=1;
while(k<=3)
k=k+2;
p=p*2;
end
fprintf ('%d', k);
a. 1
b. 3
c. 5
d. 7
e. 9
19. What is the value of p after the execution of the following code?
k=5;
p=1;
while(k<=3)
k=k+2;
p=p*2;
end
fprintf ('%d', p);
a. 1
b. 2
c. 4
d. 8
e. 5
Page 8 of 9
Your name & student #:
Lab:
Part II. Matlab program (10 marks)
The existing part of the program shall not be changed in any form. Fill in blank or partial blank
lines with one command in each line. 1 mark each line.
first =1;
last= 100;
_________________________________________________________________
% 1. use input function to prompt a user to enter the increment, save the value in a variable
named inc using assignment
_________________________________________________________________
% 2. create an evenly spaced row vector tc of values from first to last in increments of inc,
_________________________________________________________________
% 3. call the user-defined function tempk (shown on the next page) to calculate temperatures in
Kelvin, and assign the results to tk
table= [________________]
% 4. to form a matrix table, with 1st row – vector tc, and the second row – vector tk
fprintf ( '%.2f\t\t%.2f \n', ______________________________________)
% 7. use fprintf to print table as shown bellow. No need to print a header
Table format and sample printout:
1.00
2.00
3.00
….
274.15
275.15
276.15
Page 9 of 9
function output=tempk (tc)
% this function calculate temperature in Kelvin
% input: tc (a vector)
% output: output (temperature in Kelvin)
% conversion equation: K=273.15+C
________________________________________________________________
% 9. calculate the temperature in Kelvin, assign the result to the output
________________________________________________________________
% 10. last line of a function
Your name & student #: