Scripting Languages CSCI-GA.3033.003, Summer 2012 quiz2

Thursday 7/12/2012. 40 points.

quiz2_1 Lexical and dynamic scoping

(8 points) What does the following Perl script print?
our ($var1, $var2) = ('G1', 'G2'); #globals
sub funA {
  print $var1 . "\n";
  print $var2 . "\n";
}
sub funB {
  local $var1 = 'B1'; #dynamic scoping
  my    $var2 = 'B2'; #lexical scoping
  funA;
}
funB;

quiz2_2 Regular expressions

2a.
(4 points) Give two examples of strings that match the following Perl regular expression:
^"([^\\"]|(\\.))*"$
2b.
(4 points) Give two examples of strings that do not match the Perl regular expression from question 2a.
2c.
(4 points) Give an example of a Perl regular expression that is, formally speaking, not regular. In other words, your example must be a Perl regular expressions that uses features that are not merely a shortcut for the essential features of formal regular expressions.

quiz2_3 Prototypes in JavaScript

Consider the following JavaScript code.
function Dog(name) {
  this.name = name;
}
Dog.prototype.nice = true;
Dog.prototype.meet = function() {
  return this.name + " " + (this.nice ? 'wags its tail' : 'barks');
}
var d1 = new Dog("Daisy"), d2 = new Dog("Buster");
d2.nice = false;
document.write(d1.meet() + "<br/>\n");
document.write(d2.meet() + "<br/>\n");
3a.
(7 points) Draw a UML object diagram with the constructor(s), prototype(s), and new instance(s). The diagram should include the prototype and constructor pointers between objects.
3b.
(3 points) What does the script print?

quiz2_4 Closures

4a.
(5 points) Give a brief definition for the programming-languages concept “closure”.
4b.
(5 points) Give a brief example of a closure in JavaScript that illustrates the definition from question 4a.

http://cs.nyu.edu/courses/summer12/CSCI-GA.3033-003/quiz2.html