Chapter 05 - 참조 타입 확인문제

김태원·2023년 1월 10일
0

Chapter 05 - 참조 타입 확인문제

정답: 4

참조 타입은 null값으로 초기화할 수 있다.

정답: 3

자바는 객체를 직접 제거하는 방법을 제공하지 않는다. 객체를 제거하는 유일한 방법은 객체의 모든 참조를 없애는 것이다.
이러한 객체는 쓰레기(Garbage)가 되고, 자바는 쓰레기 수집기(Garbage Collector)를 실행시켜 자동으로 제거한다.

정답: 2

String 타입의 문자열 비교는 equals() 메소드를 사용해야 한다.

정답: 2

; array 가 삭제되어야 한다.

정답: 3

boolean 타입 배열 항목의 기본 초기값은 false이다.

정답:

3
5

정답:

int[] array = {1, 5, 3, 8, 2};
int maxNum = 0;

for (int j : array) {
    if (maxNum < j) maxNum = j;
}

System.out.println(maxNum);

int[][] array = {{95, 86}, {83, 92, 96}, {78, 83, 93, 87, 88}};
int length = 0;
int sum = 0;

for (int[] arr1 : array) {
    for (int arr2 : arr1) {
        sum += arr2;
        length++;
    }
}
System.out.println("전체 합: " + sum);
System.out.println("평균: " + (double) sum / length);


정답:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] scores = null;
        int studentNum = 0;
        int menuNum = 0;

        while (menuNum != 5) {
            System.out.println("--------------------------------------------------------------");
            System.out.println("1.학생수 | 2.점수입력 | 3.점수리스트 | 4.분석 | 5.종료");
            System.out.println("--------------------------------------------------------------");
            System.out.print("선택> ");
            menuNum = sc.nextInt();

            switch (menuNum) {
                case 1 -> {
                    System.out.print("학생수> ");
                    studentNum = sc.nextInt();
                    scores = new int[studentNum];
                }
                case 2 -> {
                    if (scores != null) {
                        for (int i = 0; i < scores.length; i++) {
                            System.out.printf("scores[%d]: ", i);
                            scores[i] = sc.nextInt();
                        }
                    } else System.out.println("학생수를 먼저 입력해 주세요!");
                }
                case 3 -> {
                    if (scores != null) {
                        for (int i = 0; i < scores.length; i++) {
                            System.out.printf("scores[%d]: %d\n", i, scores[i]);
                        }
                    } else System.out.println("학생수를 먼저 입력해 주세요!");
                }
                case 4 -> {
                    if (scores != null) {
                        int maxNum = 0;
                        int sum = 0;

                        for (int score : scores) {
                            maxNum = Math.max(maxNum, score);
                            sum += score;
                        }

                        System.out.println("최고 점수: " + maxNum);
                        System.out.println("평균 점수: " + (double) sum / studentNum);
                    } else System.out.println("학생수를 먼저 입력해 주세요!");
                }
                case 5 -> {}
                default -> System.out.println("다시 입력해 주세요.");
            }
        }
        System.out.println("프로그램 종료");
    }
}
profile
개발이 재밌어서 하는 Junior Backend Developer

0개의 댓글