C MCQs on “Non-Local Jumps – 1”.
1. Which of the following header file defines one function longjmp(), and one variable type jmp_buf?
a) stdarg.h
b) locale.h
c) setjmp.h
d) stdlib.h
Answer: c
Clarification: setjmp.h header file defines the macro setjmp(), one function longjmp(), and one variable type jmp_buf.
2. Which of the given options is an array type used for holding information?
a) longjmp
b) setjmp
c) jmp_buf
d) no such variable
Answer: c
Clarification: jmp_buf is an array type used for holding information of macro setjmp() and function longjmp().This is a variable type defined under the header file setjmp.h.
3. Which macro saves the current environment into the variable environment for later use by the function longjmp().
a) setjmp
b) longjmp
c) jmp
d) set_jmp
Answer: a
Clarification: The setjmp() macro saves its calling environment in its jmp-buf argument to be used later by the longjmp() function.
4. If setjmp() macro returns directly from the macro invocation, it______
a) returns zero
b) returns non-zero
c) produces error
d) nothing can be said
Answer: a
Clarification: The setjmp() macro returns zero if the return is from a direct invocation.
5. A non-zero value is returned, if setjmp() returns from a longjmp() function call.
a) false
b) true
Answer: b
Clarification: The setjmp() macro returns a nonzero value, if the return is from a call to the longjmp() function.
6. Select the correct declaration of setjmp().
a) int setjmp(jmp_buf environment)
b) int setjmp(long_jmp environment)
c) int setjmp(jmp_buf )
d) int setjmp(long_jmp)
Answer: a
Clarification: Declaration of the macro setjmp() is int setjmp(jmp_buf environment). ’environment’ is a the object of type jmp_buf.
7. How many times can the macro setjmp() return?
a) one time
b) two times
c) three times
d) many times
Answer: b
Clarification: setjmp() macro returns twice. First time, setjmp() returns zero on its direct invocation. Second time macro returns when longjmp is called with the information written to the environment; now, it returns the value passed to longjmp as second argument.
8. longjmp() function is the only function defined under the header file setjmp.h?
a) true
b) false
Answer: a
Clarification: The only function defined under the header file setjmp.h is longjmp().LONGJMP() resets the registers to the values saved in an environment.
9. Which function restores the environment saved by the most recent invocation of the setjmp() macro in the same invocation of the program?
a) jmp_buf
b) longjmp
c) jmpbuf
d) long_jmp
Answer: b
Clarification: longjmp() is the only function defined under the header file setjmp.h. What setjmp() does is save the contents of the registers so that longjmp() can restore the contents later.
10. Choose the right declaration of longjmp() function.
a) void longjmp(jmp_buf environment, int value)
b) void longjmp(setjmp environment, int value)
c) void longjmp(int value, jmp_buf environment)
d) void longjmp(int value, setjmp environment)
Answer: a
Clarification: The right declaration of the function longjmp() is void longjmp(jmp_buf environment, int value). This function works in pair with setjmp() macro.