## 面試時間:2024/10
### 1. Call by Value and Call by Address and Call by Reference
* Write following program output.
```
#include <iostream>
int num = 50;
void function1(int value1, int* value2, int& value3)
{
value1 *= num;
std::cout << value1 << std::endl;
*value2 = value1 + num;
std::cout << *value2 << std::endl;
num += num;
value3 = *value2 + num;
return;
}
void function2(int* value)
{
value = 0;
}
int main()
{
int a = 10;
int* b = &a;
int& c = a;
int* d = &c;
function1(a, b, c);
function2(d);
std::cout << "a:" << a
<< " b:"<< *b
<< " c:" << c
<< " d:" << *d
<< " num: " << num << std::endl;
return 0;
}
```
* Ans:
```
500
550
a:650 b:650 c:650 d:650 num: 100
```
### 2. Bit Operation
1) Given n = 5, 1 << n = ?
* Ans: 32(100000)
2) ~11110011b = ?
* Ans: 00001100b
3) 00001000b | 00000010b = ?
* Ans: 00001010b
4) 00001000b & 00000010b = ?
* Ans: 00000000b
5) Set the n-th bit of given number a
#define setbitN (a, n) = ?
* Ans: (a | 1 << n)
6) Clear n-th bit of given number a
#define clearbitN (a, n) = ?
* Ans: (a & ~(1 << n))
### 3. What’s the following programs output
```
int a[] = {1, 2, 3, 4, 5};
int *p = a;
*(p++) += 56;
*(++p) += 78;
printf("%d %d %d %d %d\n", a[0], a[1], a[2], a[3], a[4]);
```
* Ans:
```
57 2 81 4 5
```
### 4. On little endian machine, *b = ?, *c =?
```
int a = 0x12345678;
char *b = (char*)(&a);
short *c = (short*)(&a);
printf("b = %x, c= %x\n",*b,*c);
```
* Ans:
```
b = 78, c= 5678
```
### 5. Reverse linked list
https://leetcode.com/problems/reverse-linked-list/
* Ans:
```
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* cur = head;
ListNode* prev = nullptr;
ListNode* next = nullptr;
while(cur != NULL){
next = cur -> next;
cur -> next = prev;
prev = cur;
cur = next;
}
return prev;
}
};
```