MCQs on Swift Fundamentals | Swift

Dive into Swift Fundamentals to strengthen your programming foundation. Learn about variables, constants, data types, type inference, type safety, and code documentation with clarity and precision.


MCQs on Swift Fundamentals

Section 1: Variables and Constants (6 Questions)

  1. Which keyword is used to declare a variable in Swift?
    • a) var
    • b) let
    • c) const
    • d) mutable
  2. What does the let keyword signify in Swift?
    • a) A variable with no data type
    • b) A constant that cannot be reassigned
    • c) A placeholder for dynamic types
    • d) A mutable variable
  3. Which of the following is the correct way to declare a constant in Swift?
    • a) const name = “Swift”
    • b) let name = “Swift”
    • c) static name = “Swift”
    • d) var name = “Swift”
  4. What happens if you try to change the value of a constant in Swift?
    • a) It changes dynamically
    • b) It throws a runtime error
    • c) It causes a compile-time error
    • d) It works if the data type is compatible
  5. Which statement is true about variables in Swift?
    • a) Variables are immutable by default
    • b) Variables are declared using the var keyword
    • c) Variables cannot store numeric values
    • d) Variables can only store strings
  6. How do you declare a variable with an explicit type in Swift?
    • a) var age: Int = 25
    • b) age = Int: 25
    • c) Int var age = 25
    • d) let Int age = 25

Section 2: Data Types (6 Questions)

  1. What type would the number 42 be inferred as in Swift?
    • a) Float
    • b) Double
    • c) Int
    • d) String
  2. Which of the following is a valid Swift data type?
    • a) Integer
    • b) Bool
    • c) CharacterArray
    • d) DoubleChar
  3. How is a floating-point value represented in Swift?
    • a) Only as a Float
    • b) Only as a Double
    • c) As either Float or Double
    • d) As Int
  4. What is the difference between Float and Double in Swift?
    • a) Double has higher precision than Float
    • b) Float can store larger numbers than Double
    • c) They are interchangeable in Swift
    • d) Float is immutable while Double is mutable
  5. Which of the following types would store the value true in Swift?
    • a) Bool
    • b) String
    • c) Int
    • d) Character
  6. What is the data type of a single character in Swift?
    • a) Char
    • b) String
    • c) Character
    • d) Text

Section 3: Type Inference and Type Safety (6 Questions)

  1. What is type inference in Swift?
    • a) Automatically determining the type of a variable
    • b) Converting one type to another
    • c) Manually defining the type of a variable
    • d) Avoiding type errors at runtime
  2. Which statement about type safety in Swift is true?
    • a) Swift enforces strict type checking at runtime
    • b) Swift allows implicit type conversion by default
    • c) Swift ensures type safety during compile-time
    • d) Swift does not support type inference
  3. How can you explicitly declare the type of a variable in Swift?
    • a) var name: String = "John"
    • b) let name: String
    • c) Both a and b
    • d) It is not allowed
  4. Which of these will cause a type mismatch error in Swift?
    • a) Assigning an Int value to a Float variable
    • b) Assigning a String value to an Int variable
    • c) Assigning a Bool value to a Character variable
    • d) All of the above
  5. Why is type safety important in Swift?
    • a) It avoids type-related runtime crashes
    • b) It allows dynamic type conversion
    • c) It makes the code execute faster
    • d) It removes the need for explicit type declarations
  6. What is the default type for a floating-point number in Swift?
    • a) Float
    • b) Double
    • c) Decimal
    • d) Int

Section 4: Comments and Code Documentation (6 Questions)

  1. How do you write a single-line comment in Swift?
    • a) /* Comment */
    • b) # Comment
    • c) // Comment
    • d) /// Comment
  2. Which of the following is used for multi-line comments in Swift?
    • a) /* Comment */
    • b) // Comment //
    • c) # Comment #
    • d) * Comment *\
  3. What are comments used for in Swift code?
    • a) To add additional functionality
    • b) To document code and improve readability
    • c) To execute debug commands
    • d) To identify errors
  4. What does the /// comment syntax signify in Swift?
    • a) It marks a multi-line comment
    • b) It is used for documentation comments
    • c) It is a syntax error
    • d) It is equivalent to //
  5. Why is code documentation important?
    • a) It improves code readability and maintainability
    • b) It makes the code faster
    • c) It prevents bugs during compilation
    • d) It ensures strict type safety
  6. Which tool can generate documentation from Swift comments?
    • a) SwiftLint
    • b) Xcode’s Quick Help
    • c) SwiftDoc
    • d) Both b and c

Section 5: Miscellaneous Swift Fundamentals (6 Questions)

  1. What is the keyword to declare an immutable variable in Swift?
    • a) const
    • b) var
    • c) let
    • d) final
  2. How can you print a variable’s value in Swift?
    • a) print("Variable value: \(variable)")
    • b) println(variable)
    • c) echo variable
    • d) print(variable)
  3. Which statement is true about Swift’s type system?
    • a) It is dynamic and weakly typed
    • b) It is static and strongly typed
    • c) It is flexible with implicit conversions
    • d) It does not allow explicit type declarations
  4. How does Swift handle uninitialized variables?
    • a) It assigns a default value
    • b) It throws a compile-time error
    • c) It leaves them as null
    • d) It converts them to strings
  5. What happens if you use an undefined variable in Swift?
    • a) It causes a runtime error
    • b) It causes a compile-time error
    • c) It initializes the variable with a default value
    • d) The code executes normally
  6. Which of the following can be used to create a block of reusable code in Swift?
    • a) Function
    • b) Variable
    • c) Constant
    • d) Comment

Answer Key

QnoAnswer
1a) var
2b) A constant that cannot be reassigned
3b) let name = “Swift”
4c) It causes a compile-time error
5b) Variables are declared using the var keyword
6a) var age: Int = 25
7c) Int
8b) Bool
9c) As either Float or Double
10a) Double has higher precision than Float
11a) Bool
12c) Character
13a) Automatically determining the type of a variable
14c) Swift ensures type safety during compile-time
15c) Both a and b
16d) All of the above
17a) It avoids type-related runtime crashes
18b) Double
19c) // Comment
20a) /* Comment */
21b) To document code and improve readability
22b) It is used for documentation comments
23a) It improves code readability and maintainability
24d) Both b and c
25c) let
26a) print("Variable value: \(variable)")
27b) It is static and strongly typed
28b) It throws a compile-time error
29b) It causes a compile-time error
30a) Function

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