James Montgomery Beggs, who was indicted yesterday on charges of trying to defraud the government while an executive of General Dynamics Corp., took over at the National Aeronautics and Space Administration after the space shuttle's first flight, which was more than two years behind schedule and more than $1 billion above cost.

That was June 1981, and in four years Beggs, 59, put the $10 billion space shuttle program back on course.

The civilian space agency's sixth administrator since its formation in 1958, Beggs has presided over an agency whose fleet now has four shuttles and has conducted 22 shuttle flights, all without a serious accident.

The 23rd flight, the second voyage of the shuttle Atlantis, is to end this afternoon with a landing at Edwards Air Force Base in California.

News that Beggs had been indicted on criminal charges dating to his tenure as executive vice president of General Dyanmics came as a shock to those who have worked under him at NASA.

"You're kidding," one of his aides said yesterday when told of the indictment.

Another said, "You're either lying or joking, and I don't find it very funny."

When Beggs took over at NASA, it was troubled. Two months earlier, the shuttle Columbia had flown for the first time after more than two years of delays and supplemental budgets that cost NASA more than $1 billion and much congressional patience.

Beggs' predecessor, Robert A. Frosch, had been gone more than six months, no deputy administrator was in place and the agency's five most important planned missions faced the ax of David A. Stockman, then the new director of the Office of Management and Budget.

"The day Jim took over, the agency was almost leaderless," one Reagan administration source said. "He inherited an agency with nobody to stand up to Stockman and nobody who could answer a serious budget question from Capitol Hill. It was horrible."

When Beggs took office, NASA had under way five major space science missions, three in an advanced state of development and two on the drawing boards.

Stockman had ordered the agency to delay one on the drawing board, a radar-carrying spacecraft to orbit Venus.

He soon killed one of the three advanced plans -- the U.S. version of a German-American mission to the sun called Solar Polar -- and administration sources said he was honing his ax to kill at least one of the other two, perhaps that involving the Galileo mission to Jupiter.

Under Beggs, the agency lost only one of the other four missions, a proposal to fly by Halley's Comet. Beggs says he "deeply regrets" losing that mission.

But NASA kept its Galileo mission, another that featured the space telescope and a scaled-down mission to Venus at half of the original cost.

The agency's current budget is $7.7 billion, slightly more than $1 billion higher than when Beggs became NASA administrator.

Beggs is in his third tour of duty in the federal government. Before taking over at NASA, he served as the agency's associate administrator in 1968-1969 and, from 1969 to 1973, was undersecretary of transportation.

He is a 1947 graduate of the U.S. Naval Academy, served seven years in the Navy and received a master's degree from the Harvard Graduate School of Business Administration after leaving the service.