MCQs on Basic Syntax and Data Types | Scala

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

  1. Which keyword is used to define immutable variables in Scala?
    • A) var
    • B) val
    • C) const
    • D) final
  2. 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
  3. Which keyword is used to define mutable variables in Scala?
    • A) val
    • B) let
    • C) var
    • D) const
  4. 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
  5. How do you declare a constant in Scala?
    • A) const
    • B) val
    • C) immutable
    • D) constant

2. Primitive Data Types

  1. What is the default numeric data type in Scala for a literal like 123?
    • A) Int
    • B) Long
    • C) Float
    • D) Double
  2. Which data type in Scala is used to represent a single character?
    • A) char
    • B) String
    • C) Character
    • D) Char
  3. 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
  4. What is the default size of an Int in Scala?
    • A) 16 bits
    • B) 32 bits
    • C) 64 bits
    • D) Platform-dependent
  5. Which of the following is NOT a valid Scala numeric type?
    • A) Short
    • B) Byte
    • C) Decimal
    • D) Double

3. String Operations

  1. 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
  2. What does the following Scala code output: println("Scala".length)?
    • A) 4
    • B) 5
    • C) 6
    • D) An error
  3. 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
  4. Which method checks if a string contains a specific substring in Scala?
    • A) search()
    • B) find()
    • C) contains()
    • D) matches()
  5. 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

  1. 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
  2. What is the inferred type of val x = 10 in Scala?
    • A) Double
    • B) Int
    • C) Float
    • D) Long
  3. Which function can you use to explicitly check the type of a value in Scala?
    • A) getType()
    • B) typeOf()
    • C) type()
    • D) getClass()
  4. 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
  5. 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

  1. 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
  2. What is the result of val x = "5" + 2?
    • A) 7
    • B) 52
    • C) Error
    • D) “5 2”
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. What is the inferred type of an empty list List() in Scala?
    • A) List[Any]
    • B) List[Int]
    • C) List[Nothing]
    • D) None
  9. Which Scala feature helps ensure type safety at compile time?
    • A) Type inference
    • B) Implicits
    • C) Case classes
    • D) Traits
  10. 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

QnoAnswer
1B) val
2B) It throws a compilation error
3C) var
4D) No default value; Scala doesn’t allow uninitialized vars
5B) val
6A) Int
7D) Char
8A) Append an f or F to the number
9B) 32 bits
10C) Decimal
11D) All of the above
12B) 5
13B) Embedding variables in a string using $
14C) contains()
15A) Use triple double quotes (""")
16A) Skip declaring the type explicitly
17B) Int
18D) getClass()
19C) Yes, except for recursive functions
20D) This cannot happen; Scala ensures type safety
21D) All of the above
22B) 52
23B) No, its type is fixed
24C) It works with both val and var
25A) Append a : and the type name to the variable
26C) It infers the type as Null
27B) Yes, Scala infers the type from the elements
28C) List[Nothing]
29A) Type inference
30A) By explicitly specifying the type

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