wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Mastering R Programming Basics

Total questions: 20

Worksheet time: 10mins

Name
Class
Date
1.

What is the syntax for an if statement in R?

a)

if (condition) code_to_execute else code_to_execute_if_false

b)

if (condition) { code_to_execute } else { code_to_execute_if_false }

c)

if { condition } then { code_to_execute }

d)

if condition { code_to_execute }

2.

How do you write an if-else statement in R?

a)

if (condition) { code_if_true } else code_if_false

b)

if (condition) { code_if_true } else { code_if_false }

c)

if condition { code_if_true } else code_if_false

d)

if (condition) code_if_true else { code_if_false }

3.

What function is used to check multiple conditions in R?

a)

ifelse() or case_when()

b)

for()

c)

switch()

d)

repeat()

4.

Explain the purpose of the switch statement in R.

a)

To define a function in R.

b)

To declare variables in R.

c)

To create a loop in R.

d)

The purpose of the switch statement in R is to select and execute code based on the value of an expression.

5.

What is the difference between for loop and while loop in R?

a)

For loop is used for conditional execution; while loop is for fixed iterations.

b)

For loop requires a condition to start; while loop does not need any condition.

c)

For loop can only iterate over vectors; while loop can iterate over any data type.

d)

For loop iterates a fixed number of times; while loop continues based on a condition.

6.

How do you create a for loop to iterate over a vector in R?

a)

foreach (i in my_vector) { print(i) }

b)

for (i in my_vector) { print(i) }

c)

for (i = 1; i <= length(my_vector); i++) { print(my_vector[i]) }

d)

for (i in 1:length(my_vector)) { print(i) }

7.

What is the output of the following code: for(i in 1:5) { print(i) }?

a)

-1

b)

1 2 3 4 5

c)

6

d)

0

8.

How can you break out of a loop in R?

a)

Use the 'continue' statement to exit a loop in R.

b)

Use the 'stop' command to terminate a loop in R.

c)

Use the 'exit' function to break out of a loop in R.

d)

Use the 'break' statement to exit a loop in R.

9.

What is the purpose of the next statement in a loop?

a)

To end the loop immediately.

b)

To repeat the current iteration of the loop.

c)

To skip the current iteration of the loop.

d)

To execute the next statement in the loop.

10.

How do you define a function in R?

a)

Use the 'define' keyword followed by the function name and parameters.

b)

Functions are defined using the 'create' command and a list of arguments.

c)

You can define a function by writing 'my_function = function(x) { x^2 }' without the return statement.

d)

Use the 'function' keyword followed by the function name and parameters, e.g., my_function <- function(x) { return(x^2) }.

11.

What is the scope of a variable defined inside a function in R?

a)

Scope limited to the entire package.

b)

Global scope accessible anywhere in the script.

c)

Local scope within the function.

d)

Scope only within the same file.

12.

How can you return multiple values from a function in R?

a)

Use a list to return multiple values from a function in R.

b)

Use a data frame to return multiple values from a function in R.

c)

Return values as separate variables without any structure.

d)

Use a vector to return multiple values from a function in R.

13.

What are vectorized operations in R?

a)

Vectorized operations are slower than traditional methods in R.

b)

Vectorized operations require explicit loops for each element.

c)

Vectorized operations in R are operations that apply to entire vectors or arrays simultaneously, improving efficiency and code simplicity.

d)

Vectorized operations only work on single numbers in R.

14.

How do vectorized operations improve performance in R?

a)

Vectorized operations are only applicable to numeric data types in R.

b)

They require more memory and increase overhead significantly.

c)

Vectorized operations improve performance in R by allowing simultaneous processing of data, reducing overhead, and leveraging optimized low-level implementations.

d)

Vectorized operations slow down processing by handling data sequentially.

15.

What is the purpose of the apply function in R?

a)

To visualize data in a plot.

b)

To create a new matrix from scratch.

c)

To sort elements in a vector.

d)

To apply a function to rows or columns of a matrix or to elements of a list/data frame.

16.

Explain the difference between lapply and sapply in R.

a)

lapply returns a list, while sapply returns a simplified vector or matrix if possible.

b)

sapply can only be used with data frames, while lapply can be used with any object.

c)

lapply is faster than sapply for large datasets.

d)

lapply and sapply are identical functions in R.

17.

What does the function mapply do in R?

a)

mapply is a function for plotting graphs in R.

b)

mapply applies a function to multiple arguments in a vectorized way.

c)

mapply generates random numbers in a specified range.

d)

mapply is used to create data frames from lists.

18.

How can you use the ifelse function for vectorized conditional operations?

a)

Use ifelse(condition, value_if_true, value_if_false) for vectorized conditional operations.

b)

Apply ifelse only to data frames, not vectors.

c)

Use ifelse with only one argument for default values.

d)

Use ifelse(condition) for single value operations.

19.

What is the role of the return statement in a function?

a)

The return statement ends the function execution and specifies the value to be returned.

b)

The return statement allows for infinite loops within the function.

c)

The return statement is used to print output to the console.

d)

The return statement pauses the function until it is called again.

20.

How do you handle errors in R functions using tryCatch?

a)

Ignore errors and continue execution

b)

Use tryCatch(expression, error = function(e) { handle error }, warning = function(w) { handle warning }, finally = { cleanup code })

c)

Use if-else statements to handle errors

d)

Log errors to a file without handling them