Preview

03 - Boolean and Bitwise operators

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

  TRUE

  FALSE

 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’.

  TRUE

  FALSE

 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 OR handbags

  black AND white OR handbags

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

  black OR white AND hangbags

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

  the word handbag and green but NOT any other word

  the word green but NOT the word handbag

  the word handbag but NOT the word green

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

 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)

  TRUE

  FALSE

 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)

  FALSE

  TRUE

 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)

  FALSE

  TRUE

 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

  False,False,False

  False,True,False

  True,True,False

  True,True,True

 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")
  else:
    print("Access Denied - username or password is incorrect")

security_check()

  Line 1 should be: security_check(AND)

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

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

  Line 4 should be deleted altogether

 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():
  access=False
  while access==False:
    password=input("Enter secret password:")
    if password=="open123":
      access=False
  
  print("Access Granted") #note that this line is outside of the loop, so will be printed when you break out of the loop.
  
main()

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

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

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

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

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

def main():
  score=0
  gameover=False
  while gameover==False and score<10:
    print("The game is on.....")
    print("Your score is:",score)
    time.sleep(1.1)
    score=score+1
    
main()

  TRUE

  FALSE

 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
  seat1booked=True
  seat2booked=True
  seat3booked=False
  seat4booked=True
  
  print("Seat 1 is booked?:",seat1booked)
  print("Seat 2 is booked?:",seat2booked)
  print("Seat 3 is booked?:",seat1booked)
  print("Seat 4 is booked?:",seat4booked)

main()

  Change line 3 to: seat1booked=False or True

  All of the above statements are technically correct

  This is a boolean internal error that cannot be fixed

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

 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?
bitwiseoperators_leftshift.png

  1111 0011

  0000 1100

  0000 0010

  0001 0001

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

  0101 0101

  0010 1000

  1110 0000

  0111 0111