Young tech workers fill office parks and corporate cafeterias across Silicon Valley with few if any grey-haired colleagues in sight. It’s a widely accepted reality within the technology industry that youth rules. But at least part of the extreme age imbalance can be traced back to advertisements for open positions that government regulators say may illegally discriminate against older applicants. Many tech companies post openings exclusively for new or recent college graduates, a pool of candidates that is overwhelmingly in its early twenties.

Apple AAPL , Facebook, Yahoo, Dropbox, and video game maker Electronic Arts all recently listed openings with “new grad” in the title. Some companies say that recent college graduates will also be considered and then go on to specify which graduating classes—2011 or 2012, for instance—are acceptable.

“In our view, it’s illegal,” Raymond Peeler, senior attorney advisor at the Equal Employment Opportunity Commission, the federal agency that enforces workplace discrimination laws said about the use of “new grad” and “recent grad” in job notices. “We think it deters older applicants from applying.”

Companies must walk a fine line with discrimination laws in hiring. Any policy that has a disparate impact on older workers—defined as people 40 or older—or treats them differently is illegal. Job ads don’t need to specifically mention age to violate the law; companies can run afoul by using more subtle language that discourages older workers from submitting resumes or disproportionately rules them out being hired.

Critics have long accused the technology industry of ageism. Public comments by top Silicon Valley executives and investors help fuel to the perception. “Young people are just smarter,” Mark Zuckerberg, Facebook’s chief executive, said on a conference stage in 2007 when he was 22. (He’s now 30). Vinod Khosla, a prominent venture capitalist, once said “people over 45 basically die in terms of new ideas,” although, like many in Silicon Valley who make such statements, he’s since rejected that view.

In fact, many factors explain the age imbalance in the technology industry. Younger workers command lower salaries and benefits. They are also less likely to have families and can therefore work longer hours. Some hiring managers also look for candidates who are a good “cultural fit,” which can be code for young and hip.

“It’s nothing short of rampant,” Norm Matloff, a computer science professor at University of California at Davis who follows age bias in the tech industry, said about ageism against software programmers. “The reason is very simple: money.”

Last year, Facebook quietly settled a case with California’s Fair Employment and Housing Department involving a job posting for an attorney. In the ad, Facebook listed routine requirements for the applicants like at least four years of legal experience. But Facebook stepped on shaky ground in describing the ideal candidate’s law school credentials.

“Class of 2007 or 2008 preferred,” the ad said.

After receiving an age discrimination complaint, state regulators opened an investigation. In its written response to the agency, Facebook defended the ad by arguing that law schools are filled with students of all ages and therefore favoring a particular graduating class—even a relatively recent one—did not amount to discrimination. In any case, the company said it interviewed applicants who earned diplomas outside its narrow range of preferred dates before deciding against filling the position.

Ultimately, Facebook settled the case with state regulators by promising to no longer mention law school graduation dates in job ads for its legal team. The settlement did not include any financial penalties. Facebook, however, has continued to use “new grad” in at least one hiring notice elsewhere in the company. Until recently, the company had an opening for a “Product Design, New Grad” along with a requirement that applicants be current undergraduate students.

Facebook FB , like many companies contacted, declined to comment. The few companies willing to address the topic would only do so through emailed statements. In general, the companies said they value workplace diversity and strive to hire workers of all backgrounds. But none of them would address the legal questions around their recruiting practices or why they posted questionable ads in the first place.

Electronic Arts  EA , which also posted job ads for new college graduates, defended its ads by saying that it hires people of all ages into its new grad program. To prove the point, the company said those accepted into the program range in age from 21 to 35. But the company soon had second thoughts about releasing such information, which shows a total absence of middle-aged hires in the grad program, and asked Fortune to withhold that detail from publication. (Fortune declined.)

“We welcome any candidate to apply for a career opportunity with us,” Electronic Arts said in an email. Additionally, the company said it planned to adjust the language on its career site, but it couldn’t say whether those changes would include eliminating “new grad” from job titles.

Yahoo YHOO , which had a number of job openings specifically for new and recent college graduates in software development, data engineering and product management, took a more conciliatory tack after saying its welcomed candidates of all ages. “If any of our job listings inadvertently discourage candidates of any age group from applying, we will make appropriate changes,” it said.

In fact, job ads specifically targeting new or recent college grads quickly disappeared from Yahoo’s career pages as they did with most other companies that Fortune contacted. But many other technology companies continue to post similar job ads.

There are exceptions to age discrimination laws that let companies mention age in job notices, but they are rare. By law, airline pilots must retire at 65 because of safety reasons, for example. Otherwise, companies need to show that age is an important qualification for the job because of the physical demands or, in the case of actors, a more realistic performance.

Companies can avoid getting into trouble by being more careful about what they say in their job ads, according to government regulators. Instead of saying “new grad” or “recent grad,” they should use terms like “entry-level position” and “no experience required.”

“Employers can go about it any number of ways without saying something that might discourage older workers from applying,” said Peeler, of the EEOC.

Companies can hardly claim ignorance about the legal minefield around job ads. Warnings against using “new grad” or “recent grad” in job postings abound online including on the EEOC’s website.

The agency, based on complaints from job seekers, occasionally targets tech companies for discrimination in their ads. Last year, it opened 37 such cases compared with 43 in 2012. But those numbers go beyond just age discrimination. They also include other forms of potential bias based on gender, religion or disability.

Whatever the legal scrutiny, it can be difficult to ignore the skewed demographics when visiting the offices of many tech companies. Their workers are exceedingly young compared to the overall U.S. labor force’s median age of 42.4. At Twitter, the median is just 28, according to PayScale, a company that collects and sells compensation information. At Facebook and Google GOOG , the median is 29.

In general, the age imbalance in the technology industry varies widely from company to company. Certain sectors tend to hire younger workers while others are full of people with a few wrinkles. Consumer Internet companies are typically flush with employees who are just a few years out of college. Meanwhile, computer hardware and business software firms—particularly the more established ones—are packed with people in their thirties and forties.

For example, the median age of workers at Oracle ORCL , the business software maker, is 38, according to PayScale. At Hewlett-Packard HPQ , the median is 40.

But even companies with older workforces post ads specifically for applicants who are either a “new grad” or “recent grad.” Hewlett-Packard had more than dozen such openings recently for software engineers, business analysts and I.T. developers. Asked about them, H.P. declined to answer specific questions and instead responded by email that it “absolutely does not discriminate against employees or applicants on the basis of age or any other protected characteristic.” But it did not bother to remove a dozen or so job ads that may be illegal.