Java经典笔试题

1.请看下列代码:
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;
B.p1 =p2;
C.p2 = p4;
D.p2 = (ClassC)p1;
正确答案:A


2.运行下面程序:
public class Foo {
       public static void main(String[] args) {
              StringBuffer a=new StringBuffer("A");
              StringBuffer b=new StringBuffer("B");
              operator(a,b);
              System.out.println(a+","+b);
       }
       public static void operator(StringBuffer x,StringBuffer y){
              x.append(y);
              y=x;
       }
}
输出的结果是:()。
A.A,B
B.A,A
C.B,B
D.AB,B
正确答案:D


3.下列代码的输出结果是: ()。
public class A {
 public void info(){
  System.out.println("A info");
 }
}
public class B extends A{
 public void info(){
  System.out.println("B info");
 }
 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


4.下列代码运行的结果是()。
public class Base {
 public static final String FOO = "foo";
 public static void main(String[] args) {
  Base b = new Base();
  Sub s = new Sub();
  System.out.print(Base.FOO);
  System.out.print(Sub.FOO);
  System.out.print(b.FOO);
  System.out.print(s.FOO);
  System.out.print(((Base) s).FOO);
 }
}
class Sub extends Base {
 public static final String FOO = "bar";
}
A.foofoofoofoofoo
B.foobarfoobarbar
C.foobarfoofoofoo
D.foobarfoobarfoo
正确答案:D


5.执行下列语句:
int a = 0x9af700; //     00 00   00 10     01 10    10 1111 0111 0000 0000 00
a <<= 2;
变量a的值为:()。
A. 0x26bdc00
B. 0xc6bdc00
C. 0x3fa0000
D. 0x7e02ffff
正确答案:A


6. 下面的代码用于对数组arr实现冒泡排序:【1,2,3,4,5】
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


7.下列语句创建对象的总个数是:()。
String s=”a”+”b”+”c”+”d”+”e”;
A.1
B.2
C.3
D.4
正确答案:A


8.运行下列程序:
String str = "**oracle***oracle*****oracle***";
String str1 = "oracle";
int index = 0;
while ((index = str.indexOf(str1, index)) != -1) {
       System.out.print(index+””);
       index += str1.length();
}
控制台输出的结果是:()。
A.1 10 21
B.2 11 22
C.3 13 23
D.5 13 22
正确答案:B


9. 下列表达式中,可以得到精确结果的是()。
A. double d1 = 3.0 - 2.6;    0.4
B. double d4 = 2.5 * 1.5;
C. double d2 = 30/300;
D. double d3 = 1/2 + 0.5;
正确答案:B


10.类Super及Sub定义如下:
public class Super {
    private void f() {
      System.out.println("Super.f()");
    }
    public void g() {
      f();
   }
   public void k() {
     f();
   }
}
public class Sub extends Super {
   private void f() {
      System.out.println("Sub.f()");
   }
   public void k() {
      f();
   }
}
运行下列语句:
Super obj = new Sub();
obj.g();
obj.k();
输出的结果是:()。
A. Sub.f()
  Sub.f()
B. Sub.f()
  Super.f()
C. Super.f()
  Sub.f()
D. Super.f()
  Super.f()
正确答案:C


11. 下列数组声明语句中,错误的是:()。
A.int[] arr = new int[8];
B.int[] arr = new int[8]{};
C.int[] arr = {};
D.int[] arr = new int[]{};
正确答案:B


12.执行下列语句:
int   num=~3+2; 变量num的值为()。
A.-3
B.3
C.-2
D.-1
正确答案:C


13.请看下列代码:
interface Data { public void load(); }
abstract class Info { public abstract void load(); }
下列选项中,能正确使用Data接口和Info类的是()。
A.public class Employee extends Info implements Data {
    public void load() { /*do something*/ }
}
B.public class Employee implements Info extends Data {
    public void load() { /*do something*/ }
}
C.public class Employee implements Info extends Data {
    public void Data.load() { /*d something */ }
    public void load() { /*do something */ }
}
D.public class Employee extends Info implements Data {
   public void load() { /*do something */ }
   public void Info.load() { /*do something*/ }
}
正确答案:A


14.下列代码编译和运行的结果是()。
public class A {
  public void start() {
    System.out.println("TestA");
 }
}
public class B extends A {
  public void start() {
    System.out.println("TestB");
  }
  public static void main(String[] args) {
    ((A) new B()).start();
  }
}
A.输出:TestA
B.输出:TestB
C.输出:TestA  TestB
D.编译错误
正确答案:B


15.类A,B的定义如下:
class A {
   private int a = 100;
   A() {
     System.out.print("A()");
     System.out.println(a);
   }
}
class B extends A {
   private int a = 200;
   B() {
     System.out.print("B()");
     System.out.println(a);
   }


}
运行下面的代码:new B();
输出的结果是:()。
A. A() 100
  B() 200
B. A() 200
  B() 200
C. B() 200
  A() 100
D. B() 200
  A() 200
正确答案:A


16.下列代码的输出结果是()
    public static void main(String[] args) {
        String test = "a1b2c3";
        String[] tokens = test.split("\\d");
        for (String s : tokens)
            System.out.print(s + " ");
    }
A.a b c
B.1 2 3
C.a1b2c3
D.a1 b2 c3
正确答案:A


17. 关于Java线程说法错误的是()。
A.创建线程的有2种方式,方式1是继承Thread类,方式2是实现 Runnable 接口
B.解决线程安全使用问题 synchronized关键字,使得同一时间只有一个线程执行该关键字限定的代码段
C.线程间通信所使用的方法有,wait,notify,notifyAll,它们都是 Thread 的方法
D.Java线程包括5个状态,线程的创建,可运行,运行,阻塞和消亡
正确答案:C


18.下列代码的输出结果是()。
boolean b=true?false:true==true?false:true;//b=tuer?false:false
System.out.println(b);
A.true
B.false
C.null
D.空字符串
正确答案:B


19.请看下列代码编译和运行的结果是()。
interface DeclareStuff {
   public static final int EASY = 3;
   void doStuff(int t);// public rabstract
}
public class TestDeclare implements DeclareStuff {
  public static void main(String[] args) {
    int x = 5;
    new TestDeclare().doStuff(++x);
  }
  void doStuff(int s) {
    s += EASY + ++s;
    System.out.println("s=" + s);
  }
}
A.s=14
B.s=16
C.s=10
D.编译失败
正确答案:D


20. 运行下面的程序:
    String[] fileNames = { "abc.txt", "bcd.exe", "cde.exe", "def.dat","efg.exe" };
    for (String fileName : fileNames) {
        if (fileName.endsWith(".exe")) {
            System.out.print(fileName.substring(0, fileName
                    .lastIndexOf(".exe"))+" ");
        }
    }
    控制台的输出结果是:()。
A. bcd. cde. efg.
B. bc cd ef
C. bcd.exe cde.exe efg.exe
D. bcd cde efg
正确答案:D


21.下列代码的作用说法不正确的是:()。
class Card implements java.io.Serializable{}
A.开启序列化功能,使得Card类的对象可以存储到文件中
B.开启序列化功能,使得Card类的对象可以在网络上传输
C.使得Card类的子类的对象可以被序列化
D.导致Card的子类的对象不可以被反序列化
正确答案:D


22. 运行下列代码,输出为false的是:()。
A. String st1 = "abc";
  System.out.println("abc" == st1);
B. String st2 = "abc";
  System.out.println(st2.equals(new String("abc")));
C. Integer i = 100; // 
  System.out.println(100 == i);
D. ArrayList list = new ArrayList();
  System.out.println(list.contains(null));
正确答案:D


23.运行下列程序:
    String str = "**java***java*****java*";
    String str1 = "java";
    int index = 0;
    while ((index = str.indexOf(str1, index)) != -1) {
        System.out.print(index+””);
        index += str1.length();
    }
    控制台输出的结果是:()。
A. 1  8 17
B. 2 9 18
C. 5 12 21
D. 6 13 22
正确答案:B


24. 下列代码中不能正确获取到Class类的对象的是:()。
A.String  sub = "hello";
  Class c1 = sub.getClass();
B.Class c2 = int.TYPE;
C.Class c1 = Class.forName ("java.lang.Integer");
D.Button b = new Button();
  Class c1 = b.getClass();  
  Class c2 = c1.getSuperclass();
正确答案:B


25.URLEncoding是一种应用于HTTP协议的编码方式,字符串“你好”基于UTF-8的URLEncoding编码为: “%E4%BD%A0%E5%A5%BD”
其中E4、BD、A0为字符“你”的UTF-8编码的十六进制形式(3个字节),而E5、A5、BD为字符“好”的UTF-8编码的十六进制形式。
下面的代码用程序的方式输出字符串“你好”的基于UTF-8的URLEncoding序列:
     String msg = "你好";
         空白处1       
     StringBuilder sb = new StringBuilder();
     for (int i = 0; i < bs.length; i++) {
                 空白处2       
        sb.append("%").append(str);
    }
    System.out.println(sb.toString());
空白处1及空白处2分别应填入的代码是()。
A. byte[] bs = msg.getChars("utf-8");
和 String str = Integer.toHexString(bs[i]& 0xff).toUpperCase();
B. byte[] bs = msg.getBytes("utf-8");
和 String str = Integer.toHexString(bs[i]).toUpperCase();
C. byte[] bs = msg.getBytes("utf-8");
和 String str = Integer.toHexString(bs[i] & 0xff).toUpperCase();
D. byte[] bs = msg.getBytes();
和 String str = Integer.toHexString(bs[i]).toUpperCase();
正确答案:C


26.程序执行的结果是()。
  public class Test {
    String name="Tom";
    public Test(String name){
        name=name;
    }
    public static void main(String [] args){
        Test t = new Test("Jack");
        System.out.println(t.name);
    }
  }
A.null
B.Tom
C.Jack
D." "
正确答案:B


27. 下列属于不合法Java标识符的是()。
A._avaj
B.5save
C.Avaj
D.$80
正确答案:B


28.下列代码的输出结果是()。
public static void main(String[] args) {
        Runnable r = new Runnable() {
            public void run() {
                System.out.print("Cat");
            }
        };
        Thread t = new Thread(r) {
            public void run() {
                System.out.print("Dog");
            }
        };
        t.start();
     }
A.Cat
B.Dog
C.没有任何输出
D.抛出运行时异常
正确答案:B


29. 下面关于final说法正确的是:()。
A.final修饰类时,该类能被继承。
B.final修饰方法时,该方法能被重写。
C.当使用static final 修饰的常量时,将采用编译期绑定的方式。
D.当使用final和abstract共同修饰一个类时,final应至于abstract之前。
正确答案:C


30. 下面关于final说法错误的是:()
A. final修饰类时,该类不能被继承。
B. final修饰方法时,该方法不能被重写。
C. 当引用到使用static final 修饰的常量时,将采用编译期绑定的方式。
D. 当使用final和abstract共同修饰一个类时,final应至于abstract之前。
正确答案:D


31.请看下列代码:
     public static void main(String[] args) {
        <插入代码>
        System.out.println(s);
    }
  如果程序输出的结果是4247,那么在<插入代码>处应该填入代码是()。
A.String s = "123456789";
  s = (s-"123").replace(1,3,"24") - "89";
B.StringBuffer s = new StringBuffer("123456789");
  s.delete(0,3).replace( 1,3, "24").delete(4,6);
C.StringBuffer s = new StringBuffer("123456789");
  s.substring(3,6).delete( 1 ,3).insert( 1, "24");
D.StringBuilder s = new StringBuilder("123456789");
  s.substring(3,6).delete( 1 ,2).insert( 1, "24");
正确答案:B


32. 下面关于interface,叙述错误的是:()
A.一个interface可以继承多个interface
B.接口中的方法可以由private修饰
C.interface中可以定义static final 常量
D.interface中可以无任何方法定义
正确答案:B


33.分析如下代码,输出结果为()。
   public static void main(String[] args) {
        int i = 0;
        boolean re = false
        re = ((++i) + i == 2) ? true : false;
        System.out.println("i=" + i + ",re="+re);
}
A.i=1,re=true
B.i=0,re=true
C.i=1,re=false
D.i=0,re=false
正确答案:A


34. 下列数组声明语句中,错误的是:()。
A. int[] arr = new int[]{};
B. int[] arr = new int[];
C. int[] arr = {};
D. int[][] arr = new int[2][]
正确答案:B


35.下列代码的运行结果是()
    public static void main(String[] args) {
       String str = "420";
       str += 42;
       System.out.print(str);
}
A.42
B.420
C.462
D.42042
正确答案:D


36.所谓“水仙花”数是一个整数等于各位数字立方的和,例如:153 = 1*1*1+5*5*5+3*3*3,下面的程序用于输出2~1000内的水仙花数:
     for (int n = 2; n <= 1000; n++) {
            空白处           
        if (s == n) {
            System.out.println(n);
        }
    }
下列选项中,空白处可以填入的代码是:()。
A. int s = 0, n1 = n;
while (n1 > 0) {
    int t = n1 % 10;
    s += t * t * t;
    n1 /= 10;
}
B. int s = 0, n1 = n;
while (n1 > 0) {
    int t = n1 / 10;
    s+= t * t * t;
    n1 %= 10;
 }
C. int s = 0;
for(int n1 = n; n1>0; n1 /= 10) {
        int t = n1%10;
        s += t * t * t;
}
D.int s = 0;
 for(int n1 = n; n1>0; n1 %= 10) {
        int t = n1 / 10;
        s += t * t * t;
}
正确答案:AC


37. 下面的方法属于StringBuffer的是:()。
A. size
B. insert
C. delete
D. length
正确答案:BCD


38.类Super的定义如下:
class A {
        protected void f() throws IOException {
            ………
        }
}
下列代码段中,没有编译错误的是:()。
A. class B extends A {
    public void f() throws Exception {
        ………
    }
}
B. class B extends A {
   public void g() throws IOException {
      f();
   }
}
C. class B extends A {
   public void g() {
      try {
        f();
         ………
       }catch(Exception e) {
         ………
      }catch(IOException e1) {
        ………
      }
   }
}
D. class B extends A {
   public void g() {
      try {    
        f();
      }catch(IOException e) {
          throw new RuntimeException(e);
      }
    }
}
正确答案:BD


39.查看如下代码:
public class Foo {
       public void method(String str,int age){}
}
下列选项中,和 Foo 类中 method 方法重载的方法是()。
A.public int method(String str,int age){}
B. public void  method(int year,String s){}
C. public int  method(int year,String s){}
D. public int method(String str){}
正确答案:BCD


40. 下列关于Java的说法,错误的是()。
A. Java语言是纯粹的面向对象的语言。
B. Java程序的运行必须有Java虚拟机(JVM)的支持。
C. Java语言支持指针。
D. Java语言支持多重继承。
正确答案:CD


41.矩阵是指纵横排列的数据表格,最早来自于方程组的系数及常数所构成的方阵,如:
        a11 a12... a1n            
        a21 a22... a2n
        ... ... ...
        am1 am2... amn
矩阵乘积规则示例如下:
两个矩阵a和b可以相乘的条件是a矩阵的列数和b矩阵的行数相同,例如:
假设矩阵a为“2行3列”:
a11 a12 a13
a21 a22 a23
矩阵b为“3行2列”:
b11 b12
b21 b22
b31 b32
a和b可以相乘,乘积矩阵为:
a11*b11+a12*b21+a13*b31    a11*b12+a12*b22+a13*b32
a21*b11+a22*b21+a23*b31    a21*b12+a22*b22+a23*b32
Matrix类的定义如下:
public class Matrix {
  private double[][] data;
  private int rows;
  private int cols;
  public Matrix(int rows, int cols) {
     if (rows <= 0 || cols <= 0)
         throw new IllegalArgumentException("");
     this.rows = rows;
     this.cols = cols;
     data = new double[rows][cols];
  }
  public Matrix(int rows, int cols, String line) {
     if (rows <= 0 || cols <= 0 || line == null)
         throw new IllegalArgumentException("");
     String[] dataStr = line.split(",");
     if (  空白处1  ) {
         throw new IllegalArgumentException("");
     }
     this.rows = rows;
     this.cols = cols;
     data = new double[rows][cols];
     for (int i = 0; i < dataStr.length; i++) {
         (          空白处2          )
     }
  }
  public Matrix mul(Matrix ma) {
     if (          空白处3          ) {
         throw new IllegalArgumentException();
     }
     Matrix mc = new Matrix(rows, ma.cols);
     for (int i = 0; i < mc.getRows(); i++) {
         for (int j = 0; j < mc.getCols(); j++) {
             for (          空白处4          ) {
                           空白处5         
             }
         }
     }
     return mc;
  }
  public int getRows() {
     return rows;


  }
  public int getCols() {
     return cols;
  }
  public String toString() {
     StringBuilder sb = new StringBuilder();
     for (int i = 0; i < rows; i++) {
         for (int j = 0; j < cols - 1; j++) {
             sb.append(data[i][j]).append(",");
         }
         sb.append(data[i][cols - 1]).append("\n");
     }
     return sb.toString();
  }
}
(1). 空白处1()
A. dataStr.length != (rows-1) * (cols-1)
B. dataStr.length != (rows-1) * cols
C. dataStr.length != rows * cols
D. dataStr.length != rows * (cols-1)
正确答案:C


(2). 空白处2()
A. data[i % cols][i / cols] = Double.parseDouble(dataStr[i]);
B. data[i/cols][i % cols] = Double.parseDouble(dataStr[i]);
C. data[i/ rows][i % rows] = Double.parseDouble(dataStr[i]);
D. data[i % rows][i /rows] = Double.parseDouble(dataStr[i]);
正确答案:B


(3). 空白处3()
A. cols != ma.cols
B. rows != ma.cols
C. rows != ma.rows
D. cols != ma.rows
正确答案:D


(4). 空白处4()
A. int k = 0; k < cols; k++
B. int k = 0; k <ma.cols; k++
C. int k = 0; k <rows; k++
D. int k = 0; k <rows * ma.cols; k++
正确答案:A


(5). 空白处5()
A. mc.data[i][j] += data[k][j] * ma.data[i][k];
B. mc.data[i][j] += data[k][i] * ma.data[j][k];
C. mc.data[i][j] += data[j][k] * ma.data[k][i];
D. mc.data[i][j] += data[i][k] * ma.data[k][j];
正确答案:D


42.下面的程序用于从54张扑克牌中,随机选出13张不同的扑克牌。
public static void main(String[] args) {
       String[] cards = { "红桃3", "红桃4", "红桃5", "红桃6", "红桃7",
              "红桃8","红桃9","红桃10","红桃J","红桃Q","红桃K","红桃A",
              "黑桃3", "黑桃4", "黑桃5", "黑桃6", "黑桃7","黑桃8","黑桃9",
              "黑桃10","黑桃J","黑桃Q","黑桃K","黑桃A","红方块3", "红方块4",
              "红方块5", "红方块6", "红方块7","红方块8","红方块9","红方块10",
              "红方块J","红方块Q","红方块K","红方块A","黑方块3", "黑方块4",
              "黑方块5", "黑方块6", "黑方块7","黑方块8","黑方块9","黑方块10",
              "黑方块J","黑方块Q","黑方块K","黑方块A"};
       int len=cards.length;
                   空白处1        
       while (true) {
           Random rd = new Random();
                         空白处2        
           cardThirteen.add(cards[index]);
           if (          空白处3          == 13) {
              break;
           }
       }
                        空白处4        
       while (          空白处5         ) {
           System.out.println(it.next());
       }
    }
(1).下列选项中,能填入空白处1的代码是(    )
A.Set<String> cardThirteen = new HashSet<String>();
B.Set<String> cardThirteen = new Set<String>();
C.List<String> cardThirteen = new List<String>();
D.List<String> cardThirteen = new ArrayList<String>();
正确答案:A


(2).下列选项中,能填入空白处2的代码是(    )
A.index = rd.nextInt();
B.index = rd.nextInt(5);
C.index = rd.nextInt(len + 1);
D.index = rd.nextInt(len);
正确答案:D


(3).下列选项中,能填入空白处3的代码是(    )
A.cardThirteen.size
B.cardThirteen.length
C.cardThirteen.length()
D.cardThirteen.size()
正确答案:D


(4).下列选项中,能填入空白处4的代码是(    )
A.Iterator<String> it = cardThirteen.iterator();
B.Iterator<String> it = cardThirteen.next();
C.Iterators<String> it = cardThirteen.iterator();
D.Iterators<String> it = cardThirteen.nexts();
正确答案:A


(5).下列选项中,能填入空白处5的代码是(    )
A.it.hasNext()
B.it.hasNexts ()
C.it.next ()
D.it.nexts()
正确答案:A
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值