03 - Boolean and Bitwise operators

 1. The most common Boolean operators are AND, OR and FIND (always in capitals)



 2. Read the following excerpt and decide whether it is true or false
In general, search engines treat the query ‘black handbag’ as ‘black AND handbag’ - which means results must contain both words, eg ‘black shiny handbags for sale’.



 3. You can use the 'OR' operator to request an alternative, for example ‘black OR white handbags’. Most search engines would interpret this as ‘_______’.

  black OR white AND hangbags

  Black OR white OR handbags

  It don't matter if you're black or white

  black AND white OR handbags

 4. NOT tells a search engine what to ignore. The query ‘handbag NOT green’ will return results that contain __________________________________.

  the word green but NOT the word handbag

  the word green AND NOT the word green at the same time, creating a paradox

  the word handbag and green but NOT any other word

  the word handbag but NOT the word green

 5. Analyse the following code using an 'AND' operator. What is the output? (True or False?)
x = True
y = False

# Output: x and y is False
print('x and y is',x and y)



 6. What is the output (True or False) in this code?
x = True
y = False

# Output: x or y is True
print('x or y is',x or y)



 7. This code snippet uses the NOT operator. Is the output True or False?
x = True
y = False

# Output: not x is False
print('not x is',not x)



 8. Evaluate the following three expressions. What are the outputs?
print((9 > 7) and (2 < 4))  # Both original expressions are True
print((8 == 8) or (6 != 6)) # One original expression is True
print(not(3 <= 1))          # The original expression is False





 9. Analyse this code carefully. If the user enters "Joe" and for the password enters "boo", it will grant access. What needs to change?
def security_check():
  username=input("Enter username:")
  password=input("Enter password:")
  if username=="Joe" or password=="open123":
    print("Access Granted")
    print("Access Denied - username or password is incorrect")


  Line 4 should be deleted altogether

  Line 1 should be: security_check(AND)

  Line 4 should be: if username NOT "Joe" and password =="open123"

  Line 4 should be: if username=="Joe" and password=="open123":

 10. The following code uses a "boolean flag" but it been used incorrectly. What needs to change so that "open123" as a password will work!
#The following code asks the user to enter a secret password, 
#IF they write open123, then access is granted, else they are stuck in a loop!
#You'll notice that even when the user enters "open123", it does not print "Access Granted". 
#Change one thing to get this to work!
def main():
  while access==False:
    password=input("Enter secret password:")
    if password=="open123":
  print("Access Granted") #note that this line is outside of the loop, so will be printed when you break out of the loop.

  Line 8 - the boolean flag "password" needs to be changed to 'True'

  Line 8 - this line uses a boolean flag incorrectly, and it should be deleted

  Line 10 - the boolean flag "access" needs to be access=True, to break out of the loop!

  Line 10 - the boolean flag "access" should be set to "open123"

 11. The boolean flag in this code snippet is 'score'
import time

def main():
  while gameover==False and score<10:
    print("The game is on.....")
    print("Your score is:",score)



 12. In the following code, seat3 has been set to 'False' (e.g. it is available), but the output shows it as 'True'. Why?
def main():
  #Set Seat to True if booked, False if available
  print("Seat 1 is booked?:",seat1booked)
  print("Seat 2 is booked?:",seat2booked)
  print("Seat 3 is booked?:",seat1booked)
  print("Seat 4 is booked?:",seat4booked)


  Change line 10 to: print("Seat 3 is booked?:",seat3booked)

  This is a boolean internal error that cannot be fixed

  All of the above statements are technically correct

  Change line 3 to: seat1booked=False or True

 13. Bitwise operators act on operands as if they were a ______________. It operates bit by bit, hence the name

  string of binary digits

  string of 2s

  string of 1s

  string of characters

 14. In the following diagram what is the example answer for Bitwise RIGHT shift in Binary?

  0000 1100

  1111 0011

  0000 0010

  0001 0001

 15. In the following diagram what is the example answer for Bitwise LEFT shift in Binary?

  0010 1000

  0111 0111

  1110 0000

  0101 0101