Java考试错题

判断题

1-1 JLable can contain other components.

​ false

1-2 A local class or anonymous class cannot access all local variables from the enclosing method.

​ true

1-3 Using JPanel p = new JPanel(); BoxLayout b1 = new BoxLayout(p, BoxLayout.X_AXIS); can set the BoxLayout for Panel p.

​ false

单选题

2-1 Which one below is the correct signature of InputStream.read() ?

  • byte read()

  • char read()

  • int read()

  • long read()

    C

2-2 Which of these methods will make a thread leave the running state, and the method is not static?

  • notify()

  • Thread.KillThread()

  • yield()

  • wait()

    D

2-3 Which one below is NOT a valid Java identifier?

  • const

  • $2

  • Double

  • 名字

    A

2-4 Choose the best fill in the blanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Hello2017
{ public static void main(String[] args)
{
(__//put the best here__);
}
}
class Century extends Thread
{ String m=”Hello”;
Century(String m){
this.m=m;
}
public void run() {
System.out.println(m);
}
}
  • new Century(“Hello”).start();

  • new Thread(“Hello”).start();

  • new Century(new Thread()).start();

  • new Thread(new Century()).start();

    A

2-5 For object o and class C , which expression below is the right way to test if o is an object of C?

  • C.isInstance(o)

  • o.class == C

  • o instanceof C

  • o.getClass() == C

    not D

2-6

1
2
3
4
5
int x=0x80000000;
if (x==-x)
System.out.println("OK");
else
System.out.println("NOT");

output:

  • OK

  • NOT

  • overflow

  • error ( compilation or run-time)

    A

2-7 Which of the following is NOT correct?

  • A Generic Type Parameter of a Class Is Not Allowed in a Static Context

  • Cannot Create an Instance of a Generic Type. (i.e., new E()).

  • Generic Array Creation is Not Allowed. (i.e., new E[100]).

  • Exception Classes Can be Generic.

    not C

2-8

1
2
3
4
List<Double> ls = new ArrayList<Double>(); 
List<?> lo = ls;
lo.add(new Object());
String s = ls.get(0);
  • It compiles but exception raises at line 3

  • It does not compile

  • It compiles but exception raises at line 2

  • It compiles but exception raises at line 4

    B

填空题

3-1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class Test {
public static void main(String[] args) {
CloneT c = new CloneT();
CloneT t = (CloneT)c.clone();
t.str = "t";
t.b.setA(3);
System.out.println(t==c);
System.out.println(t.b==c.b);
System.out.println(c.toString()+t.toString());
}
}
class Base implements Cloneable{
int a = 1;
public String toString(){
return String.valueOf(a);
}
public void setA(int a){
this.a = a;
}
}

class CloneT implements Cloneable{
transient int i = 1;
private int pi = 1;
static int num = 0;
String str = "c";
Base b = new Base();

public CloneT(){
num++;
}
public Object clone(){
try{
return super.clone();
}catch(CloneNotSupportedException e){
System.out.println("clone not supported!");
return null;
}
}
public String toString(){
return String.valueOf(i) + String.valueOf(pi) + String.valueOf(num) + str + String.valueOf(b);
}
}

​ false

​ true

​ 111c3111t3

3-2

1
2
3
IntStream.range(2, 10).
filter(x->IntStream.range(2, x).filter(k->x%k==0).sum()>0).
sum()

​ 27

3-3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class A {
public int data=1;
private int pd = 2;
public void print() {
System.out.println(data+pd);
f();
}
protected void f() {
System.out.println(1);
}
}
class B extends A {
public int data=3;
private int pd = 4;
public void print() {
super.print();
System.out.println(data+pd);
}
protected void f() {
System.out.println(2);
}
}
public class TestAB {
public static void main(String[] args) {
A a = new B();
a.print();
}
}

​ 3

​ 2

​ 7

3-4

1
2
3
4
5
6
7
8
9
10
11
public class Test {
public static void main(String[] args) {
Integer a = new Integer(127);
Integer b = 127;
Integer c = Integer.valueOf("127");

System.out.println(a==b); //1
System.out.println(a==c); //2
System.out.println(b==c); //3
}
}

​ false

​ false

​ true


to be done:

解释