250+ TOP MCQs on Connection Interfaces and Answers

R Programming Language Multiple Choice Questions on “Connection Interfaces”.

1. Individual R objects can be saved to a file using the _____ function.
a) save
b) put
c) save_image
d) get

Answer: a
Clarification: The key functions for converting R objects into a binary format are save(), save.image(), and serialize().

2. Point out the correct statement?
a) The complement to the textual format is the binary format
b) If you have a lot of objects that you want to save to a file, you can save all objects in your workspace using the save.image() function
c) The serialize() function is used to convert individual R objects into a binary format that can be communicated across an arbitrary connection
d) All of the mentioned

Answer: d
Clarification: It’s better to stick with a binary format for efficiency and accuracy.

3. Which of the following R statement will save the output to the file for following R code?

> a <- data.frame(x = rnorm(100), y = runif(100))
> b <- c(3, 4.4, 1 / 3)

a) save(a, b, file = “mydata.rda”)
b) save_image(a, b, file = “mydata.rda”)
c) keep(a, b, file = “mydata.rda”)
d) keep_image(a, b, file = “mydata.rda”)

Answer: a
Clarification: You can save all objects in your workspace using the save.image() function.

4. Which of the following statement will load the objects to the file named “mydata.RData”?
a) save(“mydata.RData”)
b) load(“mydata.RData”)
c) loadAll(“mydata.RData”)
d) put(“mydata.RData”)

Answer: b
Clarification: .rda and .RData are fairly common extensions and you may want to use them because they are recognized by other software.

5. Point out the wrong statement?
a) When you call unserialize() on an R object, the output will be a raw vector coded in hexadecimal format
b) serialize() function is the only way to perfectly represent an R object in an exportable format
c) .rda extension is used when save() function is incorporated
d) The complement to the textual format is the binary format

Answer: a
Clarification: Output may get sent to a file, but it could get sent over a network or other connection.

6. ________ opens a connection to a file compressed with gzip.
a) url
b) gzfile
c) bzfile
d) file

Answer: b
Clarification: “file” opens a connection to a file.

7. Connections to text files can be created with the ________ function.
a) url
b) gzfile
c) bzfile
d) file

Answer: d
Clarification: The file() function has a number of arguments that are common to many other connection functions.

8. Which of the following R code creates a connection to ‘foo.txt’?
a) con <- file(“foo.txt”)
b) open(con, “r”)
c) opencon(con, “r”)
d) ocon(con, “r”)

Answer: a
Clarification: Open is used for opening connection to ‘foo.txt’ in read-only mode.

9. Which of the following code opens a connection to the file foo.txt, reads from it, and closes the connection when its done?
a) data <- read.csv(“foo.txt”)
b) data <- read.csvo(“foo.txt”)
c) data <- readonly.csv(“foo.txt”)
d) data <- getonly.csv(“foo.txt”)

Answer: a
Clarification: Connections must be opened, then the are read from or written to, and then they are closed.

10. Which of the following opens connection to gz-compressed text file?
a) con <- gzfiles(“words.gz”)
b) con <- gzfile(“words.gz”)
c) con <- gzfile2(“words.gz”)
d) con <- gzfiles2(“words.gz”)

Answer: b
Clarification: For more structured text data like CSV files or tab-delimited files, there are other functions like read.csv() or read.table().

Leave a Reply

Your email address will not be published. Required fields are marked *