Dive deeper into Perl’s Object-Oriented Programming (OOP) with advanced features like inheritance, polymorphism, roles, traits, and method modifiers using Moose and Moo. Enhance your OOP skills with these concepts.
Inheritance and Polymorphism (10 Questions)
What is inheritance in Perl OOP? A) The ability to create objects B) The ability of one class to use the methods and properties of another class C) The ability to modify objects at runtime D) The ability to clone objects
Which keyword is used in Perl to declare inheritance? A) extends B) inherit C) use D) super
In Perl, how do you call a method from a parent class in a subclass? A) super->method_name() B) parent->method_name() C) self->method_name() D) class->method_name()
What is polymorphism in Perl OOP? A) Multiple classes can share the same method name B) A class can have multiple constructors C) A class can inherit from multiple classes D) A method can be overloaded with different parameter types
Which operator allows method overloading in Perl? A) overload B) ++ C) & D) .
How do you define a subclass in Perl using inheritance? A) class SubClass inherits BaseClass B) package SubClass; @ISA = qw(BaseClass); C) extends BaseClass; D) class SubClass extends BaseClass
Which keyword is used in Perl to access the parent class’s methods and properties? A) super B) base C) parent D) isa
In polymorphism, how can you override a method from a parent class in a subclass? A) By using override keyword B) By redefining the method with the same name C) By using the polymorphic keyword D) By using call_parent()
What is the benefit of polymorphism in OOP? A) It allows for code duplication B) It allows the same method to behave differently based on the object C) It improves the performance of the code D) It restricts method accessibility
What method is typically used to call a parent class constructor in Perl? A) SUPER::new() B) parent->new() C) class->new() D) base->new()
Roles and Traits (Using Moose and Moo) (10 Questions)
What is the main purpose of roles in Perl OOP? A) To define the properties of a class B) To allow code sharing between unrelated classes C) To provide inheritance for a class D) To manage method overloading
Which module provides role-based programming in Perl? A) Moose::Role B) Moo::Role C) Role::Tiny D) Moose::Meta::Role
In Moose, how do you apply a role to a class? A) with 'RoleName'; B) use RoleName; C) apply 'RoleName'; D) extends 'RoleName';
How are roles different from inheritance in Perl OOP? A) Roles can only be applied to classes B) Inheritance allows a class to inherit methods from another class, while roles provide reusable behavior without hierarchy C) Roles provide stronger typing than inheritance D) Roles prevent the subclass from accessing parent methods
Which of the following is a trait of Moo in Perl? A) Lightweight alternative to Moose B) Provides advanced inheritance support C) Only works with hash-based objects D) Implements method overloading
How do you define a method in a Moose role? A) method method_name {} B) sub method_name {} C) has method_name => sub {} D) build method_name {}
What is the difference between Moose and Moo? A) Moose is faster than Moo B) Moo is a lighter version of Moose with fewer features C) Moose does not support roles D) Moo is a deprecated version of Moose
What does the with keyword do in Perl when using roles? A) It includes a role in the class definition B) It calls a method from a role C) It defines a new method in a role D) It creates an alias for a method
How do you create a role with Moo? A) package RoleName; use Moo::Role; B) package RoleName; use Moo; C) package RoleName; sub method_name {} D) package RoleName; extends Moo::Role;
Which of these is a valid feature of Moose roles? A) They can define constructors B) They cannot contain methods C) They can be composed into multiple classes D) They are not reusable
Advanced Method Modifiers (10 Questions)
What is a method modifier in Perl OOP? A) A way to modify the behavior of a method B) A tool to change the name of a method C) A way to create new methods dynamically D) A type of inheritance
Which modifier allows a method to be called before its original implementation in Perl? A) before B) after C) around D) prepend
What does the after method modifier do in Perl OOP? A) It executes code before the original method B) It executes code after the original method C) It replaces the original method entirely D) It prevents the original method from running
How do you apply a method modifier in Perl OOP? A) Using the method keyword B) Using before, after, or around on a method definition C) By using apply on the class D) By extending a parent class method
What is the purpose of the around method modifier in Perl? A) It allows modification of a method’s arguments and results B) It replaces a method entirely C) It ensures a method is called after the constructor D) It prevents the method from being called
Which module provides the before, after, and around method modifiers in Perl? A) Moose B) Moo C) Method::Modifiers D) Moose::Meta::Method
How does before method modifier differ from after? A) before executes code after the method; after executes before B) before modifies return values; after modifies arguments C) before executes code before the method, after executes after D) There is no difference between before and after
Which of these is NOT a valid method modifier in Moose? A) before B) after C) around D) finally
How do you prevent a method in Perl from executing its original functionality? A) Using the noexec modifier B) Using the around modifier and not calling the original method C) Using the block modifier D) Using die() within the method
In Perl, what does the Method::Modifiers module provide? A) It allows inheritance to be used in method definitions B) It provides modifiers like before, after, and around C) It enables the definition of private methods D) It allows method chaining
Answers Table
QNo
Answer (Option with Text)
1
B) The ability of one class to use the methods and properties of another class
2
A) extends
3
A) super->method_name()
4
A) Multiple classes can share the same method name
5
A) overload
6
B) package SubClass; @ISA = qw(BaseClass);
7
A) super
8
B) By redefining the method with the same name
9
B) It allows the same method to behave differently based on the object
10
A) SUPER::new()
11
B) To allow code sharing between unrelated classes
12
A) Moose::Role
13
A) with 'RoleName';
14
B) Inheritance allows a class to inherit methods from another class, while roles provide reusable behavior without hierarchy
15
A) Lightweight alternative to Moose
16
A) method method_name {}
17
B) Moo is a lighter version of Moose with fewer features
18
A) It includes a role in the class definition
19
A) package RoleName; use Moo::Role;
20
C) They can be composed into multiple classes
21
A) A way to modify the behavior of a method
22
A) before
23
B) It executes code after the original method
24
B) Using before, after, or around on a method definition
25
A) It allows modification of a method’s arguments and results
26
C) Method::Modifiers
27
C) before executes code before the method, after executes after
28
D) finally
29
B) Using the around modifier and not calling the original method
30
B) It provides modifiers like before, after, and around