All of the dead were said to have been students of the College of Agriculture in the town of Gujba, some 30 kilometers (18 miles) from Damaturu, the capital of Yobe state.
The early morning attack on Sunday was carried out by "Boko Haram terrorists who went into the school and opened fire on students" while they were sleeping, the military spokesman in Yobe, Lazarus Eli, told AFP.
An official at the Damaturu Specialist Hospital, who requested anonymity, said "40 bodies", all of whom appeared to be of university student age, had been received so far from Gujba.
Four others were being treated for gunshot wounds, he told AFP.
Hundreds of people "have thronged to the hospital" including relatives of those who attend the college, trying to learn if their family members are among the victims, the hospital official added.
Yobe has seen a spate of attacks on schools and universities, all blamed on Boko Haram, an extremist group that has killed hundreds in its four-year insurgency.
The name Boko Haram means 'Western education is forbidden' and the group has become notorious for slaughtering students of various ages in recent years.
In July in the town of Mamudo in Yobe, militants threw explosives and sprayed gunfire into dormitories in the middle of the night, killing 41 students.
Presumed Boko Haram gunmen shot dead seven secondary school students and two teachers in Damaturu in June.
A toll earlier this year said the conflict is estimated to have cost more than 3,600 lives, including killings by the security forces. The current figure is likely much higher.
Nigeria is Africa's most populous country and top oil producer, roughly divided between a mainly Muslim north and predominately Christian south.
HH/HH