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