问题 单项选择题

有以下程序:main(){ intk=5,n=0;while(k>0){switch(k){ default : break; case 1 : n+=k; case 2 : case 3 : n+=k; }k--;}printf("%d\n",n);}程序运行后的输出结果是______。

A.0

B.4

C.6

D.7

答案

参考答案:D

解析:在 switch 语句中,表达式的值与某一个 case 后面的常量表达式的值相等时,就执行此 case 后面的语 句,若所有的 case 中的常量表达式的值都没有与表达式的值匹配的,就执行 default 后面的语句,各个 case和 default 的出现次序不影响执行结果。所以在本题中,当 k=5 和 k=4 的时候,case 都没有与其匹配的值, 所以执行了 default 语句;当 k=3 时,执行“case 3 : n+=k;”得 n=3,然后执行 default;当 k=2 时,执行“case2 : case 3 : n+=k;”得 n=5,然后执行 default;当 k=1 时,执行“case 1 : n+=k; case 2 : case 3 : n+=k;” 使得 n 加两次 k,得到 n=7。

单项选择题

One of the good things for men in women’’s liberation is that men no longer have to pay women the old-fashioned courtesies.In an article on the new manners, Ms. Holmes says that a perfectly able woman no longer has to act helplessly in public as if she were a model. For example, she doesn’’t need getting in and out of cars. "Women get in and out cars twenty times a day with babies and dogs. Surely they can get out by themselves at night just as easily."She also says there is no reason why a man should walk on the outside of a woman on the sidewalk. "Historically, the man walked on the inside so he caught the garbage thrown out of a window. Today a man is supposed to walk where he wants so. So should a woman. If, out of love and respect, he actually wants to take the blows, he should walk on the inside — because that’’s where attackers are all hiding these days."As far as manners are concerned, I suppose I have always been a supporter of women’’s liberation. Over the years, out of a sense of respect, I imagine, I have refused to trouble women with outdated courtesies.It is usually easier to follow rules of social behavior than to depend on one’’s own taste. But rules may be safely broken, of course, by those of us with the gift of natural grace. For example, when a man and woman are led to their table in a restaurant and the waiter pulls out a chair, the woman is expected to sit in the chair. That is according to Ms. Ann Clark. I have always done it the other way, according to my wife.It came up only the other night, I followed the hostess to the table, and when she pulled the chair out I sat on it, quite naturally, since it happened to be the chair I wanted to sit in."Well," my wife said, when the hostess had gone, "you did it again.""Did what " I asked, utterly confused."Took the chair."Actually, since I’’d walked through the restaurant ahead of my wife, it would have been awkward, I should think, not to have taken the chair. I had got there first, after all. Also, it has always been my custom to get in a car first and let the woman get in by herself. This is a courtesy I insist on as the per sex, out love and respect. In times like these, there might be attackers hidden about. It would be unsuitable to put a woman in a car and then shut the door on her, leaving her at the mercy of some bad fellow who might be hiding in the back seat.

The author always gets in a car before a woman because he__________.

A.wants to protect her

B.doesn’’t need to help her

C.chooses to be impolite to her

D.fears attacks on him

单项选择题