자격증/정보처리기사 실기 - 기출문제

[정보처리기사 실기] 2022년 3회 04 - 프로그래밍 [JAVA]

쏠솔랄라 2025. 7. 23. 16:12

 

 

 

04 다음 JAVA로 구현된 프로그램을 분석하여 그 실행 결과를 쓰시오. (단, 출력문의 출력 서식을 준수하시오.)

public class Test {
	public static void main(String[] args) {
		int result[] = new int[5];
		int arr[] = {77, 32, 10, 99, 50};
		for(int i=0; i<5; i++) {
			result[i] = 1;
			for(int j=0; j<5; j++)
				if(arr[i]<arr[j])
					resullt[i]++;
		}
		for(int k=0; k<5; k++)
			System.out.print(result[k]);
	}
}

 

 

 

더보기

24513

 

 

 

해설

코드 실행순서 및 해석
1 public class Test {    
2 public static void main(String[] args) { 1 실행
3      int result[] = new int[5]; 2 5개짜리 배열 선언
4      int arr[] = {77, 32, 10, 99, 50}; 3 arr 선언
5      for(int i=0; i<5; i++) { 4 i를 j와 비교하는 함수
6           result[i] = 1;    
7           for(int j=0; j<5; j++)    
8                if(arr[i]<arr[j])    
9                resullt[i]++;    
10      }    
11      for(int k=0; k<5; k++) 5 배열 k의 값을 순서대로 출력
12           System.out.print(result[k]);    
13      }    
14 }    

 

  0 1 2 3 4
result 1+1 1+1+1+1 1+1+1+1+1 1 1+1+1
  0 1 2 3 4
arr[] 77 32 10 99 50

 

i j arr[i]<arr[j] result[i]=1
0 0 F  
  1 F  
  2 F  
  3 T result[0]=2
  4 F  
1 0 T result[1]=2
  1 F  
  2 F  
  3 T result[1]=3
  4 T result[1]=4
2 0 T result[2]=2
  1 T result[2]=3
  2 F  
  3 T result[2]=4
  4 T result[2]=5
3 0 T  
  1 T  
  2 T  
  3 T  
  4 T  
4 0 T result[4]=2
  1 F  
  2 F  
  3 T result[4]=3
  4 F