Programming (3)
Resources |
Revision Questions |
Computer Science
Login to see all questions
Click on a question to view the answer
1.
A student is writing a program to calculate the area of a rectangle. The program takes the length and width as input from the user. The area is calculated using the formula: Area = Length x Width. Write a flowchart or pseudocode to describe the steps the student should take to create this program.
Flowchart:
Start | Get Length | Get Width | Calculate Area (Area = Length x Width) | Display Area | End |
Pseudocode:
- Begin
- Input length
- Input width
- Calculate area = length * width
- Display area
- End
2.
Consider an array of character values. Write a program in a language of your choice that takes an array of characters and an integer 'index' as input. The program should iterate through the array. If the character at the specified 'index' is a vowel (a, e, i, o, u - case insensitive), replace it with a '*'. If the 'index' is invalid (out of bounds), the program should output an error message and terminate. Assume the array has at least one element.
Example Java Solution:
`java
import java.util.Arrays;
public class ArrayManipulation {
public static void main(String[] args) {
char[] characters = new char[]{'a', 'b', 'c', 'e', 'f', 'i', 'o', 'u'};
int index = 3;
if (index < 0 || index >= characters.length) {
System.out.println("Error: Invalid index.");
return;
}
for (int i = 0; i < characters.length; i++) {
if (i == index) {
char currentChar = characters[i];
char lowerChar = Character.toLowerCase(currentChar);
if (lowerChar == 'a' || lowerChar == 'e' || lowerChar == 'i' || lowerChar == 'o' || lowerChar == 'u') {
characters[i] = '*';
}
}
}
System.out.println(Arrays.toString(characters));
}
}
`
Explanation:
- The program takes an array of characters and an index as input.
- It first checks if the index is valid. If not, it prints an error message and exits.
- A
for
loop iterates through the array. - Inside the loop, if the current index matches the input 'index', the character at that index is converted to lowercase.
- Then, it checks if the lowercase character is a vowel.
- If it is, the character at that index is replaced with '*'.
- Finally, the modified array is printed.
3.
A program needs to determine if a number entered by the user is within a specific range. The range is defined as being between 10 and 50 (inclusive). Write a flowchart or pseudocode to illustrate how you would implement this using nested conditional statements. Then, provide a short code snippet (in Python) demonstrating the same logic.
Flowchart/Pseudocode:
- Start
- Get input number from user
- If number is less than 10, then display "Number is outside the range" and stop.
- If number is greater than 50, then display "Number is outside the range" and stop.
- Else (number is between 10 and 50): display "Number is within the range" and stop.
- End
Python Code Snippet:
def check_range():
number = int(input("Enter a number: "))
if number < 10:
print("Number is outside the range")
elif number > 50:
print("Number is outside the range")
else:
print("Number is within the range")
check_range()