R Variables Vectors and Random Numbers Practice Quiz (11 questions)

All questions and answers

1. In R, what type is assigned to each value in `a <- 42` and `b <- 42L`, respectively?

Integer for `a`; numeric for `b`
Numeric for `a`; integer for `b`
Integer for both `a` and `b`
Numeric for both `a` and `b`

Numeric for `a`; integer for `b`

Explanation

R treats an unsuffixed number such as `42` as double-precision numeric, while `L` explicitly marks an integer. The reversed types confuse the suffix’s role; assigning both values one type overlooks the distinction.

2. Which set lists R’s five primary data types?

Numeric, integer, character, logical, and complex
Numeric, integer, character, factor, and complex
Numeric, integer, character, list, and complex
Numeric, vector, character, logical, and complex

Numeric, integer, character, logical, and complex

Explanation

R’s primary data types are numeric, integer, character, logical, and complex. The alternatives mistakenly include data structures such as vectors or lists, or substitute factor for a primary type.

3. A programmer wants an R variable name that starts with a dot and continues with a letter. Which candidate follows the naming rule?

.hiddenVar
my-var
1hidden
.2hidden

.hiddenVar

Explanation

An R name may start with a dot provided the next character is not a number; later characters may include letters, numbers, dots, and underscores. `.2hidden` violates the dot rule, `my-var` contains a special character, and `1hidden` starts with a number.

4. A developer is choosing a name for an R variable. Which candidate avoids both spaces or special characters and reserved words?

if
score_total
score total
score-total

score_total

Explanation

R variable names cannot contain spaces or special characters and should not be reserved words, so `score_total` is permitted. `if` is reserved, while the other candidates contain a space or hyphen.

5. A dataset needs categorical labels, a mixed-type collection of individual items, and a table whose columns may have different types. Which sequence of R structures fits those needs?

Factor, list, data frame
Data frame, list, matrix
Factor, vector, matrix
Vector, matrix, data frame

Factor, list, data frame

Explanation

Factors represent categorical variables, lists can contain elements of different types, and data-frame columns can hold different types. Vectors and matrices require elements of the same type, so they do not fit the mixed-type collection or heterogeneous-column roles described.

6. In R, what kind of collection does `c(2, 6, 1, 3, 11)` create?

A two-dimensional array of same-type elements
A collection that can mix elements of different types
An ordered collection of elements of the same type
A categorical variable represented by labeled levels

An ordered collection of elements of the same type

Explanation

A vector is an ordered collection whose elements are all of the same type. A mixed-type collection describes a list, a two-dimensional array describes a matrix, and labeled categories describe a factor.

7. An R user runs `seq(from=-4, to=5)` without specifying `by`. What step size does R use for this sequence?

A step size of 9
A sequence length of 1
A step size of 1
A step size of 1.5

A step size of 1

Explanation

When `by` is omitted, `seq()` uses a step size of 1, producing the integers from −4 through 5. A step of 1.5 requires specifying it, 9 is the endpoint difference rather than the step, and sequence length is a different parameter.

8. A data analyst converts a character vector of category labels into a factor. What does the factor represent, and what lies behind its labels?

A numeric variable whose codes are displayed as category labels
A text variable whose values have no numeric codes
A categorical variable with labels backed by numeric codes
A categorical variable whose levels are automatically ranked

A categorical variable with labels backed by numeric codes

Explanation

A factor represents a categorical variable using labels, with numeric codes underlying those labels. A plain character vector is not this coded representation, and numeric codes are not themselves the displayed labels; an ordered factor additionally specifies a level order.

9. A researcher records education categories as high school, undergraduate, and postgraduate, with those levels intended to have a meaningful sequence. Which type of factor fits this variable?

An ordered factor
A character vector
A numeric vector
An unordered factor

An ordered factor

Explanation

An ordered factor is appropriate when categorical levels have a specified order, as in the education sequence. An unordered factor has no specified level order, while numeric and character vectors do not express the ordered-factor classification.

10. An R user wants to generate random values from a normal distribution. Which function prefix should they use?

dxxx
pxxx
rxxx
qxxx

rxxx

Explanation

R uses the rxxx family to generate random variates. The dxxx family evaluates density or mass, pxxx gives cumulative probabilities, and qxxx calculates quantiles.

11. A researcher wants a later call to rnorm(5) to use the pseudo-random sequence associated with seed 23. Which command should run immediately beforehand?

set.seed(23)
dseed(23)
rnorm(5); set.seed(23)
rnorm(23)

set.seed(23)

Explanation

Calling set.seed(23) sets the seed for subsequent pseudo-random generation, so it must precede rnorm(5). Calling rnorm(23) generates values rather than setting a seed, and setting the seed afterward does not affect values already generated.

Study with flashcards

Lock in the answers with 38 flashcards on R Variables Vectors and Random Numbers.

What are R's primary data types?

Numeric, integer, character, logical, and complex.

How does R represent numbers by default, and what marks an integer explicitly?

Numbers default to double-precision numeric values; the L suffix marks an integer, as in 42L.

What may begin an R variable name?

A letter or a dot not immediately followed by a number.

See the flashcards →

Read the study guide

Go over the full study guide for R Variables Vectors and Random Numbers.

Read the study guide →

Related classes

Make your own practice quizzes

Upload your class notes and Revizly writes a practice quiz with explained answers in about a minute.

Quiz maker