今天面试做了一套Java笔试题,下面是我根据记忆,对题目进行的测试,我是把几个题目综合在一起进行测试的:
TestB.java代码:
import java.util.*;
class Parant
{
Parant()
{
System.out.println("parant");
}
static
{
System.out.println("parant static");
}
{
System.out.println("parant not static");
}
public void test(ArrayList list)
{
System.out.println("parant list");
}
}
class Child extends Parant
{
Child()
{
System.out.println("child");
}
static
{
System.out.println("child static");
}
{
System.out.println("child not static");
}
public void test(List list)
{
System.out.println("child list");
}
}
class TestB
{
public static void main(String[] args)
{
//先调用父类的构造方法,然后调用子类的构造方法,不管static还是没有static的代码段,都会被执行
Child ac=new Child();
ArrayList al=new ArrayList();
ac.test(al); //输出的是parant list,子类没有重写父类的方法
String a="aaa";
String b="aaa";
String c=new String("aaa");
//如果要比较两个基本类型的值是否相等,请用= =;
//如果要比较两个对象的地址是否相等,请用= =;
//如果要比较两个对象(引用)是否一致,请用equals
System.out.println(a==b);
System.out.println(a.equals(c));
int temp=99;
test(temp); //输出的是99
System.out.println(temp); //99
System.out.println(test(temp)); //199
}
public static int test(int bb)
{
bb=bb+100;
return bb;
}
}
interface A
{
void test2(); //接口类不能带有方法主体,接口中可以没有方法,编辑能通过
}
abstract class B
{
abstract void test3(); //如果没有abstract,必须有方法主体
void test4()
{
}
}
TestC.java:
class TestC
{
public static void main(String[] args)
{
int a=test();
System.out.println(a); //输出的结果是3
}
public static int test()
{
try
{
String s=null;
s.substring(0,1);
return 1;
}
catch(Exception e)
{
return 2;
}
finally //如果没有finally,则输出2
{
return 3;
}
}
}
TestD.java
import java.util.*;
class TestD
{
public static void main(String[] args)
{
ArrayList a=new ArrayList();
test1(a);
System.out.println(a.size()); //输出是0
test2(a);
System.out.println(a.size()); //输出是1
}
static void test1(ArrayList al)
{
al=null;
}
static void test2(ArrayList al)
{
al.add("all");
}
}
TestC的运行结果: