• 필기_Test(2)

    2021. 7. 5.

    by. 와트

    ※ 아래 소스 코드의 컴파일 및 실행 결과는?

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
        public class Pass {
            void doStuff(int x) {
                System.out.print("doStuff x = " + (x++));
            }
     
            public static void main(String[] args) {
                int x = 5;
                Pass p = new Pass();
                p.doStuff(x);
                System.out.print("main x = " + x);
            }
        }
    cs

     

    callByValue로 값을 전달, 메인 메소드의 x와 doStuff 메소드의 x는 각각의 값을 갖게 된다.

    후위연산자로 값이 증가하므로 print할 때 x의 값은 5, 이후 6으로 증가한다.

    메인 메소드의 x 값은 5.

     

    ※ 아래 소스 코드의 컴파일 및 실행 결과는?

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
        public class SwitchExample {
            
            public static intswitchIt(int x) {
                
                int j=1;
            
                switch (x){
                    case 1: j++;
                    case 2: j++;
                    case 3: j++;
                    case 4:j++;
                    case 5: j++;
                    default : j++;
                    }
                return j + x;
                }
     
            public static void main(String[] args) {
                System.out.println(switchIt(4));
            }
        }
    cs

    'break'가 없다는 것만 확인하면 간단하게 풀 수 있다.

    break가 없으므로 case를 끝마쳐도 그 다음 case로 계속 해서 진행한다.

     

    ※ 아래 소스 코드의 컴파일 및 실행 결과는?

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
        public class ForExample {
            public static void main(String[] args) {
                int i=0, j=5;
            
                OUT:
                    for ( ; ; ){
                        i++;
                        for (;;){
                            if(i>--j){
                                break OUT;
                            }
                        }
                    }
                System.out.println("i=" + i + ", j=" + j);
                }
        }
    cs

    이중 for문임을 유념.

    i > --j일 때까지 이를 반복하고 OUT으로 break한다.

     

    ※ 아래 소스 코드의 컴파일 및 실행 결과는?

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
        public class Hello {
            String title;
            int value;
     
            public Hello() {
                title += " World";
            }
     
            public Hello(int value) {
                this.value = value;
                title = "Hello";
                Hello();
            }
        }
     
        //중간 생략
        
        Hello c = new Hello(5);System.out.println(c.title);
     
    }
    cs

    Hello(); →에 주목.

    this나 객체 없이 다른 생성자 호출 불가능.

    컴파일 오류 발생한다.

     

    ※ 아래 소스 코드의 컴파일 및 실행 결과는?

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
        public static void test(String str) {
            
            if(str == null | str.length() == 0) {
                System.out.println(”String is empty”);
                } 
            else {
                System.out.println(”String is not empty”);
                }
            }
     
    //    메소드실행 :
        test(null);
    cs

    | →에 주목.

    if문 앞의 str == null이 조건을 충족하여 뒤의 조건식은 판별하지 않으므로 컴파일 오류는 나지 않지만 실행하면 오류 발생.

     

    '혼자 있는 방 > Java' 카테고리의 다른 글

    Homework(11)  (0) 2021.07.08
    실기_Test(2)  (0) 2021.07.06
    Homework(10)  (0) 2021.07.02
    Homework(9)  (0) 2021.07.01
    Homework(8)  (0) 2021.06.30

    댓글

Designed by Nana