Item it = new Item();
it.setDescription(\
//desc = Gobstopper Item it2 = new Item();
it2.setDescription(\
//desc = Fizzylifting
modifyDesc(it, \//desc = Scrumdiddlyumptious
System.out.println(it.getDescription()); System.out.println(it2.getDescription());
//set方法是为对象中的属性赋值;get方法是从对象中获取属性值
A.Scrumdiddlyumptious Scrumdiddlyumptious B.Scrumdiddlyumptious Fizzylifltng C.Gobstopper Scrumdiddlyumptious D.Gobstopper Fizzylifting 正确答案:D
item=new Item();之后更改的变量内存地址不会对外部那个item的地址进行修改,在modifyDesc()自然就不会更改外部那个变量的值。
传递对象一般是引用,即地址,如果在里面重新new了后的变量地址是相当另外一个变量不会影响外面那个变量地址,这样那个内存地址的值就不会被改变了,外面变量地址没改变,指向的仍是开始所指向的对象
39.
(单选)下面的代码用于对数组arr实现冒泡排序: for (int i = 0; i < arr.length - 1; i++) { boolean isSwap = false;
空白处
if (!isSwap) break; }
下列选项中,空白处可以填入的代码是:()。
A.for (int j = arr.length - 1; j > i; j--) { if (arr[j] < arr[j - 1]) { int temp = arr[j]; arr[j] = arr[j - 1]; arr[j - 1] = temp; isSwap = true; } }
B.for (int j = arr.length - 1; j > 0; j--) { if (arr[j] < arr[j - 1]) { int temp = arr[j]; arr[j] = arr[j - 1]; arr[j - 1] = temp; isSwap = true; } }
C.for (int j = i + 1; j< arr.length; j++) { if (arr[j] < arr[j - 1]) { int temp = arr[j]; arr[j] = arr[j - 1]; arr[j - 1] = temp; isSwap = true; } }
D.for (int j = i; j< arr.length; j++) { if (arr[j] < arr[j - 1]) { int temp = arr[j]; arr[j] = arr[j - 1]; arr[j - 1] = temp; isSwap = true; } } 正确答案:A
太恐怖了,这代码,自己放eclipse运行一下吧 40.(单选)请看下列代码: class Payload { private int weight;
public Payload(int wt) { weight = wt; }
public Payload() {}
public void setWeight(int w) { weight = w; }
public String toString() {
return Integer.toString(weight); } } public class TestPayload {
static void changePayload(Payload p) { <插入代码> }
public static void main(String[] args) { Payload p = new Payload();
p.setWeight(1024);weight = 1024 changePayload(p);
把引用的地址传进去,然后再通过调用set的方法改变这个地址指向的对象的内容,把weight值由1024改为420
System.out.println(\//这里直接输出p指向的对象,会自动调用toString的方法 假设运行后输出“The value of p is 420”,那么<插入代码>处应填入代码是:
A.p.setWeight(420); B.Payload.setWeight(420); C.p = new Payload(420);
D.p = new Payload(); p.setWeight(420); 正确答案:A 41.
(单选)下列代码的输出结果是()。
abstract class Vehicle {
public int speed() { return 0; } }
class Car extends Vehicle {
public int speed() { return 60; } }
class RaceCar extends Car {
public int speed() { return 150; } }
public class TestCar {
public static void main(String[] args) { RaceCar racer = new RaceCar();
Car car = new RaceCar();
Vehicle vehicle = new RaceCar();
System.out.println(racer.speed() + \vehicle.speed()); } } A.0, 0, 0 B.150, 60, 0 C.150, 150, 150 D.抛出运行时异常 正确答案:C
当子类重写了父类的方法后,该重写方法被调用时(无论是通过子类的引用调用还是通过父类的引用调用),运行的都是子类重写后的版本
42.
(单选)题目: 下列代码的输出结果是: ()。 public class A {
public void info(){ System.out.println(\ public class B extends A{
public void info(){ System.out.println(\public static void main(String[] args) { B b=new B(); A a=b; a. info(); } }
A.B info A info
B.A info B info C.A info D.B info 正确答案:D
当子类重写了父类的方法后,该重写方法被调用时(无论是通过子类的引用调用还是通过父类的引用调用),运行的都是子类重写后的版本
43.
(单选)请看下列代码:
class ClassA {}
class ClassB extends ClassA {} class ClassC extends ClassA {} public class Test{
public static void main(String[] args) { ClassA p0 = new ClassA(); ClassB p1 = new ClassB(); ClassC p2 = new ClassC(); ClassA p3 = new ClassB(); ClassA p4 = new ClassC(); <插入代码> } }
可以在<插入代码>处,填入的代码正确的是() A.p0 = p1;A类和B类有继承关系 B.p1 = p2;B类和C类没有任何关系 C.p2 = p4;小类型向大类型转,要强转
D.p2 = (ClassC)p1; B类和C类没有任何关系,不能强制转换类型 正确答案:A
44.(单选)下列代码的运行结果是()。
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库1609第一次月考详细解析(6)在线全文阅读。
相关推荐: