To fix that I would probably run the first query to see what answer you get there and adjust to the needed result, then you can correct the dynamic portion which is failing.
Which would be:
SELECT DISTINCT subjectId from
(
SELECT * From school.dbo.Mark_details
SUM(MarkObtained)
)
which may require some adjusting but that can direct where the values are coming from, then adjust your dynamic and pivot from there.
If you want to run that as a script and trouble shoot from there you can use the following:
--alter procedure sp_getmarkforclass(@classid int,@examid int)
--as
--begin
declare @paramlist varchar(max),@query nvarchar(max)
set @paramlist=STUFF((select distinct ',[' + SubjectId + ']' from School.dbo.Mark_details for xml path('')),1,1,'')
--print @paramlist
set @query='select' + @paramlist + 'from (select * from Mark_details)src pivot(sum(MarkObtained) for SubjectId in (' + @paramlist + ')) pvt'
exec @query
--end
And can run it as a script to trouble shoot that SP.
Hope that helps