MCQs on Variables and Data Types | Kotlin

Understanding variables and data types is fundamental in programming. This guide covers key concepts like mutable vs. immutable variables, primitive data types, and type inference, essential for effective coding.


Variables and Data Types: MCQs

1. Mutable (var) vs Immutable (val) Variables

  1. What does the val keyword signify in Kotlin?
    • a) Mutable reference
    • b) Immutable reference
    • c) Static reference
    • d) Constant reference
  2. If a var variable is declared in Kotlin, what can you modify?
    • a) Its reference only
    • b) Its type only
    • c) Both value and reference
    • d) Its value only
  3. Which of the following is an example of an immutable variable in Kotlin?
    • a) var name = "John"
    • b) val name = "John"
    • c) name = "John"
    • d) mutable name = "John"
  4. Can the value of a val variable be reassigned after initialization?
    • a) Yes, always
    • b) No, never
    • c) Yes, under conditions
    • d) Only if it’s mutable
  5. What happens if you try to reassign a val variable in Kotlin?
    • a) Nothing happens
    • b) Compilation error
    • c) Runtime error
    • d) Variable becomes null

2. Primitive Data Types (Int, Float, String, etc.)

  1. Which of the following is NOT a primitive data type in Kotlin?
    • a) Int
    • b) Float
    • c) Double
    • d) Array
  2. What is the default data type of a numeric literal in Kotlin?
    • a) Int
    • b) Float
    • c) Double
    • d) Long
  3. How do you define a floating-point number in Kotlin?
    • a) Append L to the number
    • b) Append f or F to the number
    • c) Append d to the number
    • d) Use decimals only
  4. What is the correct syntax for defining a string in Kotlin?
    • a) val name: String = "Hello"
    • b) val name = 'Hello'
    • c) var name: Char = "H"
    • d) val name: String = 'Hello'
  5. Which of these is used to concatenate strings in Kotlin?
    • a) +
    • b) -
    • c) concat()
    • d) join()
  6. What is the range of the Int data type in Kotlin?
    • a) -2^16 to 2^16-1
    • b) -2^32 to 2^32-1
    • c) -2^31 to 2^31-1
    • d) -2^63 to 2^63-1
  7. Which keyword is used to define multi-line strings in Kotlin?
    • a) """
    • b) @
    • c) ''
    • d) ///

3. Type Inference

  1. What does Kotlin’s type inference feature do?
    • a) Infers method names
    • b) Automatically assigns data types
    • c) Requires explicit type definition
    • d) Removes the need for variables
  2. What is the type of the variable in the statement val x = 10?
    • a) Float
    • b) Int
    • c) String
    • d) Double
  3. Which of these is true about Kotlin’s type inference?
    • a) It works only for var variables
    • b) It guesses types at compile-time
    • c) It guesses types at runtime
    • d) It cannot infer type for val variables
  4. How do you explicitly specify a type in Kotlin?
    • a) By appending :Type to the variable name
    • b) By declaring the type before the variable name
    • c) By initializing without value
    • d) By using the typeof() function
  5. In which scenario does Kotlin fail to infer a type?
    • a) Numeric constants
    • b) Multiple possible types
    • c) Hardcoded strings
    • d) Variable reassignment
  6. What is inferred type of val flag = true in Kotlin?
    • a) Int
    • b) Boolean
    • c) String
    • d) Char
  7. Type inference cannot be used in which of these cases?
    • a) Function parameters
    • b) Variable initialization
    • c) Return types
    • d) Complex expressions
  8. What is the inferred type of val pi = 3.14 in Kotlin?
    • a) Float
    • b) Double
    • c) Int
    • d) Long

Answers

QNoAnswer
1b) Immutable reference
2d) Its value only
3b) val name = "John"
4b) No, never
5b) Compilation error
6d) Array
7a) Int
8b) Append f or F
9a) val name: String = "Hello"
10a) +
11c) -2^31 to 2^31-1
12a) """
13b) Automatically assigns data types
14b) Int
15b) It guesses types at compile-time
16a) By appending :Type to the variable name
17b) Multiple possible types
18b) Boolean
19a) Function parameters
20b) Double

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