Swift - Programming Language

Skill Test Set 5: Class, Structures and OOPs Concept

  1. Which keyword do you use to define a class?

    stuct
    class
    Class
    interface
    @class


  2. Which root class, swift classes required to inherit from ?
    Not required
    NSObject
    NSRootObject
    Object

  3. What is the name of the deinitilizer in a class declaration
    dealloc
    release
    finalize
    deinit

  4. Which keyword is used in Swift when we want a property of a class to initialize when it is accessed for the first time?

    let
    var
    const
    lazy

  5. Which keyword do you use to define a structure?

    struct
    Struct
    structure
    @struct


  6. Which keyword is used on a function inside an struct to indicate that the function will modify self?
    modifier
    mutating
    mutable
    mod
    mut

  7. Choose the right statements about Class and Structure.
    Class can adopt protocols but Structure can't.
    Structure can't inherit from other Structure but Class can.
    Class and Structure both can set-up initialiser methods and initialise values.
    Class and Structure both can set-up de-initialiser methods and clean up memory for it's resources.

  8. struct SomeStruct {
    var a: Int
    }

    Select all right code-snippets which could be used to use the above defined structure (SomeStruct).

    var s1 = someStruct(10)
    var s1 = someStruct(a: 10)
    var s1: someStruct = someStruct(10)
    var s1 = someStruct()
    s2.a = 10


  9. What type of object are Swift Structures?
    Reference Type
    Memory Type
    Abstract Type
    Value Type

  10. Few question are based on the class definition written below:

    class Square: NamedShape {
    var sideLength: Double

    func area() -> Double {
    return sideLength*sideLength
    }
    }

    1. The name of the class's only method is __.
      NamedShape
      Square
      area
      Double
      sideLength

    2. What type of return does the function ‘area’ give?
      Int
      The area of a square
      Double
      area

    3. In the below text, what is the property name?
      NamedShape
      Square
      Double
      sideLength

    4. In the bellow text, what is the super class name?
      Square
      NamedShape
      Double
      area

  11. How could we create a subclass of the Structure, CGRect?
    struct MyRect: CGRect {}
    struct CGRect(MyRect) {}
    You can not subclass a Structure
    struct MyRect extends CGRect {}

  12. What will be the final value of "a.data" and "b.data" after following codes are executed?

    struct A { var data: Int = 2 } var a = A() var b = a var c = b c.data = 10 a.data = 5

    a.data = 10 and b.data = 2
    a.data = 2 and b.data = 5
    a.data = 5 and b.data = 5
    a.data = 5 and b.data = 2

  13. Can Structures be type cast in Swift?
    Yes
    No
    Only those deriving from NSObject
    Only when they conform to protocol TypeCast

Note: Answers are not given intentionally.