menu

Basic R Programs for Beginners

Basic R Programs for Beginners - Important Points


1.

What is the output of the following R code snippet?

x <- 2
y <- 3
z <- x + y
print(z)

A. 2

B. 3

C. 5

D. Error

Discuss Work Space

Answer: option c

Explanation:

The code defines two variables x and y, adds them together and stores the result in z. Then it prints the value of z, which is 5.

2.

What is the output of the following R code snippet?

x <- "Hello"
y <- "World"
z <- paste(x, y)
print(z)

A. HelloWorld

B. Hello World

C. Error

D. NA

Discuss Work Space

Answer: option b

Explanation:

The code defines two variables x and y as character strings, concatenates them using the paste function, and stores the result in z. Then it prints the value of z, which is "Hello World".

3.

What is the output of the following R code snippet?

x <- c(1, 2, 3, 4, 5)
y <- sum(x)
print(y)

A. 1

B. 5

C. 15

D. Error

Discuss Work Space

Answer: option c

Explanation:

The code defines a vector x with 5 elements, then calculates the sum of the elements using the sum function and stores the result in y. Then it prints the value of y, which is 15.

4.

What is the output of the following R code snippet?

x <- 1:10
y <- x[x %% 2 == 0]
print(y)

A. 1 3 5 7 9

B. 2 4 6 8 10

C. 1 2 3 4 5

D. 2 3 5 7 11

Discuss Work Space

Answer: option b

Explanation:

The code defines a vector x with integers from 1 to 10, then uses the modulo operator to select only the even elements of x, and stores the result in y. Then it prints the value of y, which is the vector [2 4 6 8 10].

5.

What is the output of the following R code snippet?

x <- c(1, 2, 3, 4, 5)
y <- c(2, 3, 4)
z <- x %in% y
print(z)

A. TRUE TRUE TRUE FALSE FALSE

B. FALSE TRUE TRUE TRUE FALSE

C. TRUE TRUE FALSE FALSE FALSE

D. FALSE FALSE FALSE TRUE TRUE

Discuss Work Space

Answer: option b

Explanation:

The code defines two vectors x and y, then uses the %in% operator to check which elements of x are also present in y. The result is a logical vector with the same length as x, indicating whether each element is in y or not. The resulting vector is [FALSE TRUE TRUE TRUE FALSE].


Subscribe for Latest Career Trends
Subscribe Now
Use AI and ChatGPT for Career Guidance

Unlock Your Future

Join Now
Worried for Placements in 2024?

Join FAST TRACK Course

Join Now
Supercharge Your SUCCESS

Join All in One Placement Mock Tests-2024

Join Now