1.
import java.util.HashMap;
import java.util.Map;
public class RepeatedCharacters {
public static void findRepeatedCharacters(String input) {
// HashMap to store the frequency of each character
Map<Character, Integer> charCount = new HashMap<>();
// Traverse the string and count occurrences of each character
for (char ch : input.toCharArray()) {
if (ch != ' ') { // Ignore spaces
charCount.put(ch, charCount.getOrDefault(ch, 0) + 1);
}
}
// Check for characters repeated more than twice
boolean found = false;
System.out.println("Characters repeated more than twice and their counts:");
for (Map.Entry<Character, Integer> entry : charCount.entrySet()) {
if (entry.getValue() > 2) {
found = true;
System.out.println("'" + entry.getKey() + "': " + entry.getValue() + " times");
}
}
// If no character is repeated more than twice
if (!found) {
System.out.println("No character is repeated more than twice.");
}
}
public static void main(String[] args) {
// Example usage
String input = "It is raining today in Bangalore";
findRepeatedCharacters(input);
}
}
2.
import java.util.HashSet;
import java.util.Set;
public class NumberFactors {
public static void findFactorsInSet(int[] numbers) {
// Create a HashSet to store all numbers from the set for quick lookup
Set<Integer> numberSet = new HashSet<>();
for (int num : numbers) {
numberSet.add(num);
}
// Iterate through each number in the set
for (int num : numbers) {
System.out.println("Factors for " + num + ":");
boolean found = false;
// Check for factors less than the number itself
for (int i = 1; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
int factor1 = i;
int factor2 = num / i;
// Check if factor1 or factor2 is present in the set
if (factor1 != num && numberSet.contains(factor1)) {
System.out.println(factor1);
found = true;
}
if (factor2 != num && factor1 != factor2 && numberSet.contains(factor2)) {
System.out.println(factor2);
found = true;
}
}
}
if (!found) {
System.out.println("No factors found in the set.");
}
}
}
public static void main(String[] args) {
// Example usage
int[] numbers = {15, 5, 78, 3, 100, 2, 4};
findFactorsInSet(numbers); }}
3.
public class StringValueCalculator {
public static int calculateStringValue(String input) {
int value = 0;
// Iterate through each character in the string
for (char ch : input.toCharArray()) {
// Calculate the value for the character
if (ch >= 'A' && ch <= 'Z') {
int charValue = ch - 'A' + 1;
value += charValue;
}
}
return value;
}
public static void main(String[] args) {
// Example usage
String java = "JAVA";
String python = "PYTHON";
int javaValue = calculateStringValue(java);
int pythonValue = calculateStringValue(python);
System.out.println("Value of JAVA: " + javaValue);
System.out.println("Value of PYTHON: " + pythonValue);
// Test with any other string
String input = "EXAMPLE"; // Change this to any string you want to test
int inputValue = calculateStringValue(input);
System.out.println("Value of " + input + ": " + inputValue);
}
}
4.
public class DecryptWord {
public static String decrypt(String encryptedWord) {
StringBuilder decryptedWord = new StringBuilder();
// Iterate through each character in the encrypted word
for (char ch : encryptedWord.toCharArray()) {
// Decrypt each character by shifting it back
char decryptedChar = decryptCharacter(ch);
decryptedWord.append(decryptedChar);
}
return decryptedWord.toString();
}
private static char decryptCharacter(char ch) {
// Custom decryption logic based on the encryption pattern observed
switch (ch) {
case 'X': return 'I';
case 'C': return 'N';
case 'S': return 'D';
case 'P': return 'A';
default: return ch; // Default case to return the character as is if no match is found
}
}
public static void main(String[] args) {
// Example usage
String encryptedWord = "XCSXP";
String decryptedWord = decrypt(encryptedWord);
System.out.println("Decrypted word: " + decryptedWord);
}
}
5.
A.
SELECT dept, COUNT(emp_no) AS employee_count
FROM Employment
GROUP BY dept;
B.
SELECT SUM(Emp.salary) AS total_salary
FROM Employee E
JOIN Employment Emp ON E.emp_no = Emp.emp_no
WHERE E.gender = 'Male' AND E.age > 35 AND E.age < 40;
6.
public class BalancedPosition {
public static int findBalancedPosition(int[] arr) {
int totalSum = 0;
int leftSum = 0;
// Calculate the total sum of the array
for (int num : arr) {
totalSum += num;
}
// Traverse the array and check if any position balances the sum
for (int i = 0; i < arr.length; i++) {
// Calculate the right sum as totalSum - leftSum - arr[i]
int rightSum = totalSum - leftSum - arr[i];
// Check if leftSum equals rightSum
if (leftSum == rightSum) {
return i; // Return the position if they are equal
}
// Update leftSum by adding the current element
leftSum += arr[i];
}
// If no such position exists, return -1
return -1;
}
public static void main(String[] args) {
// Example 1
int[] arr1 = {15, 5, 3, 43, 16, 10, 20, 8, 12};
int position1 = findBalancedPosition(arr1);
System.out.println("Balanced position in arr1: " + (position1 == -1 ? "No such position" :
position1));
// Example 2
int[] arr2 = {23, 5, 8, 32, 55, 87};
int position2 = findBalancedPosition(arr2);
System.out.println("Balanced position in arr2: " + (position2 == -1 ? "No such position" :
position2));
}
}
7.
public class OtherName {
public static String getOtherName(String name) {
StringBuilder otherName = new StringBuilder();
// Traverse each character in the name
for (char ch : name.toCharArray()) {
// Convert the character to its "other" character
if (ch >= 'a' && ch <= 'z') {
char otherChar = (char) ('a' + ('z' - ch));
otherName.append(otherChar);
} else if (ch >= 'A' && ch <= 'Z') {
char otherChar = (char) ('A' + ('Z' - ch));
otherName.append(otherChar);
} else {
otherName.append(ch); // If not a letter, append the character as is
}
}
return otherName.toString();
}
public static void main(String[] args) {
// Example usage
String name = "YourName"; // Replace with the actual name you want to test
String otherName = getOtherName(name);
System.out.println("Original Name: " + name);
System.out.println("Other Name: " + otherName);
}
}