ap examination alert the apcs examination includes a variety of boolean logic questions. many...

68

Upload: toby-simmons

Post on 19-Jan-2018

215 views

Category:

Documents


0 download

DESCRIPTION

What is a Boolean Statement? The sentence, statement, condition, whatever, must be true or false. No questions, no ambiguities, no arguments. The basis of processing data is the binary system of on and off, or 1 and 0, which certainly sounds a bunch like true or false. Each of the following five statements is a Boolean statement: A mile is longer than a kilometer. July and August both have the same number of days. A pound of feathers is lighter than a pound of lead. The Moon is larger than the Sun. New York City has more people than Baltimore.

TRANSCRIPT

Page 1: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…
Page 2: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

AP Examination AlertThe APCS Examination includes a variety of Boolean Logic questions.

Many questions require indirect knowledge of Boolean Logic, and other questions are directly focused on testing a student’s understanding of Boolean concepts.

Test results have shown that many students score quite poorly on this part of the APCS Examination.

Statistical Analysis of these test results have also shown that the students who perform poorly on Boolean Logic questions, perform poorly on the AP Exam as a whole; and the students who perform well on the Boolean Logic questions, perform well on the AP Exam as a whole.

Page 3: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

What is a Boolean Statement?The sentence, statement, condition, whatever, must be true or false.

No questions, no ambiguities, no arguments.

The basis of processing data is the binary system of on and off, or 1 and 0, which certainly sounds a bunch like true or false.

Each of the following five statements is a Boolean statement:

A mile is longer than a kilometer.July and August both have the same number of days.A pound of feathers is lighter than a pound of lead.The Moon is larger than the Sun.New York City has more people than Baltimore.

Page 4: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

English Sentence Boolean Statement T/F

A mile is longer than akilometer.

Mile > Kilometer True

July and August both samedays.

JulDays == AugDays True

A pound of feathers is lighterthan a pound of lead.

LBFeat < LBLead False

The Moon is larger than theSun.

MoonSize > SunSize False

New York City has morepeople than Baltimore.

NYPop > BaltPop True

Page 5: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Sentences are not always so short and straight forward. Frequently there are multiple conditions in one statement. Special rules need to be followed to determine if the entire statement is true or false.

Consider the following sentences with compound conditions:

She is a computer science teacher and she is a math teacher.The number is odd or the number is even.Enter again if gender is not male or gender is not female.Employment requires a CPA and five years experience.The same sentences converted into Boolean statements are:

(She==CSTeacher) and (She==MathTeacher)(Number%2==1) or (Number%2!=1)(Gender!=Male) or (Gender!=Female) (CPA == "Y") and (YrExp >= 5)

Page 6: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…
Page 7: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Logical OR ExampleConsider two young ladies who have a rather simplistic, and quite politically incorrect, view of judging potential dates. The first is Kathy who will date a guy if he is Good Looking OR drives a Nice Car. This chart shows her 4 possible cases:

Good Looking? Nice Car? Date Material?

Page 8: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Boolean OperatorsBoolean OR

A

TTFF

B

TFTF

A or B

TTTF

Page 9: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Logical AND ExampleSuzy is more picky than Kathy. Suzy will only date a guy if he BOTH Good Looking AND drives a Nice Car. This chart shows her 4 possible cases:

Good Looking? Nice Car? Date Material?

Page 10: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Boolean OperatorsBoolean AND

A

TTFF

B

TFTF

A and B

TFFF

Page 11: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Boolean OperatorsBoolean XOR

A

TTFF

B

TFTF

A xor B

FTTF

Page 12: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Boolean OperatorsBoolean NOT

A

TF

~A

FT

Page 13: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…
Page 14: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Truth Table #1A and (A or B)

A

TTFF

B

TFTF

A or B

TTTF

A and (A or B)

TTFF

Page 15: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Truth Table #2(A and B) or C

A

TTTTFFFF

B

TTFFTTFF

C

TFTFTFTF

A and B

TTFFFFFF

(A and B) or C

TTTFTFTF

Page 16: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Truth Table #3(A or B) and C

A

TTTTFFFF

B

TTFFTTFF

C

TFTFTFTF

A or B

TTTTTTFF

(A or B) and C

TFTFTFFF

Page 17: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Truth Table Fact

The truth tables of equivalent Boolean expressions are identical.

Page 18: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Truth Table #4Is ~(A or B) = ~A or ~B ?

A

TTFF

B

TFTF

A or B

TTTF

~(A or B)

FFFT

~A

FFTT

~B

FTFT

~A or ~B

FTTT

^ NO ^

Page 19: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Truth Table #5Is ~(A or B) = ~A and ~B ?

A

TTFF

B

TFTF

A or B

TFFF

~(A or B)

FFFT

~A

FFTT

~B

FTFT

~A and ~B

FFFT

^ YES ^

Page 20: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Truth Tables #6 (T/F) and #7 (1/0)Is ~(A and B) = ~A or ~B ?

A

TTFF

B

TFTF

A and B

TFFF

~(A and B)

FTTT

~A

FFTT

~B

FTFT

~A or ~B

FTTT

A

1100

B

1010

A and B

1000

~(A and B)

0111

~A

0011

~B

0101

~A or ~B

0111

YES

Page 21: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…
Page 22: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Venn Diagram #1The Boolean Algebra logical or ( + ) can be demonstrated with Venn Diagrams, using union.

A union B also A or B also A + B

A B

Page 23: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Venn Diagram #2The Boolean Algebra logical and ( * ) can be demonstrated with Venn Diagrams, using intersection.

A intersect B also A and B also A * B also AB

Page 24: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Venn Diagram 3The Boolean Algebra logical not ( ~ )

not A also ~A

A B

Page 25: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Venn Diagram 4The Boolean Algebra logical not ( ~ )

not B also ~B

A B

Page 26: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Venn Diagram 5

not (A and B)~(A * B)

Page 27: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Venn Diagram 6not (A or B)

~(A + B)

A B

Page 28: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Venn Diagram 7not (A) and not (B)

~A * ~B

A B

Page 29: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Venn Diagram 8

not (A) or not (B)~A + ~B

Page 30: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Tricky Venn DiagramsA and not (B) A or not(B)

A * ~B A + ~BStep 1 – Shade A

A B

Page 31: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Tricky Venn DiagramsA and not (B) A or not(B)

A * ~B A + ~BStep 2 – Shade ~B a different way

Page 32: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Tricky Venn DiagramA and not (B)

A * ~BAnswer: The part that is shaded twice.

Page 33: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Tricky Venn DiagramA or not (B)

A + ~BAnswer: Everything that is shaded.

Page 34: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…
Page 35: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Boolean Algebra Notation

A or B A + B

A and B A B A * B AB

not (A) ~A

true 1

false 0

Page 36: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Boolean Algebra Notation

A or B A + B

A and B A B A * B AB

not (A) ~A

true 1

false 0

Page 37: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Basic Laws of Boolean Algebra

A + A = A

A * A = A

A + B = B + A

A * B = B * A

A + (B + C) = (A + B) + C

A * (B * C) = (A * B) * C

A * (B + C) = (A * B) + (A * C)

A + (B * C) = (A + B) * (A + C)

A + 1 = 1

Indempotent law for or

Indempotent law for and

Commutative law for or

Commutative law for and

Associative law for or

Associative law for and

Distributive law for and over or

Distributive law for or over and

Law of Union

Page 38: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Basic Laws of Boolean Algebra (continued)

A * 0 = 0

A * (A + B) = A

A + (A * B) = A

A * 1 = A

A + 0 = A

~ (~A) = A

A + ~A = 1

A * ~A = 0

~(A + B) = ~A * ~B

~(A * B) = ~A + ~B

Law of Intersection

Law of Absorption

Law of Absorption

1 is the identity element for and

0 is the identity element for or

Double negative law

Law of Complement for or

Law of Complement for and

DeMorgan’s law

DeMorgan’s law

Page 39: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

The Law of Absorption presents the following two expression equivalencies:

A * (A + B) = AA + (A * B) = A

AA

TTFF

BB

TFTF

A and BA*B

TFFF

A or BA+B

TTTF

A and (A or B)A * (A+B)

TTFF

A or (A and B)A + (A*B)

TTFF

^ All 3 are the same! ^ ^

Page 40: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…
Page 41: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

The first group of problems are meant to be simple and straight forward. These problems are designed to help you understand Boolean logic better. These problems are not APCS Examination type problems. Some of the problems that follow later are similar in nature to the Boolean Algebra multiple choice questions that were presented on previously released APCS examinations.

The AP Computer Science Examination includes a number of questions that are language independent. Boolean Algebra questions are one group of language independent questions.

AP Exam Alert

Page 42: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Sample Problem 1.

The Boolean expression

A >= B

is equivalent to which of the following expressions?

(A) not(A < B)(B) not(B >= A)(C) not(A <= B)(D) A != B(E) B >= A

Answer: A

Page 43: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Sample Problem 2.

The Boolean expression

(A or B) and B

is true

(A) whenever A is true.(B) whenever B is true.(C) whenever either A is true or B is true.(D) only whenever both A is true and B is true.(E) for all values of A and B.

Answer: B

Page 44: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Sample Problem 3.

The Boolean expression

(A or B) and (A or B)

is true

(A) only when A is true.(B) only when B is true.(C) whenever either A is true or B is true.(D) only whenever both A is true and B is true.(E) for all values of A and B.

Answer: C

Page 45: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Sample Problem 4.

The Boolean expression

not(A and B and C)

is equivalent to which of the following expressions?

(A) A != B != C(B) A and B and C(C) A or B or C(D) not A or not B or not C(E) not A and not B and not C

Answer: D

Page 46: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Sample Problem 5.

The Boolean expression

(A and B) and not(A and B)

evaluates to

(A) false in all cases.(B) true in all cases.(C) true whenever only A is true or only B is true.(D) true whenever both A is true and B is true.(E) false only when both A is false and B is false.

Answer: A

Page 47: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Sample Problem 6.

The Boolean expression

not((A < B) and (C > D))

is equivalent to which of the following expressions?

(A) (A < B) or (C > D)(B) (A >= B) and (C <= D)(C) (A >= B) or (C <= D)(D) (A > B) and (C < D)(E) (A > B) or (C < D)

Answer: C

Page 48: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Sample Problem 7.

The Boolean expression

(A > B) or (A <= B)

can be simplified to which of the following expressions?

(A) A or B(B) A and B(C) A and not B(D) false(E) true

Answer: E

Page 49: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Sample Problem 8.

The Boolean expression

(A and B) and (not A or not B)

evaluates to

(A) true in all cases.(B) false in all cases.(C) true only whenever both A is true and B is true.(D) false only whenever both A is false and B is false.(E) true only whenever A is true or B is true.

Answer: B

Page 50: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Sample Problem 9.

The Boolean expression

(A and B) or (not A or not B)

evaluates to

(A) true in all cases.(B) false in all cases.(C) true only whenever both A is true and B is true.(D) false only whenever both A is false and B is false.(E) true only whenever A is true or B is true.

Answer: A

Page 51: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Sample Problem 10.

The Boolean expression

(A and B) or (not A and not B)

evaluates to

(A) true in all cases.(B) false in all cases.(C) true only whenever both A is true and B is true.(D) false only whenever both A is false and B is false.(E) true only whenever both A and B are true or both A and B are false.

Answer: E

A

TTFF

B

TFTF

A and B

TFFF

~A and ~B

FFFT

~A

FFTT

~B

FTFT

(A and B) or (~A and ~B)

TFFT

Page 52: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…
Page 53: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

// Java1001.java// This program demonstrates that conditional statements have// true or false Boolean values and can display such values.

public class Java1001{

public static void main(String args[]){

System.out.println("\nJAVA1001.JAVA\n");int x =10;System.out.println(x == 10);System.out.println(x == 5);System.out.println();

}}

Page 54: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

// Java1002.java// This program introduces the <boolean> data type, which only has two// values, true or false. Boolean variables adds readability to programs.

import javax.swing.JOptionPane;

public class Java1002{

public static void main (String args[]){

String gcf1;int gcf2;boolean correct = false;int attempt = 0;while (!correct){

attempt++;gcf1 = JOptionPane.showInputDialog("What is the GCF of 120 and 108?"); gcf2 = Integer.parseInt(gcf1);

if (gcf2 == 12) correct = true; else correct = false;

}JOptionPane.showMessageDialog(null,"Answered correctly after " + attempt + " Attempt(s)");

System.exit(0);

}}

Page 55: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

// Java1003.java// This program executes in the same manner as Java1002.java.// The abbreviated Boolean assignment statement is used in place of the longer if ... else syntax.

import javax.swing.JOptionPane;

public class Java1003{

public static void main (String args[]){

String gcf1;int gcf2;boolean correct = false;int attempt = 0;while (!correct){

attempt++;gcf1 = JOptionPane.showInputDialog("What is the GCF of 120 and 108?"); gcf2 = Integer.parseInt(gcf1);

correct = (gcf2 == 12);}

JOptionPane.showMessageDialog(null,"Answered correctly after " + attempt + " Attempt(s)");

System.exit(0);}

}

Page 56: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Logical Operators In Java

• Java uses || to indicate a logical or.

• Java uses && to indicate a logical and.

• Java uses ! to indicate a logical not.

Page 57: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Think of it this way…Variable Expression Assignmentint x; 23 + 45 x = 23 + 45;double y; 66.23 - 8.11 y = 66.23 - 8.11;String name; “John” + “Smith” name = “John”+”Smith”;boolean pass; grade >= 70 pass = grade >= 70;

Page 58: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Boolean Logic ExercisesThis section provides you with ten Boolean logic exercises.

The main difference between these exercises and the previous exercises is that these use actual Java syntax.

Page 59: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Sample Problem 1.

The Boolean expression

(A && B) || (A && C)

is equivalent to which of the following expressions?

(A) A && (B || C)(B) A || (B && C)(C) (A || B) && (A || C)(D) A && B && C(E) A || B || C

Answer: A

Page 60: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Sample Problem 2.

The Boolean expression

(A || B) && B

is true

(A) only when A is true.(B) only when B is true.(C) whenever either A is true or B is true.(D) only whenever both A is true and B is true.(E) for all values of A and B.

Answer: B

Page 61: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Sample Problem 3.

The Boolean expression

(A && B) && A

is true

(A) only when A is true.(B) only when B is true.(C) whenever either A is true or B is true.(D) only whenever both A is true and B is true.(E) for all values of A and B.

Answer: D

Page 62: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Sample Problem 4.

The Boolean expression

(A || B) && (A || B)

is true

(A) only when A is true.(B) only when B is true.(C) whenever either A is true or B is true.(D) only whenever both A is true and B is true.(E) for all values of A and B.

Answer: C

Page 63: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Sample Problem 5.

The Boolean expression

!(A && B)

is equivalent to which of the following expressions?

(A) A != B(B) !A && B(C) A || B(D) !A || !B(E) !A && !B

Answer: D

Page 64: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Sample Problem 6.

The Boolean expression

(A && B) && !(A && B)

evaluates to

(A) false in all cases.(B) true in all cases.(C) true whenever only A is true or only B is true.(D) true whenever both A is true and B is true.(E) false only when both A is false and B is false.

Answer: A

Page 65: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Sample Problem 7.

The Boolean expression

(A || B) && !(A || B)

evaluates to

(A) false in all cases.(B) true in all cases.(C) true whenever only A is true or only B is true.(D) true whenever both A is true and B is true.(E) false only when both A is false and B is false.

Answer: A

Page 66: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Sample Problem 8.

The Boolean expression

(A > B) && (A <= B)

can be simplified to which of the following expressions?

(A) A || B(B) A && B(C) A && ! B(D) false(E) true

Answer: D

Page 67: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Sample Problem 9.

The Boolean expression

(A && B) || (!A || !B)

evaluates to

(A) true in all cases.(B) false in all cases.(C) true only whenever both A is true and B is true.(D) false only whenever both A is false and B is false.(E) true only whenever A is true or B is true.

Answer: A

Page 68: AP Examination Alert The APCS Examination includes a variety of Boolean Logic questions. Many questions…

Sample Problem 10.

Consider the following program segment. Assume that X is a Boolean variable.

X = !(X == true);

(A) X will always be true.(B) X will always be false.(C) The value of X does not change.(D) The value of X always changes.(E) The result cannot be determined with the given information

Answer: D

X

TF

X == true

TF

!(X == true)

FT