BankAmerica Corp. yesterday reported a $338 million loss for the second quarter, one of the biggest quarterly losses ever reported by a bank company.

BankAmerica, with $119 billion in assets, is the nation's second-biggest bank company. Its principal subsidiary is Bank of America.

The company has had earnings problems for the last several years, but yesterday's announcement of a giant loss came as a shock to the financial community. Only six weeks ago, the San Francisco bank announced that loan problems would cause it to have no earnings in the three months ended June 30.

President Samuel Armacost said yesterday that since the announcement about the bank's mounting loan problems, "evidence has been accumulating that there is a growing weakness in important sections of the economy which particularly affected our portfolio."

Because of the number of problem loans, BankAmerica added $527 million to its reserve for loan losses. The addition to the loan loss reserve is subtracted directly from earnings. When a loan is written off as uncollectable, the writeoff is made to the reserve, not earnings.

In the second quarter, the bank company said it wrote off $382 million of loans, compared with $258 million in the second quarter of 1984.

As they have with other banks, federal examiners pushed the big addition to the loan loss reserve at Bank of America. Examiners from the Office of the Comptroller of the Currency began an examination of Bank of America last April. Armacost said yesterday that the examination was nearly finished.

William Welsh, who follows bank companies for the Wall Street securities firm Sanford C. Bernstein & Co., said many of BankAmerica's loan problems date back to before 1981, when Armacost succeeded A. W. Clausen as president of BankAmerica. Clausen now is president of the World Bank.

Many of the bank's problem loans are concentrated in agriculture and California real estate -- sectors that have been hit hard in recent years. Welsh said that like energy lenders in the Southwest who believed oil prices would do nothing but rise, real estate loans in California often were premised on never-ending increases in land and building prices.

"Drinking beer out of a boot in Oklahoma was the same as writing a mortgage in California in 1981," Welsh said. But he noted that part of BankAmerica's problems are peculiar to the bank. Many other major California institutions, such as Wells Fargo, have not experienced the same level of problems as BankAmerica, Welsh said.

Analysts said the key question is whether the bank and federal examiners have identified the bulk of the problem loans and reserved against them or whether the portfolio will further deteriorate. Besides agriculture and real estate, Armacost identified shipping and foreign loans as the major sources of BankAmerica's loan problems.

Most other major banks have faced increases in problem loans in recent months. Unlike BankAmerica, however, most of them reported earnings gains. The increased revenue the banks realized from declining interest rates -- the rates banks paid to attract deposits fell faster than the rates they charged on loans -- outweighed provisions they made to increase their loan loss reserves.

First Chicago Corp., which was forced by regulators last fall to add more than $300 million to loan loss reserves, reported a large decline in earnings because of a Brazilian bank it bought last year. But its problem loans declined and the company would have had a good earnings quarter on its core lending business.

Armacost said yesterday that although the big addition to BankAmerica's loan loss reserves had a "painful" impact on short-term financial results, the bolstering of the reserve "is a prudent course for the long-range benefit of the corporation."

BankAmerica's loan loss reserves totaled $1.5 billion at the end of June. That is equivalent to 1.81 percent of total loans. On June 30, 1984 the loan loss reserve represented 1.13 percent of total loans.

BankAmerica closed at 17 5/8 on the New York Stock Exchange yesterday, down 1/4.