栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > Java面试题

2014年最新Java笔试题及答案

Java面试题 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2014年最新Java笔试题及答案

给出以下代码

public class Switch1{

final static short x=2;

public static int y=0;

public static void main(String[] args) {

for(int z=0;z<4;z++){

switch(z){

case x:System.out.print(“0”);

default:System.out.print(“def”);

case x-1:System.out.print(“1”);break;

case x-2:System.out.print(“2”);

}

}

}

}

 

 

class ParentClass{

public int doStuff(int x){

return x*2;

}

}

public class ChildClass extends ParentClass{

public static void main(String[] args){

ChildClass cc=new ChildClass();

long x=cc.doStuff(7);

System.out.println(“x=”+x);

}

public long doStuff(int x){

return x*3;

}

}

 

Public static void main(String[] args){

X.x=new X();

X.x2=m1(x);

X.x4=new X();

X2=x4;

//comment

….

}

Static X m1(X mx){

Mx=new X();

Return mx;

}

}

以上代码执行到comment处时有多少个可被垃圾收集的对象

 

给出以下代码

Public class TestObj{

Public static void main(String[] args){

Object o=new Object(){

Public boolean equals(Object obj){

Return true;

}

};

System.out.println(o.equals(“Fred”));

}

}

请给出结果:()

A 运行时抛出异常

B True

C Fred

D 第三行编译错误

 

  1. 给出以下代码

    Class Two{

    Byte x;

    }

    Class PassO{

    Public static void main(String[] args){

    PassO p=new PassO();

    P.start();

    }

    Void start(){

    Two t=new Two();

    System.out.print(t.x+””);

    Two t2=fix(t);

    System.out.print(t.x+” ” +t2.x);

    }

    Two fix(Two tt){

    tt.x=42;

    Return tt;

    }

    }

    请给出结果:

    A null null 42 C 0 42 42 E Compilation

    B 0 0 42            D 0 0 0          F An exception is thrown at runtime

    5 给出以下代码:

    Public class Switch2{

    Public static void main(String[] args){

    Int N=30;

    Boolean[] flag=new boolean[N+1];

    For(int i=2;i<=N;i++){

    If(!flag[i]){

    For(int j=i*2;j<=N;j+=i)

    Flag[j]=true;

    }

    }

    }

    for(int i=2;i<=N;i++){

    if(!flag[i]){

    System.out.print(i+” “);

    }

    }

    }

    }

     

    运行结果:

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

  2. 请写出以下程序的输出结果

    public class A2{

    public static void main(String[] args){

    int[] a={2,4,6,8,3,6,9,12};

    doSomething(a,0,a.length-1);

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

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

    }

    }

    private static void doSomething(int[] a,int start,int end){

    if(start<end){

    int p=core(a,start,end);

    doSomething(a,start,p-1);

    doSomething(a,p+1,end);

    }

    }

    Private static int core(int[] a,int start,int end)

    int x=a[end];

    int i=start;

    for(int j=start;j<=end-1;j++){

    if(a[j]>=x){

    swap(a,i,j);

    i++;//交换了几次

    }

    }//把最大的放到最后

    Swap(a,i,end);//把最大的放到i的位置

    Return i;

    }

     

    private static void swap(int[] a,inti,int j){

    int tmp=a[i];

    a[i]=a[j];

    a[j]=tmp;

    }

    }

    答案:从大到小排序

     

     

     

     

     


     

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/359567.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号