Does putting an unrelated site in a domain's subfolder (to avoid buying a new domain name) affect the domain's search engine ranking even if there are no links between them? For example, is it a bad practice will placing a cooking site at programming.com/cooking affect the search engine ranking of programming.com?
Yes because the domain name, and by extension full URL, are a factor in the rankings of any content. So by having a programming domain name in a URL about cooking will do you no good SEO wise. So the pages that are off topic for the domain name will not get any benefit from having a proper domain name.
The main site will not be affected as search engines don't rank sites, they rank pages. So there won't be any tangible association between the two sites even though they share the same domain due to the fact they are essentially segregated.
In general if you're just adding seemingly unrelated content to a site in small quantities it should be ok. However, if you're actively looking to make it a site that others will use (and link to) there could be issues, or if you're planning to put a lot of content on the site it could cause issues with crawling and indexing.
I'd recommend using a free hosted site if you're not going to buy a domain (like wordpress, typepad, tumblr, blogger et al). This way you don't have the added expense of the domain or hosting but also don't have to worry about it messing with your other site.
Even if don't encounter any SEO issues with it, having the URL of programming.com/cooking will be confusing to users.
Is your issue the cost of hosting multiple sites or simply of buying another domain name?
Domain names are cheaper than hosting accounts- parking a new domain name at the one single hosting account will solve your issue by effectively giving you an "unlimited" number of sites on one account.
would then be displaying the exact same page which is at the location...