The least number of boxes and bags needed are 3 and 2 respectively.
Explanation
Suppose, the number of boxes is [tex]x[/tex] and the number of bags is [tex]y[/tex]
Given that, pencils come in 8 in a box and erasers come 12 in a bag.
So, the total number of pencils in [tex]x[/tex] number of boxes [tex]=8x[/tex] and the total number of erasers in [tex]y[/tex] number of bags [tex]=12y[/tex]
As there are total 24 students and each student needs one pencil and one eraser to take a test, so...
[tex]8x=24\\ \\ x= \frac{24}{8}=3[/tex]
and
[tex]12y=24\\ \\ y=\frac{24}{12}=2[/tex]
So, the least number of boxes and bags needed are 3 and 2 respectively.