北京东方国信JAVA面试题-答案

更新时间:2023-10-17 13:55:02 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

Part Ⅰ(每题1分)

1) Which of the following lines will compile without warning or error? Select all

valid answers. a e

a) float f=0;

b) char c=\ c) byte b=157; d) boolean f=null; e) int i=10;

2) What are the valid types for the given variable i ? c

public class Base{

private void test() {

// i = 1;

switch (i) { } }

static public void main(String[] a) {

new Base().test(); } }

Select most appropriate answer. a) char, int

b) boolean, byte, char, short, int c) byte, char, short, int

d) byte, char, short, int, long e) Only int

3) What is the correct way of declarating main() so that the progarm can be

compiled and executed using this entry point. abcf

a) static public void main(String[] args) b) static public void main(String a[]) c) public static void main(String[] a) d) static void public main(String[] a) e) public void main(String[] a)

f) static public void main(char arg[][])

1

4) What will be output if the program is executed as below

java -DOne -DTwo -DThree Sample

public class Sample{

public static void main(String argv[]) {

System.out.println(argv[2]) } }

Select most appropriate answer. b a) One b) Two c) Three

d) Exception raised: \ e) None

Part Ⅱ(每题1分)

5) Abstract method cannot be static. True or False ? true

a) True b) False

6) What will be the output when you compile and execute the following

program. d

//////////////////////////////////////////// class Base {

void test() {

System.out.println(\} }

//////////////////////////////////////////// public class Child extends Base {

void test() {

System.out.println(\}

static public void main(String[] a) {

Child anObj = new Child(); Base baseObj = (Base)anObj;

2

//Base b=new Child(); //b.test();

baseObj.test(); }

}

Select most appropriate answer. a) Child.test()

Base.test() b) Base.test()

Child.test() c) Base.test() d) Child.test()

7) What will be the output when you compile and execute the following

program. C

//////////////////////////////////////////// class Base {

static void test() {

System.out.println(\} }

//////////////////////////////////////////// public class Child extends Base {

void test() {

System.out.println(\Base.test(); //Call the parent method }

static public void main(String[] a) {

new Child().test(); } }

Select most appropriate answer. a) Child.test()

Base.test() b) Child.test()

Child.test()

c) Compilation error. Cannot override a static method by an instance method d) Runtime error. Cannot override a static method by an instance method

3

8) What will be the output when you compile and execute the following

program.

Exception is the base class for all the Exception classes. //////////////////////////////////////////// import java.io.*;

class Base {

void test() throws IOException {

System.out.println(\String a = null;

//Complex logic goes here if(a == null)

throw new IOException(\} }

//////////////////////////////////////////// public class Child extends Base {

protected void test() throws Exception {

System.out.println(\throw new Exception(\}

static public void main(String[] a) {

try {

new Child().test(); }

catch(Exception e) { } } }

Select most appropriate answer. b

a) Child.test()

Base.test()

b) Compilation Error: The method void test() declared in class Child cannot override the method

of the same signature declared in class Base. Their throws clauses are incompatible.

c) Compilation Error: The method void test() declared in class Child cannot override the method

of the same signature declared in class Base. The access modifier is made more restrictive. d) Runtime error. Cannot make the access modifier more restrictive for test()

4

Part Ⅲ(每题1分)

9) What will be the output when you compile and execute the following

program. b

public class Base{

private void test() {

System.out.println(6 + 6 + \ }

static public void main(String[] a) {

new Base().test(); } }

Select most appropriate answer. a) 66(Result) b) 12(Result)

c) Runtime Error.Incompatible type for +. Can't convert an int to a string. d) Compilation Error.Incompatible type for +. Can't add a string to an int.

10) What will be the output when you compile and execute the following

program. The symbol ’ ?’ means space. e

1:public class Base{ 2:

3: private void test() { 4: 5:

String aStr = \?One?\

6: String bStr = aStr; 7: aStr.toUpperCase(); 8: aStr.trim(); 9: System.out.println(\7: } 8:

9: static public void main(String[] a) { 10: new Base().test(); 11: } 12: }

Select most appropriate answer.

5

a) [ONE,?One?] b) [?One?,One] c) [ONE,One] d) [ONE,ONE] e) [?One?,?One?]

11) What will be the output when you compile and execute the following

program. d

public class Base {

void test() {

String aStr = \

System.out.println(aStr.substring(3,7)); }

static public void main(String[] a) {

new Base().test(); } }

Select most appropriate answer. a) Strin b) String c) trin d) Stri

12) What can contain objects that have a unique key field of String type, if it is

required to retrieve the objects using that key field as an index? a

a) Map b) Set c) List

d) Collection e) Enumeration

Part Ⅳ(每题1分)

13) At what point are the objects i2, i3 and i4 available for garbage collection.

01:public class Base {

6

02: Base i;

03: public static void main(String [] args) { 04: Base i2 = new Base(); 05: Base i3 = new Base(); 06: Base i4 = new Base(); 07: 08: i2.i = i3; // i2 refers to i3 09: i3.i = i4; // i3 refers to i4 10: i4.i = i2; // i4 refers to i2 11: 12: i2 = null; 13: i3 = null; 14: i4 = null; 15: // do complicated, memory intensive stuff 16: } 17:}

Select most appropriate answer d a) After line 6 b) After line 9 c) After line 10 d) After line 14 e) After line 16

System.gc()

Runtime.getRuntime().gc()

Java的垃圾回收机制是Java虚拟机提供的能力,用于在空闲时间以不定时的方式动态回收无任何引用的对象占据的内存空间。

14) A try statement must always associate with a f

a) catch

b) None of these c) throws d) throw e) finally

f) catch, finally or both

15) Which of the following are correct way of throwing an exception inside a

method?

Select all valid answers b a) new throw Exception(\

7

b) throw new Exception(\c) throws IOException() d) throws IOException e) catch(Exception e) { }

16) The method test() throws MalformedURLException and EOFException.

IOException is the parent class of MalformedURLException and EOFException. What changes will make the following code to compile without any errors.

import java.io.*; import java.net.*;

public class Base{

private void test() { String a = null; String b = \

// Complex processing if(a==null)

throw new MalformedURLException(\

if(b ==null)

throw new EOFException(\

// Complex processing } }

Select all valid answers a d a) private void test() throws IOException

b) private void test() throws EOFException, throws MalformedURLException c) private void test() throws EOFException throws MalformedURLException d) private void test() throws EOFException, MalformedURLException e) private void test() throws EOFException MalformedURLException

Part Ⅴ(每题1分)

17) Which class or interface defines the wait(), notify(), and notifyAll() methods?

a

a) Object

8

b) Thread c) Runnable d) Class

18) Given the following,

1. class MyThread extends Thread { 2.

3. public static void main(String [] args) { 4. MyThread t = new MyThread(); 5. t.run(); 6. } 7.

8. public void run() { 9. for(int i=1;i<3;++i) {

10. System.out.print(i + \11. } 12. } 13. }

what is the result? c

a) This code will not compile due to line 4. b) This code will not compile due to line 5. c) 1..2.. d) 1..2..3..

e) An exception is thrown at runtime.

19) Given the following,

class MyThread extends Thread { MyThread() {

System.out.print(\}

public void run() {

System.out.print(\}

public void run(String s) { System.out.println(\} }

public class TestThreads {

public static void main (String [] args) { Thread t = new MyThread() { public void run() {

System.out.println(\}

9

};

t.start(); }

}

what is the result? b a) foo

b) MyThread foo c) MyThread bar d) foo bar e) foo bar baz f) bar foo

g) Compilation fails.

20) Given the following,

1. public class Test {

2. public static void main (String [] args) { 3. final Foo f = new Foo();

4. Thread t = new Thread(new Runnable() { 5. public void run() { 6. f.doStuff(); 7. } 8. });

9. Thread g = new Thread() { 10. public void run() { 11. f.doStuff(); 12. } 13. };

14. t.start(); 15. g.start(); 16. } 17. }

1. class Foo { 2. int x = 5;

3. public void doStuff() { 4. if (x < 10) {

5. // nothing to do 6. try { 7. wait();

8. } catch(InterruptedException ex) { } 9. } else {

10. System.out.println(\11. if (x >= 10) { 12. notify(); 13. }

10

本文来源:https://www.bwwdw.com/article/etvf.html

Top