资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java面试笔试题代码 java面试题试卷

JAVA笔试题(与构造函数、静态变量有关)

1.amethod()方法被重写

10年积累的成都网站建设、网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有通山免费网站建设让你可以放心的选择与我们合作。

2.getMyClass()中输出一次,main()中再输出一次

谁有JAVA程序员面试的程序题啊?

一、 选择题:(每题1分,共30分)

1. Which statement about the garbage collection mechanism are true? (B)

A.Garbage collection require additional program code in cases where multiple threads are running.

B.The programmer can indicate that a reference through a local variable is no longer of interest.

C.The programmer has a mechanism that explicit and immediately frees the memory used by Java objects.

D.The garbage collection mechanism can free the memory used by Java Object at expectable time.

E.The garbage collection system never reclaims memory from objects while are still accessible to running user threads.

2. Give the following method: D

1)public void method( ){

2) String a,b;

3) a=new String(“hello world”);

4) b=new String(“game over”);

5) System.out.println(a+b+”ok”);

6) a=null;

7) a=b;

8)System.out.println(a);

9) }

In the absence of compiler optimization, which is the earliest point the object a referred is definitely hand to be garbage collection. ()

A. before line 3 B.before line 5 C. before line 6 D.before line 7 E. Before line 9

3. Give the following code:

public class Example{

public static void main(String args[] ){

int l=0;

do{

System.out.println(“Doing it for l is:”+l);

}while(l0)

System.out.println(“Finish”);

}

}

Which well be output: D

A. Doing it for l is 3 B. Doing it for l is 1C Doing it for l is 2

D. Doing it for l is 0 E. Doing it for l is –1F. Finish

4. 以下(C)是JAVA的保留字。(选择一项)

A.Java B.Hello C.class D.Class

5. 下面程序运行之后,变量x的值是(B). (选择一项)

......

//swap方法的声明

public static void swap(int a,int b){

int t=a;

a=b;

b=t;

}

//main方法

public static void main(String args[]){

int x=2;

int y=3;

swap(x,y);

}

A、2 B、3 C、4 D、6

6. 下面变量var的作用域范围是(D)。(选择一项)

1. //....

2. int x;

3. switch(x){

4. case 0:

5. {

6. int var;

7. //process

8. }

9. break;

10. case 1:

11. {

12. int var1;

13. //process

14. }

15. break;

16. }

A、1和16行之间 B、4和8行之间 C、6和8行之间 D、6和14行之间

7. 以下的类(接口)定义中正确的是(A)。(选择一项)

A.

public class a {

private int x;

public getX(){

return x;

}

}

B.

public abstract class a {

private int x;

public abstract int getX();

public int aMethod(){

return 0;

}

}

C.

public class a {

private int x;

public abstract int getX();

}

D.

public interface interfaceA{

private int x;

public int getX(){

return x;

}

}

8. 已知A类被打包在packageA , B类被打包在packageB ,且B类被声明为public ,且有一个成员变量x被声明为protected控制方式 。C类也位于packageA包,且继承了B类 。则以下说话正确的是(C)(选择一项)

A. A类的实例不能访问到B类的实例

B. A类的实例能够访问到B类一个实例的x成员

C. C类的实例可以访问到B类一个实例的x成员

D. C类的实例不能访问到B类的实例

9. 编译并运行下面的Java代码段:

char c='a';

switch (c) {

case 'a':

System.out.println("a");

default:

System.out.println("default");

}

输出结果是(B)。(选择一项)

A.代码无法编译,因为switch语句没有一个合法的表达式

B.a Default

C.a

D.default

10. 在Java中,关于final关键字的说法正确的是(AC)。(选择两项)

A.如果修饰变量,则一旦赋了值,就等同一个常量

B.如果修饰类,则该类只能被一个子类继承

C.如果修饰方法,则该方法不能在子类中被覆盖

D.如果修饰方法,则该方法所在的类不能被继承

11. 在Java中,要想使只有定义该类所在的包内的类可以访问该类,应该用(A)关键字。(选择一项)

A.不需要任何关键字

B.private

C.final

D.protected

12. 在Java中,下面关于包的陈述中正确的是(AD)。(选择两项)

A.包的声明必须是源文件的第一句代码

B.包的声明必须紧跟在import语句的后面

C.只有公共类才能放在包中

D.可以将多个源文件中的类放在同一个包中

13. public static void main方法的参数描述是:AB(请选择2个正确答案)

A.String args[]

B.String[] args

C.Strings args[]z

D.String args

14. 编译并运行下面的Java程序:B

class A{

int var1=1;

int var2;

public static void main(String[] args){

int var3=3;

A a=new A();

System.out.println(a.var1+a.var2+var3);

}

}

将产生()结果。(选择一项)

A.0

B.4

C.3

D.代码无法编译,因为var2根本没有被初始化

15. 编译,运行下列代码后的结果是:D(选择一项)

public class Test {

public static void main (String args []) {

int age;

age = age + 1;

System.out.println("The age is " + age);

}

}

A.编译,运行后没有输出

B.编译,运行后输出:The age is 1

C.能通过编译,但运行时产生错误

D.不能通过编译

16. 下列哪些表达式返回true:AB(请选择2个正确答案 )

A."john" == "john"

B."john".equals("john")

C."john" = "john"

D."john".equals(new Button("john"))

17. Give the code fragment:B

1)switch(x){

2) case 1: System.out.println(“Test 1”);break;

3) case 2:

4) case 3: System.out.println(“Test 2”);break;

5) default: System.out.println(“end”);

6) }

which value of x would cause “Test 2” to the output:

A. 1B. 2C. 3D. default

18. Given the following class definition: A

class A{

protected int i;

A(int i){

this.i=i;

}

}

which of the following would be a valid inner class for this class?

Select all valid answers:

A. class B{

}

B.class B extends A{

}

C.class B extends A{

B(){System.out.println(“i=”+i);}

}

D.class B{

class A{}

}

E.class A{}

19. The following code is entire contents of a file called Example.java,causes precisely one error during compilation: D

1) class SubClass extends BaseClass{

2) }

3) class BaseClass(){

4) String str;

5) public BaseClass(){

6) System.out.println(“ok”);}

7) public BaseClass(String s){

str=s;}

9) }

10) public class Example{

11) public void method(){

12) SubClass s=new SubClass(“hello”);

13) BaseClass b=new BaseClass(“world”);

14) }

15) }

Which line would be cause the error?

A. 9 B. 10 C. 11 D.12

20. Float s=new Float(0.9F);

Float t=new Float(0.9F);

Double u=new Double(0.9);

Which expression’s result is true? B

A. s= =t B. s.equals(t) C. s= =u D. t.equals(u)

21. What happens when you try to compile and run the following program? A

class Mystery{

String s;

public static void main(String[] args){

Mystery m=new Mystery();

m.go();

}

void Mystery(){

s=”constructor”;

}

void go(){

System.out.println(s);

}

}

A.this code will not compile

B.this code compile but throws an exception at runtime

C.this code runs but nothing appears in the standard output

D.this code runs and “constructor” in the standard output

E.this code runs and writes ”null” in the standard output

22. Give the following class: B

public class Example{

String str=new String(“good”);

char ch[]={‘a’,’b’,’c’};

public static void main(String args[]){

Example ex=new Example();

ex.change(ex.str,ex.ch);

System.out.println(ex.str+”and”+ex.ch[0] +ex.ch[1] +ex.ch[2]);

}

public void change(String str,char ch[]){

str=”test ok”;ch[0]=’g’;

}

}

Which is the output:

A. good and abc B. good and gbc C. test ok and abc D. test ok and gbc

23. Which correctly create a two dimensional array of integers? CD

A.int a[][] = new int[][];

B.int a[10][10] = new int[][];

C.int a[][] = new int[10][10];

D.int [][]a = new int[10][10];

E.int []a[] = new int[10][10];

24. Examine the following code which includes an inner class: B

public final class Test4 implements A{

class Inner{

void test(){

if (Test4.this.flag);{

sample();

}

}

private boolean flag=false;

}

public void sample(){

System.out.println(“Sample”);

}

public Test4(){

(new Inner()).test();

}

public static void main(String args[]){

new Test4();

}

}

What is the result:

A.Print out “Sample”

B.Program produces no output but terminates correctly.

C. Program does not terminate.

E.The program will not compile

25. Which statement is true about an inner class? D

A.It must be anonymous

B.It can not implement an interface

C.It is only accessible in the enclosing class

D.It can access any final variables in any enclosing scope

26. public class Test{ A

public int aMethod(){

static int i=0;

i++;

return i;

}

public static void main(String args[]){

Test test = new Test();

test.aMethod();

int j=test.aMethod();

System.out.println(j);

}

}

What is the result?

A.Compilation will fail

B.Compilation will succeed and the program will print”0”.

C.Compilation will succeed and the program will print”1”.

D.Compilation will succeed and the program will print”2”

27. class Super{ D

public int i=0;

public Super(String text){

i=1;}

}

public class Sub extends Super{

public Sub(String text){

i=2;

}

public static void main(String ag[]){

Sub sub=new Sub(“Hello”);

System.out.println(sub.i);

}

}

What is the result?

A.Compilation will fail

B.Compilation will succeed and the program will print”0”.

C.Compilation will succeed and the program will print”1”

D.Compilation will succeed and the program will print”2”

28. Given A

Package foo;

public class Outer{

public static class Inner{

}

}

Which statement is true?

A.An instance of the Inner class can be constructed with “new Outer.Inner():

B.An instance of the Inner class cannot be constructed outside of package foo;

C.An instance of the Inner class can only be constructed from within the Outer class.

D.From within the package bar, an instance of the Inner class can be constructed with “new Inner()”

29. What is the result? E

public class Test{

static String s=”Hello”;

public static void main(String args[]){

Test t=new Test();

t.methodA(s);

System.out.println(s);

}

void methodA(String s){

s+=”World”;

}

}

A.print “Hello”

B.print “World”

C.print “Hello World”

D.It does not compile

30. what is the result? C

public class Test{

public static void main(String args[]){

String s=new String(”true”);

Boolean b=new Boolean(true);

If(s.equals(b))

{ System.out.println(“hello”);}

}

}

A.print “hello”

B.compile error at line 6

C.compile succeed but print nothing

D.compile succeed but throw exception at runtime

1、 B

2、 D

3、 D

4、 C

5、 B

6、 D

7、 A

8、 C

9、 B

10、 AC

11、 A

12、 AD

13、 AB

14、 B

15、 D

16、 AB

17、 B

18、 A

19、 D

20、 B

21、 A

22、 B

23、 CD

24、 B

25、 D

26、 A

27、 D

28、 A

29、 E

30、 C

补充一下:真正的面试时的笔试题,很少是选择题的,大多都是些问答题或者直接让你写程序的。如有你需要正规大公司的面试题,我这有一些word文档,可以给你发邮件。

求java考题,笔试题

Java面向对象

1. super()与this()的区别?

This():当前类的对象,super父类对象。

Super():在子类访问父类的成员和行为,必须受类继承规则的约束

而this他代表当前对象,当然所有的资源都可以访问.

在构造函数中,如果第一行没有写super(),编译器会自动插入.但是如果父类没有不带参数的构造函数,或这个函数被私有化了(用private修饰).此时你必须加入对父类的实例化构造.而this就没有这个要求,因为它本身就进行实例化的构造.

而在方法中super和this使用的方法就差不多了.只不过super 要考虑是否能访问其父类的资源.

2. 作用域public,protected,private,以及不写时的区别?

Public:不同包、 同一包、 类内都可用

Private: 类内

Protected:不同包的子类、同一包、 类内都可用

不写时: 同一包内、类内

3. 编程输出如下图形。

* * * * *

* * * *

* * *

* *

*

代码如下:

public class Print {

publicstatic void main(String[] args) {

for(int i = 0; i 5; i++) {

for(int j = 5; j i; j--) {

System.out.print("*");

}

System.out.println();

}

}

}

4. JAVA的事件委托机制和垃圾回收机制

Java事件委托机制的概念,一个源产生一个事件并将它送到一个或多个监听器那里。在这种方案中,监听器简单的等待,直到它收到一个事件。一旦事件被接受,监听器将处理这个事件,然后返回。

垃圾回收机制垃圾收集是将分配给对象但不再使用的内存回收或释放的过程。如果一个对象没有指向它的引用或者其赋值为null,则次对象适合进行垃圾回收

5. 在JAVA中,如何跳出当前的多重嵌套循环?

用break; return 方法。

6. 什么是java序列化,如何实现java序列化?(写一个实例)

序列化:处理对象流的机制,所谓对象流也就是将对象的内容进行流化。可以对流化后的对象进行读写操作,也可将流化后的对象传输于网络之间。序列化是为了解决在对对象流进行读写操作时所引发的问题。

序列化的实现:将需要被序列化的类实现Serializable接口,该接口没有需要实现的方法,implementsSerializable只是为了标注该对象是可被序列化的,然后使用一个输出流(如:FileOutputStream)来构造一个ObjectOutputStream(对象流)对象,接着,使用ObjectOutputStream对象的writeObject(Object obj)方法就可以将参数为obj的对象写出(即保存其状态),要恢复的话则用输入流。

7. 一个".java"源文件中是否可以包括多个类(不是内部类)?有什么限制?

可以。如果这个类的修饰符是public,其类名与文件名必须相同。

8. 排序都有哪几种方法?请列举。用JAVA实现一个快速排序?

排序的方法有:插入排序(直接插入排序、希尔排序),交换排序(冒泡排序、快速排序),选择排序(直接选择排序、堆排序),归并排序,分配排序(箱排序、基数排序)

快速排序的伪代码。

9. Overload和Override的区别。Overloaded的方法是否可以改变返回值的类型?

重写Override,子类覆盖父类的方法,将子类传与父类的引用调用的还是子类的方法。

重载Overloading 一个类多个方法,名称相同,参数个数类型不同。

两者都是Java多态性的不同表现。

Overloaded的方法是可以改变返回值的类型。

1, public class Ctest(){

Public static void main(){

System.out.prinln(8+8+”88”+8+8);

}

} 168888

(方法的重写Overriding和重载Overloading是Java多态性的不同表现。重写Overriding是父类与子类之间多态性的一种表现,重载Overloading是一个类中多态性的一种表现。如果在子类中定义某方法与其父类有相同的名称和参数,我们说该方法被重写 (Overriding)。子类的对象使用这个方法时,将调用子类中的定义,对它而言,父类中的定义如同被“屏蔽”了。如果在一个类中定义了多个同名的方法,它们或有不同的参数个数或有不同的参数类型,则称为方法的重载(Overloading)。

Overloaded的方法是可以改变返回值的类型。)

10. Final类有什么特点?

属性常量 方法不可以overridding 类不可以继承

11. 继承时候类的执行顺序问题,一般都是选择题,问你将会打印出什么?

答:父类:

package test;

public class FatherClass {

public FatherClass() {

System.out.println("FatherClassCreate");

}

}

子类:

package test;

import test.FatherClass;

public class ChildClass extends FatherClass{

public ChildClass() {

System.out.println("ChildClassCreate");

}

public static void main(String[] args) {

FatherClass fc = new FatherClass();

ChildClass cc = new ChildClass();

}

}

输出结果:

C:java test.ChildClass

FatherClass Create

FatherClass Create

ChildClass Create

12. 内部类的实现方式?

package test;

public class OuterClass {

private class InterClass {

Public Interlass(){

System.out.println("InterClassCreate");

}

}

public OuterClass(){

InterClass ic = new InterClass();

System.out.println("OuterClassCreate");

}

public static void main(String[] args){

OuterClass oc = new OuterClass();

}

}

输出结果:

C:java test/OuterClass InterClass Create OuterClass Create

13. 用JAVA实现一种排序,JAVA类实现序列化的方法(二种)?

14. 如在COLLECTION框架中,实现比较要实现什么样的接口?

15. 用插入法进行排序代码如下

package test;

import java.util.*;

class InsertSort {

ArrayList al;

public InsertSort(int num,int mod) {

al = new ArrayList(num);

Random rand = new Random();

System.out.println("The ArrayList SortBefore:");

for (int i=0;inum ;i++ ){

al.add(new Integer(Math.abs(rand.nextInt())% mod + 1));

System.out.println("al["+i+"]="+al.get(i));

}

}

public void SortIt(){

Integer tempInt;

int MaxSize=1;

for(int i=1;ial.size();i++){

tempInt = (Integer)al.remove(i);

if(tempInt.intValue()=((Integer)al.get(MaxSize-1)).intValue()){

al.add(MaxSize,tempInt);

MaxSize++;

System.out.println(al.toString());

} else {

for (int j=0;jMaxSize ;j++ ){

if(((Integer)al.get(j)).intValue()=tempInt.intValue()){

al.add(j,tempInt);

MaxSize++;

System.out.println(al.toString());

break;

}

}

}

}

System.out.println("The ArrayList SortAfter:");

for(int i=0;ial.size();i++) {

System.out.println("al["+i+"]="+al.get(i));

}

}

public static void main(String[] args) {

InsertSort is = new InsertSort(10,100);

is.SortIt();

}

}

JAVA类实现序例化的方法是实现java.io.Serializable接口

Collection框架中实现比较要实现Comparable 接口和 Comparator 接口

16. 编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。但是要保证汉字不被截半个,如"我ABC"4,应该截为"我AB",输入"我ABC汉DEF",6,应该输出为"我ABC"而不是"我ABC+汉的半个"。

public static void split(String source,intnum) throws Exception{

intk=0;

Stringtemp="";

for(int i = 0; i source.length(); i++){

byte[]b=(source.charAt(i)+"").getBytes();

k=k+b.length;

if(knum){

break;

}

temp=temp+source.charAt(i);

}

System.out.println(temp);

}

15、Java编程,打印昨天的当前时刻

public class YesterdayCurrent{

public void main(String[] args){

Calendar cal = Calendar.getInstance();

cal.add(Calendar.DATE, -1);

System.out.println(cal.getTime());

}

}

16、文件读写,实现一个计数器

public int getNum(){

int i = -1;

try{

String stri="";

BufferedReader in = new BufferedReader(newFileReader(f));

while((stri=in.readLine())!=null){

i = Integer.parseInt(stri.trim());

}

in.close();

}catch(Exception e){

e.printStackTrace();

}

return i;

}

public void setNum(){

int i = getNum();

i++;

try{

PrintWriter out=new PrintWriter(newBufferedWriter(new FileWriter(f,false)));

out.write(String.valueOf(i)); //可能是编码的原因,如果直接写入int的话,将出现java编码和windows编码的混乱,因此此处写入的是String

out.close() ;

}catch(Exception e){

e.printStackTrace();

}

}

17、指出下面程序的运行结果。

class A{

static{

System.out.print("1");

}

public A(){

System.out.print("2");

}

}

class B extends A{

static{

System.out.print("a");

}

public B(){

System.out.print("b");

}

}

public class Hello{

public static void main(String[] ars){

A ab = new B(); //执行到此处,结果: 1a2b

ab = new B(); //执行到此处,结果: 1a2b2b

}

}注:类的static 代码段,可以看作是类首次加载(被虚拟机加载)执行的代码,而对于类的加载,首先要执行其基类的构造,再执行其本身的构造

18、抽象类和接口的区别?

(1)接口可以被多重implements,抽象类只能被单一extends(2)接口只有定义,抽象类可以有定义和实现(3)接口的字段定义默认为:publicstatic final, 抽象类字段默认是"friendly"(本包可见)

当功能需要累积时用抽象类,不需要累积时用接口。

19、什么是类的反射机制?

通过类(Class对象),可以得出当前类的fields、method、construtor、interface、superClass、modified等,同是可以通过类实例化一个实例、设置属性、唤醒方法。Spring中一切都是返射、struts、hibernate都是通过类的返射进行开发的。

20、类的返射机制中的包及核心类?

①java.lang.Class②java.lang.refrection.Method③java.lang.refrection.Field

④java.lang.refrection.Constructor⑤java.lang.refrection.Modifier⑥java.lang.refrection.Interface

21、得到Class的三个过程是什么?

①对象.getClass()②类.class或Integer.type(int) Integer.class(java.lang.Integer)③Class.forName();

22、如何唤起类中的一个方法?

①产生一个Class数组,说明方法的参数②通过Class对象及方法参数得到Method③通过method.invoke(实例,参数值数组)唤醒方法

23、如何将数值型字符转换为数字(Integer,Double)?

Integer.parseInt(“1234”) Double.parseDouble(“123.2”)

24、如何将数字转换为字符?

1+”” 1.0+””

25、如何去小数点前两位,并四舍五入。

double d=1256.22d; d=d/100; System.out.println(Math.round(d)*100);

26、如何取得年月日,小时分秒?

Calendar c=Calendar.getInstance();

c.set(Calendar.YEAR,2004);

c.set(Calendar.MONTH,0);

c.set(Calendar.DAY_OF_MONTH,31);

System.out.println(c.get(Calendar.YEAR)+" "+(c.get(Calendar.MONTH)+1)+" "+c.get(Calendar.DAY_OF_MONTH));

27、如何取得从1970年到现在的毫秒数

Java.util.Date dat=new Date(); long now=dat.getTime();

或System.currentTimeMillis()

28、如何获取某个日期是当月的最后一天?

当前日期加一天,若当前日期与结果的月份不相同,就是最后一天。

取下一个月的第一天,下一个月的第一天-1

public static void main(String[] args){

Calendarc=Calendar.getInstance();

c.set(Calendar.YEAR,2004);

c.set(Calendar.MONTH,0);

c.set(Calendar.DAY_OF_MONTH,30);

Calendarc1=(Calendar)c.clone();

System.out.println(c.get(Calendar.YEAR)+""+(c.get(Calendar.MONTH)+1)+" "+c.get(Calendar.DAY_OF_MONTH));

c.add(Calendar.DAY_OF_MONTH,1);

if(c.get(Calendar.MONTH)!=c1.get(Calendar.MONTH)){

System.out.println("是最后一天");

}else{

System.out.println("不是取后一天");

}

}

29、如何格式化日期?

Import java.text. SimpleDateFormat;

SimpleDateFormat sdf=newSimpleDateFormat("yyyy-MM-dd hh:mm:ss");

Date dat=new Date();

String str=sdf.format(dat); //把日期转化为字符串

System.out.println(str);

Java.util.Date d1=sdf.parse(“yyyy-mm-dd”); //将字符串转化为日期

30、编码转换,怎样实现将GB2312编码的字符串转换为ISO-8859-1编码的字符串。

String a=new String("中".getBytes("gb2312"),"iso-8859-1");

String a=new String("中".getBytes("iso-8859-1"));

应该是String a=new String("中".getBytes("gb2312"),"iso-8859-1");

String a1=newString(a.getBytes("iso-8859-1"));


本文标题:java面试笔试题代码 java面试题试卷
标题网址:http://cdkjz.cn/article/ddcseoh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220