R Programming Quizzes

Control Flow

Question 1

What will be the output of the following R code? x <- 5; if (x > 10) { print("Greater") } else { print("Not Greater") }

Question 2

To iterate over each number from 1 to 10 in a loop, the standard R syntax is for (i in _____) { ... }.

Your answer here.

Question 3

Which R control flow statement is specifically designed for choosing one of many options based on the value of a variable, similar to a case statement in other languages?

Question 4

What is the primary difference between the if-else statement and the vectorized ifelse() function?

Question 5

In an R loop, which keywords are used to alter the flow of the loop's execution?

Question 6

The keyword to start a loop that continues as long as a specified condition is TRUE is _____.

Your answer here.

Question 7

What is the final value of the variable counter after this code block is executed? counter <- 0; for (i in 1:4) { if (i %% 2 == 0) { next }; counter <- counter + 1 }

Question 8

Which of the following correctly describes how an if statement handles a vector as its condition in R?

End of Quiz

Your final score is: 0 / 0

List of Questions

Question 1: What will be the output of the following R code? `x <- 5; if (x > 10) { print("Greater") } else { print("Not Greater") }`

Answers: Not Greater.

Question 2: Which R control flow statement is specifically designed for choosing one of many options based on the value of a variable, similar to a case statement in other languages?

Answers: switch().

Question 3: What is the primary difference between the `if-else` statement and the vectorized `ifelse()` function?

Answers: The `if-else` statement evaluates a single condition, while `ifelse()` can test a condition for every element of a vector and return a result vector..

Question 4: In an R loop, which keywords are used to alter the flow of the loop's execution?

Answers: break, next.

Question 5: What is the final value of the variable `counter` after this code block is executed? `counter <- 0; for (i in 1:4) { if (i %% 2 == 0) { next }; counter <- counter + 1 }`

Answers: 2.

Question 6: Which of the following correctly describes how an `if` statement handles a vector as its condition in R?

Answers: It only uses the first element of the vector and ignores the rest, often with a warning..