MCQs on Concurrency and Parallelism | Groovy

Groovy, running on the Java Virtual Machine (JVM), supports concurrency and parallelism through a variety of mechanisms. This quiz will focus on key topics such as multithreading basics, GPars (Groovy Parallel Systems), asynchronous programming, and the Actor Model in Groovy, helping you understand Groovy’s approach to concurrency and parallelism.

MCQs: Concurrency and Parallelism in Groovy

Multithreading Basics

  1. How do you create a new thread in Groovy?
    • a) Thread.start()
    • b) new Thread().start()
    • c) Thread.new()
    • d) new Thread().run()
  2. Which of the following Groovy methods is used to pause the current thread for a specified period?
    • a) Thread.sleep(time)
    • b) sleepThread(time)
    • c) pause(time)
    • d) Thread.wait(time)
  3. What is the default priority of a thread in Groovy?
    • a) 1
    • b) 5
    • c) 10
    • d) 100
  4. Which method is used to start a thread in Groovy after creating it?
    • a) startThread()
    • b) begin()
    • c) start()
    • d) runThread()
  5. In Groovy, how can you access the current thread from within a program?
    • a) Thread.current()
    • b) Thread.currentThread()
    • c) Thread.getCurrent()
    • d) Thread.getThread()

GPars (Groovy Parallel Systems)

  1. What is GPars in Groovy?
    • a) A database framework
    • b) A concurrency and parallelism library
    • c) A file I/O library
    • d) A graphics rendering library
  2. Which of the following methods is used to execute tasks in parallel using GPars?
    • a) GPars.execute()
    • b) GPars.parallel()
    • c) GPars.fork()
    • d) GPars.run()
  3. Which feature of GPars allows you to process tasks asynchronously using worker threads?
    • a) ForkJoin
    • b) Actor System
    • c) Dataflow
    • d) Parallel Collections
  4. What does GPars.withPool() do in Groovy?
    • a) It pools database connections.
    • b) It manages threads and task execution.
    • c) It monitors memory usage.
    • d) It starts a new actor system.
  5. How can you run multiple tasks in parallel in GPars and wait for them to complete?
    • a) GPars.await()
    • b) GPars.waitFor()
    • c) GPars.gather()
    • d) GPars.all()

Asynchronous Programming

  1. Which method in Groovy is used to execute code asynchronously?
  • a) async()
  • b) executeAsync()
  • c) task()
  • d) Thread.start()
  1. In Groovy, what class provides an easy way to execute asynchronous code?
  • a) Future
  • b) AsyncTask
  • c) AsyncExecutor
  • d) ThreadPool
  1. What is the benefit of asynchronous programming in Groovy?
  • a) It improves user interface performance.
  • b) It simplifies code structure.
  • c) It speeds up database access.
  • d) It prevents memory leaks.
  1. Which method is used in Groovy to wait for an asynchronous task to complete and get the result?
  • a) awaitResult()
  • b) getResult()
  • c) join()
  • d) get()
  1. Which Groovy feature allows non-blocking code execution to perform tasks in the background?
  • a) Actor Model
  • b) Future
  • c) GPars
  • d) Promises

Actor Model in Groovy

  1. What is the Actor Model in Groovy used for?
  • a) Managing databases
  • b) Performing parallel processing with multiple actors
  • c) Managing threads and synchronization
  • d) Executing asynchronous tasks with threads
  1. Which Groovy library provides support for the Actor Model?
  • a) GPars
  • b) Groovy Actors
  • c) Groovy Futures
  • d) Groovy Async
  1. How do you define an actor in Groovy using the Actor library?
  • a) actor { ... }
  • b) def actor = new Actor()
  • c) actor create()
  • d) new Actor { ... }
  1. What method is used to send a message to an actor in Groovy?
  • a) actor.sendMessage()
  • b) actor.send()
  • c) actor.sendMsg()
  • d) actor.post()
  1. How does the Actor Model help in handling concurrency in Groovy?
  • a) By managing data and threads together
  • b) By using an event loop to process messages
  • c) By creating multiple processes
  • d) By using memory pools for actors
  1. In Groovy, what is the role of an actor mailbox?
  • a) It stores messages to be processed by the actor.
  • b) It handles system messages.
  • c) It stores actor states.
  • d) It manages thread synchronization.
  1. Which of the following describes the message-passing model in the Actor Model?
  • a) Actors communicate directly with each other.
  • b) Actors share memory space to communicate.
  • c) Actors send messages through a global queue.
  • d) Actors send messages via a callback system.
  1. What is the purpose of the react method in Groovy actors?
  • a) It reacts to incoming messages asynchronously.
  • b) It synchronizes the actor with other threads.
  • c) It runs background tasks.
  • d) It stops the actor from processing messages.
  1. How can an actor in Groovy stop processing messages and terminate?
  • a) actor.stop()
  • b) actor.exit()
  • c) actor.shutdown()
  • d) actor.terminate()
  1. Which statement is true about the Actor Model in Groovy?
  • a) It provides automatic thread management.
  • b) It uses shared memory for communication.
  • c) It performs parallel computation using multiple threads.
  • d) It enables actors to directly modify the state of other actors.
  1. Which class in GPars is used to create an actor in Groovy?
  • a) GroovyActor
  • b) GParsActor
  • c) Actor
  • d) ParallelActor
  1. How do actors in Groovy ensure that they don’t block each other?
  • a) Through message passing
  • b) Through shared memory
  • c) By using semaphores
  • d) By using global synchronization
  1. In Groovy, which method is used to ensure an actor is ready to receive messages?
  • a) actor.ready()
  • b) actor.listen()
  • c) actor.react()
  • d) actor.await()
  1. What happens when an actor in Groovy receives a message?
  • a) The actor processes the message and replies.
  • b) The actor queues the message for later processing.
  • c) The actor sends a response to the sender.
  • d) The actor terminates automatically after receiving the message.
  1. Which of the following is a key advantage of using the Actor Model in Groovy for concurrency?
  • a) It simplifies the management of multiple threads.
  • b) It allows direct memory access for faster computation.
  • c) It eliminates the need for network communication.
  • d) It prevents deadlock by avoiding shared state.

Answer Key

QnoAnswer
1b) new Thread().start()
2a) Thread.sleep(time)
3b) 5
4c) start()
5b) Thread.currentThread()
6b) A concurrency and parallelism library
7c) GPars.fork()
8d) Parallel Collections
9b) It manages threads and task execution.
10c) GPars.gather()
11a) async()
12a) Future
13a) It improves user interface performance.
14d) get()
15c) GPars
16b) Performing parallel processing with multiple actors
17b) Groovy Actors
18a) actor { ... }
19b) actor.send()
20b) By using an event loop to process messages
21a) It stores messages to be processed by the actor.
22a) Actors communicate directly with each other.
23a) It reacts to incoming messages asynchronously.
24a) actor.stop()
25a) It provides automatic thread management.
26c) Actor
27a) Through message passing
28c) actor.react()
29a) The actor processes the message and replies.
30a) It simplifies the management of multiple threads.

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