Challenge - 5 Problems
S4 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 S4 class slot access?
Consider the following R code using S4 classes. What will be printed when accessing the slot
@name?R Programming
setClass("Person", slots = list(name = "character", age = "numeric")) p <- new("Person", name = "Alice", age = 30) print(p@name)
Attempts:
2 left
💡 Hint
Remember that slots in S4 objects are accessed with the @ operator.
✗ Incorrect
The slot 'name' contains the character 'Alice'. Accessing p@name returns this value.
🧠 Conceptual
intermediate1:30remaining
Which statement about S4 class inheritance is true?
In R's S4 system, which of the following is true about class inheritance?
Attempts:
2 left
💡 Hint
Think about how S4 supports complex class hierarchies.
✗ Incorrect
S4 classes support multiple inheritance by specifying multiple classes in the 'contains' argument.
🔧 Debug
advanced2:30remaining
Identify the error in this S4 method definition
What error will this code produce when defining an S4 method?
R Programming
setGeneric("greet", function(object) standardGeneric("greet")) setMethod("greet", "Person", function(obj) { paste("Hello", obj@name) })
Attempts:
2 left
💡 Hint
Check the argument names in generic and method functions.
✗ Incorrect
The generic expects an argument named 'object', but the method uses 'obj', causing argument matching issues.
📝 Syntax
advanced1:30remaining
Which option correctly defines an S4 class with a numeric slot 'score' and a character slot 'grade'?
Choose the correct syntax to define an S4 class named 'Result' with slots 'score' (numeric) and 'grade' (character).
Attempts:
2 left
💡 Hint
Slots must be declared as a named list with class names as strings.
✗ Incorrect
Slots argument requires a named list with slot names and their class names as strings.
🚀 Application
expert3:00remaining
How many slots does this S4 object have after inheritance?
Given these class definitions, how many slots does an object of class 'Student' have?
R Programming
setClass("Person", slots = list(name = "character", age = "numeric")) setClass("Employee", slots = list(employeeID = "character")) setClass("Student", contains = c("Person", "Employee"), slots = list(grade = "character")) obj <- new("Student", name = "Bob", age = 22, employeeID = "E123", grade = "A")
Attempts:
2 left
💡 Hint
Count all slots from inherited classes plus new slots.
✗ Incorrect
Student inherits slots 'name' and 'age' from Person, 'employeeID' from Employee, and adds 'grade'. Total slots = 4.