Free R Programming Language MCQ Questions and Answers — Questions and Answers
Question 1: What R syntax should I use to output "Hello World"?
- "Hello World"
- Hello World'
- All of the other answers are correct (Correct answer)
- print("Hello World")
Correct answer: All of the other answers are correct
In R, simply typing a string literal like `"Hello World"` or `'Hello World'` into the console will cause R to echo its value as output. Additionally, the `print()` function is explicitly designed to display its arguments to the console. Therefore, all listed options are valid ways to output "Hello World" in R.
Question 2: R is a React alias.
- TRUE
- FALSE (Correct answer)
Correct answer: FALSE
R is a programming language and software environment specifically designed for statistical computing and graphics. React, on the other hand, is a JavaScript library used for building user interfaces. They are entirely different technologies serving distinct purposes and are not related or aliases of each other.
Question 3: In R code, how do you add comments?
- // This is a comment
- # This is a comment (Correct answer)
- /* This is a comment
Correct answer: # This is a comment
In R, the hash symbol `#` is used to denote a comment. Any text following `#` on a line is ignored by the R interpreter, allowing developers to add explanatory notes within their code. This is the standard and only single-line comment syntax in R.
Question 4: You must specify a variable's data type in R.
- TRUE
- FALSE (Correct answer)
Correct answer: FALSE
R is a dynamically typed language, meaning you do not need to explicitly declare the data type of a variable when you create it. The R interpreter automatically infers the data type based on the value assigned to the variable. For example, assigning `x <- 5` makes `x` an integer, and `y <- "hello"` makes `y` a character string without prior declaration.
Question 5: How do you make the variable x have the number 5 as its value?
- x <- 5 (Correct answer)
- x : 5
- int x = 5
Correct answer: x <- 5
In R, the `<-` operator is the primary and idiomatic way to assign a value to a variable. It assigns the value on its right to the variable on its left. While `=` can also be used for assignment, `<-` is generally preferred and more common in R programming style.
Question 6: Which function is frequently employed to combine elements?
- Concat()
- Merge()
- Paste() (Correct answer)
- Join()
Correct answer: Paste()
The `paste()` function in R is widely used to concatenate or combine character strings and other elements into a single string. It can take multiple arguments and joins them together, by default separated by a space, or by a specified `sep` argument. This makes it ideal for constructing dynamic text outputs.
Question 7: How many variables with the same value are assigned in a single line?
- var1 <- var2 <- var3 <- "Orange" (Correct answer)
- var1, var2, var3 = "Orange"
- var1, var2, var3 <-"Orange"
Correct answer: var1 <- var2 <- var3 <- "Orange"
R supports chained assignment using the `<-` operator. In the syntax `var1 <- var2 <- var3 <- "Orange"`, the value "Orange" is first assigned to `var3`. Then, the value of `var3` is assigned to `var2`, and finally, the value of `var2` is assigned to `var1`. This efficiently assigns the same value to multiple variables in a single line.
What R syntax should I use to output "Hello World"?