Swift : Programming Language

Skill Test Set 2: Variables, Constants and Data Types

  1. Choose the correct statements:
    let and var keywords are used for immutable and mutable data storage respectively.
    The value of a constant doesn’t need to be known at compile time, but you must assign it a value exactly once.
    Values stored in one type can be changed automatically (implicitly) to other type.
    String(width) and \(width) are same and it means width is converted into String type.
  2. let name = "CodingBull"
    name = "www.codingbull.com"

    It's compile time error
    It's runtime error
    It's not an error
    Syntax is wrong as semicolons are missing in both the lines.

  3. var name : String? = "CodingBull"
    name = name + 10

    It's compile time error
    It's runtime error
    It's not an error
    Syntax is wrong as semicolons are missing in both the lines.

  4. a. Double represents a 64-bit floating-point number while Float represents 32-bit floating-point number.
    b. Double has a precision of at least 15 decimal digits while Float has upto 6 decimal digits.
    Both statements are right.
    Only a is right.
    Only b is right.
    Both are wrong.

  5. Which syntax is correct among the below given options, to create a dictionary with a key of type Integer and value of String?

    var dict: [Int: String] = ["one": 1 , "two": 2]
    var dict = [Int, String] = [1, "one", 2, "two"]
    var dict = [Int: String] = [1: "one", 2: "two"]
    var dict = {Int: String} = {1: "one", 2: "two"}


  6. Which of the following statement correctly declares a mutable array?

    let array = [Int]()
    var array = [Int]()
    let array = [Int]
    var array = [Int]


  7. What is the type of String, Array and Dictionary? Class
    Structure
    Enumeration
    Variables

  8. Which of these is not a valid property declaration? final let x = 0
    final lazy let x = 0
    final lazy var x = 0
    final var x = 0

  9. Choose the correct statements:
    Short cut literal for creating empty Array and Dictionary both, is same and it is [ ].
    Optional data-type either has some value or is nil to indicate value is missing.
    ?? [Double question mark] operator is used to provide default value to an optional value.
    Forced unwrapping of the optional's value is done by adding an exclamation mark (!) with optional's name and it means that optional data-type has some value stored.

  10. What is the size of Int?
    It is 32 bits.
    It is 64 bits.
    It is platform dependent and could be 32bits or 64bits.
    Int is not a valid keyword but it should be either Int8, Int16, Int32 or Int64.

  11. Which Swift Type is used to group multiple values into a single compound value? For example:

    let compoundValue = (3, 5)

    GroupObject
    Tuple
    Ordered
    Struct

  12. Which of the following statements could be used to determine is a given variable is of String type?
    if unknownVariable is String {}
    if unknownVariable: String {}
    if unknownVariable = String {}
    if unknownVariable <> String {}
    if String.hierarchy(unknownVariable) { }
    if (String)unknownVariable { }

  13. What would be used for safe casting and to return nil if failed?
    as?
    as!
    !as?
    !as!

  14. Which one is the correct keyword for defining a constant in Swift? ( or To declare a constant in Swift you would use: ) const
    contant
    final
    let
    def

Note: Answers are not given intentionally.