본문 바로가기
CS/자료구조

방송통신대학교 자료구조 기말 시험 대비 예상 문제 - 적합한 코드 찾기 유형

by Renechoi 2023. 12. 2.

1. 다음 코드는 배열의 요소를 검색하는 함수이다. 함수의 (가)에 들어갈 적합한 내용을 고르시오.

int retrieve(int *a, int i) {
    if (i >= 0 && i < array_size) {
        return a[i];
    } else {
        [가]
    }
}

① printf("Error\n"); return -1;
② return NULL;
③ return a;
④ continue;

정답 : 1

2. 다음 코드는 스택의 pop 연산을 수행하는 함수이다. 함수의 (나)에 들어갈 적합한 내용을 고르시오.

int pop() {
    if (top == -1) {
        return StackIsEmpty();
    } else {
        [나]
    }
}

① return stack[top];
② top--;
③ return stack[top--];
④ return ++top;

정답 : 3

3. 다음 코드는 큐의 삽입 연산을 수행하는 함수이다. 함수의 (다)에 들어갈 적합한 내용을 고르시오.

void Add_q(Element item) {
    if (rear == QUEUE_SIZE) {
        printf("Queue is full!!");
        return;
    } else {
        queue[rear] = item;
        [다]
    }
}

① rear = (rear + 1) % QUEUE_SIZE;
② rear++;
③ return queue[rear];
④ rear--;

정답 : 2

4. 다음 코드는 스택에 새 원소를 삽입하는 push 연산을 수행한다. 함수의 (라)에 들어갈 적합한 내용을 고르시오.

void push(int item) {
    if (top >= STACK_SIZE-1) {
        return StackIsFull();
    } else {
        [라]
    }
}

① stack[top++] = item;
② top++; stack[top] = item;
③ stack[++top] = item;
④ stack[top] = item; top++;

정답 : 3

5. 다음 코드는 배열을 생성하는 함수이다. 함수의 (마)에 들어갈 적합한 내용을 고르시오.

void create(int n) {
    int a[n];
    int i;
    for(i = 0; i < n; i++) {
        [마]
    }
}

① a[i] = 0;
② return a;
③ a[n] = 0;
④ printf("Array created\n");

정답 : 1

6. 다음 코드는 스택의 크기를 확인하는 함수이다. 함수의 (가)에 들어갈 적합한 내용을 고르시오.

int stackSize() {
    [가]
}

① return sizeof(stack) / sizeof(element);
② return STACK_SIZE;
③ return top + 1;
④ return top == -1 ? 0 : top;

정답 : 3

7. 다음 코드는 큐에서 front의 값을 반환하는 함수이다. 함수의 (나)에 들어갈 적합한 내용을 고르시오.

Element frontValue() {
    if (IsEmpty_q()) {
        printf("Queue is empty\n");
        return -1;
    } else {
        [나]
    }
}

① return queue[front];
② return front;
③ return queue[rear];
④ return (front + 1) % QUEUE_SIZE;

정답 : 1

8. 다음 코드는 후위 표기식에서 연산자를 처리하는 부분이다. 함수의 (다)에 들어갈 적합한 내용을 고르시오.

void processOperator(char op) {
    int a = pop();
    int b = pop();
    switch (op) {
        case '+': [다] break;
        case '-': push(b - a); break;
        case '*': push(b * a); break;
        case '/': push(b / a); break;
    }
}

① push(a + b);
② push(b + a);
③ return a + b;
④ return b + a;

정답 : 1

9. 다음 코드는 배열에서 특정 인덱스의 요소를 업데이트하는 함수이다. 함수의 (라)에 들어갈 적합한 내용을 고르시오.

void updateElement(int *a, int index, int value) {
    if (index < 0 || index >= array_size) {
        printf("Index out of range\n");
    } else {
        [라]
    }
}

① a[index] = value;
② return a[index];
③ printf("Updated\n");
④ a[value] = index;

정답 : 1

10. 다음 코드는 스택의 top 값을 확인하는 함수이다. 함수의 (마)에 들어갈 적합한 내용을 고르시오.

int topValue() {
    if (StackIsEmpty()) {
        printf("Stack is empty\n");
        return -1;
    } else {
        [마]
    }
}

① return top;
② return stack[top];
③ return STACK_SIZE - 1;
④ return stack[0];

정답 : 2

11. 다음 코드는 배열의 특정 요소를 검색하는 함수이다. 함수의 (가)에 들어갈 적합한 내용을 고르시오.

int searchArray(int *a, int value) {
    for (int i = 0; i < array_size; i++) {
        if (a[i] == value) {
            [가]
        }
    }
    return -1;
}

① return i;
② printf("Element found\n");
③ continue;
④ return -1;

정답: 1

12. 다음 코드는 스택에서 특정 요소를 검색하는 함수이다. 함수의 (나)에 들어갈 적합한 내용을 고르시오.

int searchStack(int item) {
    for (int i = 0; i <= top; i++) {
        if (stack[i] == item) {
            [나]
        }
    }
    return -1;
}

① return i;
② printf("Element found\n");
③ continue;
④ return -1;

정답: 1

13. 다음 코드는 큐에서 특정 요소를 검색하는 함수이다. 함수의 (다)에 들어갈 적합한 내용을 고르시오.

int searchQueue(int item) {
    for (int i = front; i != rear; i = (i + 1) % QUEUE_SIZE) {
        if (queue[i] == item) {
            [다]
        }
    }
    return -1;
}

① return i;
② printf("Element found\n");
③ continue;
④ return -1;

정답: 1

14. 다음 코드는 배열에서 최대값을 찾는 함수이다. 함수의 (라)에 들어갈 적합한 내용을 고르시오.

int findMaxInArray(int *a) {
    int max = a[0];
    for (int i = 1; i < array_size; i++) {
        if (a[i] > max) {
            [라]
        }
    }
    return max;
}

① max = i;
② max = a[i];
③ continue;
④ return max;

정답: 2

15. 다음 코드는 스택에서 최대값을 찾는 함수이다. 함수의 (마)에 들어갈 적합한 내용을 고르시오.

int findMaxInStack() {
    int max = stack[0];
    for (int i = 1; i <= top; i++) {
        if (stack[i] > max) {
            [마]
        }
    }
    return max;
}

① max = i;
② max = stack[i];
③ continue;
④ return max;

정답: 2

16. 다음 코드는 배열에서 특정 값보다 큰 첫 번째 요소의 인덱스를 반환하는 함수이다. 함수의 (가)에 들어갈 적합한 내용을 고르시오.

int findFirstGreaterThan(int *a, int value) {
    for (int i = 0; i < array_size; i++) {
        if (a[i] > value) {
            [가]
        }
    }
    return -1;
}

① return -1;
② return i;
③ printf("Found\n");
④ continue;

정답: 2

17. 다음 코드는 스택에서 가장 아래에 있는 원소를 반환하는 함수이다. 함수의 (나)에 들어갈 적합한 내용을 고르시오.

int bottomValue() {
    if (StackIsEmpty()) {
        printf("Stack is empty\n");
        return -1;
    } else {
        [나]
    }
}

① return stack[0];
② return top;
③ return stack[top];
④ return STACK_SIZE - 1;

정답: 1

18. 다음 코드는 큐에서 rear의 바로 앞에 있는 원소 값을 반환하는 함수이다. 함수의 (다)에 들어갈 적합한 내용을 고르시오.

Element rearPrecedingValue() {
    if (IsEmpty_q()) {
        printf("Queue is empty\n");
        return -1;
    } else {
        [다]
    }
}

① return (rear - 1) % QUEUE_SIZE;
② return queue[(rear - 1) % QUEUE_SIZE];
③ return rear;
④ return queue[rear];

정답: 2

19. 다음 코드는 주어진 배열에서 특정 값이 존재하는 횟수를 세는 함수이다. 함수의 (라)에 들어갈 적합한 내용을 고르시오.

int countOccurrences(int *a, int value) {
    int count = 0;
    for (int i = 0; i < array_size; i++) {
        if (a[i] == value) {
            [라]
        }
    }
    return count;
}

① return i;
② count++;
③ return count;
④ printf("Found\n");

정답: 2

20. 다음 코드는 스택에 저장된 모든 원소의 합을 반환하는 함수이다. 함수의 (마)에 들어갈 적합한 내용을 고르시오.

int sumStack() {
    int sum = 0;
    for (int i = 0; i <= top; i++) {
        [마]
    }
    return sum;
}

① sum += i;
② sum += stack[i];
③ return sum;
④ printf("Sum is %d", sum);

정답: 2

21. 다음 코드는 단순 연결 리스트에 새 노드를 삽입하는 함수이다. 함수의 (가)에 들어갈 적합한 내용을 고르시오.

void insertNode(linkedList_h* H, int data) {
    listNode* newNode;
    newNode = (listNode*)malloc(sizeof(listNode));
    newNode->data = data;
    [가]
}

① newNode->link = NULL;
② newNode->link = H->head; H->head = newNode;
③ H->head = newNode;
④ newNode->data = data;

정답 : 2

22. 다음 코드는 이중 연결 리스트에서 특정 노드를 삭제하는 함수이다. 함수의 (나)에 들어갈 적합한 내용을 고르시오.

void deleteDNode(linkedList_h* H, listNode* delNode) {
    if (delNode->Llink != NULL) {
        delNode->Llink->Rlink = delNode->Rlink;
    } else {
        H->Lhead = delNode->Rlink;
    }
    [나]
}

① delNode->Rlink->Llink = delNode->Llink;
② H->Rhead = delNode->Llink;
③ delNode->Llink = NULL; delNode->Rlink = NULL;
④ free(delNode);

정답 : 1

23. 다음 코드는 원형 연결 리스트에서 특정 데이터를 가진 노드를 찾아 삭제하는 함수이다. 함수의 (다)에 들어갈 적합한 내용을 고르시오.

void deleteCircularNode(linkedList_h* H, int data) {
    listNode* current;
    listNode* previous;
    current = H->head;
    do {
        if (current->data == data) {
            [다]
            return;
        }
        previous = current;
        current = current->link;
    } while (current != H->head);
}

① previous->link = current->link; free(current);
② current->link = previous;
③ H->head = current->link;
④ return;

정답 : 1

24. 다음 코드는 연결 리스트에서 마지막 노드를 삭제하는 함수이다. 함수의 (라)에 들어갈 적합한 내용을 고르시오.

void deleteLastNode(linkedList_h* H) {
    listNode* previous;
    listNode* current;
    if (H->head == NULL) return;
    if (H->head->link == NULL) {
        free(H->head);
        H->head = NULL;
        return;
    } else {
        previous = H->head;
        current = H->head->link;
        while (current->link != NULL) {
            previous = current;
            current = current->link;
        }
        [라]
    }
}

① previous->link = NULL; free(current);
② current->link = previous;
③ H->head = current;
④ free(previous);

정답 : 1

25. 다음 코드는 이중 연결 리스트에 새 노드를 삽입하는 함수이다. 함수의 (마)에 들어갈 적합한 내용을 고르시오.

void insertDNode(linkedList_h* H, int data) {
    listNode* newNode;
    newNode = (listNode*)malloc(sizeof(listNode));
    newNode->data = data;
    newNode->Llink = NULL;
    newNode->Rlink = H->Rhead;
    if (H->Rhead != NULL) {
        H->Rhead->Llink = newNode;
    }
    H->Rhead = newNode;
    if (H->Lhead == NULL) {
        H->Lhead = newNode;
    } else {
        [마]
    }
}

① newNode->Llink = H->Lhead; H->Lhead = newNode;
② newNode->Rlink = H->Lhead; H->Lhead->Rlink = newNode;
③ newNode->Llink = H->Lhead->Llink; H->Lhead->Llink = newNode;
④ H->Lhead->Rlink = newNode; newNode->Llink = H->Lhead;

정답 : 4

26. 다음 코드는 이중 연결 리스트에서 특정 데이터 값을 가진 노드를 찾아 삭제하는 함수이다. 함수의 (가)에 들어갈 적합한 내용을 고르시오.

void deleteNodeWithValue(linkedList_h* H, int value) {
    listNode* current = H->Lhead;
    while(current != NULL) {
        if (current->data == value) {
            [가]
            break;
        }
        current = current->Rlink;
    }
}

① current->Llink->Rlink = current->Rlink; current->Rlink->Llink = current->Llink; free(current);
② return;
③ H->Lhead = current->Rlink; current->Rlink->Llink = NULL; free(current);
④ current->Llink = current->Rlink; current->Rlink = current->Llink;

정답: 1

27. 다음 코드는 연결 리스트의 노드들 중에서 특정 값을 가진 노드의 총 개수를 반환하는 함수이다. 함수의 (나)에 들어갈 적합한 내용을 고르시오.

int countNodesWithValue(linkedList_h* H, int value) {
    listNode* current = H->head;
    int count = 0;
    while (current != NULL) {
        if (current->data == value) {
            [나]
        }
        current = current->link;
    }
    return count;
}

① return count++;
② count++;
③ count += value;
④ return count;

정답: 2

28. 다음 코드는 이중 연결 리스트의 노드들을 역순으로 출력하는 함수이다. 함수의 (다)에 들어갈 적합한 내용을 고르시오.

void printReverse(linkedList_h* H) {
    listNode* current = H->Rhead;
    while (current != NULL) {
        printf("%d ", current->data);
        [다]
    }
}

① current = current->Rlink;
② return;
③ current = current->Llink;
④ printf("\n");

정답: 3

29. 다음 코드는 원형 연결 리스트에서 특정 데이터를 가진 노드 뒤에 새로운 노드를 삽입하는 함수이다. 함수의 (라)에 들어갈 적합한 내용을 고르시오.

void insertAfterData(linkedList_h* H, int targetData, int newData) {
    listNode* newNode;
    newNode = (listNode*)malloc(sizeof(listNode));
    newNode->data = newData;

    listNode* current = H->head;
    do {
        if (current->data == targetData) {
            [라]
            break;
        }
        current = current->link;
    } while(current != H->head);
}

① newNode->link = current->link; current->link = newNode;
② newNode->link = current;
③ free(newNode);
④ return;

정답: 1

30. 다음 코드는 연결 리스트에서 모든 노드의 데이터 합계를 반환하는 함수이다. 함수의 (마)에 들어갈 적합한 내용을 고르시오.

int sumAllNodes(linkedList_h* H) {
    listNode* current = H->head;
    int sum = 0;
    while (current != NULL) {
        [마]
        current = current->link;
    }
    return sum;
}

① sum += current->data;
② return sum;
③ sum++;
④ sum -= current->data;

정답: 1

31. 다음 코드는 연결 리스트의 모든 노드를 역순으로 출력하는 함수이다. 함수의 (가)에 들어갈 적합한 내용을 고르시오.

void printReverse(linkedList_h* H) {
    listNode* current = H->head;
    listNode* prev = NULL;
    listNode* next = NULL;
    while (current != NULL) {
        next = current->link;
        current->link = prev;
        prev = current;
        current = [가];
    }
    current = prev;
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->link;
    }
}

① next;
② prev;
③ H->head;
④ NULL;

정답: 1

32. 다음 코드는 이중 연결 리스트의 모든 노드 데이터의 합계를 반환하는 함수이다. 함수의 (나)에 들어갈 적합한 내용을 고르시오.

int sumAllDNodes(linkedList_h* H) {
    int sum = 0;
    listNode* current = H->Lhead;
    while (current != NULL) {
        sum += current->data;
        current = [나];
    }
    return sum;
}

① current->Llink;
② current->Rlink;
③ H->Rhead;
④ NULL;

정답: 2

33. 다음 코드는 이중 연결 리스트에서 특정 데이터 값을 가진 노드를 찾아 그 노드의 오른쪽에 새 노드를 삽입하는 함수이다. 함수의 (다)에 들어갈 적합한 내용을 고르시오.

void insertNodeAfterValue(linkedList_h* H, int targetData, int newData) {
    listNode* current = H->Lhead;
    while (current != NULL) {
        if (current->data == targetData) {
            listNode* newNode = (listNode*)malloc(sizeof(listNode));
            newNode->data = newData;
            [다]
            break;
        }
        current = current->Rlink;
    }
}

① newNode->Llink = current; newNode->Rlink = current->Rlink; current->Rlink->Llink = newNode; current->Rlink = newNode;
② newNode->Rlink = current; newNode->Llink = current->Llink; current->Llink->Rlink = newNode; current->Llink = newNode;
③ newNode->Llink = current->Llink; newNode->Rlink = current; current->Llink->Rlink = newNode; current->Llink = newNode;
④ newNode->Rlink = NULL; newNode->Llink = NULL; current->Rlink = newNode; newNode->Llink = current;

정답: 1

34. 다음 코드는 이중 연결 리스트에서 주어진 데이터 값을 가진 노드를 찾아 그 노드를 삭제하는 함수이다. 함수의 (라)에 들어갈 적합한 내용을 고르시오.

void deleteNodeByValue(linkedList_h* H, int value) {
    listNode* current = H->Lhead;
    while (current != NULL) {
        if (current->data == value) {
            [라]
            free(current);
            break;
        }
        current = current->Rlink;
    }
}

① current->Llink->Rlink = current->Rlink; current->Rlink->Llink = current->Llink;
② current->Rlink->Llink = current->Llink; current->Llink->Rlink = current->Rlink;
③ current->Llink = NULL; current->Rlink = NULL;
④ H->Lhead = current->Rlink; H->Rhead = current->Llink;

정답: 1

35. 다음 코드는 연결 리스트에서 주어진 순서에 따라 노드를 추가하는 함수이다. 함수의 (마)에 들어갈 적합한 내용을 고르시오.

void insertNodeAtPosition(linkedList_h* H, int position, int newData) {
    listNode* newNode = (listNode*)malloc(sizeof(listNode));
    newNode->data = newData;
    newNode->link = NULL;
    if (position == 1) {
        newNode->link = H->head;
        H->head = newNode;
    } else {
        listNode* temp = H->head;
        for (int i = 1; i < position - 1; i++) {
            temp = temp->link;
        }
        [마]
    }
}

① newNode->link = temp; temp->link = newNode;
② newNode->link = temp->link; temp->link = newNode;
③ temp = newNode; newNode->link = temp->link;
④ temp->link = newNode; newNode->link = temp->link;

정답: 2

트리와 그래프 부분에 대한 추가적인 시험 문제를 만들어 드리겠습니다. 아래의 문제들은 교과서에서 제시된 내용을 기반으로 하며, 코드가 주어지고 빈칸에 들어갈 적절한 내용을 선택하는 형식입니다.

36. 다음 코드는 이진 트리의 노드를 삽입하는 함수이다. 함수의 (가)에 들어갈 적합한 내용을 고르시오.

node *insertNode(node *root, char newData) {
    if (root == NULL) {
        node *newNode = (node*)malloc(sizeof(node));
        newNode->data = newData;
        newNode->left = newNode->right = NULL;
        return newNode;
    }
    if (newData < root->data) {
        root->left = insertNode(root->left, newData);
    } else {
        root->[가] = insertNode(root->right, newData);
    }
    return root;
}

① left;
② right;
③ data;
④ NULL;

정답: 2

37. 다음 코드는 이진 트리의 노드를 삭제하는 함수이다. 함수의 (나)에 들어갈 적합한 내용을 고르시오.

node *deleteNode(node *root, char targetData) {
    if (root == NULL) {
        return NULL;
    }
    if (targetData < root->data) {
        root->left = deleteNode(root->left, targetData);
    } else if (targetData > root->data) {
        root->right = deleteNode(root->right, targetData);
    } else {
        if (root->left == NULL) {
            node *temp = root->right;
            free(root);
            return temp;
        } else if (root->right == NULL) {
            node *temp = root->left;
            free(root);
            return temp;
        }
        node *temp = findMin(root->right);
        root->data = temp->data;
        root->[나] = deleteNode(root->right, temp->data);
    }
    return root;
}

① left;
② right;
③ data;
④ NULL;

정답: 2

38. 다음 코드는 주어진 그래프의 깊이 우선 탐색(DFS)을 수행하는 함수이다. 함수의 (다)에 들어갈 적합한 내용을 고르시오.

void DFS(Graph *g, int v) {
    g->visited[v] = 1;
    printf("%d ", v);
    for (int i = 0; i < g->numVertices; i++) {
        if (g->adjMatrix[v][i] == 1 && g->visited[i] == 0) {
            DFS(g, [다]);
        }
    }
}

① v;
② i;
③ g;
④ NULL;

정답: 2

39. 다음 코드는 주어진 그래프의 너비 우선 탐색(BFS)을 수행하는 함수이다. 함수의 (라)에 들어갈 적합한 내용을 고르시오.

void BFS(Graph *g, int startVertex) {
    Queue q;
    initializeQueue(&q);
    g->visited[startVertex] = 1;
    enqueue(&q, startVertex);

    while (!isEmpty(&q)) {
        int currentVertex = dequeue(&q);
        printf("%d ", currentVertex);

        for (int i = 0; i < g->numVertices; i++) {
            if (g->adjMatrix[currentVertex][i] == 1 && g->visited[i] == 0) {
                g->visited[i] = 1;
                enqueue(&q, [라]);
            }
        }
    }
}

① currentVertex;
② i;
③ startVertex;
④ NULL;

정답: 2

40. 다음 코드는 그래프에서 최소 신장 트리를 찾는 프림 알고리즘을 구현한 것이다. 함수의 (마)에 들어갈

적합한 내용을 고르시오.

void prim(Graph *g, int startVertex) {
    int noOfEdges = 0;
    g->selected[startVertex] = 1;

    while (noOfEdges < g->numVertices - 1) {
        int min = INF;
        int x = 0, y = 0;

        for (int i = 0; i < g->numVertices; i++) {
            if (g->selected[i]) {
                for (int j = 0; j < g->numVertices; j++) {
                    if (!g->selected[j] && g->adjMatrix[i][j]) {
                        if (min > g->adjMatrix[i][j]) {
                            min = g->adjMatrix[i][j];
                            x = i;
                            y = [마];
                        }
                    }
                }
            }
        }
        printf("Edge %d: (%d, %d) cost: %d\n", noOfEdges++, x, y, min);
        g->selected[y] = 1;
    }
}

① x;
② y;
③ j;
④ i;

정답: 3

반응형