Programming
14 questions across 1 exam
Exams covering this topic
All questions (14)
The variable `x` is assigned a value using the statement: `x ← LEN(state)` Using Figure 1, what is the value of `x`?
What is the result of concatenating the contents of the variables `city` and `landmark` in Figure 1?
The subroutine `POSITION` finds the first position of a character in a string. For example, `POSITION('Computing', 'p')` would return 3. The variable `z` is assigned a value using the statement: `z ← POSITION(landmark, 't')` Using Figure 1, what value is assigned to `z`? *(Note: The paper states to assume 0-based indexing unless specified otherwise.)*
Explain **one** advantage of the structured approach to programming.
Figure 3 shows a program written in Python that calculates the area of a rectangle or the volume of a box from the user inputs. **Figure 3** python def calculate(width, length, height): if height == -1: return width * length else: return width * length * height numOne = int(input("Enter width: ")) numTwo = int(input("Enter length: ")) numThree = int(input("Enter height, -1 to ignore: ")) answer = calculate(numOne, numTwo, numThree) if numThree == -1: print(f"Area {answer}") else: print(f"Volume {answer}") Complete the trace table using the program in Figure 3.
Describe **one** way that the program in Figure 3 could be made more robust.
A theme park charges £15 per person for a daily ticket. If there are six or more people in a group, the group is given a £5 discount. Write a Python program to calculate the total charge for a group of people visiting the theme park. The program must: - get the user to enter the number of people in a group - calculate the total charge by: - charging £15 per person - reducing the total charge by £5 if there are six or more people - output the total charge. You **should** use indentation as appropriate, meaningful variable name(s) and Python syntax in your answer.
Figure 8 shows an algorithm that uses a RECORD data structure for storing information about a film. **Figure 8** RECORD Film title : String certificate : String year : Integer beingShown : Boolean ENDRECORD hulk ← Film('Hulk', '12A', 2005, False) ironMan ← Film('Iron Man', '12A', 2008, False) antMan ← Film('Ant-Man', '12A', 2015, False) filmCollection ← [antMan, hulk, ironMan] year ← 0 position ← 0 FOR i ← 0 TO L1 IF filmCollection[i].year > year THEN year ← filmCollection[i].year position ← i ENDIF ENDFOR OUTPUT filmCollection[position].title, ' is the newest film' How many different values can the field `beingShown` have?
Which assignment statement changes the year the film *Hulk* was made to 2003?
Write a pseudo-code statement that updates the `antMan` record to show that the film is currently being shown at the cinema.
Figure 12 shows a line of Python code that creates a list of fruit names. **Figure 12** python fruits = ["banana", "apple", "orange", "pear", "grape", "pineapple"] Extend the program in Figure 12. Your answer must be written in Python. The program should get the user to enter a word and perform a **linear search** on the list `fruits` to find if the word is in the list or not. The program should: - ask the user what word they would like to find - output the message `True` if the word is found - output the message `False` if the word is not found. You must write your own linear search routine and **not** use any built-in search function available in Python.
A programmer is writing a game using a 3x3 grid. A square on the grid is referred to by a letter (A, B, C) and a number (1, 2, 3). Figure 15 shows part of a Python program that checks the grid reference entered by a player. The grid reference is valid if: - there are exactly two characters - the first character entered is A, B or C - the second character entered is 1, 2 or 3. **Figure 15** python check = False while check == False: square = "" while len(square) != 2: square = input("Enter grid reference (eg C2): ") square = square.upper() Extend the program from Figure 15 so it completes the other checks needed to make sure a valid grid reference is entered. Your extended program must: - use the variable `check` - repeat the following steps until a valid grid reference is entered: - get the user to enter a grid reference - output an appropriate message if the grid reference is not valid.
A group of people have a meal in a restaurant. Instead of one person paying for the whole meal, each person will pay for what they eat. Write a Python program that asks each person in the group how much they are paying towards the meal and works out when the bill is fully paid. Each person can pay a different amount. The program should: - get the user to enter the total amount of the bill - get a person to enter how much they are paying towards the bill - subtract the amount entered from the bill: - if the amount left to pay is more than 0, output how much is left to pay and repeat until the amount left to pay is 0 or less - if the amount left to pay is 0, then output the message `Bill paid` - if the amount left to pay is less than 0, then output the message `Tip is` and the difference between the amount left to pay and 0 You **should** use indentation as appropriate, meaningful variable name(s) and Python syntax in your answer.
Question 16 is about a dice game played against a computer. The aim of the game is to get as close to a score of 21 as you can, without going over 21. If your score goes over 21 then you lose. The player's score starts at 0. **For each turn:** - two dice (each numbered from 1 to 6) are rolled - the total of the two dice rolls is added to the player's score - the value of each dice and the player's new total is output - if the current score is less than 21, the player is asked if they would like to roll the dice again: if the player says yes, they get another turn; otherwise, the game ends. **At the end of the game, the program should work as follows:** - if the final score is 21, output a message to say the player has won - if the final score is greater than 21, output a message to say the player has lost - if the final score is less than 21, the program generates a random number between 15 and 21 inclusive: - if this random number is greater than the player's final score, output a message to say the player has lost - otherwise, output a message to say the player has won. The dice rolls are carried out by the program generating random numbers between 1 and 6. You will need to use the Python function `random.randrange(a, b)` which generates a random integer in the range a to b starting at a but finishing one before b. Write a Python program to simulate this game.
Practice these questions with detailed guidance
Full answers, grading, and explanations on why each answer is correct.
Expert