If i take 4 years of a foreign language in highschool and i dont take the ap exam or i end up failing the exam, will I need to take it in college (among the UC colleges)
You just need to take (and pass) 2 years of a foreign language in high school.
Taking 10 seconds to google this would have said as much.
If you mean what you need to graduate from college (which can be higher than the minimum admission requirement of 2nd year high school), it depends on the campus, division within the campus, and sometimes specific major.
For APs in general pay attention to the requirements of any grad school you might be interested in attending. If a course is a pre-req for graduate school, they might not accept AP credits in lieu of the college course.