目前分類:Java認證 (6)

瀏覽方式: 標題列表 簡短摘要


Q.1 

public static Collection get() {
  Collection sorted = new LinkedList();
  sorted.add("B"); sorted.add("C"); sorted.add("A");
  return sorted;
}
public static void main(String[] args) {
  for(Object obj : get()) {
    System.out.print(obj+", ");
  }
}

What is the result?


A. A, B, C,
B. B, C, A,
C. Compilation fails
D. The code runs with no output
E. An exception is thrown at runtime

 

鈴木保齡球 發表在 痞客邦 留言(0) 人氣()

1. Encapsulation 封裝    //存取權限
2. Inheritance 繼承       //繼承別人的內容
3. Polymorphism 多型   //一個物件多種型態,目的是要抽象化,修改彈性更大

= P.I.E

教材P121,122

 

1. Which is true?

Given:

5. class Building { }
6. public class Barn extends Building {
7.   public static void main(String[] args) {
8.      Building build1 = new Building();
9.      Barn barn1 = new Barn();
10.    Barn barn2 = (Barn) build1;
11.    Object obj1 = (Object) build1;
12.    String str1 = (String) build1;
13.    Building build2 = (Building) barn1;
14.  }
15.}
 

鈴木保齡球 發表在 痞客邦 留言(0) 人氣()

1. What is the value of j at line 16 ?

Click the exhibit button:
1. int i=1, j=0;
2.
3. switch(i){
4. case 2:
5.   j+=6;
6.
7.  case 4:
8.   j+=1;
9.
10.  default:
11.   j +=2;
12.
13.  case 0:
14.   j +=4;
15.}
16.   


A. 0

鈴木保齡球 發表在 痞客邦 留言(0) 人氣()

--------------------------------------------------------------------------------
break範例
--------------------------------------------------------------------------------
class BreakWithLabelDemo {
    public static void main(String[] args) {

        int[][] arrayOfInts = {
            { 32, 87, 3, 589 },
            { 12, 1076, 2000, 8 },
            { 622, 127, 77, 955 }
        };
        int searchfor = 12;

        int i;
        int j = 0;
        boolean foundIt = false;

    search: // 迴圈名稱可自定, 要符合Java命名規則, 迴圈名稱與迴圈宣告之間不能插入Java指令, 迴圈名稱也可寫在迴圈宣告左邊
        for (i = 0; i < arrayOfInts.length; i++) {
            for (j = 0; j < arrayOfInts[i].length; j++) {
                if (arrayOfInts[i][j] == searchfor) {
                    foundIt = true;
                    break search; // break指令只能中斷把break指令包圍起來的迴圈

鈴木保齡球 發表在 痞客邦 留言(0) 人氣()

1. Java程式基本上包含了class(類別), method(方法), variable(變數)等宣告

    modifier(修飾字)  class(類別)  class名稱 {    // 此行為類別宣告格式(課本4-9頁, 113頁)
        // 註 :
        // Java內有多道指令統稱修飾字, public 是其中之一
        // public為公開的、公眾的意思, 此為Java關鍵字(課本4-19頁, 123頁), 如本支Java程式須供其他Java程式叫用則須寫上, 否則可省略
        // Java關鍵字列表網頁http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
        // class為類別的意思, 此為Java關鍵字, 固定一定要寫, class名稱也一定要有. 修飾字則可有可無.
        // 一個class內可包含0~N個method(方法)

        modifier(修飾字)  static(靜態)  回傳值型態  method名稱(參數宣告) {  // 此行為method宣告格式(課本4-16頁, 120頁)
         /*
             修飾字、static、參數宣告可有可無
          回傳值型態、method名稱一定要有
             一個method內可包含0~N行程式碼
            */
         資料型態 變數名稱 = 欲儲存的值; (課本5-8頁, 146頁)
        }

        modifier(修飾字)  static(靜態)   回傳值型態  method名稱(參數宣告) {  // 此行為method宣告格式(課本4-16頁, 120頁)
         資料型態 變數名稱 = 欲儲存的值; (課本5-8頁, 146頁)
        }

        modifier(修飾字)  static(靜態)   回傳值型態  method名稱(參數宣告) {  // 此行為method宣告格式(課本4-16頁, 120頁)

鈴木保齡球 發表在 痞客邦 留言(0) 人氣()

1. Which one is keyword of Java ? (Choose five)

keyword 關鍵字


A. Class
B. public
C. boolean
D. String
E. int
F. implement
G. volatile
H. assert

答案:

B,C,E,G,H

A,D 大寫是Class名,不是關鍵字

F implement少一個s
 

 

2. What happens when you attempt to compile and run these two files in the same directory ?

//File P1.java
package MyPackage;

鈴木保齡球 發表在 痞客邦 留言(0) 人氣()