MCQs on Object-Oriented Programming in Scala | Scala

Explore the core concepts of Object-Oriented Programming (OOP) in Scala. This set of MCQs covers topics like classes, objects, constructors, fields, and access modifiers to solidify your OOP knowledge.


Object-Oriented Programming in Scala

1. Classes and Objects

  1. What is the main difference between a class and an object in Scala?
    • A) A class is a blueprint, while an object is a singleton instance of a class
    • B) An object can inherit from another object, while a class cannot
    • C) An object can define methods, while a class cannot
    • D) A class is mutable, while an object is immutable
  2. How do you define a class in Scala?
    • A) class ClassName() {}
    • B) object ClassName() {}
    • C) class ClassName{}
    • D) class ClassName{}
  3. Which of the following is the correct way to create an instance of a class?
    • A) new ClassName()
    • B) ClassName()
    • C) create ClassName()
    • D) ClassName.new()
  4. What is a companion object in Scala?
    • A) An object defined inside a class
    • B) A class and an object with the same name in the same scope
    • C) A singleton object
    • D) A static class
  5. What can be accessed directly from a companion object?
    • A) Instance variables of the class
    • B) Instance methods of the class
    • C) Static variables
    • D) Private members of the class
  6. How do you instantiate an object of a class in Scala?
    • A) object ClassName
    • B) new ClassName()
    • C) class ClassName()
    • D) new object ClassName()
  7. Can you define methods in an object?
    • A) Yes, objects can have methods like classes
    • B) No, methods can only be defined inside a class
    • C) Yes, but only constructors
    • D) No, objects cannot have methods
  8. Which keyword is used to define a class in Scala?
    • A) define
    • B) object
    • C) class
    • D) type
  9. In Scala, how is an object used to initialize a class?
    • A) By using an initialization block
    • B) By using a companion object
    • C) Objects cannot initialize a class
    • D) Using an init() function
  10. Which of these is true about Scala’s object keyword?
    • A) It defines a singleton object
    • B) It creates a mutable instance
    • C) It defines a class
    • D) It defines an abstract class

2. Constructors and Fields

  1. In Scala, how do you define a constructor for a class?
    • A) Using the constructor keyword
    • B) By defining parameters in the class declaration
    • C) Using the def keyword
    • D) Through the new keyword
  2. What is a primary constructor in Scala?
    • A) A constructor without parameters
    • B) The first constructor defined inside the class body
    • C) A constructor that can only be invoked once
    • D) The constructor used to initialize default values
  3. How do you define a class with a parameterized constructor in Scala?
    • A) class MyClass(val x: Int)
    • B) class MyClass { def constructor(x: Int) }
    • C) class MyClass(x: Int)
    • D) object MyClass(x: Int)
  4. What is the purpose of the val keyword when defining a class field?
    • A) To define a constant field that cannot be reassigned
    • B) To define a private field
    • C) To define a mutable field
    • D) To make the field static
  5. How do you define a mutable field in a class in Scala?
    • A) var fieldName: DataType
    • B) let fieldName: DataType
    • C) constant fieldName: DataType
    • D) val fieldName: DataType
  6. In Scala, which of these keywords is used to create a default value for a field?
    • A) set
    • B) default
    • C) var
    • D) No special keyword, just assignment
  7. How do you initialize class fields in a constructor?
    • A) By using the initialize method
    • B) Through the constructor parameters
    • C) By using this keyword in the body
    • D) It is not possible to initialize fields
  8. What is the this keyword used for in a Scala class?
    • A) To refer to a static method
    • B) To access the current instance of the class
    • C) To define class methods
    • D) To initialize default fields
  9. How would you define a field that is private in Scala?
    • A) private val fieldName
    • B) fieldName private
    • C) val fieldName private
    • D) fieldName: private
  10. What does the constructor keyword do in Scala?
    • A) Defines the main constructor
    • B) Initializes fields in the class
    • C) It is not used in Scala
    • D) Makes fields accessible

3. Access Modifiers

  1. What is the default access modifier for fields and methods in Scala?
    • A) private
    • B) protected
    • C) public
    • D) no modifier
  2. Which access modifier allows a field to be accessed only within its class and its companions?
    • A) private
    • B) protected
    • C) private[this]
    • D) public
  3. How do you define a field that is accessible only within the package it belongs to?
    • A) private
    • B) protected
    • C) protected[packageName]
    • D) private[packageName]
  4. What is the effect of the protected access modifier?
    • A) It makes the field accessible from any class
    • B) It makes the field accessible within the class and subclasses
    • C) It prevents the field from being accessed outside the package
    • D) It makes the field private
  5. How would you mark a class field as private to the class but visible to subclasses?
    • A) private
    • B) private[this]
    • C) protected
    • D) protected[this]
  6. Can fields in an object be private in Scala?
    • A) Yes, by using the private keyword
    • B) No, fields in objects are always public
    • C) Yes, but only with the public modifier
    • D) No, access modifiers do not work with objects
  7. What is the role of the private[this] modifier?
    • A) It makes fields only accessible within the current instance of the class
    • B) It makes fields accessible from any class within the same package
    • C) It prevents fields from being accessed outside the class
    • D) It marks fields as constant
  8. Can a method in Scala be both protected and private?
    • A) Yes, with the correct syntax
    • B) No, it must be either protected or private
    • C) Yes, if it is inside an object
    • D) Yes, if it is inside a trait
  9. How can you restrict the access of a class to a specific package in Scala?
    • A) private[packageName]
    • B) protected[packageName]
    • C) public[packageName]
    • D) private
  10. Which modifier would you use to make a method only accessible within the class and its companion object?
    • A) private[this]
    • B) private
    • C) protected
    • D) protected[object]

Answers

QnoAnswer
1A) A class is a blueprint, while an object is a singleton instance of a class
2D) class ClassName{}
3A) new ClassName()
4B) A class and an object with the same name in the same scope
5C) Static variables
6B) new ClassName()
7A) Yes, objects can have methods like classes
8C) class
9B) By using a companion object
10A) It defines a singleton object
11B) By defining parameters in the class declaration
12B) The first constructor defined inside the class body
13A) class MyClass(val x: Int)
14A) To define a constant field that cannot be reassigned
15A) var fieldName: DataType
16D) No special keyword, just assignment
17B) Through the constructor parameters
18B) To access the current instance of the class
19A) private val fieldName
20C) It is not used in Scala
21D) no modifier
22C) private[this]
23D) private[packageName]
24B) It makes the field accessible within the class and subclasses
25C) protected
26A) Yes, by using the private keyword
27A) It makes fields only accessible within the current instance of the class
28B) No, it must be either protected or private
29A) private[packageName]
30B) private

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