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
Which statement is used in R for conditional execution? a) loop b) ifelse c) switch d) function
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
Which function allows a shorthand for an if-else statement in R? a) ifelse b) switch c) ifthenelse d) whenelse
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
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
What will the following R code print? if(5 < 3) print("Yes") else print("No") a) Yes b) No c) Error d) NULL
What type of value does the ifelse() function return in R? a) Logical b) Numeric c) Vector d) Character
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
What is the result of the following R code? ifelse(3 > 2, "True", "False") a) True b) False c) 1 d) 0
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
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
Which loop in R continues indefinitely unless explicitly broken? a) for b) while c) repeat d) ifelse
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
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
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}
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
How do you exit a repeat loop in R? a) break b) exit c) continue d) stop
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
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
How do you skip an iteration in a loop in R? a) break b) continue c) next d) skip
apply Family Functions
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
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()
Which apply function is used to apply a function to matrix rows or columns? a) sapply() b) apply() c) lapply() d) mapply()
Which function applies a function to subsets of a vector or list in R? a) tapply() b) lapply() c) sapply() d) apply()
What does sapply() return in R? a) A list b) A vector or matrix c) A data frame d) A function
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()
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
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
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
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
Qno
Answer
1
b) ifelse
2
b) Greater
3
a) ifelse
4
c) Executes the if block only if the condition is true
5
d) if (condition) statement else statement
6
b) No
7
c) Vector
8
a) It checks conditions for all elements of a vector
9
a) True
10
a) ifelse
11
c) for
12
c) repeat
13
b) It repeats a fixed number of iterations
14
a) 1 2 3
15
a) while(condition) {statement}
16
b) The loop will run infinitely
17
a) break
18
c) break;
19
b) It iterates over values of the vector
20
c) next
21
b) Applies a function to each column or row of a matrix
22
c) lapply()
23
b) apply()
24
a) tapply()
25
b) A vector or matrix
26
b) sapply()
27
b) mapply() is for applying a function to multiple arguments
28
a) 6 15 24
29
b) It indicates the axis (1 for rows, 2 for columns)