Blog

Keep up to date with the latest news

Up To Date MATLAB MCQs – MATLAB Programming ( MATLAB ) MCQs

Up To Date MATLAB MCQs – MATLAB Programming ( MATLAB ) MCQs

Latest MATLAB MCQs

By practicing these MCQs of MATLAB Programming ( MATLAB ) MCQs – Latest Competitive MCQs , an individual for exams performs better than before. This post comprising of objective questions and answers related to MATLAB Programming ( MATLAB ) Mcqs “. As wise people believe “Perfect Practice make a Man Perfect”. It is therefore practice these mcqs of MATLAB to approach the success. Tab this page to check ” MATLAB Programming ( MATLAB )” for the preparation of competitive mcqs, FPSC mcqs, PPSC mcqs, SPSC mcqs, KPPSC mcqs, AJKPSC mcqs, BPSC mcqs, NTS mcqs, PTS mcqs, OTS mcqs, Atomic Energy mcqs, Pak Army mcqs, Pak Navy mcqs, CTS mcqs, ETEA mcqs and others.

MATLAB MCQs – MATLAB Programming ( MATLAB ) MCQs

The most occurred mcqs of MATLAB Programming ( ) in past papers. Past papers of MATLAB Programming ( MATLAB ) Mcqs. Past papers of MATLAB Programming ( MATLAB ) Mcqs . Mcqs are the necessary part of any competitive / job related exams. The Mcqs having specific numbers in any written test. It is therefore everyone have to learn / remember the related MATLAB Programming ( MATLAB ) Mcqs. The Important series of MATLAB Programming ( MATLAB ) Mcqs are given below:

Branching – 1

1. Amongst multiple elseif and nested if, which would take less runtime?
a) multiple elseif
b) nested if
c) elseif
d) elseif & nested if
Answer: a
Explanation: If multiple elseif and nested if has the same number of conditions. Multiple elseif checks at each step if it at all it has to go to the next condition. But nested if checks each condition before leaving the entire control structure. Hence, the number of steps covered in multiple elseif is less than nested if. Thus the former will take less time.

2. What is the output of the following code?

if(x~=c)
p=0;
end

a) p=0
b) Output will be suppressed
c) Error
d) Cannot be determined
Answer: c
Explanation: While using relational operators on character variables, we need to use ‘’. Hence, MATLAB gives an error when we write x~=c. If we had placed c within ‘’, the output would have been displayed as p=0.

3. The end statement is given multiple times in _________
a) Multiple elseif
b) Nested if
c) elseif
d) if
Answer: b
Explanation: We need to end each if structure within a nested if structure every time we give one. The end command should be placed after the end of each if structure or MATLAB will give an error.

4. What will happen if we don’t give an otherwise block to our switch structure?
a) Error
b) Infinitely demands a correct variable for case
c) Returns 0
d) Moves on to the next block
Answer: d
Explanation: If we don’t give an otherwise block to our switch structure, it will not give any error. It will simply continue with the execution of statements after the switch structure.

5. What is the output of the following code?

syms x;
if(x>50)
fprintf('Error')
end

a) Error
b) Prints Error
c) Does not print error
d) Cannot be determined
Answer: a
Explanation: We have declared x as a symbolic variable and haven’t assigned any value to it. So it is not declared with 0, it is only a symbolic character. Hence, the if structure cannot check the condition because it cannot convert the symbolic form to logical form. Hence it won’t go inside the if structure- it will return an error but not print ‘Error’.

6. While developing a switch-case structure, the value for case can be _________
a) Single only
b) Multiple
c) Infinite
d) 0
Answer: b
Explanation: The switch-case structure has a versatile syntax in MATLAB. We can give many numbers of case values to match with the input. This can help us in solving complex problems where we need selection of values.

7. What is the output of the following code?

syms x c; 
if(x~=c)
fprintf(Error); 
end

a) Error
b) Error due to x
c) Error due to c
d) Error due to x and c
Answer: a
Explanation: Since we declared x and c with the syms command, they have been assigned to the variable ans. Hence, matlab would have printed ‘Error’. The output won’t be suppressed.

8. What is the output of the following code?

sym x c; 
if(x~=c)
fprintf('Error');
end

a) Error
b) Error due to x
c) Error due to c
d) Output suppressed
Answer: b
Explanation: Since we have used the sym command, x and c will be declared symbolic but they will be assigned to a separate variable. Hence, MATLAB will show an error since it can find the symbolic variable x. If we had used the syms command, MATLAB would have printed ‘Error’. The output won’t be suppressed.

9. Menu-driven architecture should be done by ________
a) if-else structure
b) switch-case structure
c) stack
d) loop structure
Answer: b
Explanation: A Menu implies that we have sections. A switch-case structure offers sections or cases which will be implemented based on the switching value. Hence, this is perfect for a menu-driven architecture.

10. The program to check the highest of n numbers can be done by ________
a) Sorting
b) If-else structure
c) Switch-case structure
d) Depends on the nature of n
Answer: a
Explanation: The sorting procedure is the easiest way to get the highest of n numbers. This is because the code complexity will increase, drastically, if we use an if-else structure or a switch-case structure.

11. Giving conditions using characters, we give them using ____
a) ‘’
b) ””
c) No special character
d) []
Answer: a
Explanation: The ‘’ is used while dealing with characters. If we haven’t declared our characters before using them, we can compare characters by placing them within ‘’.

12. The if structure is a _________
a) Conditional structure
b) Logical structure
c) Nested structure
d) Biased structure
Answer: a
Explanation: The steps of execution in the if structure follows a hierarchy of checking conditions. If the condition is not satisfied, the control breaks from the structure and goes to execute the next state of statements.

13. The switch-case structure is a _________
a) Conditional structure
b) Logical structure
c) Multidimensional structure
d) Hierarchical structure
Answer: b
Explanation: Any case in the switch structure gets executed if the switching variable and the case value satisfies the logical condition of equality. Hence the switch-case is a logical structure since based on the equality of the two values, the case will get executed.

14. What is the output of the following code?

if()
p=9;
end

a) Error
b) p=9
c) Output is suppressed but p=9
d) 0
Answer: a
Explanation: We cannot keep the condition as empty. We need to give a condition otherwise control will never proceed to allow us to design the if structure. It will show an error as soon as we hit enter after if(). The output could’ve been 9 and it would’ve been suppressed due to the ‘’, but this code will show an error.

15. What is the output of the following code?

if(x=99)
var=92;
end

a) Error
b) var=92
c) Output is suppressed but var=92
d) Nan
Answer: a
Explanation: We cannot use an assignment operator in our condition statement while using an if-else structure. We have to use a relational or logical operator only.

Branching – 2

1. The if-else structure does not allow ___________
a) Return statement
b) End statement
c) Endif statement
d) Break statement
Answer: d
Explanation: The break statement is not allowed within an if-else structure, it is strictly for a loop structure. We can use the return statement and the end statement but the break statement will give an error.

2. What is the output of the following code?

if((-10 &&00)||(20134 && 900))
fprintf("%s","True.")
else
fprintf("%s","False.")
end

a) True
b) False
c) Error
d) Nan
Answer: a
Explanation: The logical && will give 0 if and only if one of the anding value is 0. So -10&&00 returns 0 but 20134&&900 gives 1. The logical || gives 0 if both the values are 0. But here, 0||1 is 1. Since the logical expression yields 1, the condition is satisfied and the output is True.
Output: True

3. What is the output of the following code?

if(1)
p=9;
end

a) p=9
b) Output is suppressed but p=9
c) Error
d) Nan
Answer: b
Explanation: The condition has no kind of operator. But we have given the value 1 as a condition. So, the condition will be taken as true and control will execute the set of statements within the if structure. Now we have placed a ‘;’ so the output will be suppressed but p=9.

4. What is the output of the following code?

num=2;
switch num
case[2 3 4]
p=9;
case[1 5 6]
q=9;
end

a) No output
b) p=9
c) q=9
d) Output suppressed but p=9
Answer: a
Explanation: When we are giving multiple values for a single case, we have to give those values within {}. Since the code contains [], the switch-case structure won’t work.

5. What will be the output of the following code?

switch num
case '2'
p=2;
case '2'
p=4-5;
end

a) p=2
b) Error
c) No output
d) p=-1
Answer: c
Explanation: We observe that we have given the same case value for multiple cases. This will not generate an error in MATLAB but the switch-case structure won’t work properly and MATLAB will not give any output.

6. Before starting a new case, we have to end the previous case using ___________
a) Break statement
b) Continue statement
c) Return statement
d) No statement
Answer: d
Explanation: MATLAB is not like C. So, we don’t have to give the break statement every time we end a case. We can write the next case and during runtime, the control will leave the switch-case structure after executing the matched case. We can give the break statement during looping and the return statement during if-else structure.

7. We cannot use a ____ statement in the standalone if-else structure.
a) break
b) if-else
c) return
d) switch-case
Answer: a
Explanation: If the if-else structure is not within any loop, we cannot use the break statement. The break statement is only allowed for loops. If the if-else structure is part of a loop, we can use the break statement to end the loop before it reaches its limit.

8. The continue statement can be used in a switch-case structure.
a) True
b) False
Answer: b
Explanation: The continue statement is only allowed for and while loops. If we give the continue statement within a switch-case structure, MATLAB will give an error.

9. We cannot use a ____ statement if the if-else structure is not part of a loop.
a) continue
b) if-else
c) clc
d) plot()
Answer: a
Explanation: If the if-else structure is not within any loop, we cannot use the continue statement. The continue statement is only allowed for ‘for’ and ‘while’ loop.

10. What will be the output of the following code?

if(cos(Inf)==NaN)
p=1;
end

a) p=1
b) Output suppressed but p=1
c) Error
d) No output
Answer: d
Explanation: The condition sin(Inf)==NaN is not satisfied because MATLAB knows sin(Inf) has a value between 1 and -1, even though it cannot comprehend it. Hence, it will produce a logical 0 and the control will exit the if structure, it will give no output.

11. What will be the output of the following code?

switch num
case ‘a’
p=394;
case ‘b’
c=0;
end

a) Error
b) No output since we haven’t given num a value yet
c) p=394
d) c=0
Answer: a
Explanation: We haven’t declared num as symbolic. So we cannot use it as our switching value. Hence, MATLAB will give an error. It wouldn’t have generated any output since we haven’t assigned a character with num, but first it will show an error.

Logical Expressions – 1

1. If a logical expression is true, it will always return a logical 1 and if it’s false, it’ll always return a 0.
a) True
b) False
Answer: a
Explanation: Any logical operation is pre-defined in MATLAB to give a logical 1 if the result derived from the operation is true. If the result is false, the result returned will be a logical 0.

2. The functioning of all() and any() is same.
a) True
b) False
Answer: b
Explanation: The all() function returns a 1 iff all the elements in the input vector is non-zero. The any() function returns a 1 iff at least 1 element in the input vector is non-zero. Hence, they are different in functioning.

3. What does the function all() do?
a) Returns 1 if all elements within it are 0
b) Returns 0 if all elements within it are 0
c) Returns 0 if all elements within it are non-zero
d) Returns 1 if all elements within it are non-zero
Answer: d
Explanation: The all() function is inbuilt in MATLAB. If we give a vector input to our function, it will return a 1 if all the elements of the vector are non-zero.

4. What will be the output of the following code?

all([1 0])

a) 1
b) 0
c) Error
d) Nan
Answer: b
Explanation: The all() function returns a logical 1 iff all the elements within the input vector are non-zero. Since, here, one element is 0- the logical output is 0.

5. Choose the correct hierarchy of operations.
a) |,&,+,-
b) +,-,&,|
c) :,+,-,&
d) |,:,&,.^
Answer: b
Explanation: Amongst the given operators, the hierarchy is [.^], [+], [-], [:], [&], [|]. Hence, the only possible option is +,-,&,|. The rest are erroneous according to the pre-defined hierarchy in MATLAB.

6. What is the output of the following code?

2++++++++9

a) 11
b) 74
c) 25
d) Error
Answer: a
Explanation: Since + is a unary operator, it won’t give an error in MATLAB. Hence, the output of the above code will be 11.
Output: 11

7. What is the output of the following code?

sin(Inf) & sin(nan)

a) Error
b) 1
c) 0
d) Nan
Answer: a
Explanation: Nan cannot be used while dealing with logical operators. Hence, the above code will give an error.

8. What will be the output of the following code?

all([Nan])

a) 1
b) 0
c) Error
d) Nan
Answer: c
Explanation: Nan is not defined in MATLAB. It should have been ‘NaN’ or ‘nan’. If it was either of them, the result would’ve been 1 since NaN is not equal to 0.

9. What will be the output of the following code?

~xor(3476,1234)

a) 1
b) 0
c) Error
d) NaN
Answer: a
Explanation: The output of xor(3476,1234) will be 0 since xor() returns a logical 1 iff one of the argument is 0. Since we have added the not operator before, the output will be 1.
Output: 1

10. What will be the output of the following code?

~or(~xor(1,1),~or(0,1))

a) 1
b) 0
c) NaN
d) Error
Answer: b
Explanation: The output of ~or(0,1) is 0. The output of ~xor(1,1) is 1. Hence, the output of ~or(1,0) is 0.
Output: 0

11. What will be the output of the following code?

any(sin(0), NaN)

a) 1
b) Error
c) 0
d) No such function
Answer: a
Explanation: The any() function returns 1 if any element, within the input vector, is non-zero. Since NaN is non-zero, the output will b 1.
Output: 1

12. What will be the output of the following code?

any(xor(1,1), ~or(xor(1,0),1), sin(pi), NaN)

a) 0
b) 1
c) Error due to too many arguments
d) Error due to NaN
Answer: c
Explanation: The all() function takes a single vector as input. If we had placed all the elements within [], there wouldn’t have been any error and the output would’ve been 1. NaN won’t give any error infact the occurrence of NaN will give the output 1 or the output would’ve been 0.

13. What will be the output of the following code?

all([or(1,1), ~xor(xor(1,0),1), cos(pi)])

a) 0
b) 1
c) Error
d) No such function all
Answer: b
Explanation: The all() function returns 1 iff all the elements within the input vector is non-zero. Here, xor(1,0) is 1 and ~xor(1,1) is 1. or(1,1) is 1 and cos(pi) is -1. Thus, all the elements are non-zero.
Output: 1

14. What will be the output of the following code?

all(sin(pi), 1)

a) 0
b) 1
c) Error
d) NaN
Answer: b
Explanation: sin(pi) is not 0 since pi is just a floating point variable in MATLAB. The output of sin(sym(pi)) would’ve been 0. Thus the output of the above code is 1 since the function all() returns 1 iff all the elements of the input vector is non-zero.
Output: 1

15. The correct hierarchy of operations is ________
a) <.<=,|,&
b) >,>=,&,|
c) <,>=,|,&
d) <,|,>,&
Answer: b
Explanation: Amongst the given operators, the correct hierarchy is <, <=, >, >=, &, |. Thus the most plausible >,>=,&,| according to the pre-defined hierarchy of operations in MATLAB.

Logical Expressions – 2

1. ‘=’ is a?
a) Relational operator
b) Arithmetic operator
c) Operational operator
d) Assignment operator
Answer: d
Explanation: p=2 assigns the value 2 to the variable p. Hence, ‘=’ is an assignment operator. It doesn’t perform arithmetic operations so it isn’t an arithmetic operator. It isn’t a relational operator unlike ‘==’

2. What is the output of the following code?

sin(pi)~=0

a) NaN
b) 0
c) 1
d) Error
Answer: c
Explanation: sin(pi) is not equal to 0 since pi is represented as a floating point number in MATLAB. If pi was declared as a symbolic variable, the output would’ve been 0.
Output: 1

3. What is the output of the following code?

sin(Inf)~=NaN

a) 1
b) 0
c) Error
d) NaN
Answer: a
Explanation: sin(Inf) is actually a number since the domain of sine is -infinity to infinity and the range is always -1 to 1. Hence, sin(Inf) is still a number- but it cannot be comprehended hence the value will be returned NaN. But in the above comparison, MATLAB will observe that sin(Inf) is still a number within -1 and 1 and hence, the output is 1.

4. What is the output of the following code?

log(0)==Inf

a) 1
b) 0
c) Error
d) Inf
Answer: b
Explanation: log(0) is -Inf. This is why the output of the above code is 0.
Output: 0

5. What is the output of the following code?

and(sin(pi),112312)

a) 1
b) 0
c) 00
d) Error
Answer: a
Explanation: sin(pi) won’t return 0 since pi is an in-built floating-point constant. If we had declared pi as symbolic, the output would’ve been 0.

6. The ~ is ______
a) Quaternary operator
b) Relational Operator
c) Arithmetic Operator
d) Unary Operator
Answer: d
Explanation: ~ is a unary operator since the NOT function operates on only one operand. It isn’t a relational operator since it doesn’t do any comparison and it’s not an arithmetic operator since it doesn’t perform any arithmetic operation.

7. All logical operators operate on at least two operands.
a) True
b) False
Answer: b
Explanation: The ‘ ~ ’ operator operates on one operand but it’s a logical operator. Hence, the statement is false.

8. What is the output of the following code?

NaN==Inf

a) 0
b) 1
c) Nan
d) Inf
Answer: a
Explanation: Nan and Inf are two symbolic variables in MATLAB suggesting ‘Not a number’ and ‘Infinity’ respectively. These are not equal to each other and hence we will get a logical 0 when we enter this in MATLAB.

9. What is the output of the following code?

Sin(Inf)==NaN

a) 0
b) 1
c) Nan
d) Inf
Answer: c
Explanation: If we type sin(Inf) in MATLAB, we will get NaN as an input because MATLAB cannot find the value of sin(Inf). But, MATLAB knows that sin(Inf) possesses a value, it cannot comprehend it but still it has a value. So, even though it will show NaN when we write sin(Inf), it will not give a logical 1 for the following code since sin(Inf) is still a number between 1 and -1.

10. The precedence of : operator is after | and before &.
a) True
b) False
Answer: b
Explanation: The hierarchy for precedence of operators for the aforementioned operators is :, &, |. Hence the given statement is false.

11. What is the output of the following code?

~xor(and(1,0),~xor(and(1,0),0))

a) 0
b) 1
c) NaN
d) Error
Answer: a
Explanation: and(1,0) is 0. ~xor(0,0,) is 1. But xor(1,0) is 1. Hence, ~xor(1,0) is 0.
Output: 0

12. The precedence of Transpose operation is ____________
a) after &
b) after |
c) before .^
d) after ^
Answer: c
Explanation: The transpose operation has the highest precedence amongst all the aforementioned operators. The hierarchy is .’, .^, ^, &, |. Hence option before .^ is correct.

13. All relational operators operate on _______________
a) only real part of complex numbers
b) only imaginary part of complex numbers
c) complex numbers
d) numbers which are not complex
Answer: a
Explanation: Some relational operators do not operate on the imaginary part of the complex numbers. But all relational operators operate on the real part of complex numbers.

14. What is the output of the following code?

any([])

a) 0
b) 1
c) NaN
d) Error
Answer: a
Explanation: Since there are no elements in the input vector, it also means there are no non-zero elements in the vector. Hence, the logical result of the operation is 0.

15. What is the output of the following code?

all[1, 2, 3, NaN]

a) Error due to NaN
b) 1
c) 0
d) Syntactical Error
Answer: d
Explanation: Even though the elements were declared within a vector, the syntax of the all() function is wrong since the vector should be present within a pair of parentheses. If the vector was given within a pair of parentheses, the result would’ve been 1. No error will be given due to NaN.

More about Loops – 1

1. What is the output of the following code?

i=0;for i=0:2  
i=1; end

a) Output is suppressed
b) Output shows 1 3 times
c) Output shows 1 2 times
d) Error
Answer: a
Explanation: The output won’t be shown since we’ve added a semi-colon after the declaration of the variable i. If there was no semi-colon, the output would’ve been i=1, 3 times. There is no error.

2. What is the output of the following code?

i=0;for j=0:2  
j=j-1
end

a) Infinite loop
b) -1
c) Output is suppressed
d) +1
Answer: b
Explanation: The output is suppressed since there is a semi-colon after the decrement of j. The counter variable is really treated as a different variable hence the value of j at the end of the loop is not 2.

 

MATLAB Graphics MCQs

 

3. What is the output of the following code?

a=0;for j=0:2  
j=j+1 end

a) Syntactical Error
b) 3
c) 2
d) Keyword Error
Answer: d
Explanation: The end is a reserved keyword and it cannot be used in the same line with j+1. Hence, the for-loop will end without even beginning. The output would’ve been 3 if the end was in the next line. There is no syntactical error.

4. What is the final value of j from the following code?

for j=(0:3)  
j=j+1;
end

a) Syntactical error
b) Declaration error
c) 3
d) 4
Answer: a
Explanation: The syntax of for loop in MATLAB does not include parentheses. Here, the counter is introduced within parentheses and hence, there will be an error.

5. What is the output of the following code?

for j= 4:2   
j=j+1 
end

a) an empty vector, j
b) 3
c) 2
d) error
Answer: a
Explanation: The loop never runs since if we want to decrease the counter value, we will have to introduce the decrement value. The default form, used here, is for increment operation. Hence, the output is an empty vector, j.

6. What is the output of the following code?

for i!=1:10 
p=p+1;

a) Syntactical Error
b) Inf
c) NaN
d) Logical Error
Answer: d
Explanation: While mentioning the number of times a loop would run, we have made an error in defining the range. The above statement, really, signifies that the loop won’t run for i= [1,10]. So we did not mention, when the loop will actually run. This results in the error.

7. In nested loops, the break statement exits the ____ loop.
a) external
b) last started ongoing
c) not available in MATLAB
d) for
Answer: b
Explanation: The break statement exits the loop in which it is only called. It doesn’t care about the remaining number of iterations in the loop but it only exits from the inner loop and the control still remains in the outer loop.

8. In nested loops, the continue statement exits the ____
a) current iteration
b) loop
c) inner
d) outer
Answer: a
Explanation: The continue statement stops the current iteration and restarts a new iteration of the same loop if the condition is not matched. It does not exit from the outer or the inner loop.

9. In nested loops, the break statement, if present within a nested if the structure, will exit the _______
a) Ongoing if structure
b) Entire loop
c) Ongoing loop
d) Entire if structure
Answer: b
Explanation: The break statement will exit the loop where the break statement is present. It won’t take control out of the external loop.

10. In nested loops, the continue statement, if present within an if structure, will exit the ____________
a) If structure
b) current iteration
c) entire loop
d) Error
Answer: b
Explanation: The continue statement will only end the current iteration and it will start the next iteration. It won’t exit from the entire loop.

11. In nested loops, the continue statement, if present within a nested if structure, will exit the ____________
a) If structure
b) nested if structure
c) entire loop
d) present iteration
Answer: b
Explanation: If the continue statement is present deep-rooted in a nested if structure, it will only stop the current iteration. It won’t exit the current ongoing loop or the external loop.

12. What is the output of the following code?

for j= 4:2:2   
j=j+4 
end

a) Error
b) j=6
c) Error an empty vector, j
d) Syntactical error
Answer: c
Explanation: The counter never runs since the initial value is less than the final value but the counter value is positive. This breaks the running of the loop and the value of j is an empty vector.

13. While running a loop in MATLAB, it does not require indentation.
a) True
b) False
Answer: a
Explanation: The following statement is true for MATLAB. One may use it to make it easier to check a loop but MATLAB won’t give an error if we don’t use indentation.

14. The effective working of the continue keyword in the final iteration is same as the effective working of the break keyword.
a) True
b) False
Answer: a
Explanation: If the continue statement gets executed in the final iteration of a loop, the control breaks the loop. The break statement will exit control irrespective of the number of iterations left and hence the above statement is true.

15. The number of iterations run for any loop by MATLAB is always _____________
a) a positive integer
b) a negative integer
c) a number
d) a decimal number
Answer: c
Explanation: It is not necessary that a loop will do at least one iteration. If the condition of a while loop is not satisfied before the first iteration, it won’t iterate even once and the number of iterations is 0.

More about Loops – 2

1. The for loop performs at least ___ iteration/s.
a) 1
b) not necessarily an iteration
c) 2
d) Error
Answer: b
Explanation: Suppose the condition given to check by the for loop has a logical error- the for loop won’t iterate even once. Hence, it is not necessary that the for loop will run even once.

2. Multiple graphs can be plotted, in the same window, if we use the ___ command in a loop.
a) hold on
b) held on
c) hold off
d) not possible
Answer: a
Explanation: The hold on command can be used to plot multiple graphs in the same window by using a loop. Hold off is used to exit the functioning of the hold on command. Hence, the correct option is hold on.v

3. The number of iterations in the following loop is

p=3;for i=(3:4)
p=p+2;
end

a) 0
b) 1
c) 3
d) 4
Answer: d
Explanation: If the counter value is <= the final value, the loop will perform an iteration. Hence, the above loop iterates twice to yield p=6. There is no error.

4. What is the output of the following code?

q=0;for i=(1:1)
q=q+5;
end

a) Output is suppressed
b) Syntactical error
c) q=5
d) Declaration error
Answer: a
Explanation: The output is suppressed due to the semi-colon after q=q+5. The output of the following code would’ve only been q=5 if the output was not suppressed. There is no error in the above code.

5. What is the output of the following code?

a=0;for i=[1:1]
q=a-55;
end

a) Syntactical Error
b) Declaration error
c) Output is suppressed
d) q=-55
Answer: c
Explanation: The expression for counter can be given within [] in the for statement. The output is suppressed due to the semi-colon after q=a-55. The output of the following code would’ve only been q=-55 if the output was not suppressed. There is no error in the above code.

6. What is the nature of the error in the following statement?

for i=1:

a) Incomplete
b) Unexpected operator
c) Invalid keyword
d) No error
Answer: a
Explanation: MATLAB would recognize that the above statement for starting the for loop is incomplete. Hence, it will give the option incomplete as an error.

7. What is the nature of the error in the following statement?

for i=:1

a) Incomplete syntax
b) Unexpected MATLAB operator
c) No such keyword
d) No error
Answer: b
Explanation: The syntax of the for loop is not recognized by MATLAB. Hence, it will give the unexpected MATLAB operator as an output.

8. What is the output of the following code?

i=0; while[i<5] 
i=i+1;
end

a) Syntactical Error
b) Declaration Error
c) Output is suppressed
d) i=5
Answer: c
Explanation: There is no error due to []. The output is suppressed due to the ‘ ; ‘. The value of I stored in the workplace is i=5.

9. The condition of a while loop cannot be given in ________
a) ()
b) []
c) {}
d) <>
Answer: c
Explanation: The variables of the condition gets converted to a cell format if the condition is given in {}. Thereafter, the variables cannot be converted into a logical form. It can be given within () and [].

10. What is the output of the following code?

while{i<5}
i=i+1234;
end

a) i=1234
b) Output is suppressed
c) Error in converting the cell to logical form
d) Error in i
Answer: c
Explanation: MATLAB returns the first error it finds. Now, i is not defined before using it as a variable for a condition in the while loop. But the first error is the fact that the condition is given in {} so it is in a cell format and the conversion to logical form is not possible. Hence, MATLAB will return an error.

11. What is the output of the following code?

i=0;while(i<5)
i=i-1;
end

a) Error
b) i=-Inf
c) i=NaN
d) Infinite loop
Answer: d
Explanation: The following code will start an infinite loop. The control can be broken form the loop by pressing ctrl+c.

Commands for Parsing Input and Output – 1

1. What is the working of the nargin keyword?
a) Returns the number of inputs to a function as an output of the same function
b) Gets assigned to a value equal to the number of inputs of a function
c) Gets assigned to a value equal to the number of inputs+1 of a function
d) Gets assigned to a value equal to the number of inputs-1 function
Answer: b
Explanation: The nargin command takes a value which is equal to the number of inputs given to the function.

2. What is the working of the varargout command?
a) Returns a 1*N cellular matrix
b) Returns a 1*N cell array of outputs
c) Returns a 1*N cell array of inputs given
d) Doesn’t exist
Answer: b
Explanation: The varargout command returns a set of output elements in a 1*N cell array. It doesn’t return the number of inputs.

3. What is the working of the nargout command?
a) Gets assigned to a number which is equal to the number of outputs-1 sought from a subfunction
b) Gets assigned to a number which is equal to the number of outputs-1 sought from a function
c) Gets assigned to a number which is equal to the number of outputs sought from a function
d) Gets assigned to a number which is equal to the number of outputs+1 sought from a function
Answer: c
Explanation: The nargout command gets assigned to a number which is equal to the number of outputs sought from a function. It will control the nature of outputs produced by the function where it is present.

4. What is the working of the varargin command?
a) takes 1*N inputs after the defined inputs in a function
b) takes N*1 inputs after the defined inputs in a function
c) takes N*N inputs after the defined inputs in a function
d) takes 1*1 inputs after the defined inputs in a function
Answer: a
Explanation: The varargin command takes 1*N inputs after the defined inputs to a function. It allows the function to take many input arguments and control the output of the function.

 

Beyond The Basics MCQs

 

5. What is the output of the following command?

function s= average(varargin)
celldisp(varargin)
s=sum(varargin)
end
p=average(1:3)

a) Error in sum
b) Error using varargin in function definition
c) Error calling the average function
d)

    1 2 3
     p=6

Answer: a
Explanation: The input is acellular array called varargin. The input to the sum function in the average function is should be varargin{1} to indicate which element of the array varargin is to be taken.

6. What is the output of the following command?

function wish= ful(p,varargin)
celldisp(p)
wish=sin(varargin{1})
end
p=ful(1:3)

a) Error in calling the function
b) Error in celldisp
c) Error in the function definiton
d) Error in the wish variable
Answer: b
Explanation: p is treated to a function which takes inputs as cellular arrays. In the above code, if the input to the ful command was {1:3}, the output would have been
p{1}= 1 2 3
Hence, the error is in using celldisp command, only disp command is to be used.

7. What is the output of the following command?

function mad= heart(p,varargin)
disp(p)
wish=sin(varargin{0})
end
p=heart(1:3,0)

a) Error in wish command
b) Error while calling heart command
c) Error while calling disp command
d)

     1 2 3
     wish=0

Answer: a
Explanation: The arguments to a cell array start from the index 1. Here, varargin{0} will create an error and hence, error in wish command is correct.

8. What is the output of the following command?

function mad= rush(p,varargin)
disp(p)
wish=cos(varargin[0])
end
p=rush(1:3,pi/2)

a) Error due to element index
b) Error due to syntax of varargin
c)

  1 2 3
  wish=0

d) Error due to disp()
Answer: b
Explanation: The first error MATLAB finds is that the index of the element of the varargin array is given within []. This creates the error and MATLAB returns the first error it invokes The next error is the fact that there are no elements in varargin whose index number is 0.

9. What is the output of the following code?

function mad= rush(p,varargin)
disp(p)
wish=cos(varargin{1})
end
p=rush(1:3,Inf)

a) Error due to Inf
b) -1
c)

    p{1}= 1 2 3
    wish=0

d)

     p{1}= 1 2 3
     wish=NaN

Answer: d
Explanation: There is no error due to Inf. The disp function will display 1 2 3 due to the input vector 1:3 and wish will be assigned to NaN since cos(Inf) is NaN.

10. What is the output of the following code?

function maha= orchid(polo,varargin)
disp(polo)
wish=sum(varargin{1,2})
end
p=rush(-1:1,Inf)

a) Logical error in varargin
b) Syntactical error in varargin
c)

     p{1}=-1 0 1
     wish=0

d) No output
Answer: a
Explanation: Varargin is a 1*N cellular array. The index number given as input to the varargin command looks like we are calling the element in the 2nd row and the 1st column which is not there. Hence, there is logical error in using the varargin keyword.

11. What is the output of the following code?

function [die,absdif] = BDO(y,x)
die = y-x;
if nargout > 1
    disp('Boo !')
    absdif = abs(die);
end
end
[p,q]=BDO(Inf,Inf)

a)

     Boo!
     p=NaN
     q=NaN

b)

     Boo!
     p=0
     q=0

c) Error
d) p=NaN
Answer: a
Explanation: Since the number of outputs sought are greater than 1, the if condition gets satisfied and ‘Boo !’ is printed. Inf-Inf will be NaN and hence both p and q will be equal to NaN.

12. What is the output of the following code?

function [die,af] = Bod(y,x)
die = y*x;
if nargout < 1
    error('Boo !')
 else
   disp(‘Yo !’)
   af = abs(die);
end
end
[p]=Bod(1,[1 -2])

a) Error with Boo! displayed
b)

     Yo !
     p=1

c)

     Yo !
     p=2

d)

     Yo !
     p=-2

Answer: d
Explanation: Only one output is sought from the Bod function. The first argument it returns is die=-2 and the else condition is also satisfied so Yo! will be printed.

13. What is the output of the following code?

function [die,af] = lola(y,x)
die = y*x;
if nargout < 1
    error('Boo !')
 else
   disp(‘Yo !’)
   af = abs(die);
end
end
p=lola(1,[1 -2])

a) Error with Boo ! written
b)

     Yo !
     p=-2

c)

     Boo !
     p=-1

d)

     Boo !
     p=1

Answer: a
Explanation: Since the number of argument sought are less than 1, nargout is less than 1 and the if condition is satisfied so the function terminates with an error.

14. A function ______________
a) can be run in the command window by defining it in the window itself
b) can be run in the command window by defining it in a cpp file
c) can be run in the command window by defining it in a script file
d) can be run in the script file by defining it in the command window
Answer: c
Explanation: A function has to be saved as a M-file before it can be run in the command window. We will have to take MATLAB to the directory where it is saved and it will run the function from the directory itself.

15. What is the output of the following code?

function c = addem(ap,q)
p=nargn-1
switch p
    case 2
        c = ap*q;
    case 1
        c = sin(q)
    otherwise
        c = 0;
end
addem(1,2,3,4,5,6,7,8)

a) 2
b) Error due to too many inputs
c) 28
d) Error due to nargn
Answer: d
Explanation: The first error MATLAB invokes is the fact that too many values are given as input. So it returns that as input. The next error would be that the nargin keyword is misspelled as nargn. But MATLAB returns the first error only.

Commands for Parsing Input and Output – 2

1. The working of the varargin command is dependent on the nargin command implicitly.
a) True
b) False
Answer: b
Explanation: The varargin command takes the values as a cellular array. It takes values independent of the output of the nargin keyword.

2. The nargout command is dependent on the number of outputs sought.
a) True
b) False
Answer: a
Explanation: The nargout command acts according to the number of outputs sought from a function. It will change the output according to the value it gets assigned.

3. What is the output of the following command?

function eclairs= honey(p,q)
narginchk(2,5)
disp(‘Yo, G!’)
c=q+p;
end
honey(1)

a) Syntactical Error
b) Yo, G!
c) input error
d) 2 3 4 5
Answer: c
Explanation: The minimum number of inputs as suggested by the narginchk keyword is 2. But we have given only 1 input to the narginchk keyword. Hence, MATLAB gives an error in the input given to the function honey().

4. What is the output of the following command?

function p= marie(p,q)
narginchk[2,3]
disp(‘Yo, G!’)
a=p+q
end
marie(1)

a) Syntactical Error in narginchk
b)

    Yo, G!
     a=1
c) Input error in narginchk
d) 2 3 4 5…Inf

Answer: a
Explanation: There has been syntactical error while using the narginchk command in the marie function. The input to this keyword is given within a parenthesis. If the input was given within parentheses, the output would’ve been
Yo, G!
a=1

5. What is the output of the following command?

function ca= ola(p,q)
narginchk(0,1)
disp(‘Stick!’)
o=p-q;
end
ola(1,2)

a) o=-1
b) o=1
c) Error in inputs
d) Syntactical error while calling function
Answer: c
Explanation: The narginchk command has limited the number of inputs to the ola function to 1. But the ola function has been called and 2 inputs are given to it. This gives an error while getting an output from the function.

6. What is the output of the following command?

function pu= hu(p,varargin)
narginchk(0,1)
disp(‘Stick!’)
o=sin(varargin{1});
end
ola(1,p/2)

a) Error due to line 2
b) Error due to line 5
c) Error due to syntax
d)

     Stick!
     o=1

Answer: a
Explanation: The varargin command restricts the input to the function to only 1 argument. Hence, the function stops working and we will get an error due to line 2 of the above code.

7. The narginchk command can limit the size of which cellular array?
a) fun
b) rand(2)
c) eye(3)
d) varargin
Answer: d
Explanation: The varargin keyword is used to take inputs to function as a 1*N cellular array. The value of N can be limited by uing the narginchk command. The rand(2) command generates 2 random numbers and the narginchk command has no effect on it. Same goes for the rest of the options since they are separate operations.

8. The nargoutchk command limits the length of which cellular array?
a) varargout
b) i(2)
c) random(69)
d) laplace(s)
Answer: a
Explanation: The varargout command allows 1*N outputs to be asked from the function where it is situated. But the nargchkout command restricts the total number of outputs that can be sought from the function. So it really puts a limit to the value of N. Hence, the correct option is varargout.

9. The following operation results in?

narginchk(2,Inf)

a) Error due to Inf
b) Limits the length of nargin from 2 to Inf
c) Logical Error
d) Limits the length of varargin from 1 to Inf
Answer: d
Explanation: The narginchk keyword, if present in a function, limits the input to the same function within a range. According to the above operation, if we use the varargin array while takin’ input in a function, the function will return an error if the number of inputs to the function are less than 2.

10. The following operation result in

Function p= ol(q,varargin)
narginchk(45,Inf)

a) Error due to Inf
b) The range of the varargin array becomes (44,Inf)
c) The range of the varargin array becomes (45,Inf)
d) The range of the varargin array becomes [45,Inf]
Answer: b
Explanation: The narginchk keyword limits the size of the varargin array to atleast 44 or the function won’t run. This is because the first input to a function does not go in to the varargin array but to q and hence, the minimum number of elements the varargin array has to take is 44.

11. The following operation results in

Function p= ol(q,varargin)
narginchk(45,NaN)
end

a) Error due to NaN
b) Syntactical error
c) Logical Error
d) Nothing happens
Answer: d
Explanation: We have only written the code for the function but we did not call the function. So nothing will really happen although there is an error due to NaN. This doesn’t affect the saving of the function. It gets compiled at runtime.

12. What is the minimum number of arguments after the following operation occurs in any function?

narginchk(-2,0)

a) 0
b) 1
c) -2
d) Error
Answer: a
Explanation: The minimum number of arguments to be given to the function will become 0. This renders the function totally useless. One has to be careful while giving input to the narginchk command.

User Input and Screen Output – 1

1. What is the output of the following code?

error(“Lola !”);

a) Output is suppressed
b) Syntactical error
c) No such function exists
d) It displays Lola !
Answer: b
Explanation: The input to the error command should be in ‘’ but here, “” was used which results in a syntactical error.

2. What is the output of the following code?

error(‘Dan !’);

a) No output, it gets suppressed
b) The word Dan ! is displayed
c) No such function
d) Syntactical Error
Answer: b
Explanation: The output won’t get suppressed due to the ; since this command is used to display the input given to it. There is no syntactical error.
Output: Dan !

3. What is the output of the following code?

error(‘ No !!!!’)

a) No !!!!
b) No !!!!
c) Logical Error
d) Symbolic error
Answer: b
Explanation: The input to the error() command will be displayed in red. This is in contrast to the disp() command and hence option No !!!! is correct.
Output: No !!!!

4. What is the output of the following code?

error(‘Lola ! \n Dan !’);

a) Output is suppressed
b) Symbolic error in \n
c)

    Lola ! 
    Dan !

d) Lola ! \n Dan !
Answer: d
Explanation: We can give multiple input to the error() command. But the ‘\n’ character on;y works if we give multiple input like error(‘String argument e.g. ‘Lola ! \n Dan !’ , 1Arg, 2Arg..) where 1Arg, 2Arg are different variables which gets displayed due to the error() command itself. If that’s the case, the output would have been option c. But here we have only 1 string argument and so the \n character won’t work.
Output: Lola ! \n Dan !

5. What is the output of the following code?

MException('Lola!', '%d * %d= Why?', Inf,Inf)

a) Error due to identifier
b) Error due to Inf
c)

    MException with properties:
    identifier: 'Lola!’
    message: 'Inf * Inf= Why?'
    cause: {}
    stack: [0×1 struct]

d) No such command
Answer: b
Explanation: The message identifier, ‘ Lola !’ given as an input to the MException command is not a valid MATLAB identifier. There is a set of valid identifiers and any input other input will give an error.

6. What is the output of the following code?

MException('MATLAB:test', '%d * %d= Why?', Inf,Inf)

a)

    MException with properties:
    identifier: ‘MATLAB:test’
    message: 'Inf * Inf= Why?'
    cause: {}
    stack: [0×1 struct]

b)

    MException with properties:
    identifier: “MATLAB:test”
    message: 'Inf * Inf= Why?'
    cause: []
    stack: [0×1 struct]

c)

    MException with properties:
    identifier: ''MATLAB:test’
    message: '%d * %d= Why?'
    cause: {}
    stack: [0×1 struct]

d) Error
Answer: a
Explanation: %d would get converted to the value given as an argument after the message input in the MException command. The cause is not specified so it would remain a cell array, identified by {}. The identifier is shown within a ‘’ and not “”.There is no error in the code.
Output:

    MException with properties:
    identifier: ‘MATLAB:test’
    message: 'Inf * Inf= Why?'
    cause: {}
    stack: [0×1 struct]

7. What is the output of the following code?

disp(%d’,Inf);

a) Inf
b) No such command
c) Error due to too many arguments
d) Syntactical error
Answer: c
Explanation: The disp command takes a maximum of 1 argument which is a string argument only. Hence it will return an error due to the second argument, Inf, given to it.

8. What is the output of the following code?

sprintf(%d’,5)

a) 5
b) ‘5’
c) Syntactical error
d) Logical error
Answer: b
Explanation: The sprint() command will convert the argument variables given to it into the format specifier mentioned as an input. But, the new value output of the sprint command will be assigned to a variable, here it’ll be the ans variable, which will be a string variable.

9. What is the output of the following code?

warning(‘Pikapi !’)

a) Warning: ‘Pikapi !’
b) Warning: Pikapi !
c) Warning: ‘Pikapi !’
d) Warning: Pikapi !
Answer: d
Explanation: A warning message is always displayed in a dark yellow color. The message itself is not shown as contained within a ‘’. Hence, option Warning: Pikapi ! is correct.
Output: Warning: Pikapi !

10. What is the output of the following code?

warning('off')
warning('Pikapi !')

a) No output
b) Warning: Pikapi !
c)

    Warning: Off
    Warning: Pikapi !

d) Syntactical error
Answer: a
Explanation: No warning message will be displayed by MATLAB since we’ve disabled the working of the warning() command itself with working(‘off’). We can again enable it by simply writing warning(‘on’).

11. What is the output of the following code?

disp(‘ ‘,lola!’ 2)

a) ‘lola!’ 2
b) ‘ ‘lola!’ 2’
c) ‘lola! 2’
d) Error due to multiple arguments
Answer: d
Explanation: Within the ‘’, when we’re giving input to the disp command, the we cannot include another set of ‘’. This will given an error. The next set of ‘’ will be treated as an extra character argument while the first set of ‘’ will close itself once we give a ’ for our first set of single-inverted commas. This means, in the above command, after disp(‘ ‘..) the input argument is already taken as a space character by the command while starting from lola.. the entire thing is another argument.

12. How many arguments are given to the above command?

disp(‘ ‘lola!’ 2)

a) Cannot be defined
b) 2
c) 1
d) 0
Answer: c
Explanation: The syntax of the disp() command is disp(string argument). To introduce further arguments, we habe to use disp(string argument,1Arg,2Arg). Now, once we end our string argument, we need to put a comma. Here, we haven’t put any comma so it shows that there is only 1 string argument while the rest is not defined as an argument also.

Interacting With MATLAB MCQs

13. What is the output of the following command?

sscanf(‘.1 .2 .3’,%d)

a) []
b)

 
    .1
    .2
    .3

c)

 
    1
    2
    3

d) Error
Answer: a
Explanation: The input contains a string of floating point variables but the format specifier is for integer type.

14. The default exponential order for %e and %E is same.
a) True
b) False
Answer: a
Explanation: %e will signify the exponential order as e while %E will signify it as E. But the default order of representation of both of them is same.

15. The disp() command cannot print anything but inputs given within string arguments.
a) True
b) False
Answer: b
Explanation: The disp command can print integers given as an input to the command. It is not necessary that the input has to be given as string argument.

User Input and Screen Output – 2

1. What is the output of the following code?

sprint(%i’,91293)

a) 91293
b) Syntactical Error
c) The format specifier does not exist
d) ‘91293’
Answer: d
Explanation: The format specifier %i is used to display integers. But sprint keeps the input arguments in a character array. Hence, the output is ‘91293’.
Output: ‘91293’

2. What is the output of the following code?

sprintf(%E’,912)

a) 9.120000E+02
b) 9.120E+02
c) 9.1200E+02
d) 9.12000E+02
Answer: a
Explanation: If we don’t mention a precision amount, the default number of digits shown after the decimal place will be 6. Hence, option 9.120000E+02 is correct only. Option 9.120E+02 is true for %.3E used as the format specifier.
Output: 9.120000E+02

3. What is the output of the following code?

sprintf(%d %d %d’,.1, .2.3)

a) Error
b) ‘1.000000e-01 2.000000e-01 3.000000e-01’
c) 1.000000e-01 2.000000e-01 3.000000e-01
d) []
Answer: a
Explanation: The %d format specifier converts the arguments into those given in option ‘1.000000e-01 2.000000e-01 3.000000e-01’. But we have not given a, after .2. So, .3 is not even considered an argument and .2.3 together results in an error.

4. What is the output of the following code?

disp({12 })

a) [12]
b) 12
c) [12 ]
d) Error
Answer: a
Explanation: The input to the disp command is a cellular array. Cellular arrays are displayed within []. Output: [12]

5. What is the output of the following code?

size(‘’)

a) 0
b) Error
c) 0 0
d) 1 1
Answer: c
Explanation: The size command returns the nature of input given to the command. Here we have given an empty string as an input but the size of the string will return the number of rows and columns present in the input string which is 0 and 0. The ‘’ indicates a 2-D character array which has 0 rows and 0 columns; 0 is treated as a value while returning the size of the input.

6. What is the output of the following code?

sprintf('%04.2f',2.9121)

a) ‘2.91210000000000000000’
b) ‘2.91’
c) 2.91
d) Error
Answer: b
Explanation: %4.2f implies that the input argument, 2.9121, will be approximated to 4 places places before the decimal point and 2 places after it.

7. What is the output of the following code?

sprintf(); disp();

a) Error due to disp
b) Error due to sprintf()
c) Both gives an error
d) No output is displayed
Answer: b
Explanation: Both would give an error due to absence of any input arguments in the commands. But sprintf() will give an error first as MATLAB returns the first error it finds.

8. What is the output of the following code?

P=disp(234)

a) Error
b) P=234
c) 234
d)

     P=
        234

Answer: a
Explanation: No output arguments can be initialized by the disp command. It would have displayed 234 only if the command was disp(234) but here there will be error because we are assigning the output of the disp command to a variable. This is possible by the sprintf command.

9. How can we influence the disp command with format specifiers?
a) Not possible
b) Via the sprintf() command
c) Use format specifiers as string input
d) Give format specifiers as input only
Answer: d
Explanation: We can create a character array using the sprintf command and we can use format specifiers in the sprintf command. So, the final character array can be printed by giving that variable as an input to the disp command.

10. What is the output of the following code?

disp(12)

a) ‘12’
b) “12”
c) 12
d) Error
Answer: c
Explanation: The string argument, given as an input to the disp() command, can be given within “” and hence, the output is 12 only. There rest of the options are incorrect.

11. What is the output of the following code?

fprintf(%f”,.123)

a) .123000
b) %f
c) Error
d) .123
Answer: a
Explanation: Since we haven’t mentioned a precision while representing .123, the output will be upto 6 places of decimal. Thus, the output is .123000 and there is no error.

12. What is the size of ans variable after the following code?

sprintf('23 23')
ans=
      ’23 23

a) 14 bytes
b) 10 bytes
c) Error
d) 1*5
Answer: b
Explanation: The number of elements in the character array, given as input are 5. They are ‘2’, ‘3’, ‘ ’, ‘2’, ‘3’. Hence, the total size will be 10 bytes as each element will take 2 bytes. The ‘’ are not taken as elements.

Evaluation – 1

1. What is the output of the following code?

[q]=eval(sprintf('23 '))

a) q=23
b) Logical Error
c) Symbolic error
d) No output
Answer: a
Explanation: As eval evaluates the MATLAB expression in the input, it will evaluate the input to 23 and assign it to q. There are no errors in the above code.

2. What is the output of the following code?

[q,r]=eval(sprintf('23 45'))

a) Unexpected MATLAB expression
b) q=23,r=45
c)

   q = 23
   r = 45

d) Error due to eval
Answer: a
Explanation: The eval command returns an error if more than a pair of inverted commas are found. Hence, unexpected MATLAB expression is correct option. Option q=23,r=45 cannot happen since 23 and 45 will be stored in a single character array.

3. What is the output of the following code?

p=eval(cos(0));

a) p=1
b)

    p=
      1

c) Syntactical error
d) The eval command does not exist
Answer: c
Explanation: An input to the eval command will have to be within a pair of inverted commas. Here, we haven’t used them so MATLAB will return an error.

4. What is the output of the following code?

evalc(cos(0))

a) No such command
b) Error
c) 1
d) ‘ ans=1 ’
Answer: d
Explanation: Evalc takes the entire output of the input expression into a string. Hence, it will show a as an answer were the entire thing will be shown, within, as a string.v

5. What is the size of p from the following code?

P=evalc(‘ ’)

a) 2 bytes
b) 0 bytes
c) Error
d) 6 bytes
Answer: a
Explanation: The evalc command results in an empty character array. Hence, the size of p will be 0 but it’s class will be char.

6. The for keyword can be used to evaluate expressions using eval().
a) True
b) False
Answer: b
Explanation: Reserved keywords cannot be used explicitly in the eval() command. But they can be used by mentioning a function which is defined to use a for loop.

7. The evalc function is different from the eval command ________
a) no difference
b) from precision of output
c) from nature of output
d) from display of output
Answer: d
Explanation: The entire output is displayed as a character array using evalc while the eval command displays the output itself. There is no difference in precision or nature of output.

8. What is common to evalc and the eval commands, apart from the output?
a) The error message is displayed separately
b) The outputs are of different accuracy
c) There is nothing common
d) evalc does not exist
Answer: a
Explanation: The error messages, if any error, pertaining to the input expression, occurs, are displayed separately by both the command.

9. What is the output of the following code?

P=eval(‘ ’)

a) P= ‘ ’
b) P will be an empty character array
c) Error
d) No such function
Answer: c
Explanation: We have assigned the output of the eval command to an expression. Hence, we will have to give an expression within ‘’ which results into any value. Here, we haven’t provided any expression so the statement in not complete. Hence, MATLAB returns an error.

10. What is the output of the following code?

feval(sum”,1234,1)

a) Syntactical error
b) Logical Error
c) 1235
d) ‘1234’
Answer: c
Explanation: There is no error in the following code. The string input to the feval command can also be given within ‘’. Hence, the final output is 1235.
Output: 1235

Evaluation – 2

1. What is the output of the following code?

feval[round’,123.12]

a) 123
b) 123.12
c) 123.1
d) Error
Answer: d
Explanation: The input arguments to the feval command cannot be given within [], it should be included within parentheses. Hence, an error will be returned. If the input was within parentheses, the output would’ve been 123 only.

2. The feval command can evaluate __________
a) A single command
b) Multiple commands
c) Multiple functions
d) No such function exists
Answer: a
Explanation: The input string argument to the feval command should contain only 1 command. If we give multiple commands to be evaluated, it will give an error.

3. The eval command can evaluate __________
a) A single function
b) Only a single command
c) The function does not exist
d) Multiple commands
Answer: d
Explanation: The eval command can evaluate multiple commands at the same time and show their answer but it cannot assign to variables.

4. The input to the eval command is given using [].
a) True
b) False
Answer: b
Explanation: The input argument to the feval command is given within parentheses. Hence, MATLAB will return an error if we use [].

5. The input to the evalc command is given using {}.
a) True
b) False
Answer: b
Explanation: The input argument to the evalc command is given within parentheses. Hence, MATLAB will return an error if we use {}.

6. What is the value of ans from the following code?

feval(sin(0),cos(0),123)

a) 123
b) 0,1,123
c) Error
d) 0
Answer: a
Explanation: The value of ans will keep changing after every expression gets evaluated in the string argument. So the final expression is 123 which will be the last value assigned to the ans variable.
Output: 123

7. What is the output of the following code?

evalc(‘Laplace(t))

a) Syntactical Error
b) Symbolic error
c) ‘1/s2
d) 1/s2
Answer: b
Explanation: There is an error in the following code. It would’ve simply evaluated the laplace transform of t which is 1/s2 but t is not defined as symbolic. Hence, MATLAB will return an error.

8. Which function gets disabled while using evalc()?
a) diary
b) sin
c) inf
d) round
Answer: a
Explanation: The diary command gets disabled when we try to use the evalc command in MATLAB. The commands sin and round work just fine while Inf is not a function.

9. How much does the precision change while finding sin(x) using evalc and eval?
a) 10%
b) 2%
c) 20%
d) No change
Answer: d
Explanation: The purpose of both the commands is simply to make the commands, provided in the input argument, work. They do not hamper the precision with which the commands evaluate a value.

Debugging

1. Can we save a MATLAB program while in debugging mode?
a) True
b) False
Answer: b
Explanation: While in debugging mode, we are really trying out the function. So, when the debugging mode starts, the command line is halted at a certain line in the function and the evaluation of the function is stopped. Hence, it’s not possible to bring a change in the function and save it in the directory. We’ve to exit from the debugging mode and then we can save the function.

2. To enter a value and change the course of working of a function while it is working, we use the __________
a) dbstop
b) db
c) keyboard
d) enter
Answer: c
Explanation: The keyboard command is pre-defined in MATLAB and allows us to enter the debugging mode. We need to give the command in our m.file where we want to start debugging and then the control will return to the keyboard where we will be able to give new value to the parameters involved in the function.

3. To end the debugging mode, we use the __________
a) dbquit
b) dbend
c) debugend
d) No such function
Answer: a
Explanation: The dbquit command can be used to exit from the debugging mode. This is pre-defined in MATLAB. dbend and debugend commands don’t exist.

4. Which of the following command would allow us to continue the evaluation of a function in the debugging mode?
a) dbcont
b) dbcontinue
c) continue
d) debugcont
Answer: a
Explanation: The dbcont command would allow us to continue the working of our function after we have entered the debugging mode. The continue command is used in loops, it won’t help us in this case.

5. What is dbstop command?
a) exits from the debugging mode
b) pauses for the debugging mode when a condition is reached only
c) exits the debugging mode at any point in the function
d) does not exist
Answer: b
Explanation: The dbstop command stops the debugging mode. Now, we can give the dbstop command in many ways. One of the ways is mentioning a condition, regarding when the working of the function should stop, to enter the debugging mode. To exit from the debugging mode, we need to use the dbquit command.

6. The dbstop command, for anonymous functions __________
a) Pauses, for debugging, after the line which dbstop indicates
b) Pauses, for debugging, before the line which dbstop indicates
c) exits from the debugging mode after the line which dbstop indicates
d) exits from the debugging mode before the line which dbstop indicates
Answer: a
Explanation: For anonymous functions, the dbstop command works differently in contrast to pre-defined functions. This nature of working is only for anonymous functions which are not stored in the MATLAB memory. The dbquit command is used to exit from the debugging mode.

7. The dbquit command, if placed in an m.file, will __________
a) never run the debugging mode
b) exit from the debugging mode
c) result in an error while running the function
d) dbquit does not exist
Answer: c
Explanation: This command will result in an error if it is placed within an m.file. This is because debugging commands can only be used when the control enters debug mode and we need to give these commands only from the command window.

8. The dbcont command, if placed in an m.file, will __________
a) not allow the function to be saved
b) give error when we enter dbcont in debug mode
c) will give an error during function call
d) dbcont does not exist
Answer: b
Explanation: The function will be saved and it will run. After enter the debugging mode, and making any desirable change, when we enter the dbcont command, the function will keep working until it invokes the dbcont command. Then it will result in an error and the control will break the evaluation of the function entirely.

9. The dbstop command, for functions stored in the system memory, with a line mentioned will __________
a) pause the evaluation of the function before the mentioned line
b) exit the evaluation of the function before the mentioned line
c) pause the evaluation of the function during the mentioned line
d) pause the evaluation of the function after the mentioned line
Answer: a
Explanation: In contrast to anonymous functions, the dbstop command will pause the working of a function just before the mentioned line. It will pause the evaluation and enter the debugging mode.

10. The debug commands can be run in
a) functions
b) live scripts
c) command window
d) nowhere
Answer: c
Explanation: The debug commands can only be run from the command window. If they are present in user-defined functions or live scripts, the working of the function would entirely stop when any debug command is invoked while evaluation of the function.

11. What are the contents of a after the following events?

function p=avg(x)
n = length(x);
Command Window: 
>> x=[1 2 3 4 5]
>> dbstop in avg at 2 if length(x)>4
>> a=avg(x)

a) Enters debugging mode
b) Error in the command window
c) Error in the function
d) 5
Answer: a
Explanation: This is a simple code. The kernel will enter the debugging mode since the condition given by dbstop is not satisfied. The length of the x vector is 5 in the command window which results in the kernel to shift to the debugging mode.

12. What are the contents of a after the following events?

function p=avg(x)
n = length(x);
Command Window: 
>> x=[1 2 3 4 5]
>> dbstop at 6 if length(x)>4
>> a=avg(x)

a) Error in 2nd line of command window due to line no
b) Error in 2nd line of command window due to line missing function name
c) 5
d) Enters the debugging mode
Answer: b
Explanation: The first error encountered by the debug command is that the function name is missing i.e the function name must be given to the dbstop command. The second error is the fact that the function, defined already, has only 2 lines but we have asked the kernel to enter the debugging mode at line 5. Since MATLAB returns the first error it faces.

13. What are the contents of a after the following events?

Line 1: function p=avg(x)
Line 2: n = length(x);
Line 3: n=n+3
Command Window: 
>> x=[1 2 3 4 5]
>> dbstop in avg if length(x)>4
>> a=avg(x)

a) 8
b) Enters debugging mode after execution of the last line
c) Enters debugging mode at the first line only
d) Enters debugging mode at the 2nd line
Answer: c
Explanation: We haven’t mentioned the line no. where the debugging mode should begin if the condition provided with the dbstop command is unsatisfied. Hence, debugging will start at the first line only.

14. What are the contents of a and b after the following events?

Line 1: function p=avg(x)
Line 2: n = length(x);
Line 3: n=n+3
Command Window: 
>> x=[1 2 3 5]
>> dbstop in avg if length(x)>4
>> a=avg(x);
>> x=[1 2 3 4 5];
>> b=avg(x);

a) Error in the function
b) a=4,b=5
c) Enters debugging mode at b but a=4
d) Outputs are suppressed
Answer: b
Explanation: The dbstop command would work if the function is called after we have given the dbstop command. It won’t affect the working of the function if the function is called any more number of times i.e the condition provided in the dbstop command won’t result in entering the debugging mode if the working of the function does not satify it.

15. The return command is similar to the dbcont command.
a) True
b) False
Answer: a
Explanation: The working of the return command is similar to the dbcont command. It will get from the debugging mode and return to evaluate the function completely.

Up To Date MATLAB MCQs – MATLAB Programming ( MATLAB ) MCQs