Does an inductor stretch or compress when a current is passed through it? If an inductor is given to you and it is connected across a battery without any resistance in the circuit,then will the inductor stretch,compress or remain the same length?
 A: A solenoid carrying a current compresses. This is because the field lines start to 'splay out' 'before' the geometrical ends of the solenoid, so there is a motor effect (Laplace) force on the end turns of the solenoid that acts parallel to the axis towards the centre of the solenoid.
To understand this better, assume a particular sense of current in the solenoid. Use the right hand grip rule to get the direction of the field lines through the solenoid. Consider now the North end of the solenoid, that is the end from which the lines emerge. But because the lines splay out; there is a field component, $B_r$,  radially outward from the axis. Now apply $\mathbf {dF} = I \mathbf {dl}\times B_r \mathbf {\hat r}$ (or Fleming's left hand motor rule) to any part of any of the end turns. You should find a force parallel to the axis towards the solenoid's centre. Now consider the South end.
So the ends of the solenoid experience forces towards the solenoid centre, tending to compress the whole solenoid, just as if we used our hands to push the two ends of a spring towards each other.
Suppose now that we broke the solenoid somewhere in the middle and separated the two 'halves', maintaining the current in each. Both halves would still be 'long solenoids' and the newly formed ends would behave qualitatively and quantitatively just like the original ends.
Magnitude of the force: (a) Developing the previous qualitative argument
Let $A$ and $l$ be the cross-sectional area and length of the solenoid and $n$ be the number of turns.
The axial force on a slice, $dl$, of the solenoid (containing $\tfrac nl dl$ turns) near an end is
$$dF\ \ =\ \ B_rI\ 2\pi r \frac nl dl\ \ =\ \ \frac{d\Phi_{esc}}{2\pi r\ dl}I\ 2\pi r \frac nl dl\ \ \ =\ \ I \frac nl d\Phi_{esc} $$
in which $d\Phi_{esc}$ is the flux escaping through area $2\pi r\ dl$ of the curved surface of the solenoid. Integrating, we find that the total force on the end region is
$$F=I \frac nl \Phi_{esc} = I \frac nl \tfrac12 \Phi.$$
Here, $\Phi_{esc}$ is the total flux escaping through the sides of the end-region. This is half the main flux, $\Phi$, that is the flux through any cross-section, $A$, of the solenoid except near the ends. [We know this because half the flux density at A must be due to the 'half' solenoid to one side of $A$, and the other half to the other. Therefore the flux emerging from the geometric end of  a long solenoid must be $\tfrac12\Phi$, and so $\Phi_{esc}=\Phi-\tfrac12\Phi$]
Using Ampère's rule
$$\Phi =\mu_0 \frac nl I A$$
The magnitude of the compressive force is therefore
$$F=\tfrac12 \mu_0 A \left(\frac nl \right)^2 I^2.$$
This can be neatly recast in terms of the solenoid inductance, $L$, or the energy stored in it:
$$F= \left.\tfrac12 L I^2 \middle/l \right.$$
Magnitude of the force: (b) Virtual Work approach
We consider what happens when we increase the solenoid length by $\Delta l$ at constant current.
The flux $\Phi$ decreases because
$$\Phi=\mu_0 \frac nl I A \ \ \ \ \text{so} \ \ \ \ \ \frac{\partial \Phi}{\partial l}=-\mu_0 \frac {n}{l^2} I A$$
To first order
$$\Delta \Phi = -\mu_0 \frac{n}{l^2} IA \Delta l.$$
The energy stored in the solenoid's field is
$$U=\tfrac 12 L I^2 =\tfrac 12 \frac{n\Phi}{I}I^2=\tfrac 12 nI \Phi$$
So on stretching the solenoid by $\Delta l$,
$$\Delta U =\tfrac12 n I \Delta \Phi=-\tfrac12 \mu_0 \left(\frac nl \right)^2 A I^2 \Delta l.$$
However, changing the flux produces an electric field that does work, $W_{elec}$. We calculate $W_{elec}$ using the emf $\mathscr E(t)$ induced in the time $\Delta t$ that the flux changes by $\Delta \Phi$:
$$W_{elec}=\int_0 ^{\Delta t} \mathscr E I dt = I\int_0 ^{\Delta t} -n \frac{d\Phi}{dt}dt =-I n\Delta \Phi =+\mu_0 \left(\frac nl \right)^2 A I^2\Delta l.$$
[The induced emf is in the same sense as the external emf, $\mathscr E_{ext}$ that has been driving current through the solenoid, thereby increasing the current and opposing the decrease in flux due to the stretching. But we claimed that we were holding the current constant! It suffices simply to have $\mathscr E_{ext}>>\mathscr E_{int}$, with a suitable circuit resistance. The extra electrical work results in extra heating of this resistance.]
To recap: electrical work of amount $+\mu_0 \left(\frac nl \right)^2 A I^2 \Delta l$ has been done when the flux changes, but the solenoid's field has lost only half this energy. Therefore the missing $\tfrac 12 \mu_0 \left(\frac nl \right)^2 A I^2 \Delta l$ needed fully to fund the electrical work must be mechanical work put in when the coil is stretched by $\Delta l$.
Therefore
$$F \Delta l =\tfrac 12 \mu_0 \left(\frac nl \right)^2 A I^2 \Delta l$$
So
$$F=\tfrac 12 \mu_0 \left(\frac nl \right)^2 A I^2$$
A: Due to the Lorentz force parallel currents
attract each other, and antiparallel currents
repel each other
(see also "Magnetic force between wires").
In a current-carrying solenoid the currents through the
wire are parallel, and that is why the wires
are compressed.

Now let's make it quantitative and actually calculate the compressive force.
For simplicity we consider a solenoid coil (with $n$ turns,
length $l$, cross section area $A$).
For a very long solenoid (i.e. $l^2 \gg A$)
the magnetic field is nearly homogenous inside the coil,
and nearly zero outside. The magnetic field strength inside the coil is
$$B=\mu_0\frac{nI}{l}. \tag{1}$$
The energy density of the magnetic field is
$$u=\frac{1}{2\mu_0}B^2, \tag{2}$$
and hence the total magnetic energy
(within the whole cylindrical volume $V=Al$) is $$E=u Al. \tag{3}$$
Now let's pull the coil apart between two neighboring turns
by a small distance $\Delta l$.
The magnetic field $B$ in this newly added volume ($\Delta V=A\ \Delta l$) will be
nearly equal to the magnetic field in the other parts of the cylinder.
Hence the total field energy will increase by the amount
$$\Delta E=u A\ \Delta l. \tag{4}$$
On the other hand, by pulling with a force $F$
(compensating the magnetic compressive force) we do mechanical work
$\Delta E=F\ \Delta l. \tag{5}$
By the conservation of energy (4) and (5) must be equal, and we get the force
$$F=u A \tag{6}$$
and by inserting (1) and (2) we finally get
$$F=\frac{1}{2}\mu_0\left(\frac{nI}{l}\right)^2A \tag{7}$$
which (by no surprise) is the same result
as given in the answer by @PhilipWood.
