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
What does the val keyword signify in Kotlin?
a) Mutable reference
b) Immutable reference
c) Static reference
d) Constant reference
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
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"
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
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.)
Which of the following is NOT a primitive data type in Kotlin?
a) Int
b) Float
c) Double
d) Array
What is the default data type of a numeric literal in Kotlin?
a) Int
b) Float
c) Double
d) Long
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
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'
Which of these is used to concatenate strings in Kotlin?
a) +
b) -
c) concat()
d) join()
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
Which keyword is used to define multi-line strings in Kotlin?
a) """
b) @
c) ''
d) ///
3. Type Inference
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
What is the type of the variable in the statement val x = 10?
a) Float
b) Int
c) String
d) Double
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
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
In which scenario does Kotlin fail to infer a type?
a) Numeric constants
b) Multiple possible types
c) Hardcoded strings
d) Variable reassignment
What is inferred type of val flag = true in Kotlin?
a) Int
b) Boolean
c) String
d) Char
Type inference cannot be used in which of these cases?
a) Function parameters
b) Variable initialization
c) Return types
d) Complex expressions
What is the inferred type of val pi = 3.14 in Kotlin?