본문 바로가기

국비과정/JAVA

20230621 _[14일차]_02. 정처기 문제연습

정보처리기사 실기 Java편 (tistory.com)

 

정보처리기사 실기 Java편

안녕하세요. 정보처리기사 실기에 대해 포스팅을 주기적으로 진행하다 보니, 프로그래밍 언어에 대한 문제가 늘어나는 것을 볼 수 있었고, 이로 인해 많은 수험자분들이 많이 탈락하거나 힘들

chobopark.tistory.com

 

Q) 다음 출력 결과를 작성하시오.

public static void main(String[] args){
  Set a = new HashSet();
 
  a.add(7)
  System.out.println(a);
 
  a.add(5)
  System.out.println(a);
 
  a.add(5)
  System.out.println(a);
 
  a.remove(5)
  System.out.println(a);
 
  System.out.println(a.size());  
  
  }

[7]
[5,7]
[5,7]
[7]
1

Q) 다음 Java로 구현된 프로그램을 분석하여 그 실행 결과를 작성하시오.

public class Impl{
 
  public static viod main(String args[]){
  int a = 12, b = 5, sum = 2;
  b *= a /= 4;
  sum += ++a * b-- / 4;
  System.out.printf("%d", sum);  
  
  }
}

 		int a = 12, b = 5, sum = 2;
		b *= a /= 4;			// a = 3, b = 15
		sum += ++a * b-- / 4;		// sum += 4 * 15 / 4 
						// sum = sum + 15 
						// sum = 2 + 15 = 17 , b = 14
                        
		System.out.printf("%d", sum);		// 17

[ 형식문자열 ] _ 외우기

		int sum = 20;
		System.out.printf("%d", sum);	// 20
		
		System.out.printf("%c", 'C');	// C (캐릭터, ''홀따옴표)
		System.out.printf("%s", "문자열"); // 문자열
		
		System.out.printf("%d", 33); // 33
		System.out.printf("%f", 3.14); // 3.140000
		System.out.printf("%.1f", 3.14); // 3.1 (.1 => 소수점 뒤 한자리까지만)
		System.out.printf("%b", true);	// true
		
		System.out.printf("%o", 8);	// 10
		System.out.printf("%x", 17);	// 11
		
		System.out.printf("\n");	// 줄바꿈
		
		System.out.printf("%5d", 1);	// 5칸생성 -> 1을 출력(4칸은 공백)
		System.out.printf("\n");
		System.out.printf("%05d\n", 1);	// 5칸생성 -> 1을 출력(4칸은 0)
		System.out.println("");	// 엔터
		
		System.out.printf("%5d", 55);	// ___55
		System.out.println("");
		System.out.printf("%-5d", 55);	// 55___
		
		System.out.println("");

		System.out.printf("%10s\n", "가나다라");	// ______가나다라
		System.out.printf("%-10s\n", "가나다라");	// 가나다라______

public class Problem{
    public static void main(String[] args){
        int i = 0, hap = 0;
        do{
            ++i;
            hap += i;
        } while(i<5);
        System.out.println("%d, %d\n", i, hap);
    } 
}

5, 15

public class Test {
	public static void main(String[] args) {

		int a = 5, b = 9, c;
		c = b % 5 < 5 ? 1 : 0;	// 삼항연산자  *** 1
		c = c | c << 3;			// |(or)	***2
		c = a < 5 || c >= 10 ? c - a : c + a;	// 삼항연산자	*** 3
		System.out.printf("%d", c);

	}
}

		/* 
		1.  
			4 (b를 5로나눈 나머지) 가 5 보다 작으면 참(1) => c = 1
		
		2. 
			| (or) 이진수로 비교했을때 하나라도 1이 있으면 1.
			1 | 1000 
			0001
			1000
		=>	1001 = (십진수) 9  => c = 9
		
		3.
			a < 5 랑 c >=10  둘다 참이면 c-1, 거짓이면 c+a 
			5<5(false) | 9 >=10(false) => 거짓이므로 c = c + a = 9 + 5 = 14
		*/

Q) 다음 Java로 구현된 프로그램을 분석하여 그 실행 결과를 쓰시오.

class SuperObject {
    public void paint(){
        draw();
    }
 
    public void draw(){
        draw();
        System.out.println("Super Object");
    }
}
 
 
class SubObject extends SuperObject {
    public void paint(){
        super.draw();
    }
 
    public void draw(){
        System.out.println("Sub Object");
    }
}
 
 
public class Test{
    public static void main(String[] args){
        SuperObject a = new SubObject();
        a.paint();
    } 
}

class SuperObject (부모)

    > paint() 메소드

            > draw() 메소드를 실행

    > draw() 메소드

            > draw() 메소드를 실행 _ 실행완료후 아래기능 실행

            > "Super Object" 출력

 

class SubObject (자식)

    > paint() 메소드

            > draw() 메소드를 실행

    > draw() 메소드

          > "Sub Object" 출력

Sub Object
Super Object		// a.paint(); => SubObject의 paint() 메소드 실행

Q) 다음은 Java로 1부터 100의 범위 안에서 가장 큰 소수를 구하는 알고리즘을 구현한 것이다.

괄호에 들어갈 가장 적합한 답을 쓰시오.

public class Test{
    public static void main(String[] args){
        int p = 2;
        int n = 3;
        while(true){
            double t = Math.sqrt(n);
            int m = (int)t;
            for(int i=2; i<=m; i++){
                int r = n % i;
                if(r==0)
                    break;
                if(i==m)
                    p = (    );
            }
            n++;
            if(n>100)
                break;
        }
        System.out.println("%d\n", p);
    }
}

* Math.sqrt(n) : double타입의 인수를 전달하면 인수에 대한 double타입의 제곱근 값을 리턴