The S3 object system is a simple and informal way to implement object-oriented programming in R. It allows you to create objects and define methods that behave differently depending on the object's class.
Click to reveal answer
beginner
How do you create an S3 object in R?
You create an S3 object by assigning a class attribute to a list or other R object using the class() function, for example: <code>obj <- list(a = 1); class(obj) <- "myclass"</code>.
Click to reveal answer
intermediate
What is a generic function in the S3 system?
A generic function is a function that decides which method to call based on the class of its input. It uses the UseMethod() function to dispatch the correct method.
Click to reveal answer
intermediate
How do you define a method for an S3 class?
You define a method by creating a function named as generic.class. For example, for generic print and class myclass, define print.myclass <- function(x) { ... }.
Click to reveal answer
intermediate
What happens if no specific method exists for an S3 class?
If no specific method exists for the class, R uses the default method named generic.default. If that is missing, it may give an error or fallback behavior.
Click to reveal answer
How do you assign a class to an object in S3?
AUsing setClass() function
BUsing new() function
CUsing class() function
DUsing object() function
✗ Incorrect
In S3, you assign a class by setting the class attribute with the class() function.
What function is used inside a generic to dispatch methods in S3?
AcallMethod()
BUseMethod()
Cmethod()
Ddispatch()
✗ Incorrect
UseMethod() is the function that dispatches the correct method based on the object's class.
How is an S3 method named for a generic 'summary' and class 'data'?
Adata_summary
Bdata.summary
CsummaryData
Dsummary.data
✗ Incorrect
S3 methods are named as generic.class, so for generic 'summary' and class 'data', it's 'summary.data'.
What type of object is commonly used as an S3 object?
AList
BMatrix
CData frame
DFunction
✗ Incorrect
Lists are commonly used as S3 objects because they can hold multiple named elements.
What happens if you call a generic function on an object with no matching method?
AThe default method is used if available
BAn error is always thrown
CThe object is converted to a list
DThe function returns NULL
✗ Incorrect
If no specific method exists, R tries to use the generic.default method if it exists.
Explain how the S3 object system works in R and how method dispatch is handled.
Think about how R decides which function to run based on an object's class.
You got /4 concepts.
Describe the steps to create a new S3 class and define a print method for it.
Start from making a list and end with printing it using your method.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the S3 object system in R?
easy
A. To speed up the execution of R code
B. To provide a graphical user interface for R
C. To allow functions to behave differently based on the class of their input
D. To manage memory allocation automatically
Solution
Step 1: Understand S3 system role
S3 is designed to let functions act differently depending on the class of the object they receive.
Step 2: Compare options
Only To allow functions to behave differently based on the class of their input describes this behavior. Other options describe unrelated features.
Final Answer:
To allow functions to behave differently based on the class of their input -> Option C
Quick Check:
S3 purpose = different behavior by class [OK]
Hint: Remember: S3 dispatches methods by object class [OK]
Common Mistakes:
Confusing S3 with speed optimization
Thinking S3 manages memory
Assuming S3 provides a GUI
2. Which of the following is the correct way to define an S3 method for the generic function print for objects of class myclass?
easy
A. print_myclass <- function(x) { cat("My class object") }
B. print.myclass <- function(x) { cat("My class object") }
C. print(myclass) <- function(x) { cat("My class object") }
D. function print.myclass(x) { cat("My class object") }
Solution
Step 1: Recall S3 method naming convention
S3 methods are named as generic.class, here print.myclass.
Step 2: Check syntax correctness
print.myclass <- function(x) { cat("My class object") } uses correct assignment and function syntax. Others have invalid syntax or wrong naming.
Final Answer:
print.myclass <- function(x) { cat("My class object") } -> Option B
Quick Check:
S3 method = generic.class naming [OK]
Hint: Use generic.class naming to define S3 methods [OK]
Common Mistakes:
Using underscores instead of dots in method names
Incorrect function assignment syntax
Trying to assign function to generic(class)
3. What will be the output of the following R code?
obj <- structure(list(a = 1), class = "myclass")
print.myclass <- function(x) { cat("Class is myclass\n") }
print(obj)
medium
A. Class is myclass
B. [1] 1
C. Error: could not find function "print.myclass"
D. NULL
Solution
Step 1: Understand object class and method
The object has class "myclass" and a print.myclass method is defined.
Step 2: Method dispatch on print()
Calling print(obj) triggers print.myclass, which prints "Class is myclass" with a newline.
Final Answer:
Class is myclass -> Option A
Quick Check:
print dispatches to print.myclass [OK]
Hint: print calls method matching object's class [OK]
A. Error because print() does not dispatch to print.myclass
B. Error because print.myclass is not defined correctly
C. Error because class assignment is invalid
D. No error; it prints 'Value: 10'
Solution
Step 1: Check class assignment
Assigning class "myclass" to obj is valid.
Step 2: Check method definition and dispatch
print.myclass is correctly defined and print(obj) calls it, printing "Value: 10".
Final Answer:
No error; it prints 'Value: 10' -> Option D
Quick Check:
Class and method correctly set, print dispatches [OK]
Hint: Assign class before calling generic function [OK]
Common Mistakes:
Defining method after calling print
Not assigning class attribute
Misnaming method function
5. You want to create an S3 generic function describe that behaves differently for numeric and character vectors. Which of the following code snippets correctly sets up the generic and methods?
hard
A. describe <- function(x) UseMethod("describe")
describe.numeric <- function(x) { paste("Numeric with length", length(x)) }
describe.character <- function(x) { paste("Character with length", length(x)) }
B. describe <- function(x) { if (is.numeric(x)) "numeric" else "character" }
describe.numeric <- function(x) {}
describe.character <- function(x) {}