日期:2014-05-20  浏览次数:20939 次

java面试基础和经典总结

1会输出什么?

public class Parant {

?????? public void play(){

?????? system.out.println("Parant ");

}

}

class child extends Parant {

?????? public void play(){

?????? system.out.println("child ");

}

?????? public static void main (String []arg){

?????? Parant p=new child();

?????? p.play();

}

答案:输出child,实例子类对象然后赋给父类,在调用子父类共有的方法时,这时会显示子类方法内容。

?

2会输出什么?

Public Class simple{

public static void main (String []arg){

?????? int i=0,j=10;

?????? do{

?????????? if(i++<--j){

????????????? //测试System.out.println("i="+i+"j="+j);

????????????? continue;

?????????? }

?????? }while(i<5);

?????? System.out.println("i="+i+"j="+j);

??? }

}

答案:i=5 j=5

?

3)找错

public Class simple {

? private int m;//1

? public static void abc(){

?????? system.out.println(m);//2

}

}

答案:2处错误,静态方法不能够访问外部的非静态变量。

?

4)下面代码是否正确?

public String add(String s){

????????????? return s;

}

public void add(String s){

????????????? return "";

}

?

答:错误,方法的重载要求方法名字相同,参数个数不同或者参数类型不同,与返回值类型相不相同无关。

?

5)下面代码正确吗?

int ar[]={1,2};

int num=ar.length();

答:错误,数组有length属性,Stringlength()方法。

?

6)判断题

public class Parant {

??? public Parant(){

?????? System.out.println("Parant");

??? }

??? private void test(){

??????

?????? System.out.println("abc");

??? }

}

class child extends Parant {

??? private child() {

?????? System.out.println("child");

?????? test();

??? }

?

??? private void test() {

?

?????? System.out.println("abc");

??? }

?

??? public static void main(String[] args) {

?????? new child();

??? }

}

会输出:

child

abc

Parant

答:错,会输出Parantchild abc

?

7)这个代码合法么?

String s=null;

if(s.length()>0){

??? System.out.println("hello");

}

答:不合法,会出现java.lang.NullPointerException异常。

?

8)选出下面的这种方式合法嚒?

final String s=null;s="abc";

答:不合法,正确的方式final String s;s="abc";

?

9)下面方法合法么?

Public static void test(){

Static String s;

}

答:不合法,静态变量只能声明为类变量,不能够在方法体中进行声明,它是和类关联的。

?

10)会打印出什么?

public static boolean? test() {

?????? try {

?????????? return true;

?????? } catch (Exception e) {

?????????? e.printStackTrace();

?????? }finally{

?????????? return false;

?????? }

??? }

?

??? public static void main(String[] args) {

??????

?????? System.out.println(test());

??? }

答:打印出false

?

11)下面代码合法么?

interface b {

??? private void t();

protected void t1();

?

}

答:不合法,接口里面只能有public或者friendly所修饰的方法。

?

12)下面代码合法么?会输出什么?

public class test extends a implements b,c {

?

??? public void t() {

?????? System.out.println("test");

??? }

??? public static void main(String[] args) {

?????? test t=new test();

?????? t.t();

??? }

???

}

class a {

??? private void t() {

System.out.println("a");

??? }

}

interface b {

??? ?void t();

}

interface c {

??? ?void t();

}

答:合法,会输出test

?

13)会输出什么?

public class test {

??? public static void main(String[] args) {

?????? test t=new test();

?????? System.out.println(t.addOne(10));

??? }

??? public int addOne(int x) {

??? ??? return ++x;

??? }

}

答:输出11

?

14)下面的方法是正确的方法重载么?

public void t() {}

public void t() throws Exception {}

答:错误

?

15)下面代码会输出什么?

public static void main(String[] args) {
??System.out.println(7&4);
?}

答案:4

?

这是我在面试当中遇到的一些java笔试题,有些是自己的根据题意设计的题目,感觉比较经典,所以共享出来给大家分享。