MCQs on Basic Object-Oriented Programming | Groovy

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

  1. What keyword is used to define a class in Groovy?
    • a) class
    • b) def
    • c) object
    • d) Groovy
  2. How do you instantiate an object of a class in Groovy?
    • a) new ClassName()
    • b) create ClassName()
    • c) ClassName.create()
    • d) object = ClassName()
  3. 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() {} }
  4. Which method is used to invoke a class constructor in Groovy?
    • a) new
    • b) create
    • c) invoke
    • d) constructor
  5. 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.
  6. 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.
  7. 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
  8. 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

  1. How do you declare a private property in Groovy?
    • a) private def name
    • b) def private name
    • c) private name
    • d) name private
  2. In Groovy, which keyword is used to declare a method?
    • a) def
    • b) function
    • c) method
    • d) declare
  3. 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
  4. 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
  5. 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)
  6. In Groovy, how do you call a method of a class?
    • a) class.method()
    • b) object.method()
    • c) class.callMethod()
    • d) object.invokeMethod()
  7. How do you access the properties of an object in Groovy?
    • a) object[property]
    • b) object->property
    • c) object.property
    • d) objectproperty

Encapsulation Basics

  1. Which of the following best defines encapsulation in Groovy?
    • a) Making properties of a class public.
    • b) Using getter and setter methods to protect data.
    • c) Removing methods from a class.
    • d) Hiding the class from outside usage.
  2. How do you declare a property in Groovy as private and provide getter/setter methods?
    • a) private def name; public def getName() { return name }
    • b) private def name; def setName(value) { name = value }
    • c) private def name; def getName() { return name } def setName(value) { name = value }
    • d) private name; public name() { return name }
  3. Which method can you use to access a private property in Groovy?
    • a) Directly using the property name
    • b) Using a getter method
    • c) Using a setter method
    • d) None
  4. How does Groovy provide access to private properties and methods?
    • a) It doesn’t provide access.
    • b) Through getter and setter methods.
    • c) Through public methods only.
    • d) By creating a new instance of the class.
  5. What is the purpose of setter methods in Groovy?
    • a) To directly modify properties.
    • b) To prevent modification of properties.
    • c) To validate property values before changing them.
    • d) To retrieve the value of properties.
  6. What does Groovy’s @ToString annotation do?
    • a) Generates a string from a method.
    • b) Converts an object into a readable string format.
    • c) Adds a toString method to a class.
    • d) Converts a property to a string.

Using Default Groovy Methods

  1. Which of the following is an example of a default method in Groovy?
    • a) toString()
    • b) def add()
    • c) init()
    • d) print()
  2. In Groovy, what does the collect() method do when used on a list?
    • a) Transforms a list into a new list.
    • b) Collects items from the list.
    • c) Sorts the list.
    • d) Adds elements to the list.
  3. What is the default method for printing output in Groovy?
    • a) print()
    • b) println()
    • c) display()
    • d) echo()
  4. Which of the following methods is used to iterate over a collection in Groovy?
    • a) forEach()
    • b) each()
    • c) iterate()
    • d) loop()
  5. How can you access the first element of a list in Groovy?
    • a) list[0]
    • b) list.first()
    • c) list.getFirst()
    • d) first(list)
  6. What is the use of the find() method in Groovy?
    • a) To find the first occurrence of a value in a collection.
    • b) To sort a collection.
    • c) To find all occurrences in a collection.
    • d) To filter a collection.
  7. Which method in Groovy is used to sort a list?
    • a) sort()
    • b) order()
    • c) sequence()
    • d) arrange()
  8. In Groovy, how would you handle missing properties?
    • a) Using getProperty()
    • b) Using missingProperty()
    • c) Using @Delegate
    • d) Using expando
  9. What is the purpose of the @Category annotation in Groovy?
    • a) To define a class.
    • b) To annotate methods.
    • c) To modify the behavior of a class dynamically.
    • d) To define properties.

Answers:

QnoAnswer
1a) class
2a) new ClassName()
3b) class MyClass { MyClass() {} }
4a) new
5b) A default constructor is provided by Groovy.
6c) To automatically grab dependencies.
7b) def name
8b) The object type is dynamically assigned.
9a) private def name
10a) def
11b) Dynamic typing
12a) void
13a) def method(arg = 10)
14b) object.method()
15c) object.property
16b) Using getter and setter methods to protect data.
17c) private def name; def getName() { return name } def setName(value) { name = value }
18b) Using a getter method
19b) Through getter and setter methods.
20c) To validate property values before changing them.
21b) Converts an object into a readable string format.
22a) toString()
23a) Transforms a list into a new list.
24b) println()
25b) each()
26a) list[0]
27a) To find the first occurrence of a value in a collection.
28a) sort()
29b) Using missingProperty()
30c) To modify the behavior of a class dynamically.

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top