Groovy is a powerful, dynamic programming language that runs on the Java Virtual Machine (JVM). It’s designed to be concise and easy to learn, offering a flexible syntax for developers familiar with Java or other languages. In this quiz, we will focus on core Groovy concepts like syntax, structure, classes, variables, data types, and basic input/output operations.
MCQs: Groovy Syntax and Structure
Groovy Scripts and Classes
Which of the following is the correct way to declare a Groovy class?
a) class MyClass { ... }
b) Groovy class MyClass { ... }
c) def class MyClass { ... }
d) class: MyClass { ... }
What is the default visibility of a class in Groovy?
a) Public
b) Private
c) Protected
d) Package-private
How do you define a Groovy script?
a) A Groovy script has a .groovy extension.
b) A Groovy script uses the groovy keyword.
c) A Groovy script is enclosed in curly braces.
d) A Groovy script is a class without a main method.
In Groovy, how do you create a class with a constructor?
a) class MyClass { MyClass() { } }
b) class MyClass() { ... }
c) class MyClass { def MyClass() { } }
d) def class MyClass() { ... }
Which of the following is the correct way to instantiate an object of the class Car in Groovy?
a) new Car()
b) Car.new()
c) Car.create()
d) Car()
Declaring Variables and Data Types
What is the default data type for variables declared with def in Groovy?
a) Integer
b) String
c) Object
d) Boolean
How do you declare a variable in Groovy that holds an integer value of 5?
a) int x = 5
b) def x = 5
c) Integer x = 5
d) def Integer x = 5
Which of the following is a valid Groovy data type?
a) int
b) boolean
c) char
d) All of the above
What keyword is used to declare a constant in Groovy?
a) const
b) final
c) static
d) immutable
How would you declare a list in Groovy?
a) list = []
b) def list = [1, 2, 3]
c) list = new ArrayList()
d) list = (1, 2, 3)
Comments and Code Style
How do you write a single-line comment in Groovy?
a) /* Comment */
b) // Comment
c) # Comment
d) -- Comment
What is the purpose of @Grab annotation in Groovy?
a) To import a class
b) To grab dependencies dynamically
c) To define a package
d) To declare variables
Which of the following is the correct way to write a multi-line comment in Groovy?
a) /* Comment */
b) /** Comment **/
c) // Comment
d) """ Comment """
What does the @Override annotation do in Groovy?
a) It marks a method as deprecated
b) It indicates a method overrides a superclass method
c) It marks a class as abstract
d) It defines an interface method
How would you improve code readability in Groovy?
a) Using concise variable names
b) Adding comments
c) Avoiding excessive nesting
d) All of the above
Basic Input and Output
What is the correct way to print “Hello, Groovy” in Groovy?
a) print "Hello, Groovy"
b) println("Hello, Groovy")
c) echo "Hello, Groovy"
d) System.out.println("Hello, Groovy")
In Groovy, what method would you use to read input from the user?
a) readLine()
b) input()
c) System.in.read()
d) scanner.nextLine()
Which of the following will display the contents of a file in Groovy?
a) println new File("file.txt").text
b) File.read("file.txt")
c) display new File("file.txt")
d) print File.open("file.txt")
What function is used in Groovy to read a number from input?
a) readInt()
b) readLine()
c) nextInt()
d) parseInt()
How do you handle exceptions in Groovy?
a) try-catch-finally
b) catch-throw
c) throw-catch
d) handle-exception
Answer Key
Qno
Answer
1
a) class MyClass { ... }
2
a) Public
3
a) A Groovy script has a .groovy extension.
4
a) class MyClass { MyClass() { } }
5
a) new Car()
6
c) Object
7
b) def x = 5
8
d) All of the above
9
b) final
10
b) def list = [1, 2, 3]
11
b) // Comment
12
b) To grab dependencies dynamically
13
a) /* Comment */
14
b) It indicates a method overrides a superclass method