BERLIN — In his strongest public comments since the election, President Obama on Thursday sharply criticized the spread of fake news online and said that President-elect Donald J. Trump would not remain in office for long if he failed to take the job seriously.

Mr. Obama made his remarks at a news conference in Berlin beside the German chancellor, Angela Merkel, one of his closest allies on the Continent, calling the pair “veterans” of the international political stage. Ms. Merkel was unusually sentimental. “It is hard to say goodbye,” she said.

But instead of basking in the glow of his valedictory tour of Europe, Mr. Obama used the moment to make a passionate and pointed attack on bogus news stories disseminated on Facebook and other social media platforms, twice calling such false reports a threat to democracy in his hourlong news conference.

Mr. Obama also warned Mr. Trump of the need to take the job of the presidency seriously and to be tough on Russia.