The MCQs are designed fo the “Making choices” section of Programming in R (http://swcarpentry.github.io/r-novice-inflammation/04-cond.html)
Formative Assessment
YouAreATeenAger <- function(Age)
{
if ((Age > 20) | (Age < 12)) {
teenager <- FALSE
} else {
teenager <- TRUE
}
return(teenager)
}
yourage <- 68
if (YouAreATeenAger(yourage)){
print("You are a teenager")
} else {
print("You are not a teenager")
}
What will be printed?
- “You are a teenager”
- “You are not a teenager”
- An error message
Summative Assessment
Now, you have to figure what this code makes:
WhatYouAre <- function(Age)
{
whatyouare <- "Senior"
if ((Age >= 20) & (Age <70)) {
whatyouare <- "Adult"
}
else if ((Age >= 13) | (Age < 20)) {
whatyouare <- "Teen Ager"
} else {
whatyouare <- "Kid"
}
return(whatyouare)
}
print(WhatYouAre(78))
print(WhatYouAre(45))
print(WhatYouAre(18))
What will be printed?
- “Senior” “Adult” “Teen Ager”
- “Teen Ager” “Adult” “Teen Ager”
- “Senior” “Adult” “Kid”
Explanation of the MCQ
- Wrong. The student has not realized that second choice uses the OR operator instead of AND, so it is always TRUE if first choice is not met.
- Correct Answer
- Wrong. The student has realized that second choice uses the OR operator but thinks that is always FALSE.