java笔试题

来源:互联网 发布:电脑版淘宝下载安装 编辑:程序博客网 时间:2024/04/28 19:30

注:按Java规范书写程序代码,如果你认为程序有错误,请指出,并说明程序错误原因。

1.(2分)
写出程序结果
class Demo
{
public static void func()
{
try
{
throw new Exception();
}
finally
{
System.out.println(“B”);
}
}
public static void main(String[] args)
{
try
{
func();
System.out.println(“A”);
}
catch(Exception e)
{
System.out.println(“C”);
}
System.out.println(“D”);
}
}

编译失败,没有声明异常。

  1. (2分)
    写出程序结果
    class Test
    {
    Test()
    {

    System.out.println("Test");

    }
    }
    class Demo extends Test
    {
    Demo()
    {

    System.out.println("Demo");

    }
    public static void main(String[] args)
    {
    new Demo();
    new Test();
    }
    }

Test
Demo

Test

  1. (2分)
    写出程序结果
    interface A{}
    class B implements A
    {
    public String func()
    {
    return “func”;
    }
    }
    class Demo
    {
    public static void main(String[] args)
    {
    A a=new B();
    System.out.println(a.func());

    }
    }

a类型被向上提升了,不能调用func方法。

  1. (2分)
    写出程序结果
    class Fu
    {
    boolean show(char a)
    {
    System.out.println(a);
    return true;
    }
    }
    class Demo extends Fu
    {
    public static void main(String[] args)
    {
    int i=0;
    Fu f=new Demo();
    Demo d=new Demo();
    for(f.show(‘A’); f.show(‘B’)&&(i<2);f.show(‘C’))
    {
    i++;
    d.show(‘D’);
    }
    }
    boolean show(char a)
    {
    System.out.println(a);
    return false;
    }
    }

A B

  1. (2分)
    写出程序结果
    interface A{}
    class B implements A
    {
    public String test()
    {
    return “yes”;
    }
    }
    class Demo
    {
    static A get()
    {
    return new B();
    }
    public static void main(String[] args)
    {
    A a=get();
    System.out.println(a.test());
    }
    }

编译失败,出现了类型提升后调用了子类特有的方法。

  1. (2分)
    写出程序结果:
    class Super
    {
    int i=0;
    public Super(String a)
    {
    System.out.println(“A”);
    i=1;
    }
    public Super()
    {
    System.out.println(“B”);
    i+=2;
    }
    }
    class Demo extends Super
    {
    public Demo(String a)
    {

    System.out.println("C");i+=5;               

    }
    public static void main(String[] args)
    {
    int i=4;
    Super d=new Demo(“A”);
    System.out.println(d.i);
    }
    }

B C 5

  1. (2分)
    写出程序结果
    class TD
    {
    int y=6;
    class Inner
    {
    static int y=3;
    void show()
    {
    System.out.println(y);
    }
    }
    }
    class TC
    {
    public static void main(String[] args)
    {
    TD.Inner ti=new TD().new Inner();
    ti.show();
    }
    }

编译失败,非静态内部类中不能定义静态的变量。

  1. (2分)
    写出程序结果:
    class Fu
    {
    int num=4;
    void show()
    {
    System.out.println(“showFu”);
    }
    }
    class Zi extends Fu
    {
    int num=5;
    void show()
    {
    System.out.println(“showZi”);
    }
    }
    class T
    {
    public static void main(String[] args)
    {
    Fu f=new Zi();
    Zi z=new Zi();
    System.out.println(f.num);
    System.out.println(z.num);
    f.show();
    z.show();
    }
    }
    4
    5
    showZi

showZi

  1. (2分)
    写出程序结果
    class Demo
    {
    public static void main(String[] args)
    {
    try
    {
    showExce();
    System.out.println(“A”);
    }
    catch(Exception e)
    {
    System.out.println(“B”);
    }
    finally
    {
    System.out.println(“C”);
    }
    System.out.println(“D”);
    }
    public static void showExce()throws Exception
    {
    throw new Exception();
    }
    }

B C D

  1. (2分)
    写出程序结果
    class Super
    {
    int i=0;

    public Super(String s)
    {
    i=1;
    }
    }
    class Demo extends Super
    {
    public Demo(String s)
    {

    i=2;            

    }
    public static void main(String[] args)
    {
    Demo d=new Demo(“yes”);
    System.out.println(d.i);
    }
    }

编译失败,子类调用了父类中的空构造函数,但是父类中却没有空构造函数。

  1. (2分)
    写出程序结果
    class Super
    {
    public int get(){return 4;}
    }
    class Demo15 extends Super
    {
    public long get(){return 5;}
    public static void main(String[] args)
    {
    Super s=new Demo15();
    System.out.println(s.get());
    }
    }

编译失败,没有明确调用的函数的返回值类型。

  1. (2分)
    写出程序结果:
    class Demo
    {
    public static void func()
    {
    try
    {
    throw new Exception();
    System.out.println(“A”);
    }
    catch(Exception e)
    {
    System.out.println(“B”);
    }
    }
    public static void main(String[] args)
    {
    try
    {
    func();
    }
    catch(Exception e)
    {
    System.out.println(“C”);
    }
    System.out.println(“D”);
    }
    }

B D

  1. (2分)
    写出程序结果
    class Exc0 extends Exception{}
    class Exc1 extends Exc0{}

class Demo
{
public static void main(String[] args)
{
try
{
throw new Exc1();
}
catch(Exception e)
{
System.out.println(“Exception”);
}
catch(Exc0 e)
{
System.out.println(“Exc0”);
}
}
}
编译失败,类型提升导致Exception e变成了异常的父类出现在最前面。 ====================================================================
14. (2分)
写出程序结果
class Test
{
public static String output=”“;
public static void foo(int i)
{
try
{
if(i==1)
throw new Exception();
output+=”1”;
}
catch(Exception e)
{
output+=”2”;
return;
}
finally
{
output+=”3”;
}
output+=”4”;
}
public static void main(String args[])
{
foo(0);
System.out.println(output);
foo(1);
System.out.println(output);
}
}
134
13423

====================================================================
15. (2分)
写出程序结果
public class Demo
{
private static int j = 0;
private static boolean methodB(int k)
{
j += k;
return true;
}
public static void methodA(int i)
{
boolean b;
b = i < 10 | methodB (4);
b = i < 10 || methodB (8);
}
public static void main (String args[] )
{
methodA (0);
System.out.println(j);
}
}

4

  1. (5分)
    选择题,写出错误答案错误的原因,用单行注释的方式。
    class Demo
    {
    int show(int a,int b){return 0;}
    }
    下面那些函数可以存在于Demo的子类中。
    A.public int show(int a,int b){return 0;}// 可以,覆盖。
    B.private int show(int a,int b){return 0;}// 不可以,权限不够。
    C.private int show(int a,long b){return 0;}// 可以,只是函数的重载。
    D.public short show(int a,int b){return 0;}// 不可以,会产生调用的不确定性

E.static int show(int a,int b){return 0;}//

  1. (5分)
    选择题。写出错误答案错误的原因,用单行注释的方式。
    class Demo
    {
    public void func()
    {
    //位置1;

    }
    class Inner{}
    public static void main(String[] args)
    {
    Demo d=new Demo();
    // 位置2
    }
    }

A.在位置1写 new Inner();//可以
B.在位置2写 new Inner();// 不可以,需要fianl修饰
C.在位置2写 new d.Inner(); // 格式错误
D.在位置2写 new Demo.Inner();//inner不是静态
E.在位置2写 new Demo().new Inner();//可以

====================================================================
18. (4分)
代码补足题
interface Inter
{
void show(int a,int b);
void func();
}
class Demo
{
public static void main(String[] args)
{
//补足代码;调用两个函数,要求用匿名内部类
Inter i = new Inter()
{
public void show(int a,int b){}
public void func(){}

}
I.show(4,5);
I.func();

}

}

  1. (4分)
    interface A
    {
    void show();
    }
    interface B
    {
    void add(int a,int b);
    }
    class C implements A,B
    {
    //补足程序代码
    private int a ;
    private int b ;
    public void add(int a,int b)
    {
    this.a = a;
    this.b = b;
    }

    public void show()
    {
    System.out.println(“a+b=”+(a+b));
    }

}
class D
{
public static void main(String[] args)
{
C c=new C();
c.add(4,2);
c.show();//通过该函数打印以上两个数的和。
}
}

====================================================================
20. (4分)
interface Test
{
void func();
}
class Demo
{
public static void main(String[] args)
{
//通过主函数调用show,补足代码;通过(匿名内部类)进行show方法参数传递。
new Demo().show(new Test()
{
public void func(){}
});

}void show(Test t){    t.func();}

}

====================================================================
21. (4分)
补足compare函数内的代码,不许添加其他函数。
class Circle
{
private static double pi=3.14;
private double radius;
public Circle(double r)
{
radius=r;
}
public static double compare(Circle[] cir)
{
//补足程序代码
double max = cir[0].radius;
for (int i=1;i

}

  1. (4分)
    补足compare函数内的代码,不许添加其他函数。
    class Circle
    {
    private double radius;
    public Circle(double r)
    {
    radius=r;
    }
    public Circle compare(Circle cir)
    {
    //补足程序代码
    if(this.radius>cir.radius)
    return this;
    return cir;

    }
    }
    class TC
    {
    public static void main(String[] args)
    {
    Circle cir1=new Circle(1.0);
    Circle cir2=new Circle(2.0);
    Circle cir;
    cir=cir1.compare(cir2);
    if(cir1==cir)
    System.out.println(“cir 1的半径比较大”);
    else
    System.out.println(“cir 2的半径比较大”);
    }

}

  1. (10分)
    写出this关键字的含义,final什么时候用?
    This代表本类对象,谁调用就代表谁!

1,被final修饰类不可以被继承。
2,final修饰的方法不能被覆盖。
3,final修饰的变量只能被赋值一次。

====================================================================
24. (10分)
建立一个图形接口,声明一个面积函数。圆形和矩形都实现这个接口,并得出两个图形的面积。
注:体现面向对象的特征,对数值进行判断。用异常处理。不合法的数值要出现“这个数值是非法的”提示,不再进行运算。

//自定义异常,继承RuntimeException
class NoValueException extends RuntimeException
{
NoValueException(String msg)
{
super(msg);
}
}

//建立一个图形接口,声明面积
interface TuXing
{
double area();
}

//建立圆形,实现图形接口
class Yuan implements TuXing
{
private static final double PI = 3.14;
double d;
Yuan(double d)
{
if(d<=0)
throw new NoValueException(“这个数值是非法的”);
this.d = d;
}

//实现接口public double area(){    return d*d*PI;}

}

//建立矩形,实现图形接口
class JuXing implements TuXing
{
private double width;
private double height;
JuXing(double width,double height)
{
if(width<=0 || height <= 0 )
throw new NoValueException(“这个数值是非法的”);
this.width = width;
this.height = height;
}

//实现接口public double area(){    return width*height;}

}
class Demo
{
public static void main(String[] args)
{
Yuan y = new Yuan(1);
JuXing j = new JuXing(2,-1);

    System.out.println("Hello World!");}

}

====================================================================
25. (10分)
假如我们在开发一个系统时需要对员工进行建模,员工包含 3 个属性:
姓名、工号以及工资。经理也是员工,除了含有员工的属性外,另为还有一个
奖金属性。请使用继承的思想设计出员工类和经理类。要求类中提供必要的方
法进行属性访问。

/*
假如我们在开发一个系统时需要对员工进行建模,员工包含 3 个属性:
姓名、工号以及工资。经理也是员工,除了含有员工的属性外,另为还有一个
奖金属性。请使用继承的思想设计出员工类和经理类。要求类中提供必要的方
法进行属性访问。
*/
//建立一个抽象员工类。
abstract class Employee
{
private String name;
private String id;
private double pay;

public void setName(String name){    this.name = name;}public String getName(){    return name;}public void setId(String id){    this.id = id;}public String getId(){    return id;}public void setPay(double pay){    this.pay = pay;}public double getPay(){    return pay;}Employee(String name,String id,double pay){    this.name = name;    this.id = id;    this.pay = pay;}

}

//建立一般员工类,继承抽象员工类
class Worker extends Employee
{
Worker(String name,String id,double pay)
{
super(name,id,pay);
}
}

//建立经理类,继承抽象员工类
class Manager extends Employee
{
private double bonus;
public void setBonus(double bonus)
{
this.bonus = bonus;
}

public double getBonus(){    return bonus;}Manager(String name,String id,double pay){    super(name,id,pay);}

}
class Demo2
{
public static void main(String[] args)
{
Manager m = new Manager(“加”,”123”,123);

    m.setBonus(123);    System.out.println(m.getName());    System.out.println(m.getBonus());}

}

====================================================================
26. (10分)
在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,
如果存在,则返回这个字符在字符数组中第一次出现的位置(序号从0开始计算),
否则,返回-1。要搜索的字符数组和字符都以参数形式传递传递给该方法,
如果传入的数组为null,应抛出IllegalArgumentException异常。
在类的main方法中以各种可能出现的情况测试验证该方法编写得是否正确,
例如,字符不存在,字符存在,传入的数组为null等。
class Index
{
int[] arr;
int key;
Index(int[] arr,int key)
{
if(arr == null)
throw new IllegalArgumentException(“数组为空!”);
this.arr = arr;
this.key = key;
}
public int numIndex()
{
for(int i = 0;i < arr.length;i++)
{
if(arr[i] == key)
return i;
}

    return -1;}

}
class IndexNum
{
public static void main(String[] args)
{
int[] x = new int[]{11,12,13,14,15,16,17,};
Index i = new Index(x,12);
int numIndex = i.numIndex();
System.out.println(“index=”+numIndex);
Index i1 = new Index(x,42);
int numIndex1 = i1.numIndex();
System.out.println(“index1=”+numIndex1);
Index i2 = new Index(null,12);
int numIndex2 = i2.numIndex();
System.out.println(“index2=”+numIndex2);
}
}

====================================================================

到了第二阶段,对自己说的是?

改正学习方法,还需要多多的练习!

对老师和学校的建议和意见?

0 0