By Alexandra Scaggs 

U.S. stocks gained broadly Wednesday, bringing the S&P 500 index into positive territory for the year.

Rising hopes of economic stimulus from Asia and a sharp two-day rally from highflying stocks helped bolster investor confidence.

The Dow Jones Industrial Average rose 162.29 points, or 1%, to end at 16424.85, notching its biggest three-day gain this year. The S&P 500 index advanced 19.33 points, or 1%, to 1862.31, with all 10 sectors higher. The Nasdaq Composite Index climbed 52.06 points, or 1.3%, to 4086.23.

The rally picked up some steam in afternoon trading, led by high-octane biotechnology and social-media stocks. The Nasdaq Biotechnology Index rose 2.4%. Those so-called momentum stocks have made a series of big swings in the first half of April, roiling the tech-heavy Nasdaq Composite.

So far this month, the Nasdaq has lost 2.7%, and the Nasdaq Biotechnology Index has dropped 5.8%. Initially, many of those sharp moves were driven by trading in exchange-traded funds, traders said.

But during the latest rebound, traders saw "real buyers come in" and buy shares of individual companies, said Viren Chandrasoma, managing director in equity trading at Credit Suisse Group AG. "I don't think it's fast money... it's really led by the stocks themselves."

Stock benchmarks started the session with broad gains, after a report showing that China's gross domestic product grew 7.4% in the first quarter, the slowest rate in 18 months, but above economists' forecasts of 7.3%. Traders said the slowing pace of growth raised some hopes of more stimulus from the Chinese government.

"The Chinese data certainly corroborates the idea that the economy is slowing, but at the same time officials have become concerned enough" that more stimulus could be a possibility, said Dan Greenhaus, chief market strategist at New York brokerage firm BTIG.

And that is a positive factor for stocks globally, he said. Stocks in Europe and Asia gained, with the Stoxx Europe 600 up 1.3%. Japan's Nikkei Stock Average shot up 3% after Bank of Japan Governor Haruhiko Kuroda said he would closely monitor stock prices and that inflation was on a steady track toward a 2.0% target. China's Shanghai Composite gained 0.2%.

Despite the recent volatile trading, U.S. stock benchmarks haven't given up much ground from their record levels hit on April 2. The S&P 500 has lost 0.5% this month, and is just 1.5% below its all-time high. The Dow industrials have held up better, down 0.2% this month and 0.9% from their record level.

Benchmarks have held up despite expectations for relatively weak first-quarter corporate earnings. With 47 companies reporting, the S&P 500 is on pace to report that first-quarter earnings declined 1.9% from the previous year, according to FactSet, and that sales rose 2.2%.

U.S. stock-market gains this year will be mostly based on profit growth, said Christian Ledoux, director of equity research at South Texas Money Management, which manages $2.3 billion. "It is going to be bumpy for most of the year... equity [valuations] are slightly above average, and they're working their way toward overvalued."

Still, he said he is keeping his allocation to stocks steady, since they are a good value when compared with investments such as bonds.

Dow component Intel saw shares rise 16 cents, or 0.3%, to $26.93 after it reported late Tuesday first-quarter earnings that topped analyst estimates, while revenue was in line with forecasts and PC sales continued to decline.

Yahoo surged $2.14, or 6.3%, to $36.35 after first-quarter results exceeded expectations, and the company provided an upbeat revenue outlook for the current quarter.

Bank of America slipped 26 cents, or 1.6%, to $16.13 after the banking giant reported it swung to a first-quarter loss as a result of previously-disclosed litigation expenses, while revenue fell slightly less than expected.

Stocks held their broad gains as Federal Reserve Chairwoman Janet Yellen spoke about monetary policy and the economy at the Economic Club of New York. The Fed also released its "beige book" report, a read on the U.S. economy from anecdotes gathered by the central bank's 12 districts, which said that economic activity in most of the country rebounded after an unusually icy winter hampered growth.

In other economic news, new residential construction for March rose 2.8% to a seasonally adjusted annualized rate of 946,000, falling well below forecasts of a rise of 6.4% to a seasonally adjusted annualized rate of 965,000. Building permits fell 2.4% to 990,000, versus forecasts of 1.01 million.

Industrial production rose 0.7% in March, while a rise of 0.4% was forecast. Capacity utilization rose by more than expected as well.

The yield on the 10-year Treasury note rose to 2.637% from 2.628% late Tuesday. Gold futures gained 0.2% to $1,303.10 a troy ounce, after falling 2% Tuesday to mark the biggest one-day percentage loss since Dec. 19.

Crude-oil futures tacked on less than 0.1% to $103.76 a barrel. The dollar rose against the yen, but lost some ground against the euro.

In corporate news, New York investment bank Moelis & Co. rose $1.15, or 4.6%, to $26.15 in its public debut, after pricing its initial public offering below its forecast range.

Twitter slipped $1.10, or 2.4%, to $44.42. The stock shot up 11% Tuesday, the biggest one-day gain since the microblogging site went public in November. Tuesday's rally came one day after some of the company's earliest and biggest backers said they didn't plan to sell shares when rules barring them from doing so expire next month.

Twitter had been one of the hardest-hit of the previous highfliers, as the stock was down 37% on the year through Friday, after ending last year 145% above its $26 initial public offering price.

Write to Alexandra Scaggs at alexandra.scaggs@wsj.com