手把手带你刷好题(牛客刷题⑥)

简介: 手把手带你刷好题(牛客刷题⑥)

1、应用程序的main方法中有以下语句,则输出的结果( )

1. String s1=new String( ” xyz ” );
2. 
3. String s2=new String( ” xyz ” );
4. 
5. Boolean b1=s1.equals(s2);
6. 
7. Boolean b2=(s1==s2);
8. 
9. System .out.print(b1+ ” ” +b2); 
10. 
11. A、true false
12. B、false  true
13. C、true true
14. D、false  false

正确选项:A

注记:equals比较内容,所以是true,但是==比较对象的引用,s1和s2对象不是同一个,是堆中的不同内存区域

2、我们在程序中经常使用“System.out.println()”来输出信息,语句中的System是包名,out是类名,println是方法名。

A、正确

B、错误

正确选项:B

注记: System是java.lang中的一个类,out是System内的一个成员变量,这个变量是一个java.io.PrintStream类的对象,println呢就是一个方法了。

3、

1. public boolean returnTest()
2. {
3.     try
4.     {
5.         return true;
6.     }
7.     catch (Exception e)
8.     {
9. 
10.     }
11.     finally
12.     {
13.         return false;
14.     }
15. }

以上代码返回值是什么?

A、true

B、false

正确选项:B

注记:一旦在finally块中使用了return或throw语句,将会导致try块,catch块中的return,throw语句失效

4、给定以下JAVA代码,这段代码运行后输出的结果是()

1. public class Test
2. {  
3. public static int aMethod(int i)throws Exception
4.     {
5. try{
6. return i/10;
7.         }
8. catch (Exception ex)
9.         {
10. throw new Exception("exception in a aMethod");
11.         }finally{
12.       System.out.printf("finally");
13.         }
14. } 
15. public static void main(String[] args){
16. try
17.         {
18.             aMethod(0);
19.         }
20. catch (Exception ex)
21.         {
22.             System.out.printf("exception in main");
23.         }
24.         System.out.printf("finished");
25.     }
26. }

A、exception in main finished

B、finallyfinished

C、exception in main finally

D、finally exception in main finally

正确选项:B

注记:

本题考的不仅仅是审题,而且是try......catch......finally块的关系,以及return与finally的执行关系。 具体执行过程: 1、先进入main函数,进入try块调用aMethod(0)方法; 2、执行aMethod()方法的try块,i/10可以正确执行,故并未抛出异常,catch块不执行,而需要执行finally(该块任何时候都要执行),故打印finally; 3、回到main函数,由于aMethod()调用成功,因此main函数的catch块同样不执行,顺序执行finally块,打印finished 因此,最终的输出结果就是:finally finished

5、下列不属于访问控制符的是()

A、public

B、private

C、protected

D、static

正确选项:D

注记: public 公有 private私有 protected受保护 static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块。


相关文章
手把手带你刷好题(牛客刷题③)
手把手带你刷好题(牛客刷题③)
手把手带你刷好题(牛客刷题③)
|
缓存 Java
手把手带你刷好题(牛客刷题④)
手把手带你刷好题(牛客刷题④)
|
存储 索引 容器
手把手带你刷好题(牛客刷题⑦)
手把手带你刷好题(牛客刷题⑦)
|
Java API C++
手把手带你刷好题(牛客刷题⑤)
手把手带你刷好题(牛客刷题⑤)