Linux中国 Linux中国门户站!
设为主页 设为主页
收藏本站 收藏本站
 
当前位置 :首页 ->Linux技术 ->系统管理 ->正文

不积圭步,无以至千里 -- Java(6)

来源:Linux-cn.com 作者:Webmaster 时间:2007-05-05 点击: [收藏] [投稿]

  我写作这个Java基础系列也有一段时间了,在这个系列的文章里,我讲述了一些关于JAVA的基础知识,并且重点介绍了JAVA I/O的分类,使用方法,和需要注意的问题,我平时在编程的时候遇到的一些问题,以及解决方法。作为本系列的结束,我在这篇文章中将列出一些考试题目,并且做出分析和解答。这些考试题目都是从SCJP(Sun Certificated Java Programmer)的模拟考试题目中选出来的,其中大部分是来自于一些很好的商业考试题,另外一些是来自网上的免费考试题。作为一个JAVA程序员,在取得了一定的编程经验之后,是应该获得一个认证的。这样有助于在职业资格上的认证,也从另外一个方面来帮助你巩固JAVA的基础知识。

  在以下以及后面的几篇文章中,我将把考试的题目按照若干的方面,分门别类的列在下面,并且根据难度的不同,分别适当给予讲述和解释。在本系列的最后,将列出少量的题目,供大家练习。

  流程控制和异常机制:

  题目:What will the following program print?


public class TestClass
{
  public static void main(String[] args)
  {
     int x = 1;
     int y = 0;
     if( x/y ) System.out.println("Good");
     else System.out.println("Bad");
  }
}

  A. Good

  B. Bad

  C. Exception at runtime saying division by Zero.

  D. It will not compile.

  E. None of the above.

  这一题,容易无选为C, 因为可能考虑到,y=0那么在运行的时候会出现ArithmeticException. 但是,要考虑到,这个程序在编译的时候是不能通过的,因为,Java和C++的不同之处之一是,C++在if()里面可以是任何的数值,非0即为真,0为假。但是,Java要求在if()里面必须是boolean类型的变量不能为其它的整数。所以,这一道题的答案是D.实际在编译器中编译一下这个程序我们可以看到:


C: emp>javac TestClass.java
TestClass.java:7: incompatible types
found : int
required: boolean
     if( x/y ) System.out.println("Good");
          ^
1 error

  题目:What will be the output of the following class...


class Test 
{
   public static void main(String[] args) 
   {
      int j = 1;
      try 
      {
         int i = doIt() / (j = 2);
      } catch (Exception e) 
      {
         System.out.println(" j = " + j);
      }
   }
   public static int doIt() throws Exception 
   { throw new Exception("FORGET IT"); }
}

  A. It will print j = 1;

  B. It will print j = 2;

  C. The value of j cannot be determined.

  D. It will not compile.

  E. None of the above.

  这道题目容易错误的选择为D.

  注意两点:

  1、public static int doIt() throws Exception { throw new Exception("FORGET IT"); }这一语句编译是正确的,因为不论返回值是否为void, 只要有throw语句,可以没有return相应的返回值这一语句,因为throw就是一种返回方式。

  2、如果在运行的时候,某一方法抛出一个异常,那么,后面的语句就马上被中断,而不会被执行。在我们的题目给出的例子中,doIt()抛出一个异常, 那么,后面的(j = 2)就不会被执行,程序转而执行catch()中的语句,即,println语句,所以应该打印的数值是1。

  这道题目的答案是A.

  题目:What letters, and in what order, will be printed when the following program is compiled and run?


public class FinallyTest
{
   public static void main(String args[]) throws Exception 
   {
       try 
       {
          m1();
          System.out.println("A"); 
       } 
       finally 
       {
          System.out.println("B");
       }
       System.out.println("C");
   }
   public static void m1() throws Exception { throw new Exception(); }
}

  A. It will print C and B, in that order.

  B. It will print A and B, in that order.

  C. It will print B and throw Exception.

  D. It will print A, B, and C, in that order.

  E. Compile time error.

  这道题目容易错误的选择为C. 在m1()方法中抛出了一个异常,所以println("A")不会被执行。因为没有一个catch语句来捕获这个异常,所以main()方法将把这个异常抛出。所以println("C")不会被执行。但是finally语句必定会被执行,即使有一个return语句在try或者catch语句段中,只要不是System.exit()就行。所以println("B")将会被执行。

  这道题目的答案是:C.

  题目:What will be the result of attempting to compile and run the following program?


public class TestClass 
{
   public static void main(String args[])
   {
      Exception e = null;
      throw e;
   }
}

  A. The code will fail to compile.

  B. The program will fail to compile, since it cannot throw a null.

 如果您对本文有任何疑问或者建议,请到讨论区发表您的意见: >> 论坛入口 <<



上一篇:不积圭步,无以至千里 -- Java(5)   下一篇:Java变量类型间的相互转换

文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【我要投稿】 【论坛讨论
更多相关文章
Power by linux-cn.com 粤ICP备05006655号