Review: Event Driven Programming & GUI CSCE155N Matlab Fall 2016
1. Which of the following is not true about event-driven programming? A. An event source is a GUI component that could generate an event when user interacts with it B. An event listener is a process or thread that is attached to an event source to listen for user interaction with that event source C. An event handler is a function that processes a recorded event D. Event-driven programming does not respond as timely as conventional, procedural programming such as using loops
2. Which of the following is not true? A. Edit textboxes, buttons, checkboxes, sliders, and other uicontrol items are event sources found in MATLAB B. In MATLAB, programmers have to explicitly link or map event sources to their listeners C. In MATLAB, programmers have to explicitly link or map event sources to their handlers D. In MATLAB, programmers define, design, and implement event handlers
3. Which of the following is not part of implementing a GUI? A. Create an overall window using MATLAB s built-in function figure() B. Create individual GUI components (event sources) using MATLAB s built-in uicontrol() function C. Attach event sources to their event handlers using @ function handle D. Implement event handlers as nested functions E. All of the above are parts of implementing a GUI
4. Which of the following is not true about event handlers? A. We implement event handlers as nested functions so that variables of the primary function or main script can be accessed within the functions without having to pass them in B. An event handler can be linked or attached to an event source using the field name callback and a function handle @ C. In addition to callback, there are other event types buttondownfcn, widnowbuttonupfcn, windownbuttonmotionfcn, closerequestfcn, etc. D. An event handler can only be attached to one event source
5. Consider the given code. Which of the following is an event source that would trigger an event handler? A. The list B. The edit box C. The push button D. The figure S.fh = figure('units','pixels',... 'position',[500 500 200 300],... 'menubar','none',... 'name','gui_2',... 'numbertitle','off',... 'resize','off'); S.ls = uicontrol('style','list',... 'position',[10 110 180 180],... 'min',0,'max',2,... 'string',{'one';'two';'three';'four'}); S.ed = uicontrol('style','edit',... 'position',[10 60 180 30],... 'string','new String'); S.pb = uicontrol('style','push',... 'units','pix',... 'position',[10 10 180 40],... 'string','add String',... 'callback',{@ed_call,s});
6. Consider the given code. What is not true about the event handler? A. It is triggered when user clicks the push button B. It is attached or linked to the event source edit box and the event source list C. Grab the existing strings in the list, grab the existing strings in the edit box, concatenate them, and replace the existing strings in the list with the concatenated string D. Changing the struct S inside the event handler function also changes the struct S outside the function S.fh = figure('units','pixels',... 'resize','off'); S.ls = uicontrol('style','list',... 'string',{'one';'two';'three';'four'}); S.ed = uicontrol('style','edit',... 'string','new String'); S.pb = uicontrol('style','push',... 'string','add String',... 'callback',{@ed_call,s}); function [] = ed_call(varargin) S = varargin{3}; % Get the structure. oldstr = get(s.ls,'string'); addstr = {get(s.ed,'string')}; set(s.ls,'string',{addstr{:},oldstr{:}}); end
7. Consider the given code. Which of the following GUIs corresponds to the code? C S.fh = figure('units','pixels',... 'position',[500 500 200 300],... 'menubar','none',... 'name','gui_2',... 'numbertitle','off',... 'resize','off'); S.ls = uicontrol('style','list',... 'position',[10 110 180 180],... 'min',0,'max',2,... 'string',{'one';'two';'three';'four'}); S.ed = uicontrol('style','edit',... 'position',[10 60 180 30],... 'string','new String'); S.pb = uicontrol('style','push',... 'units','pix',... 'position',[10 10 180 40],... 'string','add String',... 'callback',{@ed_call,s}); A B D
8. Consider the given code. Which of the following GUIs corresponds to the code? A C B D S.fh = figure('units','pixels',... 'position',[300 300 390 100],... 'menubar','none',... 'name','gui_16',... 'numbertitle','off',... 'resize','off'); S.sl = uicontrol('style','slide',... 'position',[60 10 270 30],... 'min',1,'max',100,'val',50); S.ed(1) = uicontrol('style','edit',... 'position',[10 10 40 30],... 'fontsize',12,... 'string','1'); % Displays the min. S.ed(2) = uicontrol('style','edit',... 'position',[60 50 270 30],... 'fontsize',16,... 'string','50'); % Displays the value. S.ed(3) = uicontrol('style','edit',... 'position',[340 10 40 30],... 'fontsize',12,... 'string','100'); % Displays the max. set([s.ed(:);s.sl],'call',{@sl_call,s});