Groovy is a versatile programming language that simplifies Object-Oriented Programming (OOP) concepts with a clear syntax. This guide offers 30 multiple-choice questions (MCQs) on core OOP topics in Groovy, including Classes, Objects, Properties, Methods, Encapsulation, and Default Methods. These MCQs are designed to test your understanding and enhance your skills in Groovy OOP concepts. Explore and learn key programming principles that form the foundation of Groovy development.
MCQs on Groovy Object-Oriented Programming:
Classes and Objects
What keyword is used to define a class in Groovy?
a) class
b) def
c) object
d) Groovy
How do you instantiate an object of a class in Groovy?
a) new ClassName()
b) create ClassName()
c) ClassName.create()
d) object = ClassName()
Which of the following is the correct way to define a class with a constructor in Groovy?
a) class MyClass { def constructor() {} }
b) class MyClass { MyClass() {} }
c) class MyClass { MyClass() { } }
d) class MyClass { constructor() {} }
Which method is used to invoke a class constructor in Groovy?
a) new
b) create
c) invoke
d) constructor
What will happen if you don’t define a constructor in Groovy?
a) The class cannot be used.
b) A default constructor is provided by Groovy.
c) The class is incomplete.
d) Groovy throws an error.
What is the purpose of the @Grab annotation in Groovy?
a) To import a class.
b) To define the constructor.
c) To automatically grab dependencies.
d) To grab a method from a class.
Which of the following is correct to define a property in a Groovy class?
a) property name
b) def name
c) String name
d) public name
What happens when an object is created using def in Groovy?
a) The object is automatically typed.
b) The object type is dynamically assigned.
c) It causes a compilation error.
d) It creates a method, not an object.
Properties and Methods
How do you declare a private property in Groovy?
a) private def name
b) def private name
c) private name
d) name private
In Groovy, which keyword is used to declare a method?
a) def
b) function
c) method
d) declare
What does Groovy’s def keyword represent when used in method declarations?
a) Data type definition
b) Dynamic typing
c) Function keyword
d) Method name definition
What is the return type of a method that does not explicitly return a value in Groovy?
a) void
b) null
c) Nothing
d) Object
How can you define a default method argument in Groovy?
a) def method(arg = 10)
b) def method(arg : 10)
c) def method(arg? = 10)
d) def method(arg – 10)
In Groovy, how do you call a method of a class?
a) class.method()
b) object.method()
c) class.callMethod()
d) object.invokeMethod()
How do you access the properties of an object in Groovy?