Preview

Practice Test #1

 1. What is Python?

  An operating system

  A game design platform

  A storage device

  A programming language

 2. Python was created by Guido Van Rossum

  True

  False

 3. What is a variable?

  A variable can be thought of as a value for storage boxes

  A variable is an item that holds a programming term

  A variable is always x, y or z

  A variable can be thought of as a storage box for values

 4. Which of the following is NOT an operator?

  +

  y

  -

  *

 5. The output of the following code is the integer 2.
def main():
	num1=2
	num2=4
	num3=num2-num1
	print(num3)
main()

  True

  False

 6. Name the two subroutines/procedures (these are called functions in Python) in the code below:
def mainmenu():
	print("Welcome to the main menu")
	name=input("Enter your name:")
	login()

def login():
	print("Login Screen")
	username=input("Enter username:")
	password=input("Enter password:")

  def and print

  There are no functions

  mainmenu and login

  name and username

 7. Why will the following code not run?
PRINT("Hello World")

  A universal error - the world does not exist in Python

  A syntax error - PRINT should be print

  A programming error - code cannot be just one line long

  A logic error - You cannot say "Hello" to the world

 8. The following program will not run correctly. Can you predict what the output will be and explain why?
def sum():
	x=input("Enter number 1:")
	y=input("Enter number 2:")
	print("Adding these together will give you:",x+y)
sum()

  x and y need to be strings. As they are not strings, the program will crash and die.

  x and y need to be floats. They will not work because they are just letters.

  x and y need to be integers. As they are not, the program will not be able to add them

  x and y need to be deleted. You do not need them to add the two numbers

 9. What is the output of the following program?
def main():
	n1=0
	n2=4
	n3=4
	total=n3+n2/2
	print(n1)

main()

  0

  2

  8

  4

 10. What do you need to change in the following program to get it to print the total to pay with the discount applied?
def discount():
     pounds=int(input("Enter Price:"))
     discount="ten"
	 total_to_pay=pounds-discount
     print("The discount has been applied, and you have to pay:",total_to_pay)
		
discount()

  line 3 should be >> discount="TEN"

  line 3 should be >>discount=10

  line 3 should be deleted entirely.

  line 3 should be >> discount="%"

 11. Will the following code run correctly?
def discount():
     pounds=int(input("Enter Price:"))
     discount=10/100*price
	 total_to_pay=pounds-discount
     print("The discount has been applied, and you have to pay:",total_to_pay)
		
discount()

  True

  False

 12. The following is another way of applying discount to a set price: by using a formula for discount, which is a percentage. a) In the example below, if the price entered was '100', what would the output (total_to_pay) be? b) What % discount is being
def discount():
     pounds=int(input("Enter Price:"))
     discount=10/100*pounds
	 total_to_pay=pounds-discount
     print("The discount has been applied, and you have to pay:",total_to_pay)
		
discount()

  a) 10 b) 10% discount

  a) 90 b) 90% discount

  a) 90 b) 10% discount

  a) 10 b) 100% discount

 13. What is the following code snippet doing?
def facebook():
	username=input("Enter your username:")
	age=int(input("Enter your age:"))
	print("Welcome", username)
facebook()

  It is printing the whole of facebook

  It is asking the user for their username and age, and printing the username they entered

  It is asking the user to enter their username and then printing their age.

  It is asking the user to enter their username and age and printing "Welcome"

 14. Sorting is a very important feature of programming. For example, when you load your friends list on facebook, they are often presented ALPHABETICALLY, which is an example of a sort. Sort the following numbers from smallest to largest:
5,1,3,7,0

  1,3,5,7,0

  0,3,1,5,7

  0,1,3,5,7

  7,1,3,5,2

 15. A computer isn't as clever a human! If you gave it a sequence of numbers to sort, it wouldn't be able to just 'look' and do it! It would have to follow an algorithm. What is an algorithm?

  An algorithm is a list of functions that carry out calculations if x = 9

  An algorithm is simply a list of steps to follow in order to solve a problem.

  An algorithm is a list of problems needed to solve some steps

  An algorithm is an alien term for computational complexity

 16. 'Searching' is another feature that is very important in programming. Almost all programs (think of facebook, instagram, google!) have some sort of 'searching' going on. When it comes to searching, programmers should be interested in ...

  the quickest way to search for something

  the way to search for something that ends in finding nothing

  the slowest way to search for something

  the way to slow down a search in order to feel less stressed

 17. How would you describe a linear search?

  A linear search scans one item at a time, without jumping to any item.

  A linear search is like a straight linear line.

  A linear search repeatedly divides the list being searched in half

  It is not possible to search a list using programming

 18. If you were given the following list of numbers, and the computer was given the task of searching for the number '6', would it be better to use a Linear or Binary Search?
1,2,3,4,5,6,7,8,9,10,11

  Binary search, because Binary means 2 and 2 is more than 1

  Linear search, because it would start at 1, and reach 5 quite quickly

  Binary search, because it would start at 10 and work back to 5 much quicker

  Binary search, because in this case it would jump straight to the mid point

 19. In a Binary search: The middle element is looked to check if it is greater than or less than the value to be searched. If the element we are looking for is 55, what would the search be next reduced to?
0	5	13	19	22	41	55	68	72	81	98

  55 68 72 81 98

  0 5 13 19 22

  It would not be reduced

  The number would be found immediately

 20. The following program seeks to find the speed of a runner. Can you spot the error?
def findspeed():
  speed=int(input("Enter distance:"))
  time=int(input("Enter time: [for the sake of simplicity enter a whole number]>>"))
  speed=distance/time
  print("The speed of the runner is:",speed)

findspeed()

  The error is on line 4 - speed should be distance

  The error is on line 3 - time should be speed

  The error is on line 5 -print should be PRINT

  The error is on line 2 - speed should be distance

 21. In the following code, what will the output be if you change line 8 to: if budget <=99:
def cars():
     #Change the budget to over 5000 in order to display the expensive cars
     #Add a few more cars to each list
     expensive_cars=["Mercedes","Audi","BMW"]
     cheap_cars=["Skoda","Maruthi","Nano"]

     budget=int(input("How much can you spend on a car?"))
     if budget>=2000:
          print("Here are some cars you could consider",expensive_cars)
     else:
          print("You may want to check out these brands:",cheap_cars)
     cars()

cars()

  It will print the list of expensive cars and the cheap cars (at the same time)

  It will not run at all, because the budget has to be greater than a certain amount

  It will print the list of cheap cars, and this is correct

  It will print the list of expensive cars, but this is a logic error, as it should print the cheap cars

 22. Point out where parameter passing is occurring in the below program?
def main():
	x=2
	y=5
	sum(x,y)

def sum(x,y)
	return x+y

main()

  Line 1 and Line 2

  Line 1 and Line 4

  Line 6 and Line 7

  Line 4 and Line 6

 23. Note that it is possible to assign multiple variables values in one single line. For example x,y=0,2, gives x the value 0 and y the value 2. What is the output of the following program?
def main():
	x=3
	y=2
	x,y=y,y
	print(x+y)
main()

  0

  5

  2

  4

 24. What is the output of the following code?
age=input("Enter your age")
if age>20:
	print("Access Granted")
else:
	print("Denied")

  20

  Access Denied

  You would first need to know the value of the input variable 'age'

  Access Granted

 25. What is the following code doing?
def main():
  x=0
  while x<21:
    print(x)
    x=x+1
    
main()

  It will check to see if x is less than 20 and print x

  It will print(x) which is 0

  It will print numbers from 0 to 20

  It will print(x) which is 0 and then 1, and repeat this 21 times

 26. This is an A Level question: Can you guess what the following program is doing?
def mystery_program(a_list, item):


    first = 0
    last = len(a_list) - 1

    while first <= last:
        i = (first + last) / 2

        if a_list[i] == item:
            return '{item} found at position {i}'.format(item=item, i=i)
        elif a_list[i] > item:
            last = i - 1
        elif a_list[i] < item:
            first = i + 1
        else:
            return '{item} not found in the list'.format(item=item)

  performs a bubble sort to sort items by first and last

  performs a insertion sort to delete items and put them on a list

  performs a linear search to find the position of an integer in a given list

  performs a binary search to find the position of an integer in a given, sorted, list

 27. The following code uses if statements and the 'or' command. What is the output if the user enters "3333"?
def secretcode2():
     #Here we are using an "or" to give more options
     print("======SECURITY PASS REQUIRED====")
     code=input("Enter your four digit secret code:")
     if code=="7777" or code=="3333":
          print("Access Granted, Welcome AGENT")
     else:
          print("SECURITY ALERT!!!!! IMPOSTOR")

secretcode2()

  Access Granted because any number between 3333 and 7777 would be accepted

  Access Granted as the input can be either 7777 or 3333

  Access Denied because there are double equal signs which denote denial of service

  Access Denied, as the input must be both 7777 and 3333

 28. If the inputs are 3 and 2, what will the output be?
def simplecalc():
     #add another variable and ask for a number input for 'z'
     #make the total variable add up the three numbers x+y+z
     x=input("Enter a number:>>")
     y=input("Enter another number:>>")
     total=int(x)+int(y)
     print(total)
simplecalc()

  3

  0

  5

  2

 29. In the following program, the user is asked to enter their gender. One user enters "Mail" instead of "Male" and another user enters "Feehmayle" instead of "Female". What is the process, beginning with V, that ensure
gender=input("Enter Gender Please:")

  Validation

  Verification

  Valliant Expression

  Valiform

 30. The following code shows validation in place so that the user cannot enter invalid values. a) Where are the valid options stored in this example? b) What type of programming construct has been used to create this validation in line 9 and 11.
def main():
  
  print("""
  ===Main Menu===
  1. Student Login
  2. Teacher Login
  """)
  
  accepted_choices=[1,2]
  choice=0
  while choice not in accepted_choices:
     choice=int(input("Enter Choice:"))
     if choice==1:
       studentlogin()
     elif choice==2:
         teacherlogin()
     else:
       print("Please make a valid choice:")

def studentlogin():
  print("Students ===Welcome")
  

def teacherlogin():
  print("Teachers ===Welcome")
 

main()

  a) Line 13 - using IF statements b) Using a List and a WHILE loop

  a) Line 9 - in a list b) Using functions like student_login

  a) Line 7 - in triple quotes b) Using a List and an IF statement

  a) Line 9 - in a list b) Using a List and a WHILE loop

 31. What is one of the disadvantages of a Binary Search?

  The list has to be long

  The list does not have to be sorted

  The list has to be sorted

  The list has to be small

 32. In a linear search, the list has to be sorted.

  True

  False

 33. What is the != in python?

  less than or greater than

  equal to

  not equal to

  comparing to

 34. Binary search algorithms are examples of ...

  Divide and conquer algorithms

  Catchy Sorts

  Straightforward algorithms

  Mid Point Passes

 35. In the following example, validation has been considered (to some extent).
age=int=input("Enter age:")

  False

  True

 36. The output for the following code will be: 36
def main():
  x="33"
  y="3"
  print(x+y)
main()

  True

  False

 37. Gmail and Facebook have built in validation into their sign up forms. An example of validation is that you cannot enter an invalid email address.

  True

  False

 38. The following is a program written in the wonderful language of C. Can you guess what it is doing?
                
  #include<stdio.h>
 
int main() {
   int a, b, sum;
 
   printf("\nEnter the two numbers: ");
   scanf("%d %d", &a, &b);
 
   sum = a + b;
 
   printf("Sum of %d and %d is %d", a,b,sum);
 
   return(0);
}
  

  A program to multiply two integer numbers

  A program to multiply two strings

  A program to find the greater of two entered numbers

  A program to add two integer numbers

 39. Here is another C program. You may have never programmed in C, but can you guess what is output?
    #include <stdio.h>
    int main()
    {
        int i = 2;
        int j = ++i + i;
        printf("%d\n", j);
    }

  5

  4

  3

  6

 40. Python and Java are both low level programming langauges

  FALSE

  TRUE