java面试葵花宝典(整理版)
- 格式:doc
- 大小:86.00 KB
- 文档页数:14
JAVA基础部分面试题库第一部分:选择题1) 哪个不是面向对象的特征()a) 封装性b) 继承性c) 多态性d) 健壮性2) 编译JA V A文件的命令是()。
a) Java b) Javac c) Javadb d) Javaw3) JA V A源文件的扩展名是()。
a) Class b) exe c) java d) dll4) JA V A内部使用的编码格式是()。
a) UTF-8 b) ASCII c) UNICODE d) ISO8859-15) 下列变量名称不合法的是()a) Index b) $bc3& c) _cccde d) 34#bc56) 下边对基本数据类型的说明正确的是()a) Int可以自动转换为BYTE类型b) DOUBLE类型的数据占用2个字节c) JA V A中一共有4类8种基本数据类型d) JA V A中一共有3类8种基本数据类型7) 下列不是JA V A关键字的是()a) goto b) if c) count d) private8) 下列变量声明中正确的是()a) Float f = 3.13 b) Boolean b = 0c) Int number = 5 d) Int x Byte a =9) public void go() {String o = "";z:for(int x = 0; x < 3; x++) {for(int y = 0; y < 2; y++) {if(x==1) break;if(x==2 && y==1) break z;o = o + x + y;}}System.out.println(o);}程序的执行结果是()a) 00b) 0001c) 000120d) 00012021e) Compilation fails.f) An exception is thrown at runtime10) class Payload {private int weight;public Payload (int w) { weight = w; }public void setWeight(int w) { weight = w; }public String toString() { return Integer.toString(weight); }}public class TestPayload {static void changePayload(Payload p) { /* insert code */ }public static void main(String[] args) {Payload p = new Payload(200);p.setWeight(1024);changePayload(p);System.out.println("p is " + p);}}Insert code处写入哪句话,可以使程序的输出结果是420()a) p.setWeight(420);b) p.changePayload(420);c) p = new Payload(420);d) Payload.setWeight(420);e) p = Payload.setWeight(420);11) void waitForSignal() {Object obj = new Object();synchronized (Thread.currentThread()) {obj.wait();obj.notify();}}这个程序片段的运行结果是什么()a) This code can throw an InterruptedException.b) This code can throw an IllegalMonitorStateException.c) This code can throw a TimeoutException after ten minutes.d) Reversing the order of obj.wait() and obj.notify() might cause this method to complete normally.12) public class Threads2 implements Runnable {public void run() {System.out.println("run.");throw new RuntimeException("Problem");}public static void main(String[] args) {Thread t = new Thread(new Threads2());t.start();System.out.println("End of method.");} }运行结果是什么,请选择2个()a) ng.RuntimeException: Problemb) run. ng.RuntimeException: Problemc) End of method. ng.RuntimeException: Problemd) End of method. run. ng.RuntimeException: Probleme) run. ng.RuntimeException: Problem End of method.13) 下边哪2句话的描述是正确的()a) It is possible for more than two threads to deadlock at onceb) The JVM implementation guarantees that multiple threads cannot enter into a deadlocked state.c) Deadlocked threads release once their sleep() method's sleep duration has expired.d) Deadlocking can occur only when the wait(), notify(), and notifyAll() methods are used incorrectly.e) It is possible for a single-threaded application to deadlock if synchronized blocks are used incorrectly.f) f a piece of code is capable of deadlocking, you cannot eliminate the possibility of deadlocking by inserting invocations of Thread.yield().14) public class Threads2 implements Runnable {public void run() {System.out.println("run.");throw new RuntimeException("Problem");}public static void main(String[] args) {Thread t = new Thread(new Threads2());t.start();System.out.println("End of method.");}}程序运行结果是,选择2个()a) ng.RuntimeException: Problemb) run. ng.RuntimeException: Problem End of methodc) End of method. ng.RuntimeException: Problemd) End of method. run. ng.RuntimeException: Problem15) void waitForSignal() {Object obj = new Object();synchronized (Thread.currentThread()) {obj.wait();obj.notify();}}下列那句描述是正确的()a) This code can throw an InterruptedException.b) This code can throw an IllegalMonitorStateException.c) This code can throw a TimeoutException after ten minutes.d) This code does NOT compile unless "obj.wait()" is replaced with "((Thread) obj).wait()".16) 11. class PingPong2 {synchronized void hit(long n) {for(int i = 1; i < 3; i++)System.out.print(n + "-" + i + " ");}}public class Tester implements Runnable {static PingPong2 pp2 = new PingPong2();public static void main(String[] args) {new Thread(new Tester()).start();new Thread(new Tester()).start();}. public void run() { pp2.hit(Thread.currentThread().getId()); }}运行结果是()a) The output could be 5-1 6-1 6-2 5-2b) The output could be 6-1 6-2 5-1 5-2c) The output could be 6-1 5-2 6-2 5-1d) The output could be 6-1 6-2 5-1 7-117) 1. public class Threads4 {public static void main (String[] args) {new Threads4().go();}public void go() {Runnable r = new Runnable() {public void run() { System.out.print("foo");} };Thread t = new Thread(r);t.start();t.start();}}运行结果是()a) Compilation fails.b) An exception is thrown at runtime.c) The code executes normally and prints "foo".d) The code executes normally, but nothing is printed.18) 11. public class Barn {public static void main(String[] args) {new Barn().go("hi", 1);new Barn().go("hi", "world", 2); }public void go(String... y, int x) {System.out.print(y[y.length - 1] + " "); }}运行结果是()a) hi hib) hi worldc) Compilation failsd) An exception is thrown at runtime.19) 10. class Nav{. public enum Direction { NORTH, SOUTH, EAST, WEST }}public class Sprite{// insert code here}哪句代码放到14行,程序可以正常编译()a) Direction d = NORTH;b) Nav.Direction d = NORTH;c) Direction d = Direction.NORTH;d) Nav.Direction d = Nav.Direction.NORTH;20) 11. public class Rainbow {public enum MyColor {RED(0xff0000), GREEN(0x00ff00), BLUE(0x0000ff);private final int rgb;MyColor(int rgb) { this.rgb = rgb; }public int getRGB() { return rgb; }};public static void main(String[] args) {// insert code here }}哪句代码放到19行,程序可以正常编译()a) MyColor skyColor = BLUE;b) MyColor treeColor = MyColor.GREEN;c) Compilation fails due to other error(s) in the code.d) MyColor purple = MyColor.BLUE + MyColor.RED;21) 5. class Atom {Atom() { System.out.print("atom "); }}class Rock extends Atom {Rock(String type) { System.out.print(type); }}public class Mountain extends Rock {Mountain() {super("granite ");new Rock("granite ");}public static void main(String[] a) { new Mountain(); }}运行结果是()a) Compilation failsb) atom granite granitec) An exception is thrown at runtime.d) atom granite atom granite22) 1. interface TestA { String toString(); }public class Test {public static void main(String[] args) {4System.out.println(new TestA() {public String toString() { return "test"; }}}}运行结果是()a) testb) nullc) An exception is thrown at runtime.d) Compilation fails because of an error in line 1.23) 11. public static void parse(String str) {try {float f = Float.parseFloat(str);} catch (NumberFormatException nfe) {f = 0; } finally {17. System.out.println(f);}}public static void main(String[] args) {parse("invalid");}运行结果是()a) 0.0b) Compilation fails.c) A ParseException is thrown by the parse method at runtimed) A NumberFormatException is thrown by the parse method at runtime24) 1. public class Blip {protected int blipvert(int x) { return 0; }}class Vert extends Blip {// insert code here}下列哪5个方法放到代码第五行,可以让程序编译正确,选择5个()a) public int blipvert(int x) { return 0; }b) private int blipvert(int x) { return 0; }c) private int blipvert(long x) { return 0; }d) protected long blipvert(int x) { return 0; }e) protected int blipvert(long x) { return 0; }f) protected long blipvert(long x) { return 0; }g) protected long blipvert(int x, int y) { return 0; }25) 1. class Super {private int a;protected Super(int a) { this.a = a; }} ...class Sub extends Super {public Sub(int a) { super(a); }public Sub() { this.a = 5; }}下列哪2条语句是正确的()a) Change line 2 to: public int a;b) Change line 2 to: protected int a;c) Change line 13 to: public Sub() { this(5); }d) Change line 13 to: public Sub() { super(5); }e) Change line 13 to: public Sub() { super(a); }26) package test;class Target {public String name = "hello";}如果可以直接修改和访问name这个变量,下列哪句话是正确的a) any classb) only the Target classc) any class in the test packaged) any class that extends Target27) 11. abstract class Vehicle { public int speed() { return 0; }class Car extends Vehicle { public int speed() { return 60; }class RaceCar extends Car { public int speed() { return 150; } ...RaceCar racer = new RaceCar();Car car = new RaceCar();Vehicle vehicle = new RaceCar();System.out.println(racer.speed() + ", " + car.speed()+ ", " + vehicle.speed());运行结果是()a) 0, 0, 0b) 150, 60, 0c) Compilation failsd) 150, 150, 150e) An exception is thrown at runtime28) 5. class Building { }public class Barn extends Building {public static void main(String[] args) {Building build1 = new Building();Barn barn1 = new Barn();Barn barn2 = (Barn) build1;Object obj1 = (Object) build1;String str1 = (String) build1;Building build2 = (Building) barn1;} 15. }运行结果是()a) If line 10 is removed, the compilation succeeds.b) If line 11 is removed, the compilation succeedsc) If line 12 is removed, the compilation succeeds.d) If line 13 is removed, the compilation succeeds.e) More than one line must be removed for compilation to succeed.29) 29.Given:class Money {private String country = "Canada";public String getC() { return country; }}class Yen extends Money {public String getC() { return super.country; }}public class Euro extends Money {public String getC(int x) { return super.getC(); }public static void main(String[] args) {System.out.print(new Yen().getC() + " " + new Euro().getC());} 33. }运行结果是()a) Canadab) null Canadac) Canada nulld) Compilation fails due to an error on line 26e) Compilation fails due to an error on line 2930) 13. import java.io.*;class Food implements Serializable {int good = 3;}class Fruit extends Food {int juice = 5;}public class Banana extends Fruit {int yellow = 4;public static void main(String [] args) {Banana b = new Banana(); Banana b2 = new Banana();b.serializeBanana(b); // assume correct serializationb2 = b.deserializeBanana(); // assume correctSystem.out.println("restore "+b2.yellow+ b2.juice+b2.good);}// more Banana methods go here}运行结果是()a) restore 400b) restore 403c) restore 453d) Compilation fails.e) An exception is thrown at runtime.31) 11. double input = 314159.26;NumberFormat nf = NumberFormat.getInstance(Locale.ITALIAN); String b;//insert code here下列哪条语句可以设置变量b的值为314.159,26()a) .b = nf.parse( input );b) b = nf.format( input );c) b = nf.equals( input );d) b = nf.parseObject( input );32) 1. public class TestString1 {public static void main(String[] args) {String str = "420";str += 42;System.out.print(str);} 7. }输出结果是()a) 42b) 420c) 462d) 4204233) 22. StringBuilder sb1 = new StringBuilder("123");String s1 = "123";// insert code hereSystem.out.println(sb1 + " " + s1);下列哪句代码放到程序的第24行,可以输出"123abc 123abc"()a) sb1.append("abc"); s1.append("abc");b) sb1.append("abc"); s1.concat("abc");c) sb1.concat("abc"); s1.append("abc");d) sb1.concat("abc"); s1.concat("abc");e) sb1.append("abc"); s1 = s1.concat("abc");f) sb1.concat("abc"); s1 = s1.concat("abc");g) sb1.append("abc"); s1 = s1 + s1.concat("abc");34) 1. public class LineUp {public static void main(String[] args) {double d = 12.345;// insert code here}}下列哪句代码放到程序的第4行,可以输出| 12.345|? ()a) System.out.printf("|%7d| \n", d);b) System.out.printf("|%7f| \n", d);c) System.out.printf("|%3.7d| \n", d);d) System.out.printf("|%3.7f| \n", d);e) System.out.printf("|%7.3d| \n", d);f) System.out.printf("|%7.3f| \n", d);35) 11. public class Test {public static void main(String [] args) {int x = 5;boolean b1 = true;boolean b2 = false;if ((x == 4) && !b2 )System.out.print("1 ");System.out.print("2 ");if ((b2 = true) && b1 )System.out.print("3 ");}}程序的输出结果是()a) 2b) 3c) 1 2d) 2 3e) 1 2 3f) Compilation fails.g) An exception is thrown at runtime.36) 10. interface Foo {}class Alpha implements Foo {}class Beta extends Alpha {}class Delta extends Beta {public static void main( String[] args ) {Beta x = new Beta();// insert code here} 18. }下列哪句代码放到第16行,可以导致一个ng.ClassCastException()a) Alpha a = x;b) Foo f = (Delta)x;c) Foo f = (Alpha)x;d) Beta b = (Beta)(Alpha)x;37) 22. public void go() {String o = "";z:. for(int x = 0; x < 3; x++) {for(int y = 0; y < 2; y++) {if(x==1) break;if(x==2 && y==1) break z;o = o + x + y;} }System.out.println(o);}当调用go方法时,输出结果是什么()a) 00b) 0001c) 000120d) 00012022138) 11. static void test() throws RuntimeException {try {System.out.print("test ");throw new RuntimeException();}catch (Exception ex) { System.out.print("exception "); }}public static void main(String[] args) {try { test(); }catch (RuntimeException ex) { System.out.print("runtime "); }System.out.print("end ");}程序运行结果是()a) test endb) Compilation fails.c) test runtime endd) test exception ende) .A Throwable is thrown by main at runtime39) 33. try {// some code here} catch (NullPointerException e1) {System.out.print("a");} catch (Exception e2) {System.out.print("b");} finally {System.out.print("c");}如果程序的第34行会抛出一些异常,程序的运行结果是()a) ab) bc) cd) ace) abc40) 31. // some code heretry {// some code here} catch (SomeException se) {// some code here} finally {// some code here} 哪种情况下37行的代码会执行,请选择3个()a) The instance gets garbage collected.b) The code on line 33 throws an exception.c) The code on line 35 throws an exception.d) The code on line 31 throws an exception.e) The code on line 33 executes successfully.41) 10. int x = 0;int y = 10;do {y--; ++x;} while (x < 5);System.out.print(x + "," + y);程序运行结果是()a) 5,6b) 5,5c) 6,5d) 6,642) public class Drink {public static void main(String[] args) {boolean assertsOn = true;assert (assertsOn) : assertsOn = true;if(assertsOn) {System.out.println("assert is on");} }}程序运行结果是()a) no outputb) no output assert is onc) assert is ond) assert is on An AssertionError is thrown.43) 11. Float pi = new Float(3.14f);if (pi > 3) {System.out.print("pi is bigger than 3. "); }else { System.out.print("pi is not bigger than 3. "); }finally {System.out.println("Have a nice day.");}程序运行结果是()a) Compilation fails.b) pi is bigger than 3c) An exception occurs at runtime.d) pi is bigger than 3. Have a nice day.e) pi is not bigger than 3. Have a nice day.44) 1. public class Boxer1{Integer i;int x;public Boxer1(int y) {x = i+y;System.out.println(x); }public static void main(String[] args) {new Boxer1(new Integer(4)); }}运行结果是()a) The value "4" is printed at the command line.b) Compilation fails because of an error in line 5.c) A NullPointerException occurs at runtime.d) A NumberFormatException occurs at runtime.45) 1. public class Person {private String name;public Person(String name) { = name; }public boolean equals(Person p) {return .equals(); 6. } 7. }下列哪条语句是正确的()a) The equals method does NOT properly override the Object.equals method.b) Compilation fails because the private attribute cannot be accessed in line 5.c) To work correctly with hash-based data structures, this class must also implement the hashCode method.d) When adding Person objects to a java.util.Set collection, the equals method in line 4 will prevent duplicates.46) 1. public class Score implements Comparable {private int wins, losses;public Score(int w, int l) { wins = w; losses = l; }public int getWins() { return wins; }public int getLosses() { return losses; }public String toString() {return "<" + wins + "," + losses + ">";}// insert code here}下列哪句代码放到第9行,程序可以正确编译()a) public int compareTo(Object o){/*more code here*/}b) public int compareTo(Score other){/*more code here*/}c) public int compare(Score s1,Score s2){/*more code here*/}d) public int compare(Object o1,Object o2){/*more code here*/}47) 3. public class Batman {int squares = 81;public static void main(String[] args) {new Batman().go();}void go() {incr(++squares);System.out.println(squares);}void incr(int squares) { squares += 10; }}程序运行结果是()a) 81 b) 82 c) 91 d) 9248) 15. public class Yippee {public static void main(String [] args) {for(int x = 1; x < args.length; x++) {System.out.print(args[x] + " "); 19. } 20. } 21. }在控制台依次输入两条命令java Yippeejava Yippee 1 2 3 4程序的运行结果是()a) No output is produced. 1 2 3b) No output is produced. 2 3 4c) No output is produced. 1 2 3 4d) An exception is thrown at runtime. 1 2 3e) An exception is thrown at runtime. 2 3 4f) An exception is thrown at runtime. 1 2 3 449) 13. public class Pass {public static void main(String [] args) {int x = 5;Pass p = new Pass();p.doStuff(x);System.out.print(" main x = " + x);}21. void doStuff(int x) {System.out.print(" doStuff x = " + x++);}}程序的运行结果是()a) Compilation fails.b) An exception is thrown at runtime.c) doStuff x = 6 main x = 6d) doStuff x = 5 main x = 5e) doStuff x = 5 main x = 6f) doStuff x = 6 main x = 550) 3. interface Animal { void makeNoise(); }class Horse implements Animal {Long weight = 1200L;public void makeNoise() { System.out.println("whinny"); }}public class Icelandic extends Horse {public void makeNoise() { System.out.println("vinny"); }public static void main(String[] args) {Icelandic i1 = new Icelandic(); Icelandic i2 = new Icelandic();12. Icelandic i3 = new Icelandic();i3 = i1; i1 = i2; i2 = null; i3 = i1;}}程序运行结束后,有几个对象符合垃圾回收的条件()a) 0 b) 1 c) 2 d) 3 e) 4 f) 651) 11. String[] elements = { "for", "tea", "too" };String first = (elements.length > 0) elements[0] : null;程序运行结果是()a) Compilation fails.b) An exception is thrown at runtime.c) The variable first is set to null.d) The variable first is set to elements[0].52) 31. class Foo {public int a = 3;public void addFive() { a += 5; System.out.print("f "); }}class Bar extends Foo {public int a = 8;public void addFive() { this.a += 5; System.out.print("b " ); }}主方法执行下列语句Foo f = new Bar();f.addFive();System.out.println(f.a); 程序运行结果是()a) b,3 b) b,8 c) b,13 d) f,3 e) f,8 f) f,13g) Compilation fails. h) An exception is thrown at runtime.53) 1. class ClassA {public int numberOfInstances;protected ClassA(int numberOfInstances) {this.numberOfInstances = numberOfInstances; }}public class ExtendedA extends ClassA {private ExtendedA(int numberOfInstances) {uper(numberOfInstances);}public static void main(String[] args) {ExtendedA ext = new ExtendedA(420);System.out.print(ext.numberOfInstances);}} 下列哪句描述是正确的()a) 420 is the output.b) An exception is thrown at runtime.c) All constructors must be declared publicd) Constructors CANNOT use the private modifier.e) Constructors CANNOT use the protected modifier.54) 11. class ClassA {}class ClassB extends ClassA {}class ClassC extends ClassA {}and:ClassA p0 = new ClassA();ClassB p1 = new ClassB();ClassC p2 = new ClassC();ClassA p3 = new ClassB();ClassA p4 = new ClassC();下边哪句代码是正确的,请选择3个()a) p0 = p1;b) p1 = p2;c) p2 = p4;d) p2 = (ClassC)p1;e) p1 = (ClassB)p3;f) p2 = (ClassC)p4;55) 5. class Thingy { Meter m = new Meter(); }class Component { void go() { System.out.print("c"); } }class Meter extends Component { void go() { System.out.print("m"); } } 9. class DeluxeThingy extends Thingy {public static void main(String[] args) {DeluxeThingy dt = new DeluxeThingy();dt.m.go();Thingy t = new DeluxeThingy();t.m.go();}}下边哪句描述是正确的,请选择2个()a) The output is mm.b) The output is mc.c) Component is-a Meter.d) Component has-a Meter.e) DeluxeThingy is-a Component.f) DeluxeThingy has-a Component.56) 10. interface Jumper { public void jump(); } ... class Animal {} ...class Dog extends Animal {Tail tail;} ...class Beagle extends Dog implements Jumper{public void jump() {}} ...class Cat implements Jumper{public void jump() {}}下边哪句描述是正确的,请选择3个()a) Cat is-a Animalb) Cat is-a Jumperc) Dog is-a Animald) Dog is-a Jumpere) Cat has-a Animalf) Beagle has-a Tailg) Beagle has-a Jumper57) 1. import java.util.*;public class WrappedString {private String s;public WrappedString(String s) { this.s = s; }public static void main(String[] args) {HashSeths = new HashSet();WrappedString ws1 = new WrappedString("aardvark"); WrappedString ws2 = new WrappedString("aardvark"); String s1 = new String("aardvark");String s2 = new String("aardvark");hs.add(ws1); hs.add(ws2); hs.add(s1); hs.add(s2); System.out.println(hs.size()); } }运行结果是()a) 0 b) 1 c) 2 d) 3 e) 4f) Compilation fails.d) An exception is thrown at runtime.58) 2. import java.util.*;public class GetInLine {public static void main(String[] args) { PriorityQueue pq = new PriorityQueue();pq.add("banana");pq.add("pear");pq.add("apple");System.out.println(pq.poll() + " " + pq.peek()); } }运行结果是()a) apple pear b) banana pearc) apple apple d) apple banana59) 3. import java.util.*;public class Mapit {public static void main(String[] args) {Set set = new HashSet();Integer i1 = 45;Integer i2 = 46;set.add(i1);set.add(i1);set.add(i2); System.out.print(set.size() + " ");set.remove(i1); System.out.print(set.size() + " ");i2 = 47;set.remove(i2); System.out.print(set.size() + " ");} 16. }程序运行结果是()a) 2 1 0 b) 2 1 1 c) 3 2 1 d) 3 2 260) 12. import java.util.*;public class Explorer1 {public static void main(String[] args) {TreeSet s = new TreeSet();TreeSet subs = new TreeSet();for(int i = 606; i < 613; i++)if(i%2 == 0) s.add(i);subs = (TreeSet)s.subSet(608, true, 611, true);. s.add(609);System.out.println(s + " " + subs);} 23. }运行结果是()a) Compilation fails.b) An exception is thrown at runtime.c) [608, 609, 610, 612] [608, 610]d) [608, 609, 610, 612] [608, 609, 610]e) [606, 608, 609, 610, 612] [608, 610]f) [606, 608, 609, 610, 612] [608, 609, 610]61) 3. import java.util.*;public class Quest {public static void main(String[] args) {String[] colors = {"blue", "red", "green", "yellow", "orange"}; Arrays.sort(colors);int s2 = Arrays.binarySearch(colors, "orange");int s3 = Arrays.binarySearch(colors, "violet");System.out.println(s2 + " " + s3);} 12. }运行结果是()a) 2 -1b) 2 -4c) 2 -5d) 3 -1e) 3 -4f) 3 -5g) Compilation fails.h) An exception is thrown at runtime62) 34. HashMap props = new HashMap();props.put("key45", "some value");props.put("key12", "some other value");props.put("key39", "yet another value");. Set s = props.keySet();// insert code here下列哪行代码放到39行是正确的()a) Arrays.sort(s);b) s = new TreeSet(s);c) Collections.sort(s);d) s = new SortedSet(s);63) 1. public class TestOne implements Runnable {public static void main (String[] args) throws Exception {Thread t = new Thread(new TestOne());t.start();System.out.print("Started");t.join();System.out.print("Complete");}public void run() {for (int i = 0; i < 4; i++) {System.out.print(i);} 13. } 14. }语系那个结果是()a) Compilation fails.b) An exception is thrown at runtime.c) The code executes and prints "StartedComplete".d) The code executes and prints "StartedComplete0123"e) The code executes and prints "Started0123Complete".64) 下边哪行代码是正确的,请选择3个()a) private synchronized Object o;b) void go() { synchronized() { /* code here */ }c) public synchronized void go() { /* code here */ }d) private synchronized(this) void go() { /* code here */ }e) void go() { synchronized(Object.class) { /* code here */ }f) void go() { Object o = new Object(); synchronized(o) { /* code here */ }65) 1. public class TestFive {private int x;public void foo() {int current = x;. x = current + 1; }public void go() {for(int i = 0; i < 5; i++) {new Thread() {public void run() {foo();System.out.print(x + ", ");} }.start();} }程序如何修改,可以保证输出1,2,3,4,5,选择2个()a) move the line 12 print statement into the foo() methodb) change line 7 to public synchronized void go() {c) change the variable declaration on line 2 to private volatile int x;d) wrap the code inside the foo() method with a synchronized( this ) blocke) wrap the for loop code inside the go() method with a synchronized block synchronized(this) { // for loop code here }66) 11. Runnable r = new Runnable() {public void run() {System.out.print("Cat");}};Thread t = new Thread(r) {public void run() {System.out.print("Dog");} };t.start(); 程序运行结果是()a) Cat b) Dog c) Compilation fails. d) The code runs with no output.67) 1. public class Threads5 {public static void main (String[] args) {new Thread(new Runnable() {public void run() {System.out.print("bar");}}).start();} 8. }程序运行结果是()a) Compilation fails.b) An exception is thrown at runtime.c) The code executes normally and prints "bar".d) The code executes normally, but nothing prints.68) 10. class One {void foo() { } . }class Two extends One {//insert method here}下列哪个方法放到第14行,程序可以编译正常,请选择3个()a) nt foo() { /* more code here */ }b) void foo() { /* more code here */ }。
1.在太平洋的一个小岛上生活着土人,他们不愿意被外人打扰,一天,一个探险家到了岛上,被土人抓住,土人的祭司告诉他,你临死前还可以有一个机会留下一句话,如果这句话是真的,你将被烧死,是假的,你将被五马分尸,可怜的探险家如何才能活下来?答:我将会被五马分尸2.有两根不均匀分布的香,每根香烧完的时间是一个小时,你能用什么方法来确定一段15分钟的时间?答:弄断,四个头一块点燃,烧完了一根,再弄断,继续上面的过程,保证同时有四个火点,烧完后就是十五分钟。
3.一只蜗牛从井底爬到井口,每天白天蜗牛要睡觉,晚上才出来活动,一个晚上蜗牛可以向上爬3尺,但是白天睡觉的时候会往下滑2尺,井深10尺,问蜗牛几天可以爬出来?答:第8天4.27个小运动员在参加完比赛后,口渴难耐,去小店买饮料,饮料店搞促销,凭三个空瓶可以再换一瓶,他们最少买多少瓶饮料才能保证一人一瓶?答:19瓶5.一群人开舞会,每人头上都戴着一顶帽子。
帽子只有黑白两种,黑的至少有一顶。
每个人都能看到其它人帽子的颜色,却看不到自己的。
主持人先让大家看看别人头上戴的是什幺帽子,然后关灯,如果有人认为自己戴的是黑帽子,就打自己一个耳光。
第一次关灯,没有声音。
于是再开灯,大家再看一遍,关灯时仍然鸦雀无声。
一直到第三次关灯,才有劈劈啪啪打耳光的声音响起。
问有多少人戴着黑帽子?答:3人6:一位卡车司机撞倒一个骑摩托车的人,卡车司机受重伤,摩托车手却没事?答案:卡车司机当时没有开车7:有一个人站在两条分岔口的路上,一条是通往诚实的国家,另一条是通往谎话的国家!(诚实国家的人,总是说实话,从不说谎话;而谎话国家的人,总是说谎话,从不说实话! )这个人要去诚实国,正不知怎么办?!突然他看见分岔口有两个人站在那里.(一个是说实话的,一个是说谎话的.但是他不知道哪个是说实话,哪个是说谎话的).问题就来了;他现在只能问他们两个一句话.请问他怎么去诚实国8:一人被老虎穷追不舍,突然前有条大河, 他不会游泳,但他过去了,为什么?答案:吓昏过去了9:小明和小强都是张老师的学生,张老师的生日是M月N日,两个人都知道张老师的生日是下列十组数中的一组,张老师把M值告诉了小明,把N值告诉了小强,张老师问他们:知道他的生日是那一天??3月4日 3月5日 3月8日6月4日 6月7日 9月1日 9月5日12月1日 12月2日 12月8日小明说:如果我不知道的话,小强肯定也不知道.小强说:本来我也不知道,但是我现在知道了.小明说:哦~那我也知道了.请根据以上判断张老师的生日是那一天?(写出分析思路)思路1)在日期中,7日、2日是唯一的。
2025年招聘Java开发工程师面试题与参考回答面试问答题(总共10个问题)第一题:请描述一下Java中的反射机制及其在Java编程中的应用场景。
答案:Java的反射机制是指在运行时,程序能够取得任何类或对象的内部信息,并且动态创建对象、调用对象的方法以及获取对象的属性。
以下是反射机制的一些关键点:1.反射机制允许在运行时动态地加载和调用类的方法。
2.反射机制可以获取类的构造方法、字段、方法和注解等信息。
3.反射机制提供了访问和修改类内部状态的能力。
应用场景:1.创建对象:通过反射机制,可以在运行时创建任意类的实例。
2.方法调用:在运行时动态调用任意对象的方法。
3.获取类信息:在运行时获取类的名称、父类、接口等信息。
4.动态代理:在实现动态代理时,通过反射机制动态创建代理对象。
5.脚本语言集成:某些脚本语言可以通过反射机制与Java代码进行交互。
解析:反射机制在Java编程中具有广泛的应用,以下是几个具体的例子:•在框架开发中,如Spring框架,反射机制被用来动态地注册和管理Bean。
•在插件系统中,反射机制允许在运行时动态加载和调用插件。
•在测试框架中,如JUnit,反射机制被用来动态调用测试方法。
•在JDBC编程中,反射机制可以用来动态创建数据库连接和执行SQL语句。
反射机制虽然功能强大,但也存在一些缺点,如性能开销大、代码难以理解等。
因此,在使用反射时,应尽量减少不必要的反射操作。
第二题:请简述Java中的多态性及其实现方式,并举例说明在Java中如何通过多态来简化代码设计。
答案:多态性是面向对象编程中的一个核心概念,它允许同一个接口或父类在不同的情况下表现出不同的行为。
在Java中,多态性主要通过继承和接口实现。
1.继承:当一个子类继承了父类后,子类对象可以调用父类的方法和属性,如果子类对父类的方法进行了重写(即子类提供了与父类方法相同签名但不同实现的方法),那么在调用该方法时,就会根据对象的实际类型来执行对应的方法。
Java面试宝典2010版一. Java基础部分 (6)1、一个”.java”源文件中是否可以包括多个类(不是内部类)?有什么限制?62、Java有没有goto? (7)3、说说&和&&的区别。
(7)4、在JAVA中如何跳出当前的多重嵌套循环? (7)5、switch语句能否作用在byte上,能否作用在long上,能否作用在String上?86、short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错? 87、char型变量中能不能存贮一个中文汉字?为什么? (8)8、用最有效率的方法算出2乘以8等於几? (9)9、请设计一个一百亿的计算器 (9)10、使用final关键字修饰一个变量时,是引用不能变,还是引用的对象不能变?1011、"=="和equals方法究竟有什么区别? (10)12、静态变量和实例变量的区别? (11)13、是否可以从一个static方法内部发出对非static方法的调用? . 1114、Integer与int的区别 (11)15、Math。
round(11.5)等於多少? Math。
round(-11.5)等於多少?1216、下面的代码有什么不妥之处? (12)17、请说出作用域public,private,protected,以及不写时的区别. 1218、Overload和Override的区别.Overloaded的方法是否可以改变返回值的类型?1219、构造器Constructor是否可被override? (13)20、接口是否可继承接口?抽象类是否可实现(implements)接口? 抽象类是否可继承具体类(concrete class)?抽象类中是否可以有静态的main方法? (13)21、写clone()方法时,通常都有一行代码,是什么? (14)22、面向对象的特征有哪些方面 (14)23、java中实现多态的机制是什么? (15)24、abstract class和interface有什么区别? (15)25、abstract的method是否可同时是static,是否可同时是native,是否可同时是synchronized? (16)26、什么是内部类?Static Nested Class 和 Inner Class的不同。
程序员面试之葵花宝典(IBM T5 王飞)1/24/20071、面向对象的特征有哪些方面1.抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面。
抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部分细节。
抽象包括两个方面,一是过程抽象,二是数据抽象。
2.继承:继承是一种联结类的层次模型,并且允许和鼓励类的重用,它提供了一种明确表述共性的方法。
对象的一个新类可以从现有的类中派生,这个过程称为类继承。
新类继承了原始类的特性,新类称为原始类的派生类(子类),而原始类称为新类的基类(父类)。
派生类可以从它的基类那里继承方法和实例变量,并且类可以修改或增加新的方法使之更适合特殊的需要。
3.封装:封装是把过程和数据包围起来,对数据的访问只能通过已定义的界面。
面向对象计算始于这个基本概念,即现实世界可以被描绘成一系列完全自治、封装的对象,这些对象通过一个受保护的接口访问其他对象。
4. 多态性:多态性是指允许不同类的对象对同一消息作出响应。
多态性包括参数化多态性和包含多态性(overloading&overwriting)。
多态性语言具有灵活、抽象、行为共享、代码共享的优势,很好的解决了应用程序函数同名问题。
2、String是最基本的数据类型吗?基本数据类型包括byte、int、char、long、float、double、boolean和short。
ng.String类是final类型的,因此不可以继承这个类、不能修改这个类。
为了提高效率节省空间,我们应该用StringBuffer类3、int 和 Integer 有什么区别Java 提供两种不同的类型:引用类型和原始类型(或内置类型)。
Int是java的原始数据类型,Integer是java为int提供的封装类。
Java为每个原始类型提供了封装类。
原始类型封装类booleanBoolean charCharacter byteByte shortShort intInteger longLong floatFloat doubleDouble引用类型和原始类型的行为完全不同,并且它们具有不同的语义。
Java葵花宝典精华1、面向对象的特征有哪些方面1.抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面。
抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部分细节。
抽象包括两个方面,一是过程抽象,二是数据抽象。
2.继承:继承是一种联结类的层次模型,并且允许和鼓励类的重用,它提供了一种明确表述共性的方法。
对象的一个新类可以从现有的类中派生,这个过程称为类继承。
新类继承了原始类的特性,新类称为原始类的派生类(子类),而原始类称为新类的基类(父类)。
派生类可以从它的基类那里继承方法和实例变量,并且类可以修改或增加新的方法使之更适合特殊的需要。
3.封装:封装是把过程和数据包围起来,对数据的访问只能通过已定义的界面。
面向对象计算始于这个基本概念,即现实世界可以被描绘成一系列完全自治、封装的对象,这些对象通过一个受保护的接口访问其他对象。
4. 多态性:多态性是指允许不同类的对象对同一消息作出响应。
多态性包括参数化多态性和包含多态性。
多态性语言具有灵活、抽象、行为共享、代码共享的优势,很好的解决了应用程序函数同名问题。
2、String是最基本的数据类型吗?基本数据类型包括byte、int、char、long、float、double、boolean和short。
ng.String类是final类型的,因此不可以继承这个类、不能修改这个类。
为了提高效率节省空间,我们应该用StringBuffer类3、int 和Integer 有什么区别Java 提供两种不同的类型:引用类型和原始类型(或内置类型)。
Int是java的原始数据类型,Integer是java为int提供的封装类。
Java为每个原始类型提供了封装类。
原始类型封装类booleanBoolean charCharacter byteByte shortShort intInteger longLong floatFloat doubleDouble引用类型和原始类型的行为完全不同,并且它们具有不同的语义。
java程序员面试宝典这是一篇由网络搜集整理的关于java程序员面试宝典的文档,希望对你能有帮助。
java程序员面试宝典--把自己当作英语天才面试题目全部为英文,而且涉及到的知识面极广,一般来说,只有重点大学中品学兼优的在校学生才能搞定,不过鄙人以为大多数这样的人都去读研了。
这样的题目通常出现在注册资产在一亿以上的公司,他们要是成长性好的.员工,这是无可厚非的事情。
不过他们认为工作了一年以上的程序员也能轻松搞定,至少有部分人能搞。
所以当你踌躇满志的去应聘之前,请看看你的实力如何(过六级或者四级680分以上,专业课平均分80以上者可以一试)。
java程序员面试宝典--把自己视为java语言的高手现在网上流行的大公司面试题可能有些人已经看过了,我用java将近两年了,自认为资质不弱常人,但那样的题目拿到手之后的感觉就是憋气。
很多地方只是有个模糊的概念,回答不完全,查阅资料之后方能答上。
或许我的基础还有些薄弱,不过当我试着以试题上的题目与面试我的人“讨论”时发现,他知道的也就是这一题的答案而已。
java程序员面试宝典--把自己当作编译器考试题目大都是一些读程序写结果,或者找错误之类的,不过很多都是编译特例,如果给你个IDE,你肯定是轻松搞定,但你没有那样的机会,回答不出来说明你实际操作经验不足(别人就那么认为的)。
虽然这也的确是程序员应该掌握的东西,但是这样似乎有些以偏概全了。
平时忙着做项目,有空的时候学习新技术,谁会有那种闲夫去研究java原来可以这样运行的。
java程序员面试宝典--一些经验的东西对面试很重要一份十页的卷子,他只需要看一两分钟就ok了,然后过来和你谈。
询问的主要是过去做过什么项目,当然你说的越多越好(不能太夸张)。
这样的面试一般是经理或者老板出马,因为你回答的东西他们看不懂,所以就懒得看了。
如果你做过项目,那么适当的,含蓄的夸张点你所做过的东西,态度要好,同时要求的工资不能太高,市场价就可以了。
1.面向对象的特征有哪些方面a)抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面。
抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部分细节。
抽象包括两个方面,一是过程抽象,二是数据抽象。
b)继承:继承是一种联结类的层次模型,并且允许和鼓励类的重用,它提供了一种明确表述共性的方法。
对象的一个新类可以从现有的类中派生,这个过程称为类继承。
新类继承了原始类的特性,新类称为原始类的派生类(子类),而原始类称为新类的基类(父类)。
派生类可以从它的基类那里继承方法和实例变量,并且类可以修改或增加新的方法使之更适合特殊的需要。
c)封装:封装是把过程和数据包围起来,对数据的访问只能通过已定义的界面。
面向对象计算始于这个基本概念,即现实世界可以被描绘成一系列完全自治、封装的对象,这些对象通过一个受保护的接口访问其他对象。
d)多态性:多态性是指允许不同类的对象对同一消息作出响应。
多态性包括参数化多态性和包含多态性。
多态性语言具有灵活、抽象、行为共享、代码共享的优势,很好的解决了应用程序函数同名问题。
2、String是最基本的数据类型吗?不是;最基本的八大数据类型:byte,short ,long ,int ,char,float .double String 是final的,不能被继承,重写。
3、int 和 Integer 有什么区别Java 提供两种不同的类型:引用类型和原始类型(或内置类型)。
Int是java的原始数据类型,Integer是java为int提供的封装类。
Java为每个原始类型提供了封装类。
原始类型封装类boolean-Boolean char-Character byte-Byte short-Short int-Integer long-Long float-Float double-Double4、String 和StringBuffer的区别String和StringBuffer,它们都可以储存和操作字符串,即包含多个字符的字符数据。
这个String类提供了数值不可改变的字符串。
而这个StringBuffer类提供的字符串进行修改。
当你知道字符数据要改变的时候你就可以使用StringBuffer。
典型地,你可以使用StringBuffers来动态构造字符数据。
5、运行时异常与一般异常有何异同?异常表示程序运行过程中可能出现的非正常状态,运行时异常表示虚拟机的通常操作中可能遇到的异常,是一种常见运行错误。
java编译器要求方法必须声明抛出可能发生的非运行时异常,但是并不要求必须声明抛出未被捕获的运行时异常。
6.说出Servlet的生命周期,并说出Servlet和CGI的区别Servlet被服务器实例化后,容器运行其init方法,请求到达时运行其service 方法,service方法自动派遣运行与请求对应的doXXX方法(doGet,doPost)等,当会话结束是将调用其destroy方法。
与cgi的区别在于servlet处于服务器进程中,它通过多线程方式运行其service 方法,一个实例可以服务于多个请求,并且其实例一般不会销毁,而CGI对每个请求都产生新的进程,服务完成后就销毁,所以效率上低于servlet。
7.说出ArrayList,Vector, LinkedList的存储性能和特性ArrayList和Vector都是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都允许直接按序号索引元素,但是插入元素要涉及数组元素移动等内存操作,所以索引数据快而插入数据慢,Vector由于使用了synchronized方法(线程安全),通常性能上较ArrayList差,而LinkedList 使用双向链表实现存储,按序号索引数据需要进行前向或后向遍历,但是插入数据时只需要记录本项的前后项即可,所以插入速度较快。
8. EJB是基于哪些技术实现的?并说出SessionBean和EntityBean的区别,StatefulBean和StatelessBean的区别。
EJB包括Session Bean、Entity Bean、Message Driven Bean,基于JNDI、RMI、JAT等技术实现。
SessionBean在J2EE应用程序中被用来完成一些服务器端的业务操作,例如访问数据库、调用其他EJB组件。
EntityBean被用来代表应用系统中用到的数据。
对于客户机,SessionBean是一种非持久性对象,它实现某些在服务器上运行的业务逻辑。
对于客户机,EntityBean是一种持久性对象,它代表一个存储在持久性存储器中的实体的对象视图,或是一个由现有企业应用程序实现的实体。
Session Bean 还可以再细分为 Stateful Session Bean 与 Stateless Session Bean ,这两种的 Session Bean都可以将系统逻辑放在 method之中执行,不同的是 Stateful Session Bean 可以记录呼叫者的状态,因此通常来说,一个使用者会有一个相对应的 Stateful Session Bean 的实体。
Stateless Session Bean 虽然也是逻辑组件,但是他却不负责记录使用者状态,也就是说当使用者呼叫 Stateless Session Bean 的时候,EJB Container 并不会找寻特定的 Stateless Session Bean 的实体来执行这个 method。
换言之,很可能数个使用者在执行某个 Stateless Session Bean 的 methods 时,会是同一个 Bean 的Instance 在执行。
从内存方面来看, Stateful Session Bean 与 Stateless Session Bean 比较, Stateful Session Bean 会消耗 J2EE Server 较多的内存,然而 Stateful Session Bean 的优势却在于他可以维持使用者的状态。
9、Collection 和 Collections的区别。
Collection是集合类的上级接口,继承与他的接口主要有Set 和List.Collections是针对集合类的一个帮助类,他提供一系列静态方法实现对各种集合的搜索、排序、线程安全化等操作。
10、&和&&的区别。
&是位运算符,表示按位与运算,&&是逻辑运算符,表示逻辑与(and)。
11、HashMap和Hashtable的区别。
HashMap是Hashtable的轻量级实现(非线程安全的实现),他们都完成了Map接口,主要区别在于HashMap允许空(null)键值(key),由于非线程安全,效率上可能高于Hashtable。
HashMap允许将null作为一个entry的key或者value,而Hashtable不允许。
HashMap把Hashtable的contains方法去掉了,改成containsvalue和containsKey。
因为contains方法容易让人引起误解。
Hashtable继承自Dictionary类,而HashMap是Java1.2引进的Map interface的一个实现。
最大的不同是,Hashtable的方法是Synchronize的,而HashMap不是,在多个线程访问Hashtable时,不需要自己为它的方法实现同步,而HashMap 就必须为之提供外同步。
Hashtable和HashMap采用的hash/rehash算法都大概一样,所以性能不会有很大的差异。
12、final, finally, finalize的区别。
final 用于声明属性(常量)类,分别表示属性不可变,方法不可覆盖,类不可继承。
finally是异常处理语句结构的一部分,无论是否抛出异常,总是要执行的语句。
finalize是Object类的一个方法,在垃圾收集器执行的时候会调用被回收对象的此方法,可以覆盖此方法提供垃圾收集时的其他资源回收,例如关闭文件等。
13、sleep() 和 wait() 有什么区别? sleep是线程类(Thread)的方法,导致此线程暂停执行指定时间(单位是毫秒),给执行机会给其他线程,但是监控状态依然保持,到时后会自动恢复。
调用sleep不会释放对象锁。
wait是Object类的方法,对此对象调用wait方法导致本线程放弃对象锁,进入等待此对象的等待锁定池,只有针对此对象发出notify方法(或notifyAll)后,本线程才进入对象锁定池准备获得对象锁进入运行状态。
14.、Overload(重载)和Override(重写)的区别。
Overloaded的方法是否可以改变返回值的类型?方法的重写Overriding和重载Overloading是Java多态性的不同表现。
重写Overriding是父类与子类之间多态性的一种表现,重载Overloading是一个类中多态性的一种表现。
如果在子类中定义某方法与其父类有相同的名称和参数,我们说该方法被重写 (Overriding)。
子类的对象使用这个方法时,将调用子类中的定义,对它而言,父类中的定义如同被“屏蔽”了。
如果在一个类中定义了多个同名的方法,它们或有不同的参数个数或有不同的参数类型,则称为方法的重载(Overloading)。
Overloaded的方法是可以改变返回值的类型15、error和exception有什么区别?error 表示恢复不是不可能但很困难的情况下的一种严重问题。
比如说内存溢出。
不可能指望程序能处理这样的情况。
是由Java虚拟机控制的。
Exception是不能被忽略的,所以在程序的某一部分一定会得到处理,异常提供了一种重错误到可靠恢复的途径。
可以通过调试程序进行人为的控制。
16、同步和异步有何异同,在什么情况下分别使用他们?举例说明。
如果数据将在线程间共享。
例如正在写的数据以后可能被另一个线程读到,或者正在读的数据可能已经被另一个线程写过了,那么这些数据就是共享数据,必须进行同步存取。
当应用程序在对象上调用了一个需要花费很长时间来执行的方法,并且不希望让程序等待方法的返回时,就应该使用异步编程,在很多情况下采用异步途径往往更有效率。
同步可以保证线程间的数据实时的进行数据交换,通常同过socket实现,异步如果业务上不要求数据实时的进行交换,可以通过交换文件或者直接进行数据交换.17、abstract class和interface有什么区别?声明方法的存在而不去实现它的类被叫做抽象类(abstract class),它用于要创建一个体现某些基本行为的类,并为该类声明方法,但不能在该类中实现该类的情况。