1. 阅读下列程序片段,写出运行结果。

public class Test {

 public static void main(String args[]) {

  double d = 12.3;

  decrement(d);

  System.out.println(d);

 }

 public static void decrement(double decMe) {

  decMe = decMe - 1.0;

 }

}

代码执行后输出的结果是______

答案:12.3

[解析] 本题参数传递采用的是传值引用,函数中不会改变实参d的值,因此d的值不变。

2. 阅读下列代码,写出运行结果。

public class Arrays {

 public static void main(String[] args) {

  int[] a = new int[5];

  for (int i = 0; i < a.length; i = i + 1) {

   a[i] = 10 + i;

  }

  for (int i = 0; i < a.length; i++) {

   System.out.print(a[i] + "  ");

  }

  String[] s = { "Frank", "Bob", "Jim" };

  for (int i = 0; i < s.length; i = i + 1) {

   System.out.print(s[i] + "  ");

  }

  s[2] = "Mike";

  System.out.print(s[2]);

 }

}

代码执行后输出的结果是______

答案:10  11  12  13  14  Frank  Bob  Jim  Mike

[解析] for循环执行时,首先执行初始化操作,其次判断终止条件是否满足,如果满足,则执行循环体中的语句,最后执行迭代部分的。完成一次循环后重新判断终止条件。在本题的程序段中第一个for循环功能是定义一个一维数组a,第二个for循环的功能是输出一维数组a的各个元素,第三个for循环功能是输出~维数组s的各个元素。

7. 阅读下列程序片段,写出运行结果。

public class Test {

 public static void main(String[] args) {

  int x = 3, y = 4, z = 5;

  if (x > 3) {

   if (y < 2)

    System.out.println("show one");

   else

    System.out.println("show two");

  } else {

   if (z > 4)

    System.out.println("show three");

   else

    System.out.println("show four");

  }

 }

}

代码执行后输出的结果是______

答案:show  three

[解析] 该程序主要是考查if语句的用法。由于x=3,所以第一个if里面的表达式的值为false,进入下面对应的else的运算中。又由于z=5,使得里面的z4的值为true,所以程序运行的最后结果为show three

8. 阅读下列程序片段,写出运行结果。

public class Test {

 public static void main(String[] args) {

  int[] m = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };

  int sum = 0;

  for (int i = 0; i < 8; i++) {

   sum += m[i];

   if (i == 3)

    break;

  }

  System.out.println(sum);

 }

}

代码执行后输出的结果是______

答案:10

[解析] 该程序的功能是将m数组中的前四个数相加,将和保存在sum中,最后输出sum的值。因为程序中for循环停止的条件有两个,一个是当i8的时候,另一个是当i==3的时候,即运行到m数组中第四个值的时候,可以跳出for循环。程序执行的是后

者,所以输出的结果是10

9. 阅读下列程序片段,写出运行结果。

public class Test {

 public static void main(String[] args) {

  int percent = 10;

  tripleValue(percent);

  System.out.println(percent);

 }

 public static void tripleValue(int x) {

  x = 3 * x;

 }

}

代码执行后输出的结果是______

答案:10

[解析] static 关键字应用的场合有:①用来修饰类中定义的变量,这样的变量称为类变量或静态变量。②可以用来修饰类中定义的方法,这样的方法称为静态方法。③用来修饰初始化语句块,这样的语句块常称为静态初始化语句块。static 在这里表示这个方法为类方法,不属于任何对象实例,而是类所有,描述对象的共有动作,可以用类名直接调用。在调用了tripleValue函数之后,函数的值没有返回,所以percent值还是10

10. 阅读下列程序片段,写出运行结果。

class Shape {

 public Shape() {

  System.out.print("Shape");

 }

}

class Circle extends Shape {

 public Circle() {

  System.out.print("Circle");

 }

}

public class Test {

 public static void main(String[] args) {

  Shape d = new Circle();

 }

}

代码执行后输出的结果是______

答案:ShapeCircle

[解析] 继承是而向对象编程的一个主要优点之一,它对如何设计Java类有着直接的影响。继承有如下几点好处:

①它可以利用已有的类来创建自己的类,只需要指出自己的类和已有的其他类有什么不同即可,而且还可以动态访问其他有

关类中的信息。

②通过继承,可以利用Java类库所提供的丰富而有用的类,这些类都已经被很好地实现。

③当设计很大的程序时,继承可以使程序组织得层次清晰,有利于程序设计相减少错误的发生。该程序首先编写了一个Shape的类,然后又编写一个类Circle去继承Shape类。由于子类拥有父类所有的属性和方法,所以输出的是ShappeCircle

 

11. 阅读下列程序片段,写出运行结果。

import javax.swing.*;

import java.awt.*;

public class Test {

 public static void main(String[] args) {

  JFrame f = new JFrame();

  JPanel p = new JPanel();

  f.setLayout(new BorderLayout());

  f.getContentPane().add(p, "Center");

  p.setBackground(Color.blue);

  f.setVisible(true);

  f.setSize(200, 200);

 }

}

代码执行后输出的结果是______

答案:程序可以运行,显示一个窗口,窗口大小是200×200 ,背景为蓝色。

[解析] 该程序没有语法和逻辑上面的错误,所以应该是可以运行的。由JFrame f=new JFrame()可以看出,该程序创建了一个窗口,p.setBackground(Color.blue)告诉我们,窗口的背景颜色是蓝色的,而f.setSize(200,200)则设黄了窗口的大小,最后窗口是可见的。

12. 阅读下列程序片段,写出运行结果。

public class ArrayTest {

 public static void main(String[] args) {

  int data[][] = { { 1, 2, 3, 4, 5 }, { 11, 22, 33, 44, 55 },

    { 111, 222, 333, 444, 555 } };

  for (int i = 0; i < data.length; i++) {

   if (i % 2 == 0) {

    System.out.print(data[i][4] + "");

   }

  }

 }

}

代码执行后输出的结果是______

答案:5  555

[解析] 该程序的功能是遇到行下标数为偶数的时候,输出这一行的列下标为4的数,即这一行的第五个数。而程序定义的二维数组一共有三行五列。第0行的时候,可以被2整除,所以输出第5个数“5”,第2行的时候,也可以被2整除,所以输出这一行的第5个数“555”。

 

13. 阅读下列程序片段,写出运行结果。

public class Test {

 public static void main(String args[]) {

  System.out.println(89 >> 1);

 }

}

代码执行后输出的结果是______

答案:44

[解析] 算数右移一位相当于除2取商。89除以2,商是44。所以89>>1的结果是44。因此,本题的正确答案是A

14. 下面是PeopleChild类的定义和构造方法,每个构造方法都输出编号。在执行new Child("mike")的时候都有哪些构造方法被顺序调用?写出运行结果。

class People {

 String name;

 public People() {

  System.out.print(1);

 }

 public People(String name) {

  System.out.print(2);

  this.name = name;

 }

}

class Child extends People {

 People father;

 

 public Child(String name) {

  System.out.print(3);

  this.name = name;

  father = new People(name + ":F");

 }

 public Child() {

  System.out.print(4);

 }

}

public class Demo {

 public static void main(String[] args) {

  Child c = new Child("5");

 }

}

代码执行后输出的结果是______

答案:132

[解析]

15. 下面程序中类Class Demo中定义了一个静态变量sum,阅读下列程序片段,写出运行结果。

class Class Demo {

 public static int sum = 1;

 

 public Class Demo() {

  sum = sum + 5;

 }

}

public class Class DemoTest {

 public static void main(String args[]) {

  Class Demo demo1 = new Class Demo();

  Class Demo demo2 = new Class Demo();

  System.out.println(demo1.sum);

 }

}

代码执行后输出的结果是______

答案:11

[解析]

16. 阅读下列程序片段,写出运行结果。

public class Example {

 String str = new String("good");

 char[] ch = { 'a', 'b', 'c' };

 public static void main(String args[]) {

  Example ex = new Example();

  ex.change(ex.str, ex.ch);

  System.out.print(ex.str + " and ");

  System.out.print(ex.ch);

 }

 public void change(String str, char ch[]) {

  str = "test ok";

  ch[0] = 'g';

 }

}

代码执行后输出的结果是______

答案:good and gbc

[解析]

17. 阅读下列程序片段,写出运行结果。

public class Short {

 public static void main(String[] args) {

  StringBuffer s = new StringBuffer("Boy");

  if ((s.length() < 3) && (s.append("男孩").equals("Flase")));

  System.out.println("结果为:" + s);

 }

}

代码执行后输出的结果是______

答案:结果为:Boy

[考点范围] 常用的系统类

18.阅读下列程序片段,写出运行结果。

public class EqualsMethod {

 public static void main(String[] args) {

  Integer n1 = new  Integer(47);

  Integer n2 = new  Integer(47);

  System.out.println(n1.equals(n2));

 }

}

代码执行后输出的结果是______

答案:ture

[考点范围] 常用的系统类

19.阅读下列程序片段,写出运行结果。

import java.io.*;

public class StringTest1 {

 public static void main(String[] args) {

  String s1 = "hello";

  String s2 = new String("hello");

  if(s1==s2){

   System.out.println("s1=s2");

  }else{

   System.out.println("s1!=s2");}

 }

}

代码执行后输出的结果是______

答案:s1!=s2

[考点范围] 常用的系统类

20.阅读下列程序片段,写出运行结果。

public class TestString {

 public static void main(String[] args) {

  StringCs = new StringC("hello","world!");

  System.out.println(s);

 }

}!

class StringC{

 String s1;

 String s2;

 StringC(String str1,String str2){

  s1 = str1;

  s2 = str2;

 }

 public String toString(){

  returns1+s2;

 }

}

代码执行后输出的结果是______

答案:helloworld

[考点范围] 常用的系统类

21.阅读下列程序片段,写出运行结果。

importjava.util.*;

public class Vec {

 public static void main(String[] args) {

  String[] s;

  s = new String[2];

  s[0] = new String("no1");

  s[1] = new String("no2");

  Vector v = new Vector();

  for(inti= 0;i<2;i++)

   v.addElement(s[i]);

  v.insertElementAt(new String("no3"),2);

  Enumeration e = v.elements();

  while(e.hasMoreElements())

   System.out.println(e.nextElement()+" ");

  System.out.println();

  v.removeElement("no2");

  for(inti = 0;i<v.size();i++)

   System.out.println(v.elementAt(i)+" ");

  System.out.println();

 }

}

代码执行后输出的结果是______

答案:no1 no2 no3    no1 no3

[考点范围] 常用的系统类

22.阅读下列程序片段,写出运行结果。

public class EqualsMethod {

 public static void main(String[] args) {

  Integer n1 = new Integer(47);

  Integer n2 = new Integer(47);

  System.out.println(n1.equals(n2));

 }

}

代码执行后输出的结果是______

答案:true

[考点范围] 常用的系统类

 

23.阅读下列程序片段,写出运行结果。

File file=new File(abc.txt);

int n=0;

Byte b[]=new byte [255];

n=fis.read(b);

System.out.println(n);

Sysout.out.println(file.length());

Sysout.out.println(fis.available());

 

如果System.out.printf(file,length())的输出是24,则System.out.println(n)的输出是_____;

System.out.println(fis.available())的输出是________

 

答案:240

[考点范围] JAVA输入输出系统

24.阅读下列程序片段,写出运行结果。

RandomAccessfile randfile=new RandomAccessFile(./adc.dat,rw)

Sysout.out.println(文件长度:+randfile.length());

Sysout.out.println(文件指针:+randfile.getFilePointer());

Randfile.writeDouble(2.1);

Sysout.out.println(文件指针:+randfile.getfilePointer());

如果程序段第二行输出0,则第三行输出_______;当执行完第四行后,文件长度是________,5行输出__________

答案:008

[考点范围] JAVA输入输出系统

25.阅读下列程序片段,写出运行结果。

class D {

 public static void main(String args[]) {

  int d = 21;

  Dec dec = new Dec();

  dec.decrement(d);

  System.out.println(d);

 }

}

class Dec {

 public void decrement(int decMe) {

  decMe = decMe - 1;

 }

}

代码执行后输出的结果是______

答案:21

[考点范围] 类和对象

11. 阅读下列程序片段,写出运行结果。

class Q6 {

 public static void main(String args[]) {

  Holder h = new Holder();

  h.held = 100;

  h.bump(h);

  System.out.println(h.held);

 }

}

class Holder {

 public int held;

 public void bump(Holder theHolder) {

  theHolder.held--;

 }

} 

代码执行后输出的结果是______

答案:99

[考点范围] 类和对象