Syntax - Level 1 (W2)
- Q1 -
It asks us to write these equations in MATLAB. First, we can eliminate the wrong ones, in MATLAB after an if
statement we use elseif
statement to add another condition so we can eliminate option A, B and C. We have D and E options left. There is only one different in these options, &&
and &
. Both are logical AND
operations. The &&
though, is a “short-circuit” operator.
From the MATLAB documentation: “They are short-circuit operators in that they evaluate their second operand only when the result is not fully determined by the first operand.”
For example:
A & B (A and B are evaluated)
A && B (B is only evaluated if A is true)
Correct answer is E
.
- Q2 -
x & y
performs a logical AND
of arrays x and y and returns an array containing elements set to either logical 1 (true) or logical 0 (false). An element of the output array is set to logical 1 (true) if both x and y contain a nonzero element at that same array location. Otherwise, the array element is set to 0.
~y = not(y)
returns a logical array of the same size as y. The array contains logical 1 (true) values where y is zero and logical 0 (false) values where y is nonzero. So in our question it gives us an array of full zero because given y has nonzero elements.
x&(~y)
returns a logical array of [0, 0, 0, 0, 0, 0] because we are taking their intersection. For this &
operator to return a nonzero number, we need nonzero numbers both from x and y.
Correct answer is C
- Q3 -
A polyline is a list of points, where line segments are drawn between consecutive points.
While plotting, we can use different styles to plot our graph. There are some shorcuts for colors such as r
is for red, g
is for green etc. Also there are some shortcuts for line styles such as -
is for solid line, --
is for dashed line, :
is for dotted line etc.
Correct answer is A
.
- Q4 -
From MATLAB documentation: clc
clears all the text from the Command Window, resulting in a clear screen.
Correct answer is A
- Q5 -
In this question, we were asked 10 sub-intervals. linspace function creates n points between x and y but it creates n-1 sub-intervals between x and y points. So for this question, we need to choose linspace(-1,1,11) to get a 10 subintervals.
Correct answer is C
- Q6 -
In this question, to divide the interval into N
sub-intervals we need to use linspace(x,y,n+1) because linspace divides the intervals into n-1 sub-intervals but we need N amount of sub-intervals. We need to read carefully.
Correct answer is D
- Q7 -
From MATLAB documentation: clear
removes all variables from the current workspace, releasing them from system memory. clear
removes a global variable from the current workspace but not other workspaces.
Correct answer is B
- Q8 -
A == B
returns a logical array with elements set to logical 1 (true) where arrays A and B are equal; otherwise, the element is logical 0 (false). In this question fourth elements and seventh elements of x and y is equal so the answer will be [0 0 0 1 0 0 1].
Correct answer is C
- Q9 -
From MATLAB documentation: A./B divides each element of A by the corresponding element of B. A and B must have compatible sizes. In the simplest cases, they can be the same size or one can be a scalar.
Correct answer is A
- Q10 -
From MATLAB documentation: contour(Z) creates a contour plot containing the isolines of matrix Z, where Z contains height values on the x-y plane. MATLAB® automatically selects the contour lines to display. The column and row indices of Z are the x and y coordinates in the plane, respectively. For more checkout here .
Correct answer is D
- Q11 -
.^
Array power. From MATLAB documentation: “A.^B denotes element-by-element powers. A and B must have compatible sizes. In the simplest cases, they can be the same size or one can be a scalar.” We can eliminate option B, C and E from this definition. For option D, it doesn’t needs to be n x n, we can use .^
operator with 2 x 3 and 1 x 3 matrices so we can eliminate D.
Correct answer is A
- Q12 -
From MATLAB documentation: “>
Greater than operator. A > B does element by element comparisons between A and B and returns an array with elements set to logical 1 (TRUE) where the relation is true and elements set to logical 0 (FALSE) where it is not.”
Correct answer is E
- Q13 -
For this kind of questions, we need to be careful for small details. Question asked us to plot the function in an interval. We have x values in [1, 2] interval so we will use plot
function because while using fplot
function we define a function generally and let MATLAB choose the x values and compute the y values. With this information, we can eliminate fplot
, options B and E. In options A and C, there are typos in y function, it needs to be y=x.^2+log(x)
. Be careful while using .^
operator.
Correct answer is D
- Q14 -
Unlike other code languages Python and Javascript, in MATLAB indexing starts from 1. To get an index from given x vector, simply we can use x(start index : number of steps : end index)
.
Correct answer is A
- Q15 -
From MATLAB documentation:
while expression
statements
end
For example:
while n > 1
n = n-1;
end
Correct answer is D