MCQs on Groovy Syntax and Structure | Groovy

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

  1. 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 { ... }
  2. What is the default visibility of a class in Groovy?
    • a) Public
    • b) Private
    • c) Protected
    • d) Package-private
  3. 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.
  4. 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() { ... }
  5. 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

  1. What is the default data type for variables declared with def in Groovy?
    • a) Integer
    • b) String
    • c) Object
    • d) Boolean
  2. 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
  3. Which of the following is a valid Groovy data type?
    • a) int
    • b) boolean
    • c) char
    • d) All of the above
  4. What keyword is used to declare a constant in Groovy?
    • a) const
    • b) final
    • c) static
    • d) immutable
  5. 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

  1. How do you write a single-line comment in Groovy?
    • a) /* Comment */
    • b) // Comment
    • c) # Comment
    • d) -- Comment
  2. 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
  3. Which of the following is the correct way to write a multi-line comment in Groovy?
    • a) /* Comment */
    • b) /** Comment **/
    • c) // Comment
    • d) """ Comment """
  4. 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
  5. 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

  1. 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")
  2. In Groovy, what method would you use to read input from the user?
    • a) readLine()
    • b) input()
    • c) System.in.read()
    • d) scanner.nextLine()
  3. 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")
  4. What function is used in Groovy to read a number from input?
    • a) readInt()
    • b) readLine()
    • c) nextInt()
    • d) parseInt()
  5. How do you handle exceptions in Groovy?
    • a) try-catch-finally
    • b) catch-throw
    • c) throw-catch
    • d) handle-exception

Answer Key

QnoAnswer
1a) class MyClass { ... }
2a) Public
3a) A Groovy script has a .groovy extension.
4a) class MyClass { MyClass() { } }
5a) new Car()
6c) Object
7b) def x = 5
8d) All of the above
9b) final
10b) def list = [1, 2, 3]
11b) // Comment
12b) To grab dependencies dynamically
13a) /* Comment */
14b) It indicates a method overrides a superclass method
15d) All of the above
16b) println("Hello, Groovy")
17a) readLine()
18a) println new File("file.txt").text
19c) nextInt()
20a) try-catch-finally

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