R Programming Language Multiple Choice Questions on “Debugging”.
1. What will be the output of the following R expression?
a) Warning in log(-2.3): NaNs produced
b) 1
c) Null
d) 0
Answer: a
Clarification: This warning lets you know that taking the log of a negative number results in a NaN value because you can’t take the log of negative numbers.
2. Point out the correct statement?
a) Vectorizing the function can be accomplished easily with the Vectorize() function
b) There are different levels of indication that can be used, ranging from mere notification to fatal error
c) Vectorizing the function can be accomplished easily with the vector() function
d) Warnings are generated by the run() function
Answer: a
Clarification: Vectorize() function does not preserve the invisibility of the return value.
3. What will be the value of following R expression?
a) Warning in log(c(-1, 2)): NaNs produced
b) Error in log(c(-1, 2)): NaNs produced
c) Message
d) All of the mentioned
Answer: b
Clarification: Warning is produced due to negative values.
4. What will be the output of the following R code?
> printmessage2 <- function(x) { + if(is.na(x)) + print("x is a missing value!") + else if(x > 0) + print("x is greater than zero") + else + print("x is less than or equal to zero") + invisible(x) + } > printmessage2(NA)
a) “x is a missing value!”
b) “x is greater than zero”
c) “x is less than or equal to zero”
d) Error
Answer: a
Clarification: We can fix Missing value problem by anticipating the possibility of NA values and checking to see if the input is NA with the is.na() function.
5. What will be the output of the following R code?
> printmessage <- function(x) { + if(x > 0) + print("x is greater than zero") + else + print("x is less than or equal to zero") + invisible(x) + } > printmessage(NA)
a) Error
b) Warning
c) Messages
d) x is greater than zero
Answer: a
Clarification: You can’t do that test if x is a NA or NaN value.
6. What will be the output of the following R code?
> mean(x) Error in mean(x) : object 'x' not found > traceback()
a) 1: mean(x)
b) Null
c) 0
d) 2
Answer: a
Clarification: The traceback() function shows you how many levels deep you were when the error occurred.
7. The recover() function will first print out the function call stack when an _______ occurs.
a) Error
b) Warning
c) Messages
d) delete
Answer: a
Clarification: When you choose a frame number, you will be put in the browse and will have the ability to poke around.
