Java Questions For Cloudy Coders
Java Questions For Cloudy Coders
(Cloudy Coders)
1. Reverse a String
Problem: Write a Java program to reverse a given string without using the built-in reverse() method.
Solution:
System.out.println(reversed);
Solution:
return str.equals(reversed);
3. Fibonacci Sequence
Solution:
int n = 10;
int a = 0, b = 1;
int c = a + b;
a = b;
b = c;
4. Factorial Calculation
Solution:
if (n == 0) return 1;
Solution:
if (n % i == 0) return false;
return true;
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
Solution:
import java.util.Arrays;
return Arrays.stream(arr).distinct().toArray();
Solution:
if (n > first) {
second = first;
first = n;
return second;
Solution:
import java.util.Arrays;
int i = 0, j = 0, k = 0;
merged[k++] = arr1[i++];
} else {
merged[k++] = arr2[j++];
merged[k++] = arr1[i++];
merged[k++] = arr2[j++];
return merged;
}
10. Find Missing Number in Array
Solution:
int sum = n * (n + 1) / 2;
sum -= num;
return sum;
Solution:
while (temp != 0) {
temp /= 10;
return sum == n;
Solution:
Solution:
import java.util.Arrays;
Solution:
str = str.toLowerCase();
else consonants++;
import java.util.HashMap;
import java.util.Map;
countMap.forEach((k, v) -> {
});
Solution:
return max;
Solution:
return words.length;
Solution:
System.out.print(" ");
System.out.print("*");
System.out.println();
Solution:
import java.util.HashSet;
import java.util.Set;
set1.add(num);
if (set1.contains(num)) common.add(num);
return common;
Solution:
int rev = 0;
while (n != 0) {
n /= 10;
return rev;
Solution:
largest = num;
second = num;
return second;
Solution:
str = str.toLowerCase();
int i = 0, j = str.length() - 1;
while (i < j) {
i++;
j--;
return true;
Solution:
}
}
Solution:
int a = 0, b = 1;
int c = a + b;
a = b;
b = c;
System.out.println();
Solution:
int sum = 0;
while (num != 0) {
num /= 10;
return sum;
Solution:
int total = n * (n + 1) / 2;
total -= num;
return total;
Solution:
System.out.println();
return true;
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
Solution:
while (temp != 0) {
temp /= 10;
Solution:
Solution:
if (b == 0) return a;
Solution:
if (b == 0) return a;
Solution:
str = str.toLowerCase();
if (Character.isLetter(ch)) {
else consonants++;
Solution:
return words.length;
Solution:
import java.util.Arrays;
Arrays.sort(arr);
Solution:
Solution:
Solution:
return n * (n + 1) / 2;
Solution:
}
40. Swap Two Strings Without Third Variable
Solution:
a = a + b;
a = a.substring(b.length());
Solution:
Solution:
int rev = 0;
while (num != 0) {
num /= 10;
return rev;
}
}
Solution:
if (num % 2 == 0) even++;
else odd++;
Solution:
str = str.toLowerCase();
Solution:
}
}
Solution:
if (n <= 1) return 1;
Solution:
return maxDiff;
Solution:
int a = 0, b = 1;
for (int i = 0; i < n; i++) {
int c = a + b;
a = b;
b = c;
Solution:
if (n % i == 0) return false;
return true;
Solution:
temp /= 10;
Solution:
int sum = 0;
while (num != 0) {
num /= 10;
return sum;
Solution:
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
return a;
Solution:
return Integer.parseInt(str);
Solution:
if (b == 0) return a;
Solution:
Solution:
secondMax = max;
max = num;
secondMax = num;
return secondMax;
Solution:
Solution:
int length = 0;
length++;
return length;
Solution:
return sentence.trim().split("\\s+").length;
Solution:
import java.util.HashSet;
set.add(num);
}
for (int num : set) {
Solution:
return result.toString().trim();
Solution:
Solution:
public class SimpleInterest {
return (p * r * t) / 100;
Solution:
int sum = n * (n + 1) / 2;
return sum;
Solution:
str = str.toLowerCase();
if (Character.isLetter(c)) {
else consonants++;
}
66. Find Maximum Occurring Character
Solution:
import java.util.HashMap;
int maxCount = 0;
maxCount = map.get(c);
maxChar = c;
return maxChar;
Solution:
while (num != 0) {
num /= 10;
Solution:
import java.util.HashMap;
Solution:
Solution:
Solution:
return Integer.toHexString(num).toUpperCase();
Solution:
Problem: Check if a number is a Harshad number (divisible by the sum of its digits).
Solution:
Solution:
return Integer.toOctalString(decimal);
Solution:
return (n * (n + 1) * (2 * n + 1)) / 6;
Problem: Check if a number is a Duck number (contains zero but not starting with zero).
Solution:
Solution:
if (n <= 1) return 1;
Solution:
int a = 0, b = 1;
int next = a + b;
a = b;
b = next;
Solution:
if (y == 0) return 1;
}
80. Find HCF (GCD) of Two Numbers
Solution:
if (b == 0) return a;
Solution:
if (b == 0) return a;
Solution:
class Node {
int data;
Node next;
}
public class ReverseLinkedList {
current.next = prev;
prev = current;
current = next;
return prev;
Solution:
import java.util.Arrays;
Arrays.sort(arr1);
Arrays.sort(arr2);
Solution:
while (i < j) {
return true;
Solution:
int sum = n * (n + 1) / 2;
return sum;
Solution:
import java.util.LinkedHashMap;
return '\0';
}
Solution:
int count = 0;
if (ch == c) count++;
return count;
Solution:
int maxLen = 0;
return maxLen;
Solution:
double s = (a + b + c) / 2;
Solution:
num /= 10;
if (sum == i) System.out.println(i);
Solution:
import java.util.*;
return result;
Solution:
int sum = 0;
num /= 10;
return sum;
Solution:
Problem: Check whether a number is a perfect number (sum of its proper divisors equals the
number).
Solution:
int sum = 0;
if (num % i == 0) sum += i;
Solution:
import java.util.HashMap;
System.out.println(freqMap);
Problem: Remove duplicate characters and print a string with unique characters.
Solution:
return sb.toString();
Solution:
sb.append(Character.toUpperCase(word.charAt(0)))
.append(word.substring(1).toLowerCase()).append(" ");
return sb.toString().trim();
Solution:
long factor = 2;
else factor++;
return num;
}
Solution:
int product = 1;
num /= 10;
return product;
Solution:
int num = 1;
System.out.println();
Integration Cloud
Problem: Integrate three simulated payment gateways, choose the fastest response.
class PaymentGateway {
String name;
this.name = name;
this.responseTime = responseTime;
PaymentGateway[] gateways = {
};
App Cloud
import java.util.*;
class User {
System.out.println("Register: Username?");
System.out.println("Password?");
users.put(user, u);
System.out.println("Login Username?");
System.out.println("Password?");
System.out.println("Add Note:");
users.get(loginUser).notes.add(note);
IoT Cloud
import java.util.*;
Manufacturing Cloud
class BankAccount {
String accName;
double balance;
acc.deposit(500);
acc.withdraw(200);
acc.showBalance();
Education Cloud
import java.util.*;
grades.put("John", 85);
grades.put("Emma", 92);
grades.put("Liam", 78);
Non-Profit Cloud
import java.util.*;
donations.put("DonorA", 1000.0);
donations.put("DonorB", 500.0);
Marketing Cloud
Health Cloud
import java.util.*;
class Patient {
String name;
int age;
String condition;
patients.forEach(p -> System.out.println(p.name + ", Age: " + p.age + ", Condition: " +
p.condition));
Vaccine Cloud
import java.util.*;
vaccineDoses.put("Pfizer", 1200);
vaccineDoses.put("Moderna", 800);