MCQs on Control Structures and Loops | R

Control structures and loops are fundamental for flow control in R programming. Learn how to use if-else, various loops like for, while, and repeat, and the apply family functions to write efficient code.


if, else, and ifelse Statements

  1. Which statement is used in R for conditional execution?
    a) loop
    b) ifelse
    c) switch
    d) function
  2. What will the following R code output?
    x <- 10; if(x > 5) print("Greater") else print("Smaller")
    a) Smaller
    b) Greater
    c) NULL
    d) Error
  3. Which function allows a shorthand for an if-else statement in R?
    a) ifelse
    b) switch
    c) ifthenelse
    d) whenelse
  4. In an if-else statement, what happens if no else part is provided?
    a) Returns NULL
    b) Returns an error
    c) Executes the if block only if the condition is true
    d) Skips the condition check
  5. Which is the correct syntax for an if-else statement in R?
    a) if condition {statement} else {statement}
    b) if condition then statement else statement
    c) if condition: statement else statement
    d) if (condition) statement else statement
  6. What will the following R code print?
    if(5 < 3) print("Yes") else print("No")
    a) Yes
    b) No
    c) Error
    d) NULL
  7. What type of value does the ifelse() function return in R?
    a) Logical
    b) Numeric
    c) Vector
    d) Character
  8. How does the ifelse() function work in R?
    a) It checks conditions for all elements of a vector
    b) It only works for numbers
    c) It is used for looping
    d) It replaces switch statements
  9. What is the result of the following R code?
    ifelse(3 > 2, "True", "False")
    a) True
    b) False
    c) 1
    d) 0
  10. Which statement evaluates an expression and returns one of two results in R?
    a) ifelse
    b) if
    c) switch
    d) break

Loops: for, while, and repeat

  1. Which loop in R is used to repeat a block of code a fixed number of times?
    a) while
    b) repeat
    c) for
    d) if
  2. Which loop in R continues indefinitely unless explicitly broken?
    a) for
    b) while
    c) repeat
    d) ifelse
  3. How does a for loop in R work?
    a) It runs until a condition is false
    b) It repeats a fixed number of iterations
    c) It runs indefinitely
    d) It only works with numeric values
  4. What will the following R code output?
    for(i in 1:3) print(i)
    a) 1 2 3
    b) Error
    c) 1 1 1
    d) 3 3 3
  5. What is the syntax for a while loop in R?
    a) while(condition) {statement}
    b) for(condition) {statement}
    c) repeat(statement)
    d) if(condition) {statement}
  6. In R, what happens if the condition in a while loop is never met?
    a) The loop will not run
    b) The loop will run infinitely
    c) The loop will run once
    d) The loop will execute a default value
  7. How do you exit a repeat loop in R?
    a) break
    b) exit
    c) continue
    d) stop
  8. What is the correct way to use a break statement in a loop in R?
    a) break()
    b) break statement
    c) break;
    d) break after condition
  9. What is the default behavior of the for loop in R when iterating over a vector?
    a) It iterates over indices of the vector
    b) It iterates over values of the vector
    c) It stops if the vector length exceeds 10
    d) It skips even-indexed values
  10. How do you skip an iteration in a loop in R?
    a) break
    b) continue
    c) next
    d) skip

apply Family Functions

  1. What does the apply() function in R do?
    a) Applies a function to an entire dataset
    b) Applies a function to each column or row of a matrix
    c) Applies a function to every element of a vector
    d) None of the above
  2. Which of the following is used to apply a function to each element of a list in R?
    a) sapply()
    b) apply()
    c) lapply()
    d) tapply()
  3. Which apply function is used to apply a function to matrix rows or columns?
    a) sapply()
    b) apply()
    c) lapply()
    d) mapply()
  4. Which function applies a function to subsets of a vector or list in R?
    a) tapply()
    b) lapply()
    c) sapply()
    d) apply()
  5. What does sapply() return in R?
    a) A list
    b) A vector or matrix
    c) A data frame
    d) A function
  6. Which function in R is used to apply a function to all elements of a list and return a vector?
    a) lapply()
    b) sapply()
    c) apply()
    d) tapply()
  7. How does mapply() differ from apply() in R?
    a) mapply() works on matrices, while apply() works on vectors
    b) mapply() is for applying a function to multiple arguments
    c) mapply() is used for iteration, while apply() is for function application
    d) There is no difference
  8. What will the following R code output?
    apply(matrix(1:9, nrow=3), 1, sum)
    a) 6 15 24
    b) 12 18 24
    c) 1 2 3
    d) Error
  9. What is the role of the MARGIN argument in the apply() function?
    a) It specifies which function to apply
    b) It indicates the axis (1 for rows, 2 for columns)
    c) It defines the type of output
    d) It defines the number of repetitions
  10. What will lapply(list(1, 2, 3), sqrt) return?
    a) A list of square roots of the elements
    b) A numeric vector
    c) A matrix of square roots
    d) An error

Answer Key

QnoAnswer
1b) ifelse
2b) Greater
3a) ifelse
4c) Executes the if block only if the condition is true
5d) if (condition) statement else statement
6b) No
7c) Vector
8a) It checks conditions for all elements of a vector
9a) True
10a) ifelse
11c) for
12c) repeat
13b) It repeats a fixed number of iterations
14a) 1 2 3
15a) while(condition) {statement}
16b) The loop will run infinitely
17a) break
18c) break;
19b) It iterates over values of the vector
20c) next
21b) Applies a function to each column or row of a matrix
22c) lapply()
23b) apply()
24a) tapply()
25b) A vector or matrix
26b) sapply()
27b) mapply() is for applying a function to multiple arguments
28a) 6 15 24
29b) It indicates the axis (1 for rows, 2 for columns)
30a) A list of square roots of the elements

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top