Fri, 22nd Oct 2021
1. 연산자와 연산식에 대한 설명 중 틀린 것은 무엇입니까? (3)
① 연산자는 피연산자의 수에 따라 단항, 이항, 삼항 연산자로 구분된다.
② 비교 연산자와 논리 연산자의 산출 타입은 boolean(true/false)이다.
③ 연산식은 하나 이상의 값을 산출할 수도 있다. -> 연산식은 하나의 값을 산출한다.
④ 하나의 값이 올 수 있는 자리라면 연산식도 올 수 있다.
2. 다음 코드를 실행했을 때 출력 결과는 무엇입니까?
package sec04_exercise;
public class Exercise02 {
public static void main(String[] args) {
int x = 10;
int y = 20;
int z = (++x) + (y--);
//1. x에 1 증가 = x=11
//2. y는 20 그대로 둠 (y-- 이기 때문에 대입연산자 먼저 수행 후 1 감소시켜야 함)
//3. z = 11 + 20 = 31
//4. y = y - 1 = 19
System.out.println(z); //31
}
}
3. 다음 코드를 실행했을 때 출력 결과는 무엇입니까?
package sec04_exercise;
public class Exercise03 {
public static void main(String[] args) {
int score = 85;
String result = (!(score > 90))? "가" : "나"; //score가 90초과가 아니라면(90 이하라면) "가", 90초과라면 "나"
System.out.println(result); //가
}
}
4. 534자루의 연필을 30명의 학생들에게 똑같은 개수로 나누어 줄 때 학생당 몇개를 가질 수 있고, 최종적으로 몇 개가 남는지를 구하는 코드입니다. ( #1)과 ( #2)에 들어갈 알맞은 코드를 작성하세요.
package sec04_exercise;
public class Exercise04 {
public static void main(String[] args) {
int pencils = 534;
int students = 30;
//학생 한 명이 가지는 연필 수
int pencilsPerStudent = pencils / students;
System.out.println(pencilsPerStudent); //17
//남은 연필 수
int pencilsLeft = pencils & students;
System.out.println(pencilsLeft); //22
}
}
5. 다음은 십의 자리 이하를 버리는 코드입니다. 변수 value의 값이 356이라면 300이 나올 수 있도록 ( #1)에 알맞은 코드를 작성하세요(산술 연산자만 사용하세요).
package sec04_exercise;
public class Exercise05 {
public static void main(String[] args) {
int value = 356;
System.out.println( (value/100) * 100); //300
}
}
6. 다음 코드는 사다리꼴의 넓이를 구하는 코드입니다. 정확히 소수자릿수가 나올 수 있도록 ( #1)에 알맞은 코드를 작성하세요.
package sec04_exercise;
public class Exercise06 {
public static void main(String[] args) {
int lengthTop = 5;
int lengthBottom = 10;
int height = 7;
double area = ((lengthTop + (lengthTop / 2.0)) * height);
//double area = ( (lengthTop + lengthBottom) * height / 2.0 );
//사다리꼴 넓이 공식 = ( (lenghTop + lengthBottom) * height / 2.0 )
System.out.println(area); //52.5
}
}
7. 다음 코드는 비교 연산자와 논리 연산자의 복합 연산식입니다. 연산식의 출력 결과를 괄호 ( ) 속에 넣으세요.
package sec04_exercise;
public class Exercise07 {
public static void main(String[] args) {
int x = 10;
int y = 5;
System.out.println( (x>7) && (y<=5) ); //true
System.out.println( (x%3 == 2) || (y%2 != 1) ); //false
}
}
8. 다음은 % 연산을 수행한 결과갑에 10을 더하는 코드입니다. NaN 값을 검사해서 올바른 결과가 출력될 수 있도록 ( #1)에 들어갈 NaN을 검사하는 코드를 작성하세요.
package sec04_exercise;
public class Exercise08 {
public static void main(String[] args) {
double x = 5.0;
double y = 0.0;
double z = x % y;
if(Double.isNaN(z)) {
System.out.println("0.0으로 나눌 수 없습니다.");
} else {
double result = z + 10;
System.out.println("결과 : " + result);
}
}
}
0.0으로 나눌 수 없습니다.
'Java > 3. 연산자 (Operator)' 카테고리의 다른 글
Day 4 : Practice #2 (0) | 2021.10.25 |
---|---|
Day 3 : Practice #1 (0) | 2021.10.25 |
Day 3 : 연산자 (Operator) (0) | 2021.10.25 |