Clojure Programs (L18) Flashcards

1
Q

What error is generated if a recursive function never reaches its stopping condition?

A

StackOverflowError

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When can “recur” be used, and what does it do?

A

Recur can be used when the recursive call is the last expression of a function, and it creates a tail call which saves stack space.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the Clojure equivalent to header guards in C, such that an imported namespace is only processed once.

A

There is no equivalent, as Clojure only processes libraries once by default.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Define the namespace for a file called test.clj.

A

(ns test)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

As there are no public/private keywords, Clojure functions and variables are private by default.

A

False - they are public by default.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

In what way(s) can data be made private in Clojure?

A

By placing “^:private” between “def” and the data’s label.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In what way(s) can functions be made private in Clojure?

A

By using “defn-“ instead of “defn”, or by placing “^:private” between “defn” and the function name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The following is valid if myfunc is defined in test.bar.
(ns test.foo (:require test.bar))
(bar/myfunc x)

A

False, the function invocation must be fully qualified: (test.bar/myfunc x)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The following is valid if myfunc is defined in test.bar.
(ns test.foo (:require test.bar :as bar))
(bar/myfunc x)

A

False - syntax error:
(ns test.foo (:require [test.bar :as bar]))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write code that imports test.bar into test.foo, and make test.bar’s f1 function directly callable (without full qualification).

A

(ns test.foo
(:require [test.bar :as bar :refer f1]))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How are Java classes imported?

A

(ns test.foo (:import java.util.Date))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Assume java.util.Date has been imported. Create an instance of this class within Clojure.

A

(Date.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Assume java.util.Date has been imported. Invoke the getTime() method from an instance of this class.

A

(. (Date.) getTime)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly