JAVA笔试题精华版1.0

JAVA笔试题精华版1.0


public class Test2

{

    public static void main(String[] args)

    {

        Float a = new Float(3.4);

        System.out.println(a.SIZE);

        a = new Float(34000);

        System.out.println(a.SIZE);

    }

 

}

让我们来看看此程序会输出什么呢?

我们先来看看JDK 的解释吧 .

 

public static final int SIZE

The number of bits used to represent a float value.

 

意思是说: 通常去描述一个 float 值的位数 .

这个是一个常量, 来看看源码吧 :

 public static final int SIZE = 32;

 

final int 变量一旦被定义就不能被改变 ~

 

 

1.写出下面代码的结果

 

 

 

 public class TestString{

     public static void link(String a){

         a += "World";

     }

 

     public static void main(String[] args){

         String a = "Hello";

         link(a);

         System.out.println(a);

     }

 }

 

 

这道题考两个方面 :

 

1).  String 对象的内容是不能改变的 ,a+="World" 并不是把 a 所指对象改变 ,

 

  而是先生成一个临时String 对象 , 其值为 a+"World", 然后在把这个临时

 

  对象赋给a.

 

2).  Java 中函数参数传递方式为 Call by value,link 方法中会生产 a 的一个

 

  拷贝, 同样指向 a 所指的对象 , 综合以上两点 , 得出结果为  Hello

 

 

 

2.写出下面代码的结果

 

 

 

System.out.println("ja"+"va" == "java");

 

 

:"==" 用来比较对象的引用 , equals() 用来比较对象的内容 , 但是如果是字符串常量 , "==" 也可以比较内容

 

是否相等,"ja"+"va" "java" 都是字符串常量 , 因此结果为 true

 

同理, 下面代码结果也是 true

 

 

 

final String str = "java";

 

System.out.println(str=="java")

 

1.

选出用法错误的:

a: Stirng a ="Gone With Wind";

String t= "Wind";

String m;

m=a-t;

B: Stirng a ="Gone With Wind";

String m ;

m=a[3]+"one";

C: Stirng a ="Gone With Wind";

Sting m;

m=a.toUpperCase();

D: 不记得了

AB.

 

2.

选出能正确赋值的:

public class A {

private int a;

public void change(int m) {

return m;

}

 

}

public class B extends A{

public int b;

public static void main() {

A aa = new A();

B bb = new B();

int k;

//px

}

}

px 处可以正确赋值的有 :

A k= m; B k=b; C k=aa.a; D k=bb.change(30); E k=bb.a

 

 

C

3.

此程序会输出什么?

package com;

 

class A

{   

 

    public A()

    {

        a1();

    }

 

    public void a1()

    {

        System.out.println("A-a1");

    }

 

}

 

public class B extends A

{

    int bb = 0;

 

    public B()

    {

 

        bb = 1000;

    }

 

    public void a1()

    {

        System.out.println("bb is " + bb);

        System.out.println("B-a1");

    }

 

    public static void main(String[] args)

    {

 

        new B();

    }

 

}

 

答案:

bb is 0

B-a1

看看执行顺序就明白了.

package com;

 

class A

{   

    //3

    public A()

    {

        a1();

    }

 

    public void a1()

    {

        System.out.println("A-a1");

    }

 

}

 

public class B extends A

{

    int bb = 0;

    //2

    public B()

    {

        //5

        bb = 1000;

    }

    //4

    public void a1()

    {

        System.out.println("bb is " + bb);

        System.out.println("B-a1");

    }

 

    public static void main(String[] args)

    {

        //1

        new B();

    }

 

}

 

在方法被a1() 被重写的情况下 , 父类的 a1 是没有机会

被调用的.

posted on 2007-12-15 10:43 々上善若水々 阅读 (235)  评论 (3)   编辑    收藏所属分类 : Java 笔试与面试

 

 

Comments

# re: JAVA笔试题(金山软件) [ 未登录 ]

lovejava

比较喜欢面试题.

Posted @ 2007-12-16 11:21  回复    更多评论    

# re: JAVA笔试题(金山软件) [ 未登录 ]

古风

public class A

{

public int a;

public int change(int m)

{

return m;

}

public A()

{

System.out.println("constructA");

a1();

}

public void a1()

{

System.out.println("A - a1");

}

 

}

 

 

 

 

 

 

 

public class B extends A

{

 

int bb = 0;

public B()

{

System.out.println("constructB");

bb = 1000;

System.out.println("bb = " + bb);

}

public void a1()

{

System.out.println("bb is " + bb);

System.out.println("B - a1");

}

public static void main(String[] args)

{

new B();

}

}

 

 

 

 

执行结果:

constructA

bb is 0

B - a1

constructB

bb = 1000

 

 

.

public class Test {

public static void changeStr(String str){

str="welcome";

}

public static void main(String[] args) {

String str="1234";

changeStr(str);

System.out.println(str);

}

}

此题结果为:1234;

比较简单分析下内存就行了.

 

2.

public class ForTest

{

   

    static boolean foo(char c)

    {

        System.out.println(c);

        return true;

    }

   

    public static void main(String[] args)

    {

        int i = 0;

        for(foo('A');foo('B')&&(i<2);foo('C'))

        {

            i ++;

            foo('D');

        }

    }

 

}此题结果为 :ABDCBDCB

 

这道题考查的for 循环的执行顺序 .

for(int i  = 0; i < 10; i ++)

{}

首先先执行int i = 0; 注意这个只是初始化一次 ,

就是在第一次的时候. 接着执行 i < 10;

然后执行方法体{} 里面的内容 , 最后才执行 i++;

 

第二次及以后就直接执行i <10; 然后方法体;最后

i ++;如此顺序直到结束为止 .

 

3.

1. class A {

2. protected int method1(int a, int b) { return 0; }

3. }

Which two are valid in a class that extends class A? (Choose two)

A. public int method1(int a, int b) { return 0; }

B. private int method1(int a, int b) { return 0; }

C. private int method1(int a, long b) { return 0; }

D. public short method1(int a, int b) { return 0; }

E. static protected int method1(int a, int b) { return 0; }

 

此题考查的是继承重写问题.

当一个子类重写父类的方法时,重写的方法的访问权限

必须大于等于父类的访问权限.

在此题中父类中的方法访问权限为protected, 子类只能是

protected public. 这时A是符合题意的.

由于选项C的形参和父类的不一样,没有重写的效果,所以

在子类出现也是没问题的.

所以此题选:AC

 

.

1. public class Outer{

2. public void someOuterMethod() {

3. // Line 3

4. }

5. public class Inner{}

6. public static void main( String[]argv ) {

7. Outer o = new Outer();

8. // Line 8

9. }

10. }

Which instantiates an instance of Inner?

A. new Inner(); // At line 3

B. new Inner(); // At line 8

C. new o.Inner(); // At line 8

D. new Outer.Inner(); // At line 8//new Outer().new Inner()

此题选A.

内部类的实例化可以在普通方法里也可以在

static方法里实例化.

如下:

package com.test.a;

 

public class Outer

{

    Inner i = new Outer.Inner();

   

    public void method()

    {

        new Inner();

    }

   

    public class Inner{

    }

    public static void main(String[] args)

    {

        Outer o = new Outer();

        Inner i =  o.new Inner();

    }

   

    static void a()

    {

        Outer o = new Outer();

        Inner i = o.new Inner();

    }

 

}

 

 

public class Jtest{

int m=1;

int i=3;

void Jtest(){

m=2;

i=4;

}

 

public static void main(String[] args){

Jtest app=new Jtest();

System.out.println(app.m+","+app.i);

}

}

写出输出.

结果是1,3;

因为在这里void Jtest(); 并没有并调用 , 它只是一个

方法, 而非构造方法 , 这样的编写是有警告的 , 不过

可以运行.

 

public class Jtest{

int m=1;

int i=3;

Jtest(){

m=2;

i=4;

}

public static void main(String[] args){

Jtest app=new Jtest();

System.out.println(app.m+","+app.i);

}

}

写出输出:

结果是2,4;

调用了构造方法, 不加修饰符 , 默认访问权限是

package access, Java 里没有关键字表示 , 就是

包内的能访问, 包外就不行了 ( 即使导入也不行 ).

public class Test

{

    static void oper(int b)

    {

        b = b + 100;

    }

   

    public static void main(String[] args)

    {

        int a = 99;

        oper(a);

        System.out.println(a);

    }

 

}

输出为99.

我们来分析一下内存:

int a = 99;

首先在栈里面开辟一块空间保存a

比如:a:xxxx

然后调用oper(a);

这时把 的值 99 赋给 int b;

b在内存里也开辟了自己的空间 , 此时

值也是99.

然后执行oper(a); 方法体 ,b = b + 100;

此时b 的值为 199,a 的值为 99.

public class Test {

  public static void main(String[] args) {

        String a=new String("A");

        String b=new String("B");

        oper(a,b);

        System.out.print(a+","+b);

    }

  static void oper(String c,String d){

      c.concat("B");

      d=c;

  }

}

 

此程序输出:A B.

原因就是String final 类型的 . 并不会被改变 .

public class Test

{

    public static void main(String[] args)

    {

        String a = new String("A");

        String b = new String("B");

        a.concat("aa");

        System.out.println(a + "," + b);

    }

 

}

 

这个还是会输出A,B

原因同上.

package intervie;

 

public class Test

{

    public static void main(String[] args)

    {

        String a = new String("A");

        String b = new String("B");

        a = a.concat("aa");

        System.out.println(a + "," + b);

    }

 

}

 

做了下改动, 再来看看 . 结果就不同了 .

输出的是Aaa,B

因为String  final 类型的 . 所以执行到

a = c.concat("aa");

会在heap 里新创建一个对象 , a 指向它 .

这是一新的地址, String a  这个已经不同了 .

所以输出的是后一个. 即改变后的值 .

public class Test

{

    static void oper(StringBuffer c,StringBuffer d)

    {

        d = c.append("B");

    }

   

    public static void main(String[] args)

    {

        StringBuffer a = new StringBuffer("A");

        StringBuffer b = new StringBuffer("B");

        oper(a, b);

        System.out.println(a + "," + b);

    }

 

}

 

 

此程序会输出:AB,B

 

 

package org;

 

public class Test

{

 

    public static void main(String[] args)

    {

        A a = new B();

        a.print();

    }

 

}

 

class A

{

    private int i = 1;

    public A()

    {

        int i = 2;

    }

   

    public void print()

    {

        System.out.println("The result is:" + i);

    }

}

 

class B extends A

{

    private int i = 3;

   

    public B()

    {

        int i = 6;

    }

   

    public void print()

    {

        System.out.println("The result is:" + i);

    }

}

输出结果是:3

此题考查的是多态.

在这里是父类的引用指向子类的对象.

父类的引用只能访问子类和父类共有的

方法.

这个程序我通过Eclipse Debug 程序观察它的

执行顺序是这样的:

package org;

 

public class Test

{

 

    public static void main(String[] args)

    {

        A a = new B();

        a.print();

    }

 

}

 

class A

{

    //3

    private int i = 1;

    //2

    public A()

    {

        //4

        int i = 2;

    }

   

    public void print()

    {

        System.out.println("The result is:" + i);

    }

}

 

class B extends A

{

    //5

    private int i = 3;

    //1

    public B()

    {

        //6

        int i = 6;

    }

   

    public void print()

    {

        System.out.println("The result is:" + i);

    }

}

 

现在将程序稍微改动一下:

package org;

 

public class Test

{

 

    public static void main(String[] args)

    {

        A a = new B();

        a.print();

    }

 

}

 

class A

{

    //3

    private int i = 1;

    //2

    public A()

    {

        //4

        int i = 2;

    }

   

    public void print()

    {

        System.out.println("The result is:" + i);

    }

}

 

class B extends A

{

    //5

    private int i = 3;

    //1

    public B()

    {

        //6

        int i = 6;

    }

   

    public void print2()

    {

        System.out.println("The result is:" + i);

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值