정보처리기사 실기 프로그래밍 문제 유형
이 글에서는 개정된 정보처리기사 실기 시험의 프로그래밍 문제 유형에 대해서 다룬다.
프로그래밍 문제는 전공자 비전공자 할 것 없이, 해당 언어를 좀 써봤 으면 하루 벼락치기도 가능한 수준이다.
언어에 익숙하다면 거저 주는 문제인데 10문제 가량 되므로, 나오지도 않을 개념 달달 외우는 불상사가 없도록 하자.
들어가기에 앞서..
🕗️ 벼락치기러를 위한 정상화 타임 존재
9시에 시험 시작하는줄 알았는데 9시까지 입실이고
9시부터 9시 30분까지 방송을 핑계로 벼락치기러들 기억을 포맷시키는 정상화 타임이 존재한다.
🦓 검은건 코드요 흰건 종이로다
흑백 인쇄이므로 코드에 prettify 따윈 되어있지 않고, 라인 넘버도 없다.
그리고 파이썬 제외 Java 등에선 indent를 정확히 안지킨다.
public static void main(String[] args) {
코드가
이렇게
써있어요
}
공통
📌 재귀함수
public class Main {
public static void main(String[] args) {
int[] data = {3, 5, 8, 12, 17};
System.out.println(func(data, 0, data.length - 1));
}
static int func(int[] a, int st, int end) {
if (st >= end) return 0;
int mid = (st + end) / 2;
return a[mid] + Math.max(func(a, st, mid), func(a, mid + 1, end));
}
}
class Node:
def __init__(self, value):
self.value = value
self.children = []
def tree(li):
nodes = [Node(i) for i in li]
for i in range(1, len(li)):
nodes[(i - 1) // 2].children.append(nodes[i])
return nodes[0]
def calc(node, level=0):
if node is None:
return 0
return (node.value if level % 2 == 1 else 0) + sum(calc(n, level + 1) for n in node.children)
li = [3, 5, 8, 12, 15, 18, 21]
root = tree(li)
print(calc(root))
#include
int f(int n) {
if(n<=1) return 1;
else return n*f(n-1);
}
int main() {
printf("%d", f(7));
}
📌 switch - case 낚시
#include <stdio.h>
void main(){
int n[3] = {73, 95, 82};
int sum = 0;
for(int i=0;i<3;i++){
sum += n[i];
}
switch(sum/30){
case 10:
case 9: printf("A");
case 8: printf("B");
case 7:
case 6: printf("C");
default: printf("D");
}
}
Java
정처기에서 제일 현웃인건 자바 파트이다. 수험생을 어떻게든 틀리게 하려는 출제진의 눈물나는 노력이 느껴진다.
대체 어디서 다들 알아온건지 모를, 실무에서 쓸 리 없는 각종 안티패턴을 들고 온다.
객체지향 언어답게 상속 문제 위주로 출제된다.
📌 스텔스 super
상속 관계에서 자식 클래스의 생성자에 super()
가 명시되어있지 않으면
컴파일러가 자동으로 super()
를 첫 줄에 삽입하는데, 이를 알고있는지 묻는 유형이다.
📌 업캐스팅
거의 무조건적으로 출제되는 유형.
public class Main {
public static void main(String[] args) {
new Child();
System.out.println(Parent.total);
}
}
class Parent {
static int total = 0;
int v = 1;
public Parent() {
total += (++v);
show();
}
public void show() {
total += total;
}
}
class Child extends Parent {
int v = 10;
public Child() {
v += 2;
total += v++;
show();
}
@Override
public void show() {
total += total * 2;
}
}
public class main{
public static void main(String[] args) {
A b = new B();
b.paint();
b.draw();
}
}
class A {
public void paint() {
System.out.print("A");
draw();
}
public void draw() {
System.out.print("B");
draw();
}
}
class B extends A {
public void paint() {
super.draw();
System.out.print("C");
this.draw();
}
public void draw() {
System.out.print("D");
}
}
📌 업캐스팅과 멤버변수
이 또한 업캐스팅 관련 문제인데,
메소드는 실 객체를 기준으로 하는 반면 멤버변수는 참조 타입 기준임을 알아야 하는 문제이다.
public class Main {
public static void main(String[] args) {
Base a = new Derivate();
Derivate b = new Derivate();
System.out.print(a.getX() + a.x + b.getX() + b.x);
}
}
class Base {
int x = 3;
int getX() {
return x * 2;
}
}
class Derivate extends Base {
int x = 7;
int getX() {
return x * 3;
}
}
📌 static
메소드와 동적바인딩
static
메소드는 동적바인딩이 적용되지 않는 점을 이용해서 낚시를 시도한다.
public class Main{
public static class Parent {
public int x(int i) { return i + 2; }
public static String id() { return "P";}
}
public static class Child extends Parent {
public int x(int i) { return i + 3; }
public String x(String s) { return s + "R"; }
public static String id() { return "C"; }
}
public static void main(String[] args) {
Parent ref = new Child();
System.out.println(ref.x(2) + ref.id());
}
}
📌 call by value를 아십니까?
public class Main {
public static void change(String[] data, String s){
data[0] = s;
s = "Z";
}
public static void main(String[] args) {
String data[] = { "A" };
String s = "B";
change(data, s);
System.out.print(data[0] + s);
}
}
📌 문자열 비교
레퍼런스 비교인지 값 비교인지 구분할 수 있어야 하며,
String Constant Pool과 같은 다소 지엽적인 interning 관련 내용을 출제하기도 한다.
public class Main{
public static void main(String[] args) {
String str1 = 'Programming';
String str2 = 'Programming';
String str3 = new String('Programming');
println(str1==str2)
println(str1==str3)
println(str1.equals(str3))
print(str2.equals(str3))
}
}
📌 Type Erasure 낚시
자바의 오버로딩이 정적 바인딩인 점을 이용하여 제네릭 Type Erasure 관련 낚시 문제를 출제한 적 있다.
자격증 문제 치고는 지엽적인 편이다.
class Main {
public static class Collection<T> {
T value;
public Collection(T t) {
value = t;
}
public void print() {
new Printer().print(value);
}
class Printer {
void print(Integer a){
System.out.print("A" + a);
}
void print(Object a){
System.out.print("B" + a);
}
void print(Number a){
System.out.print("C" + a);
}
}
}
public static void main(String[] args) {
new Collection<>(0).print();
}
}