LISP Questions & Answers – Difference Between Setq and Setf

This set of LISP Multiple Choice Questions & Answers (MCQs) focuses on “Difference Between Setq and Setf”.

1. What is the difference between setf and setq?
a) Setq is used for symbols only but setf can be used anywhere
b) No difference is there
c) Setf is used only for numbers but setq used anywhere
d) Setf is for lists and setq for numbers
View Answer

Answer: a
Explanation: Setq is used only for setting the values of symbols but setf can be used for anything. Setf can be used for setting value of any data-type and not only symbols.

2. Choose the correct about Setf and Setq.
a) Setq can be used in place of setf but not vice versa
b) Setf can be used in place of setq but not vice versa
c) Both can be used in place of each other
d) Neither can be used in place of each other
View Answer

Answer: b
Explanation: Setq is used only for symbols and setf is used for any data type, so Setf can be used in place of setq but setq cannot be used in place of setf.

3. How many arguments do setf or setq can have in single braces?
a) 2
b) 4
c) 28
d) Any even number of arguments.
View Answer

Answer: d
Explanation: The setf and setq takes any number of arguments but the number must be even. Even is required because one of the two will be value taker and another one will be value giver. So, it always comes in a pair.
advertisement
advertisement

4. In multiple arguments in setf and setq, it prints ____
a) All the values
b) Only the first value
c) Only the last value
d) Numerically smallest value
View Answer

Answer: c
Explanation: In the process of assigning values to multiple variables with setf and setq, the setf and setq prints only the last value assigned to a variable. It does not print all the values.

5. Setf refers to set field?
a) Yes
b) No
View Answer

Answer: a
Explanation: The full form of setf is set field and it means that it sets the field with the value given.

6. What will the output after the execution of following LISP statement?

(setq (car x) 2)

a) 2
b) Error
c) X
d) 2X
View Answer

Answer: b
Explanation: The statement will show error because (car x) is not a symbol and setq requires arg1 to be a symbol.
Output: Error
advertisement

7. What will be the output after the execution of the following LISP statement?

advertisement
(setf (car '(a b cd)) 2)

a) Error
b) 2
c) A
d) A B
View Answer

Answer: b
Explanation: The (car ‘(a b cd)) will give A and then the setf give value 2 to it.
The setf doesn’t show error to this as it allows any type of datatype as arg1.
Output: 2

8. What is the value of (cdr ‘(a b c d)) after following LISP statements?

(write (setf (cdr '(a b c d)) 2))
(write (cdr '(a b c d)))

a) 2
b) B C D
c) A
d) A 2
View Answer

Answer: b
Explanation: The setf will store the value 2 in (cdr ‘(a b c d)) but when it comes out of the setf braces and (cdr ‘(a b c d)) is again called then it is treated as a new statement and the value of (cdr ‘(a b c d)) again changes to B C D. We can say scope of setf’s cdr remain in that braces only. And the again called cdr has its new scope with its own values.
Output: 2 B C D.

9. What will be the value of pow(x,y) after this LISP statement?

(setf x 5 y 4)

a) 20
b) 1024
c) 625
d) 645
View Answer

Answer: c
Explanation: The value of x is 5 and that of y is 4 after this statement’s execution. And pow(x,y) means x raised to power y in general. So pow(5,4) will give 625.

10. What will be the value of p after this program?

(setf p 2) (setf p 3) (setf p 4)

a) 2
b) 3
c) 4
d) Error
View Answer

Answer: c
Explanation: P is being assigned value 2, then 3 and finally 4. So, the final value assigned will be the current value assigned.

Sanfoundry Global Education & Learning Series – LISP Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.