0
0
R Programmingprogramming~20 mins

S4 object system in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
S4 Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[1] "Alice"
B[1] 30
CError: no slot of name "name" for this object of class "Person"
D[1] "p@name"
Attempts:
2 left
💡 Hint
Remember that slots in S4 objects are accessed with the @ operator.
🧠 Conceptual
intermediate
1:30remaining
Which statement about S4 class inheritance is true?
In R's S4 system, which of the following is true about class inheritance?
AAn S4 class can inherit from multiple classes simultaneously.
BAn S4 class can only inherit from one class at a time.
CInheritance is automatically applied to all slots without declaration.
DInheritance is not supported in S4 classes.
Attempts:
2 left
💡 Hint
Think about how S4 supports complex class hierarchies.
🔧 Debug
advanced
2: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)
})
AError: no slot of name "name" for this object of class "Person"
BError: formal argument "object" matched by multiple actual arguments
CError: unused argument (obj)
DNo error; method defined successfully
Attempts:
2 left
💡 Hint
Check the argument names in generic and method functions.
📝 Syntax
advanced
1: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).
AsetClass("Result", slots = list(score = numeric, grade = character))
BsetClass("Result", slot = list(score = numeric, grade = character))
CsetClass("Result", slots = c(score = numeric, grade = character))
DsetClass("Result", slots = list(score = "numeric", grade = "character"))
Attempts:
2 left
💡 Hint
Slots must be declared as a named list with class names as strings.
🚀 Application
expert
3: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")
A5
B2
C4
D3
Attempts:
2 left
💡 Hint
Count all slots from inherited classes plus new slots.