본문 바로가기
자격증/정보처리기사 실기

정보처리기사[실기] 기출문제_자바(2023년도)

by 김엉배 2023. 9. 22.
728x90
반응형

◎  2023년 1회 01번

class Static{
 
    public int a = 20;
    static int b = 0;
    
}
 
public class Main{
    public static void main(String[] args) {
        
        int a;
        a = 10;
        Static.b = a;
 
        Static st = new Static();
 
        System.out.println(Static.b++);
        System.out.println(st.b);
        System.out.println(a);
        System.out.print(st.a);
    }
}
더보기

* main함수와 class의 변숫값 헷갈리지 않게

 

1) main에서 a = 10으로 초기화, static에 있는 b 변수를 10으로 

2) Statuc.b++ 출력결과에 b++는 출력 이후 값이 증가, 그러므로  출력결과는 10

3) st.b는 main에서 b를 10으로 초기화해줬고, 증가되어서 11

4) a는 main에 있는 변수이므로 10

5) st.a는 20

 

♣ 정답  ->  10

                   11

                   10

                   20

 

◎  2023년 1회 14번

public class Sort {
 
    public static void swap(int[] arr, int idx1, int idx2){
        int temp = idx1;
        arr[idx1] = arr[idx2];
        arr[( 1 )] = temp;
    }
 
    public static void Usort(int[] array, int length){
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    swap(array, j, j + 1);
                }
            }
        }
    }
 
    public static void main(String[] args) {
        int[] item = new int[] { 5, 3, 8, 1, 2, 7 };
        int nx = 6;   
        Usort(item, ( 2 ));
 
        for (int data : item) {
            System.out.print(data + " ");
        }
    }
}
______________________________________________________________
출력값
1 2 3 5 7 8
더보기

1) main에서 Usort(item, (2)) 는 item은 int [] array이고, (2)는 int length가 된다.

2) 출력결과의 length는 6이므로 (2)에 6이 들어가는 것을 유추 할 수 있다.

3) for에서 arry[i] > arr [j+1] 참이면 swap(array, j, j+1) 실행

4) swap 메서드는 a=b, b=c, c=a 패턴이다. 그러므로 (1)은 idx2가 된다.

 

♣ 정답  ->  1) idx , 2) nx

 

◎  2023년 1회 17번

abstact class Vehicle {
        String name;
        abstract public String getName(String val);

        public String getName() {
                return "Vehicle name: " + name;
        }
}

class Car extends Vehicle {
        public Car(String val) {
                name=super.name=val;
        }
        public String getName(String val) {
                return "Car name:" + val;
        }
        public String getName(byte val[]) {
                return "Car name:" + val;
        }
}

public class Test {
        public static void main(String[] args) {
        Vehicle obj = new Car("Spark");
        System.out.println(obj.getName());
        }
}
더보기

1) Veicle obj  = new Car("Spark") 메서드를 실행해서 name, super.name가 Spark 된다.

3) System.out.println(obj.getName())은 Vehicle 실행

 

♣ 정답  -> Vehicle name: Spark

 

◎  2023년 1회 20번

class Parent {
    int x = 100;

    Parent() {
        this(500);
    }
    Parent(int x) {
        this.x = x;
    }
    int getX() {
        return x;
    }
}
class Child extends Parent {
    int x = 4000;

    Child() {
        this(5000);
    }

    Child(int x) {
        this.x = x;
    }
}

public class Main {
    public static void main(String[] args) {
        Child obj = new Child();
        System.out.println(obj.getX());
    }
}
더보기

1) new Child() 생성자 호출 순서는(부모->자식)

2) Parent 부터 실행 this(500)가 되고 다음 메서드 호출하여 x = 500이 된다.

3) Child(자식)이 실행되고 마찬가지로 x = 5000이 된다.

4) getX를 실행하게 되면 이전에 x 가 500이 되었으므로 500이 된다.

 

♣ 정답  -> 500

 

◎  2023년 2회 02번

돈이 총 4620원일 경우 1000원, 500원, 100원, 10원의 지폐 및 동전을 이용해 조건 맞추기
__________________________________________________________________________________
public class Problem {
 public static void main(Stirng[] args) {
  m = 4620;
  
  a = (            );
  b = (            );
  c = (            );
  d = (            );
  
  System.out.println(a); //천원짜리 4장 출력
  System.out.println(b); //오백원짜리 12개 출력
  System.out.println(c); //백원짜리 1개 출력
  System.out.println(d); //십원짜리 2개 출력
  }
 }
더보기
♣ 정답  -> 
a = m / 1000
b= (m % 1000) / 500
c = (m % 500) / 100
d = (m % 100) / 10

 

728x90
반응형