[커널아카데미] 백엔드 개발 부트캠프 12기 2주차 - ch02 - ch.05. 연습문제 노트 및 회고

굴착드릴·2025년 4월 4일

개요

연습문제를 풀어보며 헷갈렸던 개념과 오답노트를 작성해보았습니다.

연습문제

ch02. 자료형


[2-9] 다음 중 형 변환을 생략할 수 있는 것은? (모두 고르시오)

byte b = 10;
char ch = 'A';
int i = 100;
long l = 1000L;

a. b = (byte)i;
b. ch = (char)b;
c. short s = (short)ch;
d. float f = (float)l;
e. i = (int)ch;

1차 답 (25.03.30.)

✅ d, e

맞았지만 e가 확실하지 않았었음

해답

d, e


[2-10] char 타입의 변수에 저장될 수 있는 정수 값의 범위는?

1차 답 (25.03.30.)

❌ 0~256

  • 2byte인 것은 알고 있었지만 범위를 잘못 생각

해답

0~65535

  • 2byte = 2*8bit =16bit =2의 16승

[2-11] 다음 중 변수를 잘못 초기화 한 것은? (모두 고르시오)

a. byte b = 256;
b. char c = '';
c. char answer = 'no';
d. float f = 3.14;
e. double d = 1.4e3f;

1차 답 (25.03.30.)

❌ a, c, d

  • 당시에는 생각 못했음

해답

a, b, c, d

  • char는 반드시 공백이라도 저장해야 함

[2-12] 다음 중 main 메서드의 선언부로 알맞은 것은? (모두 고르시오)

a. public static void main(String[] args)
b. public static void main(String args[])
c. public static void main(String[] arv)
d. public void static main(String[] args)
e. static public void main(String[] args)

1차 답 (25.03.30.)

❌ a, b

해답

a, b, c, e

a. public static void main(String[] args) // a와 b는 배열 선언 방식 차이
b. public static void main(String args[])
c. public static void main(String[] arv) // 매개변수 이름은 중요하지 않음
d. public void static main(String[] args) // void는 main 앞에 와야 함
e. static public void main(String[] args) //

헷갈렸거나 몰랐던 개념

  • int보다 작은 값은 int로 자동 형변환된다는 뜻
    • a+b의 경우 a를 int로 치환, b를 int로 치환 후 a+b 수행
    • 계산 후 해당 자료형에 저장이 가능하다면 묵시적 형변환이 일어남
      char ch = 'A'+ 1 // 65 + 1 = 66, 66은 ch에 담을 수 있으므로 'B'로 자동 형변환
      char ch = 'A' + 1000 // 1065는 ch에 담을 수 없으므로 컴파일 에러 발생
  • 아무리 타입이 float이더라도 연산 중 int ex) 5/9를 만나면 int 연산으로 처리, 0이 되어버림
  • 키워드 조건
    • 제어자의 순서는 상관 없음
    • 제어자는 반환형 + 메소드 명 앞에 위치해야 함

Ch03.


[3-1] 다음 연산의 결과를 적으시오

class Exercise3_1 {
	public static void main(String[] args){
		int x = 2;
		int y = 5;
		char c = 'A'; // A의 문자코드는 65
		
		System.out.println(1 + x << 33);
		System.out.println(y >= 5 || x < 0 && x > 2);
		System.out.println(y += 10 - x++);
		System.out.println(x+=2);
		System.out.println(!('A' <= c && c <= 'z'));
		System.out.println('C' - c);
		System.out.println('5' - '0');
		System.out.println(c+1);
		System.out.println(++c);
		System.out.println(c++);
		System.out.println(c);
	}
}

1차 답

class Exercise3_1 {
	public static void main(String[] args){
		int x = 2;
		int y = 5;
		char c = 'A'; // A의 문자코드는 65
		
		System.out.println(1 + x << 33); // ?
		System.out.println(y >= 5 || x < 0 && x > 2); // true
		System.out.println(y += 10 - x++); // 13
		System.out.println(x+=2); // 5
		System.out.println(!('A' <= c && c <= 'z')); // false
		System.out.println('C' - c); // 2
		System.out.println('5' - '0'); // 5
		System.out.println(c+1); // 66
		System.out.println(++c); // 66
		System.out.println(c++); //66
		System.out.println(c); // 67
	}
}

해답

헷갈렸거나 몰랐던 개념

  1. 비트연산은 shift시 0으로 채움
    • 비트 연산은 자릿수만 옮기므로 CPU 부하를 줄일 수 있습니다.
  2. 증감연산자는 계산 시 자료형을 보존하며 진행합니다.

회고

조건문과 반복문, 그리고 배열부분은 틀린 것이 없었지만 자료형과 연산자 부분에서 문제가 있었습니다. 'IDE가 해주니까 그냥 넘겨버리자', '뭐 대충 다 아는데' 이런 생각으로 넘겼던 부분입니다. 이번 계기를 통해 기초를 튼튼히 공부하자 라는 마음가짐을 되새기게 되었습니다.

profile
두두두두..

0개의 댓글