Challenge - 5 Problems
S3 Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this S3 method call?
Consider the following R code using S3 classes and methods. What will be printed when
print(obj) is called?R Programming
obj <- structure(list(name = "Alice", age = 30), class = "person") print.person <- function(x) { cat("Person:", x$name, "is", x$age, "years old\n") } print(obj)
Attempts:
2 left
💡 Hint
Remember that S3 methods are functions named like
generic.class and are called automatically by the generic.✗ Incorrect
The
print.person function is the S3 method for class 'person'. When print(obj) is called, R dispatches to print.person, which uses cat to print the formatted string without quotes or index.🧠 Conceptual
intermediate1:30remaining
How does S3 method dispatch work in R?
Which statement best describes how R finds the correct S3 method to call when a generic function is used?
Attempts:
2 left
💡 Hint
Think about the naming pattern of S3 methods.
✗ Incorrect
S3 dispatch is based on naming conventions. When calling a generic like
print(), R looks for a function named print.classname matching the object's class and calls it if it exists.🔧 Debug
advanced2:30remaining
Why does this S3 method not get called?
Given the code below, why does
summary(obj) not call summary.myclass as expected?R Programming
obj <- structure(list(x = 1:5), class = "myclass") summary.myclass <- function(object) { # Custom summary } summary(obj)
Attempts:
2 left
💡 Hint
Check what the method function returns.
✗ Incorrect
The
summary.myclass function contains only a comment and produces no output. R prints the result of summary(obj), which is NULL because the function lacks an explicit return or print statement.📝 Syntax
advanced2:00remaining
Which option correctly defines an S3 method for class 'data.frame' for the generic 'head'?
Choose the correct syntax to define an S3 method
head.data.frame that returns the first 3 rows of a dataframe.Attempts:
2 left
💡 Hint
The original head generic accepts an argument n with default 6.
✗ Incorrect
The correct method matches the generic signature including the argument
n with default value. It returns the first n rows of the dataframe. Option D lacks the n argument, C hard-codes 3 rows without the n argument, and D lacks default for n.🚀 Application
expert2:30remaining
How many items are in the list returned by this S3 generic with multiple classes?
Consider this R code where an object has multiple classes and a generic returns a list. How many elements does the list returned by
foo(obj) contain?R Programming
foo <- function(x) UseMethod("foo") foo.class1 <- function(x) list(a = 1, b = 2) foo.class2 <- function(x) list(c = 3) obj <- structure(list(), class = c("class1", "class2")) result <- foo(obj) length(result)
Attempts:
2 left
💡 Hint
S3 dispatch calls the method for the first class in the class vector.
✗ Incorrect
The object has classes c("class1", "class2"). S3 dispatch calls
foo.class1 because it is the first class. That method returns a list with two elements named 'a' and 'b'. So length(result) is 2.