---
# System prepended metadata

title: 後端/ JAVA 面試考題
tags: [面試, java]

---

# 後端/ JAVA 面試考題

## 1. 分別寫出下面程式碼會印出什麼

a. 

```java=
class Parent {
    Parent() {
        System.out.println("Parent");
    }
}

class Child extends Parent {
    Child() {
        super();
        System.out.println("Child");
    }
}

public class Main {
    public static void main(String[] args) {
        Child child = new Child();
    }
}
```

b.
```java=
class Parent {
    Parent() {
        System.out.println("Parent");
    }
}

class Child extends Parent {
    Child() {
        System.out.println("Child");
    }
}

public class Main {
    public static void main(String[] args) {
        Child child = new Child();
    }
}
```

c.

```java=
class Parent {
    
}

class Child extends Parent {
    Child() {
        System.out.println("Child");
    }
}

public class Main {
    public static void main(String[] args) {
        Child child = new Child();
    }
}
```

d.

```java=
class Parent {
    static {
        System.out.println("static Parent");
    }
}

class Child extends Parent {
    static {
        System.out.println("static Child");
    }

    Child() {
        System.out.println("Child");
    }
}

public class Main {
    public static void main(String[] args) {
        Child child = new Child();
    }
}

```

e.

```java=
class Parent {
    static {
        System.out.println("static Parent");
    }
}

class Child extends Parent {
    static {
        System.out.println("static Child");
    }

    Child() {
        System.out.println("Child");
    }
}

public class Main {
    static {
        System.out.println("static Main");
    }
    
    public static void main(String[] args) {
        Child child = new Child();
    }
}
```

f.

```java=
class Parent {
    static {
        System.out.println("static Parent");
    }

    {
        System.out.println("instance Parent ");
    }

    Parent() {
        System.out.println("Parent");
    }
}

class Child extends Parent {
    static {
        System.out.println("static Child");
    }

    {
        System.out.println("Instance Child");
    }

    Child() {
        System.out.println("Child");
    }
}

public class Main {
    public static void main(String[] args) {
        Child child = new Child();
    }
}
```

## 2. 分別寫出下面程式碼會印出什麼

a.

```java=
public class Main {
    Integer num;
    
    public static void main(String[] args) {
        Main obj = new Main(); 
        System.out.println(obj.num);
    }
}
```

b.

```java=
public class Main {
    
    public static void main(String[] args) {
        int num;
        System.out.println(num);
    }
}
```

c.

```java=
class Main {
    int num;
    
    public static void main(String[] args) {
        Main obj = new Main(); 
        System.out.println(obj.num);
    }
}
```

d.

```java=
public class Main {
    
    public static void main(String[] args) {
        Integer num;
        System.out.println(num);
    }
}
```

## 3. 請以資料結構的角度去說明Array、List、Map、Slice 四者特性或差異

## 4. 抽象類別與介面有什麼區別

## 5. 分別用下面指定方式寫出可以印出 9x9 乘法表程式碼

***a.*** for
    
***b.*** while

***c.*** do while

***d.*** forEach

***e.*** 遞迴

***f.*** Stream API

***g.*** lambda

## 6.  請實作氣泡排序
    
## 7.  請實作二元搜尋(Binary Search)，不能使用內建函式

## 8. 給定一個正整數 n，請寫一個函式計算 n 的數字總和(不能使用字串遍歷的方式)。

EX:

```
123 → 1 + 2 + 3 = 6

9876 → 9 + 8 + 7 + 6 = 30
```

## 9. 請實作單例以外的任意設計模式，並說明應用場景

## 10. 依據下面下面給兩張資料表，回答下面問題

**employees** Table

| id  | name    | department_id | status |
| --- | ------- | ------------- | ------ |
| 1   | Alice   | 1             | 1      | 
| 2   | Bob     | 2             | 1      | 
| 3   | Charlie | NULL          | 2      | 
| 4   | David   | 3             | 1      | 
| 5   | Lu      | 1             | 1      | 
| 6   | Kevin   | 1             | 1      | 
| 7   | Tom     | 3             | 2      | 
| 8   | Wan     | 2             | 1      | 


**departments**  Table

| id  | department_name |
| --- | --------------- |
| 1   | HR              |
| 2   | IT              |
| 3   | Finance         |


a. 寫出下面SQL的結果

```sql=
SELECT e.id AS emp_id, e.name, d.department_name
FROM employees e
INNER JOIN departments d ON e.department_id = d.id;
```

b. 寫出下面SQL的結果

```sql=
SELECT e.id AS emp_id, e.name, d.department_name
FROM employees e
LEFT JOIN departments d ON e.department_id = d.id;
```

c. 寫出下面SQL的結果

```sql=
SELECT e.id AS emp_id, e.name, d.department_name
FROM employees e
RIGHT JOIN departments d ON e.department_id = d.id;
```

## 11. 請根據以下需求並基於RESTful API規範進行API設計

a. 搜尋文章
b. 取得文章詳細資料
c. 新增文章
d. 刪除文章
e. 根據文章ID批次取得文章

## 12. 請分別說明MVC、MVVM、MVP