티스토리 뷰
1. 변수의 스코프에 대하여 설명하시오.
중괄호와 중괄호 사이를 가리킨다.
Scope란 영역을 의미한다.
즉, 변수의 스코프는 변수의 영역으로 직역이 가능하고 변수의 범위라고 보면 되겠다.
해당 변수의 스코프는 중괄호 { } 이다.
변수명이 중복되면 에러가 난다.
2.지역변수란?
함수 안에 선언 된 모든 변수
cf) 전역변수: 어디서나 사용 가능한 변수. 함수 바깥에 선언된 변수
3.인스턴스 변수란?
클래스 내에 선언된 변수 (인스턴스란 객체를 의미 )
4. 아래의 함수를 펙토리얼로 표현하시오.
System.out.println("3 factorial: " + factorial(3));
System.out.println("3 factorial: " + factorial(12));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class Factorial{
public static void main(String[] args) {
System.out.println("3 factorial: " + factorial(3));
System.out.println("3 factorial: " + factorial(12));
}
public static int factorial(int i) {
if (i == 1)
return 1;
else
return i * factorial(i - 1);
}
}
|
cs |

5.클래스의 구성요소는?
클래스 = 데이터 + 기능
클래스는 변수와 메소드 2개로 구성되어 있다.
6.원의 넓이는 구하는 프로그램을 아래와 같이 작성하시오.
- 원클래스를 만들것
- 변수 radius
- 변수 radius에 대한 setter getter 함수 만들것
- getArea 함수
- 메인 메소드를 가진 다른 클래스(AreaTest)에서 원 객체를 생성할것
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
|
class Circle {
int radius;
public void setRadius(int r) {
radius = r;
}
public int getRadius() {
return radius;
}
public double getArea() {
return radius * radius * Math.PI;
}
}
public class AreaTest {
public static void main(String[] args) {
Circle circle = new Circle();
circle.setRadius(6);
int radius = circle.getRadius();
double area = circle.getArea();
System.out.println("반지름:" + radius);
System.out.println("원의 넓이:" + area);
}
}
|
cs |

7. 객체에 대하여 설명하시오.
객체란 인스턴스를 의미하며, class를 메모리에 올린 상태를 뜻한다.
8. 아래의 클래스에 대하여, 메모리 그림을 그리시오.
class Rectangle{
int width;
int height;
public int getArea() {
return width * height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}

9.클래스와 객체의 차이는?
- 클래스 : .class
- 객체: .class를 메모리에 올린것 (우리가 그림으로 그리는 것처럼 올라감. 그게 객체 생성)
10.아래의 프로그램을 작성하시오.
1 부터 num 까지 합을 구하는 class 를 작성하도록 하시오.
힌트:
public class SumMain {
public static void main(String[] args) {
GetSum getsum = new GetSum(); //1)객체 생성
int num; //2)num 변수 선언
num = 50;
getsum.setNum(num); //3)getsum 객체의 setNum함수 호출, num값 50 저장
getsum.sum(); //4)getsum객체의 sum함수 호출, 1-50까지 합 구함
}
}
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
|
class GetSum {
int num;
public void setNum(int num) {
this.num = num;
}
public int sum() {
int sum = 0;
for (int i = 1; i <= num; i++) {
sum += i;
}
System.out.println(sum);
return sum;
}
}
public class SumMain {
public static void main(String[] args) {
GetSum getsum = new GetSum();
int num;
num = 50;
getsum.setNum(num);
getsum.sum();
}
}
|
cs |

11.아래의 프로그래밍을 작성하시오.
Gugudan gugudan = new Gugudan();
gugudan.printGugu(10); //1단부터 10단까지 출력
gugudan.printGugu(20); //1단부터 20단까지 출력
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
|
class Gugudan {
int gugudan;
public void printGugu(int dan) {
gugudan = dan;
for (int i = 1; i <= dan; i++) {
for (int j = 1; j < 10; j++) {
System.out.print(i + "x" + j + "=" + (i * j) + " ");
}
System.out.println();
}
System.out.println();
}
}
public class GugudanTest{
public static void main(String[] args) {
Gugudan gugudan = new Gugudan();
gugudan.printGugu(10);
gugudan.printGugu(20);
}
}
|
cs |

'면접준비 > KOSMO 허쌤 숙제' 카테고리의 다른 글
절대경로, 상대경로, 정보은닉, 접근제한자, 지역변수, Rec함수좌표(21/10/14) (0) | 2021.10.27 |
---|---|
생성자, 디폴트 생성자, null (21/10/13) (0) | 2021.10.27 |
함수,별찍기 복습, 도형넓이(21/10/11) (0) | 2021.10.27 |
화폐, 구구단, 별찍기 (21/10/08) (0) | 2021.10.27 |
switch문, 절대값, while, break, continue, 구구단, 배수 (21/10/07) (0) | 2021.10.27 |