The U.S. economy grew at a 4% annual pace during the second quarter, beating forecasts and reflecting optimism from the American consumer.