1.

In the code below when what will happen if we compile and execute SwiftBasics. test() method. Provide reasons.

Answer»

It would provide a COMPILATION error as  emp1 == emp2 cannot be RESOLVED by compiler. It depends if the structs/classes are adapted to the Hashable protocol. The primitive data type like Int, String by default implement the Hashable protocol, SHOW they do not show any error if we compare instances of such data types.

The corrected code would be:

struct EMPLOYEE: Hashable {     var name:String     var  id:Int     init(name:String, id:Int) {         self.name = name         self.id = id     }     var  hashValue: Int { RETURN self.id} } The printed result would be true.


Discussion

No Comment Found

Related InterviewSolutions