Here's my solution to this problem.
If you compile without -shell-escape, it just runs as normal (i.e., if you are using the answers option it will output answers, otherwise not; the default \jobname is used.)
If you compile with -shell-escape, then it outputs two files: \jobname.pdf with questions and \jobname-solved.pdf with answers. It does this regardless of whether the answers option is passed to the class or not.
I've used latexmk so that required multiple compilations are taken into account automatically.
MWE
\documentclass{exam}
\usepackage{pdftexcmds}
\usepackage{iftex}
\makeatletter
\ltx@IfUndefined{pdf@shellescape}
{}
{\ifnum\pdf@shellescape=1
\ifpdftex
\def\latexmkengine{-pdf}%
\fi
\ifluatex
\def\latexmkengine{-lualatex}%
\fi
\ifxetex
\def\latexmkengine{-xelatex}%
\fi
\pdf@system{%
latexmk \latexmkengine\space -jobname="\jobname-solved"
-usepretex="\string\AtBeginDocument{\string\printanswerstrue}"
"\jobname"
}%
\pdf@system{%
latexmk \latexmkengine\space
-usepretex="\string\AtBeginDocument{\string\printanswersfalse}"
"\jobname"
}%
\expandafter\stop
\fi}
\makeatother
\begin{document}
\begin{questions}
\question One of these things is not like the others; one of these things is
not the same. Which one is different?
\begin{oneparchoices}
\choice John
\choice Paul
\choice George
\choice Ringo
\CorrectChoice Socrates
\end{oneparchoices}
\end{questions}
\end{document}
Output
\jobname.pdf

\jobname-solved.pdf

If you want to only output one file at a time, you can slightly modify the above answer so that latexmk is only called when the answers option is actually passed to the exam class. This answers your question as you've asked it. It still needs to be compiled with -shell-escape.
\documentclass[answers]{exam}
\usepackage{pdftexcmds}
\usepackage{iftex}
\makeatletter
\ltx@IfUndefined{pdf@shellescape}
{}
{\ifnum\pdf@shellescape=1
\ifpdftex
\def\latexmkengine{-pdf}%
\fi
\ifluatex
\def\latexmkengine{-lualatex}%
\fi
\ifxetex
\def\latexmkengine{-xelatex}%
\fi
\ifprintanswers
\pdf@system{%
latexmk \latexmkengine\space -jobname="\jobname-solved" "\jobname"
}%
\expandafter\expandafter\expandafter\stop
\fi
\fi}
\makeatother
\begin{document}
\begin{questions}
\question One of these things is not like the others; one of these things is
not the same. Which one is different?
\begin{oneparchoices}
\choice John
\choice Paul
\choice George
\choice Ringo
\CorrectChoice Socrates
\end{oneparchoices}
\end{questions}
\end{document}
And an expl3 solution that handles file names with spaces. Still needs -shell-escape of course.
Ouptut two files
\documentclass{exam}
\usepackage{expl3}
\ExplSyntaxOn
\str_new:N \l__diaa_latexmk_engine_str
\str_new:N \g__diaa_solved_jobname_str
\str_const:Nn \l__diaa_latexmk { latexmk }
\cs_new:Nn \__diaa_build_solved_jobname:
{
\str_gset:Nx \g__diaa_solved_jobname_str { \c_sys_jobname_str }
\str_gremove_all:Nn \g__diaa_solved_jobname_str { " }
\str_gput_left:Nn \g__diaa_solved_jobname_str { " }
\str_gput_right:Nn \g__diaa_solved_jobname_str { ~[solved]" }
}
\sys_if_shell_unrestricted:T
{
\sys_if_engine_luatex:T
{ \str_set:Nn \l__diaa_latexmk_engine_str { -lualatex } }
\sys_if_engine_pdftex:T
{ \str_set:Nn \l__diaa_latexmk_engine_str { -pdf } }
\sys_if_engine_xetex:T
{ \str_set:Nn \l__diaa_latexmk_engine_str { -xelatex } }
\__diaa_build_solved_jobname:
\sys_shell_now:x
{
\l__diaa_latexmk \c_space_tl
\l__diaa_latexmk_engine_str \c_space_tl
-usepretex="\string\AtBeginDocument{\string\printanswerstrue}" \c_space_tl
-jobname=\g__diaa_solved_jobname_str \c_space_tl
\c_sys_jobname_str
}
\sys_shell_now:x
{
\l__diaa_latexmk \c_space_tl
\l__diaa_latexmk_engine_str \c_space_tl
-usepretex="\string\AtBeginDocument{\string\printanswersfalse}" \c_space_tl
\c_sys_jobname_str
}
\stop
}
\ExplSyntaxOff
\begin{document}
\begin{questions}
\question One of these things is not like the others; one of these things is
not the same. Which one is different?
\begin{oneparchoices}
\choice John
\choice Paul
\choice George
\choice Ringo
\CorrectChoice Socrates
\end{oneparchoices}
\end{questions}
\end{document}
Output one file
\documentclass[answers]{exam}
\usepackage{expl3}
\ExplSyntaxOn
\str_new:N \l__diaa_latexmk_engine_str
\str_new:N \g__diaa_solved_jobname_str
\str_const:Nn \l__diaa_latexmk { latexmk }
\cs_new:Nn \__diaa_build_solved_jobname:
{
\str_gset:Nx \g__diaa_solved_jobname_str { \c_sys_jobname_str }
\str_gremove_all:Nn \g__diaa_solved_jobname_str { " }
\str_gput_left:Nn \g__diaa_solved_jobname_str { " }
\str_gput_right:Nn \g__diaa_solved_jobname_str { ~[solved]" }
}
\sys_if_shell_unrestricted:T
{
\sys_if_engine_luatex:T
{ \str_set:Nn \l__diaa_latexmk_engine_str { -lualatex } }
\sys_if_engine_pdftex:T
{ \str_set:Nn \l__diaa_latexmk_engine_str { -pdf } }
\sys_if_engine_xetex:T
{ \str_set:Nn \l__diaa_latexmk_engine_str { -xelatex } }
\legacy_if:nT { printanswers }
{
\__diaa_build_solved_jobname:
\sys_shell_now:x
{
\l__diaa_latexmk \c_space_tl
\l__diaa_latexmk_engine_str \c_space_tl
-jobname=\g__diaa_solved_jobname_str \c_space_tl
\c_sys_jobname_str
}
\stop
}
}
\ExplSyntaxOff
\begin{document}
\begin{questions}
\question One of these things is not like the others; one of these things is
not the same. Which one is different?
\begin{oneparchoices}
\choice John
\choice Paul
\choice George
\choice Ringo
\CorrectChoice Socrates
\end{oneparchoices}
\end{questions}
\end{document}
Output two files with a call directly to lualatex instead of latexmk:
\documentclass{exam}
\usepackage{expl3}
\ExplSyntaxOn
\str_new:N \g__diaa_solved_jobname_str
\str_const:Nn \l__diaa_latex_cmd { lualatex }
\str_const:Nn \l__diaa_latex_options { -synctex=1 ~ -interaction=nonstopmode }
\cs_new:Nn \__diaa_build_solved_jobname:
{
\str_gset:Nx \g__diaa_solved_jobname_str { \c_sys_jobname_str }
\str_gremove_all:Nn \g__diaa_solved_jobname_str { " }
\str_gput_left:Nn \g__diaa_solved_jobname_str { " }
\str_gput_right:Nn \g__diaa_solved_jobname_str { ~[solved]" }
}
\sys_if_shell_unrestricted:T
{
\__diaa_build_solved_jobname:
\sys_shell_now:x
{
\l__diaa_latex_cmd \c_space_tl
-jobname=\g__diaa_solved_jobname_str \c_space_tl
\l__diaa_latex_options \c_space_tl
"\string\AtBeginDocument{\string\printanswerstrue}" \c_space_tl
"\string\input{\c_sys_jobname_str}"
}
\sys_shell_now:x
{
\l__diaa_latex_cmd \c_space_tl
-jobname=\c_sys_jobname_str \c_space_tl
\l__diaa_latex_options \c_space_tl
"\string\AtBeginDocument{\string\printanswersfalse}" \c_space_tl
"\string\input{\c_sys_jobname_str}"
}
\stop
}
\ExplSyntaxOff
\begin{document}
\begin{questions}
\question One of these things is not like the others; one of these things is
not the same. Which one is different?
\begin{oneparchoices}
\choice John
\choice Paul
\choice George
\choice Ringo
\CorrectChoice Socrates
\end{oneparchoices}
\end{questions}
\end{document}
--shell-escape? If yes, I would be grateful if you could provide an answer to show me how to do so. – Diaa Dec 24 '19 at 21:24answersis given as an option. That compilation process could output whatever filename you want, but you can't change the output filename of the current compilation process. (But you could probably run the new process and then end the current one without outputting anything, so you'd only end up with the other-named PDF. Your log file and all the rest would get the current filename, though, and you'd have to run any additional things you need for the new filename (e.g. bibtex/biber/makeindex/another run etc.). – cfr Dec 25 '19 at 03:33answersor not. On Linux/Mac you can usegrepfor this, in Windowsfindstr. – Marijn Dec 25 '19 at 17:35