Can I attach floating shelves to the wall in this way?
Yes - It does sound feasible. A few things I would consider for cutting off the lag bolt heads.


Install the bolts first into the wall studs so you can utilize the heads for screwing in the bolts. Do not make the common mistake of forgetting to pre-drill a proper sized pilot hole for the lag bolt threads into the stud.
Use a reciprocating type saw (i.e. Sawzall) with a high quality metal cutting blade to cut the bolt heads off.
Dress up the cut bolt stubs to eliminate the sharp edges by using a Dremel type tool with a small grinder stone.
Only now mark the shelf pieces for the proper hole locations and drill your holes into the shelves. 


Additional considerations -- -- 


A 4 x 24 inch shelf cannot get too much stuff on it. You may consider using a smaller size than 1/2 inch bolt and achieve a suitable result. A 1/2 inch lag into the edge of a stud has to be very carefully centered on the stud.
Consider safety factors regarding shelves that can simply pull off the wall mounted studs. You may want to consider the possibility of some type of set screw in from the top or bottom side of the shelf that would help clamp the shelf to the bolt studs. (You could of course glue the shelves to the bolt studs with construction adhesive but then the installation would be virtually permanent).