牛客网java - 握瑾怀瑜
牛客网java
2020-01-10 11:10
握瑾怀瑜
阅读(277)
评论(0)
编辑
收藏
举报
1:下列代码运行的结果是什么?
public class P { public static int abc = 123; static{ System.out.println("P is init"); } } public class S extends P { static{ System.out.println("S is init"); } } public class Test { public static void main(String[] args) { System.out.println(S.abc); } }
View Code
原因:
2:下面程序的运行结果是:( )
public static void main(String args[]) { Thread t = new Thread() { public void run() { pong(); } }; t.run(); System.out.print("ping"); } static void pong() { System.out.print("pong"); }
View Code
解释:并不是多线程竞争问题,就是正常执行,t.run是调用的Thead类中的run()方法,t.start才是执行线程,所以这题就是执行普通run()方法,先输出pong,在输出ping。
3:下面程序的输出结果为B
public class Demo { public static String sRet = ""; public static void func(int i) { try { if (i%2==0) { throw new Exception(); } } catch (Exception e) { sRet += "0"; return; } finally { sRet += "1"; } sRet += "2"; } public static void main(String[] args) { func(1); func(2); System.out.println(sRet); } }
View Code
解释:第一步,func(1),if条件不成立,不抛出异常,catch不运行,final运行,拼串得到“1”,程序继续往下走,拼串得到“12”。 第二步,fun(2),if条件成立,抛出异常,catch捕获异常,运行catch里面代码,拼串得到“120”,虽然有return,但是不管出不出异常,final里代码必须执行,执行final,拼串得到“1201”,然后return结束。所以最终结果“1201”
4:关于下面的程序Test.java说法正确的是
解释:被static修饰的变量称为静态变量,静态变量属于整个类,而局部变量属于方法,只在该方法内有效,所以static不能修饰局部变量。static 修饰的变量属于类,只能定义在类中方法外。
5:下列哪个对访问修饰符作用范围由大到小排列是正确的
解释:
6:在Java中,以下关于方法重载和方法重写描述正确的是
解释:
解释:
8:多态的定义:《疯狂java讲义》的标准解释是: 相同类型的变量、调用同一个方法时呈现出多种不同的行为特征,这就是多态
9:Continue语句跳出整个循环?
解释:
解释:
采用synchronized修饰符实现的同步机制叫做互斥锁机制,它所获得的锁叫做互斥锁。每个对象都有一个monitor(锁标记),当线程拥有这个锁标记时才能访问这个资源,没有锁标记便进入锁池。
任何一个对象系统都会为其创建一个互斥锁,这个锁是为了分配给线程的,防止打断原子操作。每个对象的锁只能分配给一个线程,因此叫做互斥锁。
11:对于子类的构造函数说明,下列叙述中错误的是( )
12:下列程序的运行结果
解释:
13:下面有关java final的基本规则,描述错误的是
解释:
14:
class Two{ Byte x; } class PassO{ public static void main(String[] args){ PassO p=new PassO(); p.start(); } void start(){ Two t=new Two(); System.out.print(t.x+””); Two t2=fix(t); System.out.print(t.x+” ” +t2.x); } Two fix(Two tt){ tt.x=42; return tt; } }
View Code
解释:
15:以下类型为Final类型的为
解释:
16:关于Java中的数组,下面的一些描述,哪些描述是准确的
解释:
17: