Client Side Sample Exam Answers 2011 Question 1. These questions deal with general properties of HTML and JavaScript. a) Recall that HTML is a mark-up language while JavaScript is a programming language. Name three crucial differences between mark-up and programming languages. (8 marks) Markup languages are executed upon page request. Markup languages modify the look and layout of web pages. Programming languages are often event driven. Programming languages such as JavaScript modifies rendering of received HTML markup. b) Briefly explain the term dynamically typed programming language. (Note that JavaScript belongs to this category of programming languages.) (8 marks) A programming language is said to be dynamically typed when the majority of its type checking is performed at run-time as opposed to at compile-time. In dynamic typing values have types, but variables do not; that is, a variable can refer to a value of any type. c) Recall that JavaScript functions are frequently event driven, i.e. their execution is triggered by the occurrence of certain events, such as mouse or keyboard events. While mouse and keyboard events are caused by human user input, some JavaScript functions are triggered by other events which are not caused by user interaction with input hardware. Name an example of such an event and briefly describe the purpose of a corresponding JavaScript function (even handler) that gets executed when this event occurs. You do not need to write any code in your answer. (8 marks) When an Ajax request completes, the Ajax event handler .ajaxComplete() may be called. All of the event handlers that have been registered with this method execute at this time. d) Recall that JavaScript supports first-class functions. Briefly explain this term and describe a typical programming situation where first-class functions are used. An exact sample code listing is not required. (8 marks) A programming language is said to have first-class functions if the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures. 1 A simple example of a first-class function is the map function, which takes as its arguments a function and a list, and returns the list formed by applying the function to each member of the list Question 2. These questions cover the AJAX web programming method. a) Consider the JavaScript code below. xmlHttp.open('GET', url, false); xmlHttp.send(null); use_response(xmlHttp.responseText); We assume that the variable xmlHttp contains the AJAX object, the variable url holds a valid URL, and the user defined function use_response(txt) does something useful with the response from the remote server. The actual details are not relevant. Please note that this code makes a synchronous (or blocking) request. In other words, loading of the remote data and the main execution thread do not take place in parallel. Thus, the main benefit of AJAX, i.e. asynchronous execution is not exploited. Explain why the code above still offers some advantage over the classic (non-AJAX) HTTP GET request. (8 marks) b) Briefly explain the meaning of the event onreadystatechange. (8 marks) Stores a function (or the name of a function) to be called automatically each time the readyState property changes. c) Explain why in the code block above no event handler is bound to onreadystatechange. (8 marks) Question 3. These questions deal with the JSON data-interchange format. a) Name three crucial differences between data storage in a JSON file as opposed to data storage in a SQL database. (8 marks) Data in JSON files is stored in associative arrays. Data in SQL databases is stored as entries in tables. A username, password & database name have to be provided by database users before a database can be used. 2 b) Name three crucial differences between the JSON format and the XML format. (8 marks) When defining variables and/or values both the variable name and its corresponding value are each enclosed in a pair of quotes. When defining variables and/or values the variable value is surrounded by the variable name which is enclosed in both the ‘<’ & ‘>’ signs respectively. In XML there must always be a variable name in ‘<’ & ‘>’ sings before the variable value and the exact same variable name in ‘</’ & ‘>’ signs after the variable value. This must be done in order for xml code to run correctly. XML relies on an external parser while JSON can be parsed directly by JavaScript interpreter. c) Recall the function getJSON from the jQuery library. It might be called in the following form: $.getJSON(x, function(y) {z}) where x,y,z are only short notations for other code. Explain the meaning of each of these three substitutions x,y,z. (8 marks) x: This is name of the file from which the JSON data is loaded. y: This is the function needed to run the getJSON function. z: This is the code to be executed by the getJSON function. 3