The \def macro doesn't accept \begingroup...\endgroup instead of { }.
No macro does. Some macros (like \hbox) accept \bgroup and \egroup which are implicit braces, i.e. created using \let\bgroup={ and \let\egroup=} (the = is optional).
To store the content of an environment use the environ package which was created for this purpose. The content is presented as \BODY which can be copied to \answertext. Keep in mind that the code is executed inside the environment, i.e. in a group, therefore \global\let\answertext\BODY or \xdef\answertext{\BODY} must be used. However, this only allows one answer text to be stored. If you want to accumulate answers use either \g@addto@macro<macro>{<code>}, e.g. \expandafter\g@addto@macro\expandafter\answertext\expandafter{\BODY} or the \xappto macro of the etoolbox package.
Some example (without the exam class):
\documentclass{article}
\usepackage{environ}
\newcommand{\answertext}{}
\NewEnviron{answer}{\global\let\answertext\BODY}
% or
\makeatletter
\NewEnviron{answer}{\expandafter\g@addto@macro\expandafter\answertext\expandafter{\BODY}}%
\makeatother
% or
\usepackage{etoolbox}
\NewEnviron{answer}{\xappto\answertext{\expandonce{\BODY}}}%
\begin{document}
The question
\begin{answer}
The answer text
\end{answer}
\newpage
% Some time later
\answertext
\end{document}
examclass have something built-in like this for multiple choice questions (using thechoicesenvironment), or should I use something like your answer? – cmhughes Nov 16 '11 at 00:12verbatimenvironment inside theanswerenvironment, great! I will try to hook yoursolutionenvironment in theexamclass to see whether I can put all the answers together separately. Thanks. – ollydbg23 Jan 08 '15 at 03:51etootlbox', but can't see to make it work:\AtBeginEnvironment{answer}{\vspace{\baselineskip}\thequestion,} ` prints the counter just after the question (which makes a lot of sense), but I cannot figure out how to print that counter on the box you defined. Any ideas? – mathbekunkus Feb 25 '20 at 05:31