Midterm Exam #1

CS 152 -- Spring 2008

Mark your answers on a scantron 882-E marking sheet, not on these question sheets as only the 882-E form will be used for scoring.

On the 882-E form, in the boxes provided in the margin, be sure to write the following:

Your full name for NAME
CS 152 for SUBJECT
M1-SPRING-2008 for TEST NO.

At the conclusion of the exam, turn in only the 822-E marking sheet and keep these question sheets.

.1. What does the Scheme interpreter print after evaluating following Scheme fragment:

(car (cdr (cdr (list 1 2 3))))

A) 2

B) 3

C) (3)

D) (2 3)

E) Error, need to quote the values 1, 2, and 3

.2. What does the Scheme interpreter print after evaluating following Scheme fragment:

(cons 1 2)

A) (quote 1 2)

B) (cons 1 2)

C) (1 . 2)

D) (1 2)

E) Error, need to quote 1 and 2

.3. What does the Scheme interpreter print after evaluating following Scheme fragment:

(cons a b)

A) (quote a b)

B) (cons a b)

C) (a . b)

D) (a b)

E) There is an error, because a and b need to be quoted

.4. What does the Scheme interpreter print after evaluating following Scheme fragment:

(cons 1 (cons 2 3))

A) (cons 1 (cons 2 3))

B) (1 2 3)

C) (1 . 2 3)

D) (1 2 . 3)

E) Error, need to quote 1, 2, and 3

.5. What does the Scheme interpreter print after evaluating following Scheme fragment:

(quote a b)

A) (quote a b)

B) '(a b)

C) a b

D) (a b)

E) There is an syntax error due to wrong number of arguments

.6. What does the Scheme interpreter print after evaluating following Scheme fragment:

(quote (a b))

A) (quote (a b))

B) '(a b)

C) a b

D) (a b)

E) There is a syntax error due to argument structure

.7. What does the Scheme interpreter print after evaluating following Scheme fragment:

(cdr (quote ((3 4) (5 6))))

A) (5 6)

B) ((5 6))

C) ((3 4) (5 6))

D) ()

E) Scheme prints an error

.8. What does the Scheme interpreter print after evaluating following Scheme fragment:

(cdr ((3 4) (5 6)))

A) (5 6)

B) ((5 6))

C) ((3 4) (5 6))

D) ()

E) Scheme prints an error

.9. What does the Scheme interpreter print after evaluating following Scheme fragment:

(caadr (quote ((3 4) (5 6))))

A) 3

B) 5

C) 6

D) (5 6)

E) (6)

.10. What does the Scheme interpreter print after evaluating following Scheme fragment:

(cdadr (quote ((3 4) (5 6))))

A) 3

B) 5

C) 6

D) (5 6)

E) (6)

.11. What does the Scheme interpreter print after evaluating following Scheme fragment:


(cons '(11 22) '(33 44))

A) ((11 22) 33 44)

B) ((11 22) (33 44))

C) (11 22 (33 44))

D) (11 22 33 44)

E) Scheme reports an error

.12. What does the Scheme interpreter print after evaluating following Scheme fragment:


(list '(11 22) '(33 44))

A) ((11 22) 33 44)

B) ((11 22) (33 44))

C) (11 22 (33 44))

D) (11 22 33 44)

E) Scheme reports an error

.13. What does the Scheme interpreter print after evaluating following Scheme fragment:


(append '(11 22) '(33 44))

A) ((11 22) 33 44)

B) ((11 22) (33 44))

C) (11 22 (33 44))

D) (11 22 33 44)

E) Scheme reports an error

.14. Which of the following Scheme expressions evaluates #t (true)?:

A) (list? ())

B) (list? cons 1 2))

C) (list? '(1 2 3))

D) All of the above

E) (A) and (C)

.15. Which of the following Scheme expressions evaluates #t (true)? (Pick one answer):

A) (eq? 7 7)

B) (eq? (+ 3 4) (+ 2 5))

C) (eq? '(11 22) '(11 22))

D) All of the above

E) (A) and (B)

.16. Which of the following Scheme expressions evaluates #t (true)? (Pick one answer):

A) (equal? 7 7)

B) (equal? (+ 3 4) (+ 2 5))

C) (equal? '(11 22) '(11 22))

D) All of the above

E) (A) and (B)

.17. Consider the following MzScheme interactive session:


> (define (foo n)
   (lambda (i)
     (set! n (+ n i))
     n))
> (define x (foo 100))
> (x 3)
103
> (x 10)
113
> (define y (foo 50))
>

What gets printed when the user enters:
(y 3)

A) Nothing prints

B) 113

C) 116

D) 50

E) 53

.18. Consider the following Scheme program:


(define (bar n)
  (if (null? n) n
    (cons (car n) (bar (cdr n)))))

If bar is called with a list argument, it

A) Always returns null, ()

B) Returns the argument list

C) Returns the reversed argument list

D) Returns the first element of the argument list

E) Returns the last element of the argument list

.19. Consider the following Scheme program:


(define (bar n)
  (define (bar-iter n result)
     (if (null? n) result
        (bar-iter (cdr n) (car n))))
   (bar-iter n '()))

If bar is called with a list argument, it

A) Always returns null, ()

B) Returns the argument list

C) Returns the reversed argument list

D) Returns the first element of the argument list

E) Returns the last element of the argument list

.20. Consider the following Scheme program:


(define (bar n)
  (define (bar-iter n result)
     (if (null? n) result
        (bar-iter (cdr n) (cons (car n) result))))
   (bar-iter n '()))

If bar is called with a list argument, it

A) Always returns null, ()

B) Returns the argument list

C) Returns the reversed argument list

D) Returns the first element of the argument list

E) Returns the last element of the argument list

.21. Consider the following Java program:


interface Incrementor {
  int inc (int i);
};

class Foo
  static public Incrementor gen( int n ) {
    return new Incrementor() {
      public int inc( int i ) {
        n = n + i;
        return n;
      }
    };
  }
}

A) Fails to compile because Foo is not declared "public"

B) Fails to compile because class Foo needs to be in it own separate file

C) Fails to compile and emits a message that n should be declared "final"

D) Fails to compile because Java doesn't support closures

E) (C) and (D)

.22. A "binding":

A) Allocates a memory entity for a variable or object

B) Associates a name to an entity

C) Represents the rest of the computation given a particular point in the computation

D) Is an abstraction binding a function to its extent

E) None of the above

.23. A "continuation":

A) Allocates a memory entity for a variable or object

B) Associates a name to an entity

C) Represents the rest of the computation given a point in the computation

D) Is an abstraction binding a function to its extent

E) None of the above

.24. A "closure":

A) Allocates a memory entity for a variable or object

B) Associates a name to an entity

C) Represents the rest of the computation given a point in the computation

D) Is an abstraction binding a function to its extent

E) None of the above

.25. "First-class functions:"

A) Are not supported by Java

B) Are functions that can be the return result of a procedure/function

C) Are supported by Scheme, JavaScript

D) All of the above

E) None of the above

(Note, be careful not to confuse with first class objects)

.26. "Anonymous functions":

A) Are not supported by Java

B) Are definable by lambda expressions

C) Are supported by Scheme, JavaScript

D) All of the above

E) None of the above

(Note, be careful not to confuse with anonymous classes)

.27. Which of the following is most likely used in an implementation of BASH's scoping semantics?:

A) A Stack of Activation Records

B) An A-list (Association List)

C) A Hash Table

D) Closures

E) None of the Above

.28. Which of the following is most likely used in an implementation of emacs-lisp's scoping semantics?:

A) A Stack of Activation Records

B) An A-list (Association List)

C) A Hash Table

D) Closures

E) None of the Above

.29. Consider the following code fragment:

int x = 0;
int f () { return x; }
int g () { int x = 1; return f(); }

Consider what g() returns under two situations:

  1. lexical scope and dynamic extent semantics (e.g., Java style semantics)
  2. indefinite scope and dynamic extent semantics (e.g., emacs-lisp style semantics)

A) g() returns 0 under either of these semantics

B) g() returns 1 under either of these semantics

C) g() returns 0 using lexical scope (and dynamic extent) semantics

D) g() returns 1 using lexical scope (and dynamic extent) semantics

E) None of the above

.30. Consider the following JavaScript program:

outer = function (x) {
    inner = function (y) {
      x = x + y;
      return x;
    };
    return inner;
};

afunc = outer(5);
afunc(3);
afunc(2);
The last call, afunc(2), returns 10 because JavaScript uses:

A) A-Lists

B) Continuations

C) Closures

D) Prototype inheritance

E) Private inheritance