Challenge - 5 Problems
Prime Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of prime check for number 29
What is the output of the following Java code that checks if 29 is prime?
DSA Java
public class PrimeCheck { public static boolean isPrime(int n) { if (n <= 1) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } public static void main(String[] args) { System.out.println(isPrime(29)); } }
Attempts:
2 left
💡 Hint
Check if 29 has any divisors other than 1 and itself.
✗ Incorrect
The code checks divisors from 2 up to the square root of 29. Since 29 is prime, no divisor is found, so it returns true.
❓ Predict Output
intermediateOutput of prime check for number 1
What is the output of the following Java code that checks if 1 is prime?
DSA Java
public class PrimeCheck { public static boolean isPrime(int n) { if (n <= 1) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } public static void main(String[] args) { System.out.println(isPrime(1)); } }
Attempts:
2 left
💡 Hint
Remember prime numbers are greater than 1.
✗ Incorrect
The code returns false immediately for numbers less than or equal to 1, so 1 is not prime.
🔧 Debug
advancedIdentify the error in prime check code
What error will this Java code produce when checking if 10 is prime?
DSA Java
public class PrimeCheck { public static boolean isPrime(int n) { if (n <= 1) return false; for (int i = 2; i <= n / 2; i++) { if (n % i == 0) return false; } return true; } public static void main(String[] args) { System.out.println(isPrime(10)); } }
Attempts:
2 left
💡 Hint
Check the loop condition and divisor range.
✗ Incorrect
The code checks divisors from 2 up to n/2 (inclusive). For 10, it finds divisor 2 and returns false correctly.
❓ Predict Output
advancedOutput of prime check for number 2
What is the output of the following Java code that checks if 2 is prime?
DSA Java
public class PrimeCheck { public static boolean isPrime(int n) { if (n <= 1) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } public static void main(String[] args) { System.out.println(isPrime(2)); } }
Attempts:
2 left
💡 Hint
2 is the smallest prime number.
✗ Incorrect
The loop condition i * i <= n is false for i=2 (4 <= 2 is false), so loop does not run and returns true.
🧠 Conceptual
expertWhy check divisors only up to square root of n?
Why is it enough to check divisors only up to the square root of a number n when checking if n is prime?
Attempts:
2 left
💡 Hint
Think about pairs of factors.
✗ Incorrect
If n = a * b, then one of a or b must be less than or equal to sqrt(n). So checking beyond sqrt(n) is redundant.
