UNM Computer Science



Programming Languages Exam --- Spring 1997

Programming Languages and Systems
Comprehensive Exam
In-Class Version


January 15, 1997
Computer Science Department
University of New Mexico

The test monitor will answer some parts of these questions for you if you are unsure of what some of these terms mean. The only penalty is that you will not get any points for the part that was answered for you. You can still answer the other parts of the question. This can get you started on a question even if you are not sure of the definitions.

Do enough questions to total at least 100 points. It is okay to go over 100 points and you can answer as many questions as you wish but all the questions you answer will be counted in your grade and the total will be normalized to 100 points. We will not take the best 100 points. There are two pairs of questions labeled ``part 1'' and ``part 2'' (names and namespace and threads). You can do neither part, either part or both parts of these questions.

  1. (Names and namespaces, part 1) (15 points)
    1. (3 points) What is a name (in computer science)? What is a namespace?
    2. (3 points) Describe a hierarchical file naming system and path names (such as found in UNIX) in terms of names and namespaces.
    3. (3 points) Normally a name is only defined once in a namespace. Explain why this is so but also give any exceptions you can think of.
    4. (3 points) Consider the variable definition in C (or C++): int x = 1;. What is actually being defined? Now consider the assignment: x = 2;. Is this a redefinition of x? Is it s redefinition of anything else? Explain your reasoning.
    5. (3 points) Normally a name can only have one meaning (or one value) at a time in a namespace. Explain why this is so but also give any exceptions you can think of.
  2. (Names and namespaces, part 2) (10 points) Describe namespaces in five programming languages: Scheme, C, C++, and your choice of two from (Smalltalk, Ada, Common Lisp, Fortran (state which version) Java, Perl 5, a shell language, Tcl, or any other language approved by the test monitor). That is, describe all types of namespaces that exist in the language (most languages have more than one), how each one works, what rules apply to it, how it is different from the other namespaces, etc. Describe anything that you think is interesting or important about this type of namespace.
  3. (Object models) (15 points) In classical object-oriented languages (like Smalltalk) a class definition defines the data contained in an object of this class and the functions that can be called on objects of this class. In CLOS the class definitions only define the data in objects of that class. The functions are described separately, independently of any class. In prototype languages (like Self) there are no classes. Compare these three object models. What are the advantages and disadvantages of each one? For each one describe a situation where this object model works better or more naturally than either of the other two object models.
  4. (Threads, part 1) (10 points) The Java language defines threads as part of the language.
    1. (2 points) What is a thread (in this context)?
    2. (3 points) Name one or more other programming languages that include threads as part of their language definition?
    3. (5 points) Explain how programmers using languages that do not define threads (like C++) can still use threads in their programs. Compare the advantages and disadvantages of defining threads in a programming language versus defining them external to the language.
  5. (Threads, part 2) (10 points)
    1. (3 points) Explain what the term thread-safe means. What kinds of entities does the adjective thread-safe apply to?
    2. (4 points) Describe the things you must do to make a program thread-safe. Describe some common things that make a program non thread-safe.
    3. (3 points) What is the relationship (if any) of threads to multiprocessor computers?
  6. (Scope and lifetime) (20 points)

    (2 points) Define scope and lifetime as they relate to names in a programming language.

    For each of the following examples, describe the scope and lifetime of the variable named ``x''. Indicate how many lifetimes the name can have during a single execution of the program it is in. Some of the programs may not be programs you would ever really write but just consider the semantics of the program as written, not how it would be used in a real program. (2 points each)

    1. ; In Scheme
      (define x 1)
    2. ; In Scheme
      (define f (lambda (x) (+ x 1)))
    3. ; In Scheme
      (define f (lambda (x) (lambda (y) (+ x y))))
    4. ; In Scheme
      (define f (let ((x 0)) (lambda () (lambda (y) (set! x (+ x y))
      x))))
    5. // In C++
      int x = 1; // an external definition
    6. // In C++
      int f(int x) { return x+1; } // NOT inside a class definition
    7. // In C++
      int f(int y) { int x = y*y; return x+x; }// NOT inside a class
      definition
    8. // In C++
      // Where f is a public member function in a class declaration
      class AClass {
      public:
        int f(int x) { return x+1; }
        // ...
      };
    9. // In C++
      // Where x is a public data function in a class declaration
      class AClass {
      public:
        int x;
        // ...
      };
  7. Byte-code Compilers (10 points)
    1. (5 points) What is meant by the term byte-code compilation of a programming language? Clearly state the differences between byte-code compilation and regular compilation. Give the advantages of each form of compilation. What is a virtual machine and what is its role in a byte-code compilation system?
    2. (5 points) Compare a byte-code-compiler based system with an interpreter based system. What are the advantages of each? Some interpreters preprocess source code, for example, by converting it into a tree form. Compare this with byte-code compiler.
  8. Constructors and Garbage Collection (15 points)

    1. (4 points) C++ allows you to define constructors and destructors for classes. What is the purpose of each one?
    2. (4 points) What is the equivalent of a constructor in Smalltalk? What is the equivalent of a destructor in Smalltalk?
    3. (4 points) Object-oriented languages with garbage collection have less need for destructors that languages like C++. Explain why. Is there still a need for something like a destructor in such a language or has garbage collection completely eliminated the need for them? Explain your answer.
    4. (3 points) What is the main problem with reference counting as a means of garbage collection?
  9. Run-time Support (15 points)

    A linked program will contain three types of machine code:

    • Machine code that is the compiled version of the programming language code you wrote.
    • Library functions and procedures that you called explicitly in the programming language code you wrote.
    • Run-time support code necessary to support the compiled code.

    You do not call the run-time code directly but it is needed by the compiled code.

    1. (10 points) Give as many examples as you can of the kinds of things that may be in the run-time support for a language. You could look at other questions in this exam for hints.
    2. (5 points) C has very little run-time support code while Ada has a lot. Explain why this is.
  10. Exceptions (15 points)

    1. (5 points) Several languages now have exception handling mechanisms. These are sometimes called software interrupt mechanisms. Explain the ways in which an exception handling mechanism is really a ``software interrupt'' mechanism and explain the ways in which that term is not appropriate, that is, in what ways are exceptions different from software interrupts.
    2. (5 points) Compare the exception handling mechanisms in Ada and C++. How are they the same and how are they different? If you wish, you can pick any two language with exception handling mechanisms instead of C++ and Ada.
    3. (5 points) Explain the role of dynamic binding in interrupt handling mechanisms.