이것이 자바다 개정판 5장 연습문제

고태경·2023년 7월 1일

1. 참조 타입에 대한 설명으로 틀린 것

2번 참조 타입 변수의 메모리 생성 위치는 스택이다.

메모리 생성 위치는 힙임

2. 자바에서 메모리 사용에 대한 설명으로 틀린 것

3번 참조되지 않는 객체는 프로그램에서 직접 소멸 코드를 작성하는 것이 좋다.

개발자가 직접 객체를 없애는 코드는 없는 대신, 가비지 컬렉터가 사용하지 않는 객체를 제거한다.

3. String 타입에 대한 설명으로 틀린 것

2번 String 타입의 문자열 비교는 ==를 사용해야 한다.

==를 사용하면 메모리 번지를 비교하게 된다. equals 메소드를 이용해야 한다.
+ String은 클래스 타입

4. 배열을 생성하는 방법으로 틀린 것

2번 int[] array; array = {1, 2, 3};

5. 배열의 기본 초기값에 대한 설명으로 틀린 것

3번 실수 타입 배열 항목의 기본 초기값은 true이다.

false임

6. 배열 길이 출력 코드 실행 결과

3 (array.length는 첫번째 차원의 길이를 출력함)
5

7. 주어진 배열 항목에서 최대값을 출력하는 코드

		int[] array  = {1, 5, 3, 8, 2};
		
		int top;
		top = array[0];
		for(int i = 1; i < array.length; i++) {
			if(top < array[i])
				top = array[i];
		}
		System.out.print(top);

8. 주어진 배열 항목의 전체 합과 평균을 구해 출력하는 코드

		int[][] array = {{95, 86}, {83, 92, 96}, {78, 83, 93, 87, 88}};
		
		int sum = 0;
		int count = 0;
		for(int i = 0; i < array.length; i++) {
			for(int j = 0; j < array[i].length; j++) {
				sum += array[i][j];
				count++;
			}
		}
		double avg = (double)sum / count;
		System.out.println(sum);
		System.out.println(avg);

9. 점수 분석 프로그램

		Scanner sc = new Scanner(System.in);
		
		boolean run = true;
		
		int stunum = 0;
		int[] scores = null;
			
		while(run) {
			System.out.println("---------------------------------------------------------");
			System.out.println("1. 학생수 | 2. 점수 입력 | 3. 점수리스트 | 4. 분석 | 5. 종료");
			System.out.println("---------------------------------------------------------");
			System.out.print("선택>");
			
			int choice = sc.nextInt();
			
			switch(choice) {
				case 1:
					System.out.print("학생수>");
					stunum = sc.nextInt();
					scores = new int[stunum];
					for (int i = 0; i < stunum; i++) {
						scores[i] = 0;
					}
					break;
				case 2:
					for(int i = 0; i < stunum; i++) {
						System.out.printf("scores[%d]>", i);
						scores[i] = sc.nextInt();
					}
					break;
				case 3:
					for(int i = 0; i < stunum; i++) {
						System.out.printf("scores[%d]: %d\n", i, scores[i]);
					}
					break;
				case 4:
					int top;
					top = scores[0];
					for(int i = 1; i < scores.length; i++) {
						if(top < scores[i])
							top = scores[i];
					}
					System.out.printf("최고 점수: %d\n", top);
					
					int sum = 0;
					int count = 0;
					for(int i = 0; i < scores.length; i++) {
						sum += scores[i];
						count++;
					}
					double avg = (double)sum / count;
					System.out.printf("평균 점수: %4.1f\n", avg);
					break;
				case 5: 
					System.out.print("프로그램 종료");
					run = false;	
					break;
			}
		}
profile
컴퓨터정보과

0개의 댓글