Java

7주차 퀴즈

디지몬진화 2024. 11. 27. 23:18

객관식 문제

1. Java에서 try 블록에 포함할 수 없는 것은 무엇인가요?

답 : B) catch 블록

 

2. finally 블록의 실행 시점은 언제인가요?

답 : try 블록 이후 항상 실행된다

 

3. 다음 코드에서 출력 결과는?

public class Test {
    public static void main(String[] args) {
        try {
            int result = 10 / 0;
        } catch (ArithmeticException e) {
            System.out.println("Arithmetic Exception");
        } finally {
            System.out.println("Finally Block");
        }
    }
}

답 : A) Arithmetic Exception

      B) Finally Block

 

4. 다음 코드에서 catch 블록이 처리할 수 있는 예외는?

public class Test {
    public static void main(String[] args) {
        try {
            String s = null;
            System.out.println(s.length());
        } catch (ArithmeticException e) {
            System.out.println("Arithmetic Exception");
        }
    }
}

답 : C) 아무것도 처리하지 않음

 

5. throw와 throws의 차이는 무엇인가요?

B) throw는 예외 발생, throws는 선언

 

6. catch 블록에서 예외 처리 후 프로그램이 실행을 계속하기 위해 필요한 것은?

답 : C) 올바른 예외 처리가 이루어짐

 

7. finally 블록이 실행되지 않을 경우는 언제인가요?

답 : C) JVM이 강제 종료될 때

 

8. 다음 코드에서 출력 결과는?

public class Test {
    public static void main(String[] args) {
        try {
            System.out.println("Try Block");
            return;
        } finally {
            System.out.println("Finally Block");
        }
    }
}

답 : A) Try Block

      B) Finally Block

 

9. 다음 코드에서 출력 결과는?

public class Test {
    public static void main(String[] args) {
        try {
            throw new Exception();
        } finally {
            System.out.println("Finally Block");
        }
    }
}

답 : C) Finally Block Exception 발생

 

10. 다음 코드에서 출력 결과는?

public class Test {
    public static void main(String[] args) {
        try {
            throw new RuntimeException();
        } catch (Exception e) {
            System.out.println("Catch Block");
        } finally {
            System.out.println("Finally Block");
        }
    }
}

답 : A) Catch Block

      B) Finally Block

 

11. Java 8에서 스트림의 주요 특징이 아닌 것은?

답 : B) 데이터 변경 가능

 

12. 람다 표현식의 문법 중 올바르지 않은 것은?

답 : D) (x, y) -> { return; x + y; }

 

13. 다음 코드에서 출력 결과는?

import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        Stream.of(1, 2, 3, 4)
              .filter(x -> x % 2 == 0)
              .forEach(System.out::print);
    }
}

 답 : B) 24

 

14. 람다 표현식과 메서드 참조의 차이는?

답 : C) 람다는 메서드 참조보다 유연성이 높다

 

15. 다음 스트림 코드의 출력 결과는?

import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {
        int sum = IntStream.range(1, 5).reduce(0, Integer::sum);
        System.out.println(sum);
    }
}

답 : B) 15

 

16. Java의 함수형 프로그래밍에서 "고차 함수"란 무엇인가요?

답 : B) 다른 함수를 매개변수로 받거나 반환하는 함수

 

17. 다음 코드는 어떤 스트림 연산을 사용하고 있나요?

Stream.of("a", "b", "c")
      .map(String::toUpperCase)
      .forEach(System.out::println);

답 : B) 매핑

 

코드 실행 결과 예측 문제 (5개)

1.

import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        Function<String, Integer> length = String::length;
        System.out.println(length.apply("Lambda"));
    }
}

답 : B) 6

 

2.

import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("a", "b", "c");
        list.stream()
            .filter(x -> x.equals("b"))
            .forEach(System.out::print);
    }
}

답 : B) b

 

3.

import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {
        IntStream.range(1, 5)
                 .map(x -> x * x)
                 .forEach(System.out::print);
    }
}

답 : A) 14916

 

4.

import java.util.Optional;

public class Main {
    public static void main(String[] args) {
        Optional<String> optional = Optional.of("Lambda");
        System.out.println(optional.orElse("Default"));
    }
}

답 : A) Lambda

 

5.

import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        Stream.of("A", "B", "C")
              .filter(s -> s.startsWith("B"))
              .forEach(System.out::println);
    }
}

답 : B) B

 

코딩 테스트

1.  https://www.acmicpc.net/problem/2920 - 브2

 

2. https://www.acmicpc.net/problem/1259- 브1