How can context switch affect the modification of a variable?
A context switch is simply the operating system pausing the execution of one process and starting another. As such, context switches are required for any concurrency problem since, if there are no context switches, there's only one process executing so there is no concurrency.

In your example, the race condition occurs because what the function update really does is:


Read the value of myTotalAmount from memory;
Add amt to this value;
Store the result back in the memory.


Context switches at bad times can cause a race if the processes don't protect against that.  Let's say that myTotalAmount currently has value zero.


Process B reads the value of myTotalAmount, which is zero.
Process B calculates the new value 0+5=5.
Context switch.
Process A reads the value of myTotalAmount, which is still zero.
Process A calculates the new value 0+10=10.
Process A writes the value 10 to myTotalAmount.
Context switch.
Process B writes the value 5 to myTotalAmount.


(Note that ordering the steps 13456728 would give the same result.)