5급 공채 2차

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

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

제1문 (10점)

C++ 언어의 static 선언에 대한 다음 물음에 답하시오. (총 10점)
  1. (1) 아래 StaticError 클래스는 컴파일 오류가 발생한다. 오류가 발생한 이유를 설명하고 오류가 발생하지 않도록 프로그램을 수정하시오. (단, main 함수는 수정하지 않는다) (5점)
    class StaticError {
    int rValue;
    public:
    static int getStatic() { return rValue; }
    void setStatic(int value) {
    this->rValue = value;
    }
    };

    int main() {
    int n = StaticError::getStatic();

    StaticError errorStatic;
    errorStatic.setStatic(50);
    return 0;
    }
  2. (2) 아래 프로그램에는 전역 함수들이 존재한다. 이를 static 멤버 함수를 이용하여 캡슐화한 프로그램으로 수정하시오. (5점)
    #include <iostream>
    using namespace std;

    int abs(int a) { return (a>0)?a:-a; }
    int max(int a, int b) { return (a>b)?a:b; }
    int min(int a, int b) { return (a>b)?b:a; }

    int main() {
    cout << abs(-2) << endl;
    cout << max(5, 3) << endl;
    cout << min(-2, -4) << endl;
    return 0;
    }

제2문 (10점)

다음 자바 프로그램에 대해서 대상 위치에 1)~3)의 각 문장을 넣을 경우 실행한 결과와 그 이유를 설명하시오. (단, 오류가 있는 경우 원인을 설명하시오)
(총 10점)
class A {
String x = "A";
public void f() {
System.out.println("A");
}
}
class B extends A {
int x = 10;
public void f() {
System.out.println("AA");
}
public void g() {
System.out.println("B");
}
public static void main(String args[]) {
A a = new B();

____________ ; // 대상 위치

}
}
  1. (1) a.f() (4점)
  2. (2) a.g() (4점)
  3. (3) System.out.println(a.x) (2점)

제3문 (10점)

다음 C++ 프로그램에 대해서 대상 위치에 1)~10)의 각 문장을 넣을 경우 컴파일 오류 여부를 제시하고, 그 이유를 설명하시오. (각 1점, 총 10점)
class animal {
private: int ValA;
protected: int ValB;
public: int ValC;
void initAnimal( int ValX ) { ValA = ValX; }
};
class dog: protected animal {
public: int ValD;
void initDog( int ValY ) { ValB = ValY; }
};
int main() {
animal blue;
dog sky;
____________ ; // 대상 위치
return 0;
}
  1. (1) blue.ValA=10
  2. (2) blue.ValB=20
  3. (3) blue.ValC=30
  4. (4) blue.initAnimal(40)
  5. (5) sky.ValA=100
  6. (6) sky.ValB=200
  7. (7) sky.ValC=300
  8. (8) sky.ValD=400
  9. (9) sky.initDog(500)
  10. (10) sky.initAnimal(600)

제4문 (10점)

다음 C++ 프로그램에 대한 물음에 답하시오. (총 10점)
#include <iostream>
using namespace std;

class Circle {
int radius;
public:
Circle();
Circle(int r);
~Circle();
void setRadius(int r) { radius = r; }
double getArea() { return 3.14*radius*radius; }
};

Circle::Circle() {
radius = 1;
cout << "radius = " << radius << endl;
}

Circle::Circle(int r) {
radius = r;
cout << "radius = " << radius << endl;
}

Circle::~Circle() {
cout << "radius = " << radius << endl;
}

void foo() {
Circle circles[3];
int r = 0;

for (int i = 0; i < 3; i++) {
circles[i].setRadius(r+=10);
cout << circles[i].getArea() << endl;
}
}

int main() {
foo();
return 0;
}
  1. (1) 위 프로그램이 실행되는 동안 Circle 클래스의 생성자와 소멸자가 호출되는 시점을 설명하시오. (5점)
  2. (2) 위 프로그램을 실행한 출력 결과를 제시하시오. (5점)

제5문 (10점)

주어진 문맥 자유 문법(Context Free Grammar)을 보고 다음 물음에 답하시오.
(총 10점)
S→aAS | bBS | bA | bdB
A→SA | AB | AC
B→cSd | a
C→cSa | ε
(주어진 문법에서 소문자는 터미널 심볼, 대문자는 논터미널 심볼을 의미한다. 또한, S는 시작 심볼을 나타내고, ε은 빈 문자열을 나타낸다)
  1. (1) 주어진 문법에서 필요 없는 생성 규칙을 제거하고 그 과정을 보이시오. (5점)
  2. (2) 1)에서 얻어진 문법으로 하향식 구문분석기(top-down parser)를 구현하려고 할 때 발생할 수 있는 문제점을 서술하고 이를 해결하기 위해 문법을 변환하시오. (5점)