Homework question about an engine pulling a train 
An 8000 kg engine pulls a train of 5 wagons, each of 2000 kg, along a
  horizontal track. If the engine exerts a force of 40000N , calculate the acceleration of the train.

To find the acceleration of the train , I divided 40000N by mass of 5 wagons. 
But in the book the mass of the engine was also included for finding the acceleration.
I cannot understand how is the force of 40000 N acting on (engine + wagons). According to me this should only act on the wagons as the engine is applying this force. 
 A: IMO you're right: the problem's wording is bad. It's meaningless to say "the engine exerts a force of 40000 N". The engine interacts both with the first wagon and with the rail, exerting forces on both and these forces are different. How can we know what the book's author had in mind? If the former, your answer is right, if the latter, the book's one is right. Of course in the latter case (force exerted on rail) it's not that force which accelerates the train, but the rail's reaction.
As a general rule we shouldn't say "A exerts a force" - much better "A exerts a force on B". Better yet "A exerts a force on point P of B". The point where a force acts can make a great difference.
Edit 
Many things have been said but IMO it's useful to be still clearer. After further reflections I think it's not wording that's bad, but the thinking behind. 
As already stated e.g. by @Farcher in mechanics problems the first thing to be done is to make clear which the (mechanical) system is. In our case, it's wagons or wagons + engine? Note that it wouldn't make
difference as to acceleration, which is the same for all parts of the train, but isn't by no means the same if forces are concerned. Consider the two choices:
a) The system consists of wagons alone. Then it makes sense to say that engine (which doesn't belong to the system) applies a given force $F$ to the system and compute acceleration as $a=F/m$, where 
$m=10000\,\rm kg$. Given the book's answer, this isn't the author's choice.
b) The system consists of the whole train, engine included. Then there are two alternatives as to the given force $F$ (here I'm reiterating) 
b1) $F$ is a force engine applies to wagons. Then it's an internal force and as such it cannot accelerate the system. Its acceleration is only due to the net external force (in computing the resultant internal forces cancel thanks to third Newton's law). Then the
solution is nonsense.
b2) $F$ is a force engine applies to external world. In that case it will never accelerate the system or a part of it. It could accelerate the rail, were it not firmly fixed to ground. Not to mention that a force engine applies to rail exists, but is in the wrong direction.
So not even choice b) is acceptable. Neither as it's used in gandalf61's answer. 
I'd like to add another comment. Unfortunately "engine" in English may mean two different things: either the motor proper (electric, diesel,
or else) which ultimately is the cause of motion - or the car carrying it. Of course the datum of mass suggests the latter interpretation. But my suspicion is that the author - maybe without realizing - thought
of the former.
I'm led to say so because it's a very common way of thinking about cars and other motorized vehicles. If you ask someone "which force causes a car move?" I bet you'll get as an answer "the motor's force". Nobody thinks that in strict mechanical meaning the motor can only produce internal forces and as such will never move a car by a cm. That the only external force which can put a car in motion is the road's friction on the wheels.
A: You have to add mass of engine also.The force is applied on whole system.What will be the acceleration of only engine if you remove wagons?You will consider it's mass also.
A: Imagine if you only had the engine by itself exerting the same 40,000N of thrust, to find the acceleration of the engine you would not do 40000/0kg would you? The 40,000N the engine exert is also used to thrust itself forward.
But let’s say it didn’t, and that only the wagons were pulled. Well now you have a stationary engine, pulling 5 wagons, each with an acceleration of 4m/s^2. These wagons will catch up to the engine in no  time and obliterate the engine from behind. we can at least agree this is not how train works 
A: The question is flawed in that can be deemed to be ambiguous and therefore requires interpretation.  
Using Newton’s second law requires the system under consideration to be defined and that has not been done in this question.  
Without the system being defined the statement  

the engine exerts a force of 40000N  

is the cause of the ambiguity.  
So which is it?  
1 The engine is exerting an external force of 40000N on the system of five wagons.  
2 The engine exerts an internal force of 40000N on the system of five wagons and the engine.  
3 There is an external force of 40000N on the engine and five wagons.  
The author of the book really meant option 3 but given the way the question was written I have to agree with the OP that an external force of 40000N is acting on the five wagons whcich is option 1.
A: The force of 40 kN is exerted on the rails, not the wagons
We have to assume that the engine and wagons are all coupled together, and therefore that they move together. If the engine exerted a force on the wagons but did not move with them, the wagons would provide a different force in reaction. If the train was off the track, it would not be exerting that force on the wagons. 
The engine turns the wheels which exert a force backwards on the rails, which also imparts the reaction forwards on the train. 
That force is transmitted from the rails through the engine and the coupling to the wagons.
To take a different circumstance, suppose the train was going uphill? The engine will still exert a force of the same magnitude, but this time the balance of forces and angles are different, so the acceleration is different too.
Thus that force must accelerate the total mass of the engine and the wagons together.
A: The question is ambiguous, but I think we are meant to assume that the engine exerts a force of $40000$ N on the track. From the reaction force of $40000$ N on the engine some part, say $F_1$, is acting to accelerate the engine itself and some other part, say $F_2$, is exerted on the wagons. The acceleration of the engine and of the wagons is the same, so if this acceleration is $a$ then
$F_1 = 8000a \\ F_2 = 10000a \\ 40000 = F_1 + F_2 = 18000a \\ \Rightarrow a = \frac{40000}{18000}$
