Are lektion and läxa interchangeable, or does läxa refer to lessons learned during homework?
No. While homework is uncountable in English, läxa is countable as one of the tasks assigned for homework. The translations here are idiomatic, meaning being forced to learn or adapt.
Also, the 1 hr session you might have with a teacher and other students can only be called en lektion, never 'en läxa'. Also a chapter in a book can be Lektion 1, 'Lesson 1', but it'll never have the title "Läxa 1" (that would be 'Homework 1')
Basically this lesson/läxa is if he had done something bad and had to learn a lesson
I tried "he tried to learn the piece of homework" ( as I was taught to translate 'läxa' in a previous part of the course) , but was marked wrong. I'll admit "lesson" sounds correct here, but all this sentence achieves is confusion. Now I'll have to think of both lektion and läxa every time I see lesson! :(