Before 2005, the answer to both questions is ‘yes’. But the government passed the Bankruptcy Abuse Prevention and Consumer Protection Act (BAPCPA) in 2005 that changed things dramatically. It is not that you cannot file for bankruptcy when being evicted (you still have the right to do so), but the process has now become more expensive.
Most likely the landlord has won a judgment for possession against you. In view of that, you must file a “certification” which states that the judgment allows you to stay in the premise if the judgment amount is paid in full and that you have deposited with the bankruptcy clerk “any rent which would become due during the thirty (30) day period after the filing of the bankruptcy petition”. This means that when you file for bankruptcy under such circumstances, you will have to deposit one month’s rental to the bankruptcy court immediately. This “certification” must be made on oath under penalty of perjury.
Obtaining a judgment for possession does not mean the landlord has to allow you to stay in the premises. The landlord still has the right to terminate the lease or tenancy agreement after giving you notice according to the agreement even if you have paid the judgment amount. Furthermore, most tenants land up in such a situation because they have been delinquent in paying their rent, therefore they would most likely not have the means to pay the judgment amount or deposit anything with the bankruptcy court. If you are in such a situation, you will probably have to vacate your premise once the landlord obtains a judgment for possession.