Programming (3)
Resources |
Revision Questions |
Computer Science
Login to see all questions
Click on a question to view the answer
1.
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.
2.
A user enters a password. The program needs to validate the password. The password must be at least 8 characters long AND contain at least one digit. Write a Boolean expression using arithmetic, logical, and Boolean operators to determine if the password is valid. Assume the password is stored in the variable 'passwordLength' (integer) and 'hasDigit' (boolean).
Here's a Boolean expression to validate the password:
(passwordLength >= 8) AND (hasDigit = True)
Explanation:
- (passwordLength >= 8): This checks if the password length is greater than or equal to 8 characters.
- (hasDigit = True): This checks if the password contains at least one digit. We assume 'hasDigit' is a boolean variable that is set to True if a digit is present in the password, and False otherwise.
- AND: This logical operator ensures that both conditions are met for the password to be considered valid. The password must be at least 8 characters long AND contain at least one digit.
3.
Explain the difference between buffered input and unbuffered input. Give an example of a situation where buffered input would be preferable to unbuffered input.
Unbuffered input means that data read from an input device (like the keyboard) is immediately available to the program. Each character typed is processed as soon as it's received. Buffered input, on the other hand, involves storing data in a buffer – a temporary storage area – before it's passed to the program. The program retrieves data from the buffer at its own pace, rather than immediately upon reception.
Example: Buffered input would be preferable in a situation where the input device is slower than the program's processing speed. For instance, reading data from a serial port connected to a device like a sensor. If the sensor generates data faster than the program can handle it, buffered input allows the program to collect data in the buffer and process it in chunks, preventing the program from being overwhelmed and potentially missing data. Unbuffered input could lead to the program becoming unresponsive or dropping data if it can't process the data as quickly as it arrives.