Search Your Question

What is output of following code?

Ans:

1.

var a = 6;
var b = 9;
let newClouser: ()->() = {[a,b] in
print(a+b)
}

a=3
b=3
newClouser()

Ans : 15
Explanation : We can't change value of a and b for closure. Reassignment will create new reference. We have captured value of a and b as we have used capture list ( [a,b] ). Capture list takes value of a and b as point of time.

If we have not used capture list ( [] ) then changing value for a and b affecting though value changing after closure declaration.

2.

print("1")
DispatchQueue.main.async {
    print("2")
    DispatchQueue.main.async {
        print(3)
    }
    print("4")
}
print("5")

Ans :
1
5
2
4
5

Explanation : 

You will always get 1, 2, 4, 3. The 5 will always be after the 1. But where it ends up in relation to the others depends on what queue the whole thing started on.

If this is started from the main queue then 5 will always be between 1 and 2.

Here's why:  This code starts on the main queue. 1 is printed. You then enqueue another block to run asynchronously on the main queue so that block will be run after the current block completes and the main queue gets to the end of the current run loop. The code continues to the next line which is to print 5. The current block ends and the next block on the main queue is run. This is the block of the first call to DispatchQueue.main.async. As this block runs it prints 2 (so now we have 1 5 2). Another block is enqueued to the main queue just like the last one. The current block continues and prints 4 (so now we have 1 5 2 4). The block ends and the next block on the main queue is run. This is the final block we added. That block runs and it prints 3 giving the final output of 1 5 2 4 3.

3.

for i in 0...10 {
    DispatchQueue.main.async {
        DispatchQueue.main.async {
            print("Print1",i)
        }
        print("print2",i)
    }
}

Ans : 

Print2 0
Print2 1
Print2 2
Print2 3
Print2 4
Print2 5
Print2 6
Print2 7
Print2 8
Print2 9
Print2 10
Print1 0
Print1 1
Print1 2
Print1 3
Print1 4
Print1 5
Print1 6
Print1 7
Print1 8
Print1 9
Print1 10

Explanation : Read explanation-2

4.

for i in 0...10 {
    DispatchQueue.main.async {
        DispatchQueue.main.sync {
            print("Print1",i)
        }
        print("Print2",i)
    }
}

Ans :

Deadlock occurs. May be crashed.

Explanation : Dispatch.main is a serial queue which has single thread to execute all the operations. If we call "sync" on this queue it will block all other operations currently running on the thread and try to execute the code block inside sync whatever you have written. This results in "deadlock".

This is a deadlock. It crashes because GCD detects this particular form of deadlock and aborts the program.

5.

for i in 0...10 {
    DispatchQueue.main.async {
        DispatchQueue.global().sync {
            print("Print1",i)
        }
        print("Print2",i)
    }
}

Ans : 

Print1 0
Print2 0
Print1 1
Print2 1
Print1 2
Print2 2
Print1 3
Print2 3
Print1 4
Print2 4
Print1 5
Print2 5
Print1 6
Print2 6
Print1 7
Print2 7
Print1 8
Print2 8
Print1 9
Print2 9
Print1 10
Print2 10

Explanation : 

6. 

for i in 0...10 {
    DispatchQueue.main.async {
        DispatchQueue.global().async {
            print("Print1",i)
        }
        print("Print2",i)
    }
}

Ans : 

Print2 0
Print1 0
Print2 1
Print1 1
Print2 2
Print1 2
Print2 3
Print1 3
Print2 4
Print1 4
Print2 5
Print2 6
Print1 5
Print1 6
Print2 7
Print1 7
Print2 8
Print1 8
Print2 9
Print1 9
Print2 10
Print1 10

Explanation : 

7. 

for i in 0...10 {
    DispatchQueue.global().async {
        DispatchQueue.global().async {
            print("Print1",i)
        }
        print("Print2",i)
    }
}

Ans :

Order is not fixed. Print in any order.

Explanation : 

8. 

for i in 0...10 {
    DispatchQueue.global().sync {
        DispatchQueue.global().sync {
            print("Print1",i)
        }
        print("Print2",i)
    }
}

Ans :

Print1 0
Print2 0
Print1 1
Print2 1
Print1 2
Print2 2
Print1 3
Print2 3
Print1 4
Print2 4
Print1 5
Print2 5
Print1 6
Print2 6
Print1 7
Print2 7
Print1 8
Print2 8
Print1 9
Print2 9
Print1 10
Print2 10

Explanation : 

9. 

for i in 0...10 {
    DispatchQueue.global().async {
        DispatchQueue.main.async {
            print("Print1",i)
        }
        print("Print2",i)
    }
}

Ans :
Print2 5
Print2 4
Print2 8
Print2 7
Print2 10
Print2 3
Print2 6
Print2 2
Print2 1
Print2 0
Print2 9
Print1 3
Print1 4
Print1 0
Print1 5
Print1 6
Print1 2
Print1 1
Print1 7
Print1 8
Print1 9
Print1 10

First all print from Print2 in random order, after that, Print1 printed in random order.

10. 

for i in 0...10 {
    DispatchQueue.global().sync {
        DispatchQueue.main.async {
            print("Print1",i)
        }
        print("Print2",i)
    }
}

Ans :

Print2 0
Print2 1
Print2 2
Print2 3
Print2 4
Print2 5
Print2 6
Print2 7
Print2 8
Print2 9
Print2 10
Print1 0
Print1 1
Print1 2
Print1 3
Print1 4
Print1 5
Print1 6
Print1 7
Print1 8
Print1 9
Print1 10

Explanation :

11.

for i in 0...10 {
    print("Manan1")
    DispatchQueue.main.async {
        print("Manan2")
        DispatchQueue.main.async {
            print("Print1",i)
        }
        print("print2",i)
    }
}

Ans : 

Manan1
Manan1
Manan1
Manan1
Manan1
Manan1
Manan1
Manan1
Manan1
Manan1
Manan1
Manan2
print2 0
Manan2
print2 1
Manan2
print2 2
Manan2
print2 3
Manan2
print2 4
Manan2
print2 5
Manan2
print2 6
Manan2
print2 7
Manan2
print2 8
Manan2
print2 9
Manan2
print2 10
Print1 0
Print1 1
Print1 2
Print1 3
Print1 4
Print1 5
Print1 6
Print1 7
Print1 8
Print1 9
Print1 10

Explanation :

No comments:

Post a Comment

Thanks