@liayun
2016-05-25T23:19:09.000000Z
字数 5262
阅读 1731
java基础
注:按Java规范书写代码,如果你认为程序有错误,请指出,并说明程序错误原因。
1.写出程序结果
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");
}
}
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();
}
}
3.写出程序结果
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());
}
}
4.写出程序结果
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;
}
}
5.写出程序结果
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());
}
}
6.写出程序结果
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);
}
}
7.
interface Inter {
void show(int a, int b);
void func();
}
class Demo {
public static void main(String[] args) {
// 补足代码:调用两个函数,要求用匿名内部类
}
}
8.写出程序结果
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();
}
}
9.选择题,写出错误答案错误的原因,用单行注释的方式
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;}
10.写出this关键字的含义,final有哪些特点?
11.写出程序结果
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();
}
}
12.
interface A {
void show();
}
interface B {
void add(int a, int b);
}
class C implements A, B {
// 程序代码
}
class D {
public static void main(String[] args) {
C c = new C();
c.add(4, 2);
c.show(); // 通过该函数打印以上两个数的和
}
}
13.写出程序结果
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();
}
}
14.写出程序结果
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);
}
}
15.写出程序结果
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());
}
}
16.写出程序结果
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");
}
}
17.
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();
C.在位置2写 new d.Inner();
D.在位置2写 new Demo.Inner();
18.写出程序结果
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");
}
}
}
19.
interface Test {
void func();
}
class Demo {
public static void main(String[] args) {
// 补足代码:(匿名内部类)
}
void show(Test t) {
t.func();
}
}
20.写出程序结果,一道java国际考试题。
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);
}
}
21.补足compare函数内的代码,不许添加其他函数。
class Circle {
private static double pi = 3.14;
private double radius;
public Circle(double r) {
radius = r;
}
public static double compare(Circle[] cir) {
// 程序代码,其实就是在求数组中的最大值
}
}
class TC {
public static void main(String[] args) {
Circle cir[] = new Circle[3]; // 创建一个类类型数组
cir[0] = new Circle(1.0);
cir[1] = new Circle(2.0);
cir[2] = new Circle(4.0);
System.out.println("最大的半径值是:"+Circle.compare(cir));
}
}
22.写出程序结果
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);
}
}
23.在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,如果存在,则返回这个字符在字符数组中第一次出现的位置(序号从0开始计算),否则,返回-1。要搜索的字符数组和字符都以参数形式传递给该方法。如果传入的数组为null,应抛出IllegalArgumentException异常。在类的main方法中,以各种可能出现的情况测试验证该方法编写得是否正确,例如,字符不存在,字符存在,传入的数组为null等。
24.补足compare函数内的代码,不许添加其他函数。
class Circle {
private double radius;
public Circle(double r) {
radius = r;
}
public Circle compare(Circle 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("圆1的半径比较大");
else
System.out.println("圆2的半径比较大");
}
}