标签:
1.Given the following code:
public class Test { private static int j = 0; private static Boolean methodB(int k) { j += k; return true; } public static void methodA(int i) { boolean b; b = i < 10 | methodB(4); b = i < 10 || methodB(8); } public static void main(String args[]) { methodA(0); System.out.println(j); } }
What is the result?
A:The program prints”0”
B:The program prints”4”
C;The program prints”8”
D:The program prints”12”
E:The code does not complete.
答案:B。
解析:
2.下列代码的输出结果是_____
boolean b=true?false:true==true?false:true; System.out.println(b);
A:true
B:false
C:null
D:空字符串
答案:B
解析:
A.方法重载和方法的重写实现的功能相同
B.方法重载出现在父子关系中,方法重写是在同一类中
c.方法重载的返回值类型必须一致,参数项必须不同
D.方法重写的返回值类型必须相同或相容。(或是其子类)
public static void main(String[]args)throws Exception { final Object obj = new Object(); Thread t1 = new Thread() { public void run() { synchronized (obj) { try { obj.wait(); System.out.println("Thread 1 wake up."); } catch (InterruptedException e) { } } } }; t1.start(); Thread.sleep(1000);//We assume thread 1 must start up within 1 sec. Thread t2 = new Thread() { public void run() { synchronized (obj) { obj.notifyAll(); System.out.println("Thread 2 sent notify."); } } }; t2.start(); }
A:Thread 1 wake up Thread 2 sent notify.
B:Thread 2 sent notify. Thread 1 wake up
c:A、B皆有可能
D:程序无输出卡死
j解析:notify()就是对对象锁的唤醒操作。但有一点需要注意的是notify()调用后,并不是马上就释放对象锁的,而是在相应的 synchronized(){}语句块执行结束,自动释放锁后,JVM会在wait()对象锁的线程中随机选取一线程,赋予其对象锁,唤醒线程,继续执 行。这样就提供了在线程间同步、唤醒的操作。
5.往OuterClass类的代码段中插入内部类声明, 哪一个是错误的:
public class OuterClass{
private float f=1.0f;
//插入代码到这里
}
A: lass InnerClass{ public static float func(){return f;} }
B: abstract class InnerClass{ public abstract float func(){} }
C: static class InnerClass{ protected static float func(){return f;} }
D: public class InnerClass{ static float func(){return f;} }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public class Outer{ private int age = 99 ; String name = "Coco" ; public class Inner{ String name = "Jayden" ; public void show(){ System.out.println(Outer. this .name); System.out.println(name); System.out.println(age); } } public Inner getInnerClass(){ return new Inner(); } public static void main(String[] args){ Outer o = new Outer(); Inner in = o. new Inner(); in.show(); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class Outer{ private int age = 99 ; static String name = "Coco" ; public static class Inner{ String name = "Jayden" ; public void show(){ System.out.println(Outer.name); System.out.println(name); } } public static void main(String[] args){ Inner i = new Inner(); i.show(); } } |
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
|
/* 使用的形参为何要为 final??? 在内部类中的属性和外部方法的参数两者从外表上看是同一个东西,但实际上却不是,所以他们两者是可以任意变化的, 也就是说在内部类中我对属性的改变并不会影响到外部的形参,而然这从程序员的角度来看这是不可行的, 毕竟站在程序的角度来看这两个根本就是同一个,如果内部类该变了,而外部方法的形参却没有改变这是难以理解 和不可接受的,所以为了保持参数的一致性,就规定使用 final 来避免形参的不改变 */ public class Outer{ public void Show(){ final int a = 25 ; int b = 13 ; class Inner{ int c = 2 ; public void print(){ System.out.println( "访问外部类:" + a); System.out.println( "访问内部类:" + c); } } Inner i = new Inner(); i.print(); } public static void main(String[] args){ Outer o = new Outer(); o.show(); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class OuterClass { public InnerClass getInnerClass( final int num,String str2){ return new InnerClass(){ int number = num + 3 ; public int getNumber(){ return number; } }; /* 注意:分号不能省 */ } public static void main(String[] args) { OuterClass out = new OuterClass(); InnerClass inner = out.getInnerClass( 2 , "chenssy" ); System.out.println(inner.getNumber()); } } interface InnerClass { int getNumber(); } |
A:request
B:out
C:application
D:config
答案:A B C D
解析:
7.A,B,C,D 中哪些是 setvar的重载?
public
class
methodover
{
public
void
setVar(
int
a,
int
b,
float
c) {}
}
标签:
原文地址:http://www.cnblogs.com/shenxiaoquan/p/5907633.html