1. Marker interface란?
· Marker는 interface의 한 종류로 클래스에 특정 표시를 해두기 위한 목적으로 사용된다.
· 즉, 클래스를 마킹하여 종류를 분류해주는 역할을 하며 Lower{}, Upper{} 등과 같은 방식으로 사용한다.
2. abstract 키워드에 대하여 설명하시오.
· abstract 키워드는 메소드와 클래스 앞에 표기 가능하고, 추상메소드/추상클래스라 정의한다.
· abstract는 추상메소드를 이용하여 "자손 class가 구현하라" 라는 의미를 지닌다.
· 클래스 내에 추상메소드가 한 개라도 존재한다면 추상클래스라 정의하며, abstract class로 변경해야한다.
(메소드와 마찬가지로 구현부분이 존재하지 않기 때문에 인스턴스 생성이 불가, 변수 선언은 가능하다.)
· ex)
3. 추상클래스와 인터페이스의 차이점
· 추상클래스는 ababstract 키워드를 표기해야 하지만, 인터페이스는 abstract public을 표기하지 않아도 컴파일러에 의해 자동 생성된다.
· abstract는 일반메소드와 추상메소드로 구성 가능하지만, interface는 추상메소드만으로 구성된다.
· 추상클래스를 상속받을 때 extends 키워드를 사용하고 단일 상속되며, 인터페이스는 implements 키워드를 사용하고 다중 구현이 가능하다.
4. 에러와 예외의 차이는?
※에러란?
· 시스템에 비정상적인 상황을 말하며, 원인이 로직에서 존재하지 않는다(코딩상의 문제X)
· 코딩상의 문제가 아니며 메모리가 꽉 찼을 떄 등의 문제를 말한다.
※예외란?
· Exception은 개발자가 구현한 로직에서, 실행 도중 발생하는 정상적이지 않은 상황을 말한다.
· 자바의 예외처리 메커니즘은 문제가 발생하는 지점에 대한 정보 출력과 동시에 프로그램을 종료시킨다.
5. unchecked와 cheked 예외의 차이는?
· unchecked Exception은 RuntimeException을 상속하고, 명시적인 처리를 강제하지 않는다.
· cheked Exception은 RuntimeException을 상속하지 않고, 반드시 예외 처리가 필요하다.(try~catch)
6. 예외처리 UML를 그리시오. 외울 것!!
· 예외처리 메커니즘(Try Catch finally)을 제공한다.
· Exception은 코드의 문법적 오류가 아닌, 프로그램 실행 과정에서 발생하는 예외적 상황을 표현하기 위한 클래스들이며,
RuntimeException을 상속하는 Unchecked Exception와 Checked Exception로 구성된다.
· RuntimeException은 실행중간에 오류가 발생하고, 코드 오류로 발생하는 경우가 대부분이다.
대표적인 예)
ArithmeticException: 수학 연산에서의 오류를 의미, InputMismatchException: Scanner를 통한 값입력의 오류를 의미
7. 사칙연산 계산기를 아래의 조건으로 짜시오.
· 참고: interface를 활용하고, 예외 처리 메커니즘을 적용할 것
· 작성
1
2
3
4
5
6
7
|
public interface ICalculate{
int add(int x, int y);
int sub(int x, int y);
int mul(int x, int y);
int div(int x, int y);
int play();
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
class Calculator implements ICalculate{
private int x;
private int y;
private String oper;
@Override
public int add(int x, int y) {
return x + y;
}
@Override
public int sub(int x, int y) {
return x - y;
}
@Override
public int mul(int x, int y) {
return x * y;
}
@Override
public int div(int x, int y) {
return x / y;
}
@Override
public int play() {
Scanner sc = new Scanner(System.in);
while(true) {
try{
System.out.println("숫자, 연산자를 입력해주세요.");
int x = sc.nextInt();
String oper = sc.next();
int y = sc.nextInt();
}
catch(InputMismatchException | ArithmeticException e) {
System.out.println("다시 입력해주세요.");
}
switch(oper) {
case "+" :
System.out.println(add(x, y));
break;
case "-" :
System.out.println(sub(x, y));
break;
case "*" :
System.out.println(mul(x, y));
break;
case "/" :
System.out.println(div(x, y));
break;
}
}
}
}
|
cs |
1
2
3
4
5
6
|
class Calmain{
public static void main(String[] args) {
Calculator cal = new Calculator();
cal.play();
}
}
|
cs |
8. 다음 Stack interface를 상속받아 실수를 저장하는 StringStack 클래스를 구현하라. **필수**
· 참고
· 작성
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
class StringStack implements Stack {
private String stack[];
private int top; // top은 index와 같은 의미.
StringStack(int length) {
stack = new String[length];
top = -1; // 0도 상관은 없음.
}
@Override
public int length() {
return stack.length;
}
@Override
public String pop() {
if (top == -1) // 한개도 없다는 의미, 즉 꺼낼게 없다.
return "스택이 비어있습니다.";
return stack[top--]; // 꺼냈으니 줄여야하므로.
}
@Override
public boolean push(String val) {
if (top == stack.length - 1) // 꽉 찼다 라는 의미
return false;
stack[++top] = val;
return true;
}
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
class StackApp{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("총 스택 저장 공간의 크기 입력>>");
int num = sc.nextInt();
StringStack stack = new StringStack(num);
while(true) {
System.out.println("문자열 입력>> ");
String word = sc.next();
if(word.equals("그만"))
break;
if(!stack.push(word)) {
System.out.println("스택이 꽉 차서 푸시 불가");
break;
}
}
System.out.print("스택에 저장된 모든 문자열 팝 : ");
int len = stack.length();
for(int i = 0; i<len; i++) {
String s = stack.pop();
System.out.print(s + " ");
}
sc.close();
}
}
|
cs |
9. 다음 필드와 메소드를 가진 4개의 클래스 Add, Sub, Mul, Div를 작성하여라.
· 참고
· 작성
1
2
3
4
5
6
7
8
9
10
|
abstract class Calculator {
protected int num1;
protected int num2;
public void setValue(int num1, int num2) {
this.num1 = num1;
this.num2 = num2;
}
abstract public int calculate();
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
class Add extends Calculator{
@Override
public int calculate() {
return num1 + num2;
}
class Sub extends Calculator{
@Override
public int calculate() {
return num1 - num2;
}
}
class Mul extends Calculator{
@Override
public int calculate() {
return num1 * num2;
}
}
class Div extends Calculator{
@Override
public int calculate() {
return num1 / num2;
}
}
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
class Calculatormain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int result = 0;
System.out.println("두 정수와 연산자를 입력하시오 >> ");
try {
int num1 = sc.nextInt();
int num2 = sc.nextInt();
char op = sc.next().charAt(0);
Calculator cal = null;
switch(op) {
case '+':
cal = new Add();
break;
case '-':
cal = new Sub();
break;
case '*':
cal = new Mul();
break;
case '/':
cal = new Div();
break;
default:
System.out.println("잘못된 연산입니다. ");
}
cal.setValue(num1, num2);
System.out.println(cal.calculate());
}
catch(Exception e) {
System.out.println("비정상 종료입니다.");
e.printStackTrace();
}
sc.close();
}
}
|
cs |
10. 도형을 정의한 Shape클래스를 조상으로 하는 Circle, Rectangle클래스를 작성하시오.
· 참고: 생성자도 각 클래스에 맞게 적절히 추가할 것!
· 작성
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
class Point {
int x;
int y;
Point() {
this(0, 0);
}
Point(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "{" + x + "," + y + "]";
}
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class Circle extends Shape {
double r;
Circle(double r) {
this(new Point(0, 0), r);
}
Circle(Point p, double r) {
super(p);
this.r = r;
}
double calcArea() {
return r * r * Math.PI;
}
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
class Rectangle extends Shape {
double width;
double height;
Rectangle(double width, double height) {
this(new Point(0, 0), width, height);
}
Rectangle(Point p, double width, double height) {
super(p);
this.width = width;
this.height = height;
}
double calcArea() {
return width * height;
}
boolean isSquare() {
if (width == height) {
return true;
} else
return false;
}
}
|
cs |
11. 10번에서 정의한 클래스들의 면적을 구하는 메소드를 작성하고 테스트 하시오.
· 작성
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class Test {
static double sumArea(Shape[] arr) {
double sum = 0;
for(int i = 0; i < arr.length; i++)
sum += arr[i].calcArea();
return sum;
}
public static void main(String[] args) {
Shape[] arr = {new Circle(5, 0), new Rectangle(3, 4), new Circle(1)};
System.out.print("면적의 합: " + sumArea(arr));;
}
}
|
cs |
'bitcamp > 면접족보' 카테고리의 다른 글
면접족보 20/12/17_Shallow, Deep copy (0) | 2021.02.01 |
---|---|
면접족보 20/12/16_예외처리 (0) | 2021.02.01 |
면접족보 20/12/14_인터페이스, 추상클래스 개념 (0) | 2021.01.31 |
면접족보 20/12/11_다형성, 오버라이딩 (0) | 2021.01.31 |
면접족보 20/12/10_상속 (0) | 2021.01.30 |