There are 40 opcodes, it means, there will be 40 distinct operations like ADD, SUB, MUL etc...So in order to represent one operation, we need some bits right!!! If we have 4 distinct operations like ADD, MUL, DIV, SUB, we have to represent each operation with some bits, here we need two bits.
So we need log24 = 2 bits
Similarly, in this question, we have 40 distinct opcodes, so in order to name them in binary
we need log240 = 6 bits