java模拟题4 3.docx
《java模拟题4 3.docx》由会员分享,可在线阅读,更多相关《java模拟题4 3.docx(17页珍藏版)》请在冰豆网上搜索。
java模拟题43
四、程序阅读题
1、阅读以下程序,写出输出结果。
publicclassAbc{
publicstaticvoidmain(Stringargs[]){
Abs=newAb("Hello!
","IloveJAVA.");
System.out.println(s);
}
}
classAb{
Strings1;
Strings2;
Ab(Stringstr1,Stringstr2){
s1=str1;
s2=str2;
}
publicStringtoString(){
returns1+s2+"You?
";
}
}
2、阅读以下程序,写出输出结果。
publicclassCompare{
publicstaticvoidmain(String[]args){
Stringstr1="abc";
Stringstr2=newString("abc");
Stringstr3="ab"+"c";
Stringstr4=newString(str2);
Stringstr5=str1;
System.out.println(str1==str2);
System.out.println(str2==str3);
System.out.println(str2==str4);
System.out.println(str5==str1);
}
}
3、阅读以下程序,写出输出结果。
publicclassGroupTwo{
privateintcount;
publicclassStudent{
Stringname;
publicStudent(Stringn1){
name=n1;
count++;
}
publicvoidOutput(){
System.out.println(this.name);
}
}
publicvoidoutput(){
Students1=newStudent("Johnson");
s1.Output();
System.out.println("count="+this.count);
}
publicstaticvoidmain(Stringargs[]){
GroupTwog2=newGroupTwo();
g2.output();
}
}
4、阅读以下程序,写出输出结果。
classsuperClass{
inty;
superClass(){
y=30;
System.out.println("insuperClass:
y="+y);
}
voiddoPrint(){
System.out.println("InsuperClass.doPrint()");
}
}
classsubClassextendssuperClass{
inty;
subClass(){
super();
y=50;
System.out.println("insubClass:
y="+y);
}
voiddoPrint(){
super.doPrint();
System.out.println("insubClass.doPrint()");
System.out.println("super.y="+super.y+"sub.y="+y);
}
}
publicclassinviteSuper{
publicstaticvoidmain(Stringargs[]){
subClasssubSC=newsubClass();
subSC.doPrint();
}
}
5、阅读以下程序,写出输出结果。
publicclassGroupThree{
privatestaticintcount;
privateStringname;
publicclassStudent{
privateintcount;
privateStringname;
publicvoidOutput(intcount){
count++;
this.count++;
GroupThree.count++;
GroupThree.this.count++;
System.out.println(count+""+this.count+""+GroupThree.count+""+GroupThree.this.count++);
}
}
publicStudentaStu()
{
returnnewStudent();
}
publicstaticvoidmain(Stringargs[]){
GroupThreeg3=newGroupThree();
g3.count=10;
GroupThree.Students1=g3.aStu();
GroupThree.Students1.Output(5);
}
}
6、阅读以下程序,写出输出结果。
classMammal{
privateintn=40;
voidcrySpeak(Strings){
System.out.println(s);
}
}
publicclassMonkeyextendsMammal{
voidcomputer(intaa,intbb){
intcc=aa*bb;
System.out.println(cc);
}
voidcrySpeak(Strings){
System.out.println("**"+s+"**");
}
publicstaticvoidmain(Stringargs[]){
Mammalmammal=newMonkey();
mammal.crySpeak("Ilovethisgame");
Monkeymonkey=(Monkey)mammal;
puter(10,10);
}
}
7、阅读以下程序,写出输出结果。
publicclassFlower{
intpetalCount=0;
Strings="initialvalue";
Flower(intpetals){
petalCount=petals;
print("Constructorw/intargonly,petalCount="+petalCount);
}
Flower(Stringss){
print("Constructorw/Stringargonly,s="+ss);
s=ss;
}
Flower(Strings,intpetals){
this(petals);
this.s=s;//Anotheruseof"this"
print("String&intargs");
}
Flower(){
this("hi",47);
print("defaultconstructor(noargs)");
}
voidprintPetalCount(){
print("petalCount="+petalCount+"s="+s);
}
voidprint(Strings){
System.out.println(s);
}
publicstaticvoidmain(String[]args){
Flowerx=newFlower();
x.printPetalCount();
}
}
8、阅读以下程序,写出输出结果。
classCup{
Cup(intmarker){
System.out.println("Cup("+marker+")");
}
voidf(intmarker){
System.out.println("f("+marker+")");
}
}
classCups{
staticCupcup1;
staticCupcup2;
static{
cup1=newCup
(1);
cup2=newCup
(2);
}
Cups(){
System.out.println("Cups()");
}
}
publicclassExplicitStatic{
publicstaticvoidmain(String[]args){
System.out.println("Insidemain()");
Cups.cup1.f(99);
}
}
9、阅读以下程序,写出输出结果。
classTree{
intheight;
Tree(){
System.out.println("Plantingaseedling");
height=0;
}
Tree(intinitialHeight){
height=initialHeight;
System.out.println("CreatingnewTreethatis"+
height+"feettall");
}
voidinfo(){
System.out.println("Treeis"+height+"feettall");
}
voidinfo(Strings){
System.out.println(s+":
Treeis"+height+"feettall");
}
}
publicclassOverloading{
publicstaticvoidmain(String[]args){
for(inti=0;i<5;i++){
Treet=newTree(i);
t.info();
t.info("overloadedmethod");
}
//Overloadedconstructor:
newTree();
}
}
10、阅读以下程序,写出输出结果。
classA{
doublef(doublex,doubley){
returnx*y;
}
}
classBextendsA{
doublef(doublex,doubley){
returnx+y;
}
}
publicclassTest{
publicstaticvoidmain(Stringargs[]){
Bobj=newB();
System.out.println("Theprogramoutputis"+obj.f(6,7));
}
}
11、阅读以下程序,写出输出结果。
classLargeCup{
LargeCup(intmarker){
System.out.println("LargeCup("+marker+")");
}
voidf1(intmarker){
System.out.println("f1("+marker+")");
}
}
classShelf{
staticLargeCupcup1=newLargeCup
(1);
Shelf(){
System.out.println("Shelf()");
cup2.f1
(1);
}
voidf2(intmarker){
System.out.println("f2("+marker+")");
}
staticLargeCupcup2=newLargeCup
(2);
}
classCupshelf{
staticLargeCupcup3=newLargeCup(3);
staticLargeCupcup4=newLargeCup(4);
Cupshelf(){
System.out.println("Cupshelf()");
cup4.f1
(2);
}
voidf3(intmarker){
System.out.println("f3("+marker+")");
}
staticLargeCupcup5=newLargeCup(5);
}
publicclassInitialization{
staticShelfshelf=newShelf();
staticCupshelfcupshelf=newCupshelf();
publicstaticvoidmain(String[]args){
System.out.println("CreatingnewCupshelf1()inmain");
newCupshelf();
System.out.println("CreatingnewCupshelf2()inmain");
newCupshelf();
shelf.f2
(1);
cupshelf.f3
(1);
newInitialization();
}
}
12、阅读以下程序,写出输出结果。
classPlate{
Plate(intmarker){
System.out.println("Plate("+marker+")");
}
voidf1(intmarker){
System.out.println("f1("+marker+")");
}
}
classDesk{
staticPlateplate1=newPlate
(1);
Desk(){
System.out.println("Desk()");
plate2.f1
(1);
}
voidf2(intmarker){
System.out.println("f2("+marker+")");
}
staticPlateplate2=newPlate
(2);
}
classBoard{
Plateplate3=newPlate(3);
staticPlateplate4=newPlate(4);
Board(){
System.out.println("Board()");
plate4.f1
(2);
}
voidf3(intmarker){
System.out.println("f3("+marker+")");
}
staticPlateplate5=newPlate(5);
}
publicclassInitialization{
staticDeskdesk=newDesk();
BoardBoard=newBoard();
publicstaticvoidmain(String[]args){
System.out.println("CreatingnewBoard()inmain");
newBoard();
System.out.println("Creating2newBoard()inmain");
newBoard();
desk.f2
(1);
Board.f3
(1);
}
}
13、阅读以下程序,写出输出结果。
classSwap{
voidsw1(Personx,Persony){
inta=y.id;
y.id=x.id;
x.id=a;
}
voidsw2(Personx,Persony){
Persone;
e=x;
y=x;
x=e;
}
}
publicclassPerson{
intid=0;
Person(intid){
this.id=id;
}
publicstaticvoidmain(String[]args){
Swapcid=newSwap();
Personp1=newPerson(10);
Personp2=newPerson(11);
Personp3=newPerson(12);
Personp4=newPerson(13);
cid.sw1(p1,p2);
cid.sw2(p3,p4);
System.out.println("p1:
"+p1.id+"p2:
"+p2.id);
System.out.println("p3:
"+p3.id+"p4:
"+p4.id);
}
}
14、阅读以下程序,写出输出结果。
publicclassFoo{
publicstaticvoidmain(String[]args){
StringBuffera=newStringBuffer(“ThisisinSCUT”);
StringBufferb=newStringBuffer(“Twice”);
operate(a,b);
System.out.printIn(a+“,”+b);
}
staticvoidoperate(StringBufferx,StringBuffery){
x.append(y);
y=x;
}
}
15、阅读以下程序,写出输出结果。
importstaticnet.mindview.util.Print.*;
classCandy{
static{print("LoadingCandy");}
}
classGum{
static{print("LoadingGum");}
}
classCookie{
static{print("LoadingCookie");}
}
publicclassSweetShop{
publicstaticvoidmain(String[]args){
print("insidemain");
newCandy();
print("AftercreatingCandy");
try{
Class.forName("Gum");
}catch(ClassNotFoundExceptione){
print("Couldn’tfindGum");
}
print("AfterClass.forName(\"Gum\")");
newCookie();
print("AftercreatingCookie");
}
}
16、阅读以下程序,写出输出结果。
publicclassClass3{
publicstaticvoidmain(Stringargs[]){
SubSubClassx=newSubSubClass(10,20,30);
x.show();
}
}
classSuperClass{
inta,b;
SuperClass(intaa,intbb){
a=aa;
b=bb;
}
voidshow(){
System.out.println("a="+a+"\nb="+b);
}
}
classSubClassextendsSuperClass{
intc;
SubClass(intaa,intbb,intcc){
super(aa,bb);
c=cc;
}
}
classSubSubClassextendsSubClass{
inta;
SubSubClass(intaa,intbb,intcc){
super(aa,bb,cc);
a=aa+bb+cc;
}
voidshow(){
System.out.println("a="+a+"\nb="+b+"\nc="+c);
}
}
17、阅读以下程序,写出输出结果。
classSmallPlate{
SmallPlate(inti){
System.out.println("Plateconstructor");
}
}
classDinnerPlateextendsSmallPlate{
DinnerPlate(inti){
super(i);
System.out.println("DinnerPlateconstructor");
}
}
classUtensil{
Utensil(inti){
System.out.println("Utensilconstructor");
}
}
classSpoonextendsUtensil{
Spoon(inti){
super(i);
System.out.println("Spoonconstructor");
}
}
classForkextendsUtensil{
Fork(inti){
super(i);
System.out.println("Forkconstructor");
}
}
classKnifeextendsUtensil{
Knife(inti){
super(i);
System.out.pr