---
# System prepended metadata

title: C/C++ Interview Exam (周奕呈)

---

C/C++ Interview Exam (周奕呈)
=================================

1. Where in memory are the variables stored?

    * Global variables
    * Static variables
    * Constant data types
    * Local variables (declared and defined in functions)
    * Variables declared in *main* function
    * Dynamically allocated space

    ```
    Please answer here...
    * 
    * 
    * 
    * 
    * 
    * 
    ```

2. What are the modifiers "volatile" and "mutable"? When should they be used?

    ```
    Please answer here...
    
    ````

3. Are the following legal or illegal uses of const pointers? Which lines are illegal ones? What are the benefits of using const?

    ~~~c
    // Question 3
    const char *str = "hello";
    char c = *str;
    str++;
    *str = 'a';
    str[1] = 'b';
    ~~~

    ```
    str++; is illegal
    Benefits are
    1. 預防數值被更改
    2. 可當作常數使用 例如pi = 3.1415 若設定為const 則variable可視為同等數值
    
    ```

4. What is the output of the following program?

    ~~~c++
    // Question 4
    #include <iostream>

    class A
    {
    public:
        A(int n = 0)
            : m_n(n)
        {
            std::cout << 'd';
        }

        A(const A& a)
            : m_n(a.m_n)
        {
            std::cout << 'c';
        }

    private:
        int m_n;
    };

    void f(const A &a1, const A &a2 = A())
    {
    }

    int main()
    {
        A a(2), b;
        const A c(a), &d = c, e = b;
        b = d;
        A *p = new A(c), *q = &a;
        static_cast<void>(q);
        delete p;
        f(3);
        std::cout << std::endl;

        return 0;
    }
    ~~~
    ```
    Please answer here...
    ddccd
    
    ```

5. Please write a function to shuffle a vector of integers.

    ~~~c++
    // Question 5
    #include <vector>
    #include <stdexcept>
    #include <iostream>

    class Shuffler
    {
    public:
        static void shuffle(std::vector<int>& sequence)
        {
            // TODO: Waiting to be implemented
            std::vector<int> temp, tempSort;
            for (int i = 0; i<6; i++)
            {
                int timeV = getCurrentTime();
                int Value = rand(timeV);
                temp.push_back(Value);
                tempSort.push_back(Value);
            } 
            tempSort.sort();
            for (int i= 0; i<6; i++)
            {
                for (int j=0; j<6; j++)
                {
                    if (temp[j]==tempSort[i])
                        sequence[j] = i+1;
                }
            }



























        }
    };

    #ifndef RunTests
    int main()
    {
        std::vector<int> v = { 1, 2, 3, 4, 5, 6 };
        Shuffler::shuffle(v);
        return 0;
    }
    #endif
    ~~~
    

6. Implement function *countNumbers* that accepts a sorted vector of integers and counts the number of vector elements that are less than the parameter *lessThan*. For example, the following program should return 2 because there are two vector elements less than 4. (Note: the vector size may be extremely large.)

    ~~~c++
    // Question 6
    #include <vector>
    #include <stdexcept>
    #include <iostream>

    class SortedSearch
    {
    public:
        static int countNumbers(
            const std::vector<int>& sortedVector, int lessThan)
        {
            // TODO: Waiting to be implemented
            if (sortedVector.size()<1)
                return 0;
            int l = 0, r = sortedVector.size()-1;
            int m;
            do
            {
                m = l+(r-l)/2;
                if (sortedVector[m]<lessThan)
                {    
                    l = m+1;
                    continue;
                }
                if (sortedVector[m]>lessThan)
                {
                    r = m-1;
                    continue;
                }
                if (sortedVector[m]==lessThan)
                    return m+1;
            }
            while (l<r);
            return l+1;























        }
    };

    #ifndef RunTests
    int main()
    {
        std::vector<int> v = { 1, 3, 5, 7 };
        std::cout << SortedSearch::countNumbers(v, 4);
        return 0;
    }
    #endif
    ~~~
    

7. The following code is for inserting an element at the front of a list. Please correct it if you find anything inappropriate, and describe what you find. 

   ~~~c++
   struct IntElement
   {
       int data;
       IntElement *next;
   };
   
   bool insertInFront (IntElement *head, int data) {
       IntElement *newElem = new IntElement;
       if (!newElem) return false;

       newElem->data;
       head = newElem;

       return true;
   }
   ~~~

   ```
   Please describe what you find here ...
   newElem->data; 沒有給data
   head = newElem; 有給head 但原本的head沒有串上去 就遺失了
   
   ```

   ~~~c++
   // Please write the correct code here...
   bool insertInFront (IntElement *head, int data) {
       IntElement *newElem = new IntElement;
       if (!newElem) return false;

       newElem->data = data;
       newElem->next = head;
       head = newElem;

       return true;
   }
   
   

   
    
   
   
   
   
   
   ~~~
