5급 공채 2차

2021년 5급 공채 기술 2차 프로그래밍언어론

문제 5개 · 50점. 참고 답안·채점 기준은 앱에서 볼 수 있습니다.

제1문 (10점)

다음 BNF(Backus-Naur Form) 규칙에 대해 물음에 답하시오. (총 10점)
<assign> -> <id> = <expr>
<id> -> A | B | C
<expr> -> <expr> + <term>
| <expr> - <term>
| <term>
<term> -> <term> * <factor>
| <term> / <factor>
| <factor>
<factor> -> <expr>
| (<expr>)
| <number>
<number> -> <number><digit>
| <digit>
<digit> -> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
  1. (1) 위 문법을 이용하여 수식 “B=12+5*3”에 대한 파스트리를 작성하시오. (5점)
  2. (2) 위 BNF 규칙을 EBNF(Extended Backus-Naur Form) 규칙으로 변경하시오. (5점)

제2문 (10점)

Java 언어에 대한 다음 물음에 답하시오. (총 10점)
  1. (1) 아래 Java 코드를 실행한 출력 결과를 기술하시오. (5점)
    public class A {
    public static void main(String args[]) {
    String s0 = "Hello PL.";
    String s1 = "Hello ";
    String s2 = "PL.";
    String s3 = new String("Hello ");
    String s4 = s1 + s2;
    String s5 = "Hello " + "PL.";

    if (s1 == s3) System.out.println("s1 == s3");
    if (s0 == s4) System.out.println("s0 == s4");
    if (s0 == s5) System.out.println("s0 == s5");
    if(s1.equals(s3)) System.out.println("s1 equals s3");
    if(s0.equals(s4)) System.out.println("s0 equals s4");
    }
    }
  2. (2) Java 언어는 기본형(primitive type)과 참조형(reference type)을 혼합하여 사용하고 있다. 기본형과 참조형에 대해 설명하고, Java 언어가 두 가지 자료형을 혼합하여 사용하는 이유를 설명하시오. (5점)

제3문 (10점)

다음 C 프로그램에 대해 물음에 답하시오. (총 10점)
#include <stdio.h>
#define PI 3.14
int area(int);
double area_double(double);

int main(void) {
int r;
printf("반지름을 입력하시오: ");
scanf("%d", &r);
printf("%d\n", area(r));
printf("%.2lf\n", area_double(r));
}

int area(int r) {
return r*r*PI;
}

double area_double(double d) {
return d*d*PI;
}
  1. (1) 위 프로그램에서 좁히는 묵시적 타입 변환(narrowing implicit type conversion)이 발생하는 문장을 명시하고, 그 변환된 내용을 기술하시오. (5점)
  2. (2) 위 프로그램에 사용되는 중복 연산자(overloaded operator)에 대해 설명하시오. (5점)

제4문 (10점)

다음 Java 언어 프로그램에 대해 물음에 답하시오. (총 10점)
class ZeroException extends RuntimeException { }

public class EHandler {
void x(int a, int b) {
try {
if(b == 0) throw new ZeroException();
} catch (ZeroException e) {
System.out.println("A");
}
a=a/b;
System.out.println("B");
}
void y( ) {
x(10, 0);
System.out.println("C");
}
public static void main(String[] args) {
EHandler p = new EHandler( );
try {
p.y( );
} catch (ArithmeticException e) {
System.out.println("D");
} catch(ZeroException e) {
System.out.println("E");
}
System.out.println("F");
}
}
  1. (1) 위 프로그램을 실행한 출력 결과를 기술하시오. (4점)
  2. (2) 위 프로그램의 실행 결과를 토대로 예외의 전파 과정을 설명하시오. (6점)

제5문 (10점)

C 프로그램의 함수 호출에 대한 다음 물음에 답하시오. (총 10점)
  1. (1) 활성화레코드(activation record)를 설명하고, 그 구성요소에 대하여 기술하시오. (5점)
  2. (2) 다음 프로그램 실행 시 생성되는 활성화레코드의 개수가 최대가 되는 시점의 활성화레코드 스택을 그리시오. (5점)
    #include <stdio.h>

    void printWithCommas(long n){
    if (n < 1000) {
    printf("%d" , n);
    }else{
    printWithCommas(n/1000);
    printf(",%d", n % 1000);
    }
    };

    int main(void){
    long val=1234567;
    printWithCommas(val);
    printf("\n");
    return 0;
    }