Free R Programming Language: Computer Science 114 Questions and Answers — Questions and Answers
Question 1: Which one of the following creates a string in R correctly?
- user <- c("Jane", "Emily", "Shauna") (Correct answer)
- user <- 'Dave"
- user <- c(Dave, Paul, Jane)
- user <- 'Shana
Correct answer: user <- c("Jane", "Emily", "Shauna")
In R, strings are typically enclosed in double quotes (`"`) or single quotes (`'`). To create a vector containing multiple strings, the `c()` function (combine) is used to concatenate them. Option A correctly demonstrates this syntax by creating a character vector where each string element is properly enclosed in double quotes and combined using `c()`.
Question 2: Which of the following statements most accurately sums up R's operator?
- Vector
- Assignment (Correct answer)
- Subtraction
- Factor
Correct answer: Assignment
The `<-` operator is the primary and most commonly used assignment operator in R. It is used to assign values to variables or objects. While the `=` operator can also perform assignment in many contexts, `<-` is the conventional and recommended choice in R programming for clarity and consistency.
Question 3: The starting index for R vectors is.
- 0
- -1
- NULL
- 1 (Correct answer)
Correct answer: 1
Unlike many other programming languages (e.g., Python, Java, C++) that use 0-based indexing, R vectors and other indexed data structures use 1-based indexing. This means that the first element of a vector is accessed using the index `1`, the second with `2`, and so on. This convention is important to remember when working with R data structures.
Question 4: Which of the following best justifies changing an R string (or vector of strings) from lowercase to uppercase?
- It aids in data validation (Correct answer)
- Lowercase strings are harder to work with
- R can't handle lower case letters
- It is a bug in the core compiler
Correct answer: It aids in data validation
Changing the case of strings (e.g., from lowercase to uppercase) in R is a common data cleaning and standardization practice. This process aids significantly in data validation by ensuring consistency across entries, preventing issues where 'apple' and 'Apple' might be treated as distinct values. Standardizing case helps in accurate comparisons, merges, and analyses, improving data quality.
Question 5: What R classes exist for scalar data types?
- numeric, character, integer, boolean, complex
- numeric, character, integer, logical, complex (Correct answer)
- numeric, string, whole, logical, complex
- numeric, string, integer, logical, complex
Correct answer: numeric, character, integer, logical, complex
R's fundamental scalar data types, often referred to as atomic vector types, include `numeric` (for real numbers), `character` (for text strings), `integer` (for whole numbers), `logical` (for boolean TRUE/FALSE values), and `complex` (for complex numbers). These five classes represent the basic building blocks for data storage and manipulation in R.
Question 6: To repeat over a functional matrix?
- If-else statement
- Nested for-loop (Correct answer)
- Conditional expression
- For-loop
Correct answer: Nested for-loop
A matrix is a two-dimensional data structure, organized into rows and columns. To process every element within a matrix, you need to iterate through both its dimensions. A nested for-loop provides the perfect mechanism for this, with an outer loop typically handling rows and an inner loop handling columns, allowing access to each individual cell.
Question 7: The statements in the following program will be executed in the following order: <br> Line 1: print (''welcome'') <br> Line 2: printHello <- function() { <br> Line 3: print(''Hello'') <br> Line 4: } <br> Line 5: printHello() <br> Line 6: print(''done'')
- 5, 6, 1, 2, 3, 4
- 1, 2, 3, 4, 5, 6
- 1, 2, 5, 6, 3, 4
- 1, 5, 2, 3, 4, 6 (Correct answer)
Correct answer: 1, 5, 2, 3, 4, 6
R executes code sequentially. Line 1 prints immediately. Lines 2-4 define the function `printHello`, but the code inside the function body (Line 3) is not executed until the function is called. Line 5 calls `printHello()`, causing Line 3 to execute. Finally, Line 6 executes after the function call completes.
Which one of the following creates a string in R correctly?