본문 바로가기
자바(Java)/자바 AP

[자바AP연습문제]03.Boolean Expressions and If Statements

by 긱펀 2024. 3. 23.
반응형

[자바AP연습문제]03.Boolean Expressions and If Statements

 

 

문제에 대한 정답은 제일 아래 "더보기" 클릭

 

 

*03.Boolean Expressions and If Statements에 대한 복습 내용은 아래 "더보기" 클릭

더보기

In this chapter you learned about conditionalsConditionals are used to execute code when a boolean expression is true or false. A boolean expression is one that is either true or false like x > 0.

 

  • Block of statements - One or more statements enclosed in an open curly brace { and a closing curly brace }.
  • Boolean expression - A mathematical or logical expression that is either true or false.
  • compound Boolean expressions - A Boolean expression with two or more conditions joined by a logical and && or a logical or ||.
  • conditional - Used to execute code only if a Boolean expression is true.
  • DeMorgan’s Laws - Rules about how to distribute a negation on a complex conditional.(드 모르간에 대한 더 자세한 내용은 링크: https://wooduino.tistory.com/207 )
  • logical and - Used in compound boolean expressions that are true if both conditions are true.
  • logical or - Used in compound boolean expressions that are true if one of the conditions is true.
  • negation ! - turns a true statement false and a false statement true
  • short circuit evaluation - The type of evaluation used for logical and (&&) and logical or (||) expressions. If the first condition is false in a compound boolean expression joined with a logical and, then the second condition won’t be evaluated. If the first condition is true in a compound boolean expression joined with a logical or then the second condition won’t be evaluate.
  • if (Boolean expression) - used to start a conditional statement. This is followed by a statement or a block of statements that will be executed if the Boolean expression is true.
  • else - used to execute a statement or block of statements if the Boolean expression on the if part was false.
  • else if (Boolean expression) - used to have 3 or more possible outcomes such as if x is equal, x is greater than, or x is less than some value. It will only execute if the condition in the ‘if’ was false and the condition in the else if is true.
  • 문자열 비교, Comparing Objects with == , equals() => 복습 링크: https://wooduino.tistory.com/209

 

[문제1]

What is the value of x after the statement has been executed?

1
2
boolean x = (5 % 3 == 0== (3 > 5);
 
cs

 

A. false

B. true

C. (5 % 3 == 0)

D. (3 > 5)

E. 2

 

 

[문제2]

Consider the following Boolean expression in which the int variables x and y have been properly declared and initialized. Which of the following values for x and y will result in the expression evaluating to true ?

1
2
(x >= 10== (y < 12)
 
cs

 

A. x = 10 and y = 12

B. x = 9 and y = 9

C. x = 10 and y = 11

D. x = 10 and y = 13

E. x = 9 and y = 12

 

 

[문제3]

What is printed as a result of executing the code segment?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int speed = 35;
boolean rain = false;
 
if (rain)
{
   speed -= 10;
}
 
if (rain == false)
{
  speed += 5;
}
 
if (speed > 35)
{
   speed = speed - 2;
}
 
System.out.println(speed);
cs

 

A. 28

B. 35

C. 38

D. 25

E. 33

 

[문제4]

What is printed as a result of executing the code segment?

1
2
3
4
5
6
7
8
9
10
11
12
13
int x = 5;
 
if (x < 5)
{
   x = 3 * x;
}
 
if (x % 2 == 1)
{
   x = x / 2;
}
 
System.out.print(2*+ 1);
cs

 

A. 3

B. 11

C. 31

D. 15

E. 5

 

[문제5]

Consider the following code segment where a range of “High”, “Middle”, or “Low” is being determined where x is an int and a “High” is 80 and above, a “Middle” is between 50 - 79, and “Low” is below 50.

Which of the following initializations for x will demonstrate that the code segment will not work as intended?

1
2
3
4
5
6
7
8
9
10
11
12
13
if (x >= 80)
{
   System.out.println("High");
}
 
if (x >= 50)
{
  System.out.println("Middle");
}
else
{
   System.out.println("Low");
}
cs

 

A. 80

B. 60

C. 50

D. 30

E. -10

 

[문제6]

Assume an int variable x has been properly declared and initialized. Which of the following code segments will print out “High” if x is 66 and above, “Medium” is x is between 33-65, and “Low” if x is below 33.

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
I.   if (x > 66)
     {
       System.out.println("High");
     }
     else if (x > 33)
     {
       System.out.println("Medium");
     }
     else {
       System.out.println("Low");
     }
 
II.  if (x < 33)
     {
       System.out.println("Low");
     }
     else if (x < 66)
     {
       System.out.println("Medium");
     }
     else {
       System.out.println("High");
     }
 
III. if (x >= 66)
     {
       System.out.println("High");
     }
     if (x >= 33)
     {
       System.out.println("Medium");
     }
     if (x < 33)
     {
       System.out.println("Low");
     }
cs

 

A. I only

B. II only

C. III only

D. I and II only

E. II and III only

 

[문제7]

Consider the following code segment. What is printed as a result of executing the code segment?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int x = 10;
int y = 5;
 
if (x % 2 == 0 && y % 2 == 0 || x > y)
{
    System.out.print("First ");
 
    if (y * 2 == x || y > 5 && x <= 10)
    {
       System.out.print("Second ");
    }
    else
    {
       System.out.print("Third ");
    }
}
cs

 

A. Nothing is printed out.

B. First

C. Third

D. FirstSecond

E. FirstThird

 

[문제8]

Which of the following best describes the value of the Boolean expression:

1
&& !(b || a)
cs

 

A. The value is always true.

B. The value is always false.

C. The value is true when a has the value false, and is false otherwise.

D. The value is true when b has the value false, and is false otherwise.

E. The value is true when either a or b has the value true, and is false otherwise.

 

 

[문제9]

Which of the following expressions evaluate to true after the code segment above executes?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
String message = new String("AP Practice");
String note = new String("AP Practice");
String memo = new String("memo");
int i = 5;
 
if (message.equals(note) && !message.equals("memo"))
{
    message = note;
 
    if (message == note && message.length() > i)
    {
       i = 3;
       memo = message.substring(i);
    }
}
cs

 

A. message == note && message == memo

B. message.equals(note) && message.equals(memo)

C. message == note && memo.equals(“Practice”)

D. message != note || message == memo

E. message.equals(memo) || memo.equals(note)

 

[문제10]

What does the following code print when x has been set to 187?

1
2
3
4
5
6
7
8
9
10
11
12
if (x < 0)
{
   System.out.println("x is negative");
}
else if (x == 0)
{
    System.out.println("x is zero");
}
else
{
    System.out.println("x is positive");
}
cs

 

A. x is negative

B. x is zero

C. x is positive

 

[문제11]

What is printed when the following code executes and x equals 4 and y equals 3?

1
2
3
4
if (!(x < 3 || y > 2))
   System.out.println("first case");
else
   System.out.println("second case");
cs

 

A. first case

B. second case

 

[문제12]

What is the value of grade when the following code executes and score is 80?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if (score >= 90)
{
    grade = "A";
}
if (score >= 80)
{
    grade = "B";
}
if (score >= 70)
{
    grade = "C";
}
if (score >= 60)
{
    grade = "D";
}
else
{
    grade = "E";
}
cs

 

A. A

B. B

C. C

D. D

E. E

 

[문제13]

What is printed when the following code executes and x has been set to zero and y is set to 3?

1
2
3
4
5
6
7
8
if (x > 0 || (y / x) == 3)
{
    System.out.println("first case");
}
else
{
    System.out.println("second case");
}
cs

 

A. first case

B. second case

C. You will get a error because you can't divide by zero.

 

[문제14]

Which of the following expressions is equivalent to !(c || d) ?

A. (!c) && (!d)

B. (c || d)

C. (c && d)

D. !(c && d)

E. (!c) || (!d)

 

[문제15]

Which of the following is equivalent to the code segment below?

1
2
3
4
if (x > 2)
   x = x * 2;
if (x > 4)
   x = 0;
cs

 

A. x = 0;

B. if (x > 2) { x *= 2; }

C. if (x > 2) { x = 0; }

D. if (x > 2) { x = 0; } else { x *= 2; }

 

 

[문제16]

Which of the following is equivalent to the code segment below?

1
2
3
4
if (x > 0)
   x = -x;
if (x < 0)
   x = 0;
cs

 

A. x = 0;

B. if (x > 0) { x = 0; }

C. if (x < 0) { x = 0; }

D. if (x > 0) { x = -x; } else { x = 0; }

E. if (x < 0) { x = 0; } else { x = -1; }

 

 

[문제17]

At a certain high school students receive letter grades based on the following scale: 93 or above is an A, 84 to 92 is a B, 75 to 83 is a C, and below 75 is an F. Which of the following code segments will assign the correct string to grade for a given integer score?

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
I.   if (score >= 93)
        grade = "A";
     if (score >= 84 && score < 93)
        grade = "B";
     if (score >=75 && score < 84)
        grade = "C";
     if (score < 75)
        grade = "F";
 
II.  if (score >= 93)
        grade = "A";
     if (score >= 84)
        grade = "B";
     if (score >=75)
        grade = "C";
     if (score < 75)
        grade = "F";
 
III. if (score >= 93)
        grade = "A";
     else if (score >= 84)
        grade = "B";
     else if (score >= 75)
        grade = "C";
     else
        grade = "F";
cs

 

A. I and III only

B. II only

C. III only

D. I and II only

E. I, II, and III

 

[문제18] 

Assuming that x and y have been declared as valid integer values, which of the following is equivalent to this statement?

1
2
(x > 15 && x < 18|| (x > 10 || y < 20)
 
cs

 

A. (x > 15 && x < 18) && (x > 10)

B. (y < 20) || (x > 15 && x < 18)

C. ((x > 10) || (x > 15 && x < 18)) || (y < 20)

D. (x < 10 && y > 20) && (x < 15 || x > 18)

 

[문제19]

What would the following print?

1
2
3
4
5
int x = 3;
int y = 2;
if (y / x > 0)
   System.out.print("first ");
   System.out.print("second ");
cs

 

A. first

B. second

C. first second

D. Nothing will be printed

 

[문제20]

What would the following print?

1
2
3
4
5
6
7
8
9
10
11
int x = 3;
int y = 2;
if (x > 2)
   x++;
if (y > 1)
   y++;
if (x > 2)
   System.out.print("first ");
if (y < 3)
   System.out.print("second ");
System.out.print("third");
cs

 

A. first

B. first second

C. first second third

D. first third

E. third

 


 

[문제 정답은 아래 "더보기" 클릭]

더보기

[문제1 정답]

B

( (5 % 3 == 0) is false and (3 > 5) is false, and false == false is true! Tricky! )

 

[문제2 정답]

C, E

 

[문제3 정답]

C

(The first if statement condition is false, and the second and third if conditions are true)

 

[문제4 정답]

E

(The first if statement is not true. The second one is true since 5 is odd, and x becomes 2. And 2*2 + 1 = 5 is printed out)

 

[문제5 정답]

A

(This would print out both “High” and “Middle”, showing that there is an error in the code. As you will see in the next lesson, one way to fix the code is to add another else in front of the second if)

 

[문제6 정답]

B

 

[문제7 정답]

D

 

[문제8 정답]

B

(Yes, a && !(b || a) = a && !b && !a. Since (a && !a) can never be true, the result will always be false)

 

[문제9 정답]

C

(both if statements in the code above execute changing message to equal note and memo to equal “Practice”)

 

[문제10 정답]

C

 

[문제11 정답]

B

(This will print if x is less than 3 or y is greater than 2)

 

[문제12 정답]

 D

(Each of the if statements will be executed. So grade will be set to B then C and finally D)

 

[문제13 정답]

 C

 

[문제14 정답]

 A

(NOTing (negating) an OR expression is the same as the AND of the individual values NOTed (negated). See DeMorgans laws)

 

[문제15 정답]

 C

(If x is greater than 2 it will be set to 0)

 

[문제16 정답]

 A

(No matter what x is set to originally, the code will reset it to 0)

 

[문제17 정답]

 A

(Choice I uses multiple if's with logical ands in the conditions to check that the numbers are in range. Choice II won't work since if you had a score of 94, it would first assign the grade to an "A" but then it would execute the next if and change the grade to a "B" and so on until the grade was set to a "C". Choice III uses ifs with else if to make sure that only one conditional is executed)

 

[문제18 정답]

 C

(The commutative property allows the terms to be switched around, while maintaining the value. In this case, the || symbol is used with the commutative property and the statement included the && must stay together to follow the laws of logic)

 

[문제19 정답]

 B

(The first will not print because integer division will mean that y / x is 0. The second will print since it is not in the body of the if (it would be if there were curly braces around it)

 

[문제20 정답]

 D

(The first will print since x will be greater than 2 and the second won't print since y is equal to 3 and not less than it. The third will always print)

 

 

 

 


 

728x90
반응형

댓글