问题 问答题

下列程序的功能是为变量赋值,程序运行后,输出i=51。请改动main方法中的错误,使程序能够正确编译、运行并输出正确的结果。 注意:不改动程序结构。 class A { private int a; public void setA (int x) { a=x; } public int getA() { return a; } } public class MethodTest { public static void main(String args[]) { A a=A(); a.getA(51); int i=a.getA(); System.out.println ("i="+i); } }

答案

参考答案:

解析:改正后的main方法如下: public static void main(String args[]) { A a=new A(); a.getA(51); int i=a.getA(); System.out.println("i="+i); }[讲解] 本题综合考查类及类成员的定义与使用方面的知识。该程序中定义了两个类:A和MethodTest,类A中封装了一个私有的成员变量a和两个公有的方法setA和getA。在类MethodTest中包含了main方法。创建对象应使用new操作符来实例化对象,程序在创建对象a时未使用new,故存在错误。由于a是对象a的私有变量,在main方法中不能直接访问,只能通过对象a的公有方法setA和getA来访问。公有方法setA的功能是将传递回来的参数值赋给a,所以应当调用setA方法来为变量a赋值。

阅读理解

请阅读下列寓言小故事和故事寓意,从所给的六个选项( A、B、C、D、E和F)中,选出附和各小题要求的最佳选项

下面是几则寓言小故事:

46. An ant went to the bank of a river to quench its thirst, and being carried away by the rush of the stream, was on the point of drowning.  A dove sitting on a tree overhanging the water plucked a leaf and let it fall into the stream close to her.  The ant climbed onto it and floated in safety to the bank.  Shortly afterwards a birdcatcher came and stood under the tree, aiming at the dove.  The ant, perceiving his design, stung him in the foot.  In pain the birdcatcher shouted, and noise made the dove take wing.

47. Two men were travelling together, when a bear suddenly met them on their path.  One of them climbed up quickly into a tree and hid himself in the branches.  The other, seeing that he must be attacked, fell flat on the ground, and when the bear came up and felt him with his snout, and smelt him all over, he held his breath, and pretended to be dead as much as he could.  The bear soon left him, for he will not touch a dead body.  When he disappeared, the other traveler descended from the tree, and asked his friend what it was the bear had whispered in his ear.  “He gave me this advice,” his companion replied.  “Never travel with a friend who deserts you at the approach of danger.”

48. A prince had some monkeys trained to dance.  Being naturally good at learning, they showed themselves excellent pupils, and in their rich clothes and masks, they danced as well as any of the courtiers.  Their performance was often repeated with great applause, till on one occasion a courtier, bent on mischief, took from his pocket a handful of nuts and threw them upon the stage.  The monkeys at the sight of the nuts forgot their dancing and became (as indeed they were) monkeys instead of actors.  Pulling off their masks and tearing their robes, they fought with one another for the nuts.  The dancing spectacle thus came to an end in the laughter and ridicule of the audience.

49. A cock was once strutting up and down the farmyard among the hens when suddenly he noticed something shining in the straw.  “Ho! Ho!” said he, “that’s for me,” and soon rooted it out from beneath the straw.  It turned out to be a pearl that by some chance had been lost in the yard.  “You may be a treasure,” signed the cock, “to man, but for me I would rather have a single barley-corn.”

50. An old man on the point of death summoned his sons around hi to give them some parting advice.  He ordered his servants to bring in a bunch of sticks, and said to his eldest son: “Break it.”  The son tried hard, but with all his efforts was unable to break the bundle.  The other sons also tried, but none of them was successful.  “Untie it,” said the father, “and each of you take a stick.”  When they had done so, he called out to them: “Now, break,” and each stick was easily broken.

请阅读下面的故事寓意,然后匹配与之对应的小故事。

A. Not everything you see is what it appears to be.

B. One man’s pleasure may be another’s pain. / One man’s meat is another’s poison.

C. Misfortune tests the sincerity of friends.

D. One good turn deserves another.

E. Union gives strength.

F. Precious things are for those that can prize them.

不定项选择