Test your knowledge of Scala’s core concepts, including values, variables, constants, primitive types, string operations, and type inference, with these 30 carefully designed multiple-choice questions.
Basic Syntax and Data Types in Scala
1. Values, Variables, and Constants
Which keyword is used to define immutable variables in Scala?
A) var
B) val
C) const
D) final
What happens if you try to reassign a val variable in Scala?
A) It overwrites the value
B) It throws a compilation error
C) It creates a new instance
D) It works the same as var
Which keyword is used to define mutable variables in Scala?
A) val
B) let
C) var
D) const
What is the default value of an uninitialized var in Scala?
A) 0
B) null
C) Depends on the type
D) No default value; Scala doesn’t allow uninitialized vars
How do you declare a constant in Scala?
A) const
B) val
C) immutable
D) constant
2. Primitive Data Types
What is the default numeric data type in Scala for a literal like 123?
A) Int
B) Long
C) Float
D) Double
Which data type in Scala is used to represent a single character?
A) char
B) String
C) Character
D) Char
How do you define a floating-point literal in Scala?
A) Append an f or F to the number
B) Append an l or L to the number
C) Use a decimal point only
D) Use double quotes around the number
What is the default size of an Int in Scala?
A) 16 bits
B) 32 bits
C) 64 bits
D) Platform-dependent
Which of the following is NOT a valid Scala numeric type?
A) Short
B) Byte
C) Decimal
D) Double
3. String Operations
How do you concatenate two strings in Scala?
A) Use the + operator
B) Use the concat() method
C) Use string interpolation
D) All of the above
What does the following Scala code output: println("Scala".length)?
A) 4
B) 5
C) 6
D) An error
What is string interpolation in Scala?
A) Combining two strings using a separator
B) Embedding variables in a string using $
C) Splitting strings into substrings
D) Removing white spaces from a string
Which method checks if a string contains a specific substring in Scala?
A) search()
B) find()
C) contains()
D) matches()
Which is a valid way to create a multiline string in Scala?
A) Use triple double quotes (""")
B) Use single quotes (''')
C) Use brackets ({{ }})
D) Use a backslash at the end of each line
4. Type Inference
What does type inference in Scala allow you to do?
A) Skip declaring the type explicitly
B) Define variables without initializing them
C) Use variables of multiple types
D) Avoid type checking
What is the inferred type of val x = 10 in Scala?
A) Double
B) Int
C) Float
D) Long
Which function can you use to explicitly check the type of a value in Scala?
A) getType()
B) typeOf()
C) type()
D) getClass()
Can Scala infer the return type of a function?
A) Yes, always
B) No, it must be explicitly defined
C) Yes, except for recursive functions
D) Only for void functions
What happens if a Scala variable type is incorrectly inferred?
A) The program throws a runtime error
B) The program throws a compile-time error
C) The program runs with warnings
D) This cannot happen; Scala ensures type safety
General Questions
Which of the following is a valid variable declaration in Scala?
A) var x: Int = 10
B) val x = 10
C) var x = "Hello"
D) All of the above
What is the result of val x = "5" + 2?
A) 7
B) 52
C) Error
D) “5 2”
Can a var in Scala hold different types of values during its lifecycle?
A) Yes, always
B) No, its type is fixed
C) Yes, only if it’s explicitly mutable
D) Yes, in REPL only
Which statement about type inference is true?
A) It only works with val
B) It only works with var
C) It works with both val and var
D) It does not work with uninitialized variables
How do you explicitly define the type of a variable?
A) Append a : and the type name to the variable
B) Use as followed by the type name
C) Declare the variable in a specific block
D) You cannot explicitly define types in Scala
What happens if you initialize a variable with null in Scala?
A) It infers the type as Any
B) It throws a compile-time error
C) It infers the type as Null
D) It infers the type as Nothing
Can Scala perform type inference for collections?
A) No, collection types must always be defined explicitly
B) Yes, Scala infers the type from the elements
C) Yes, but only for Lists
D) Yes, but only for immutable collections
What is the inferred type of an empty list List() in Scala?
A) List[Any]
B) List[Int]
C) List[Nothing]
D) None
Which Scala feature helps ensure type safety at compile time?
A) Type inference
B) Implicits
C) Case classes
D) Traits
How can you override type inference in Scala?
A) By explicitly specifying the type
B) By redefining the variable
C) By using the asInstanceOf method
D) You cannot override type inference
Answers
Qno
Answer
1
B) val
2
B) It throws a compilation error
3
C) var
4
D) No default value; Scala doesn’t allow uninitialized vars