MATLAB Questions and Answers – Debugging

«
»

This set of MATLAB Multiple Choice Questions & Answers (MCQs) focuses on “Debugging”.

1. Can we save a MATLAB program while in debugging mode?
a) True
b) False
View Answer

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.
advertisement

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
View Answer

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
View Answer

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.
advertisement
advertisement

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
View Answer

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
View Answer

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.
advertisement

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
View Answer

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
View Answer

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.
advertisement

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
View Answer

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
View Answer

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.
advertisement

10. The debug commands can be run in
a) functions
b) live scripts
c) command window
d) nowhere
View Answer

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
View Answer

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
View Answer

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
View Answer

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
View Answer

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
View Answer

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.

Sanfoundry Global Education & Learning Series – MATLAB.

To practice all areas of MATLAB, here is complete set of 1000+ Multiple Choice Questions and Answers.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter