Test Flashcards
How do you check if a number is divisible by 7?
Use if (num % 7 == 0) to check divisibility.
What does the modulus operator (%) do?
It returns the remainder of a division.
Write a for loop to iterate over numbers 1 to 100.
for (int i = 1; i <= 100; i++) {
System.out.println(i);
}
How can you skip even numbers in a loop?
Use if (i % 2 == 0) continue; inside the loop.
What is the output of System.out.println(10 % 3);?
1, as 10 divided by 3 leaves a remainder of 1.
How do you reverse a string using StringBuilder?
String reversed = new StringBuilder(original).reverse().toString();
How do you convert a string to uppercase?
A: Use str.toUpperCase().
Write a snippet to capitalize the first letter of a string.
A:
String capitalized = str.substring(0, 1).toUpperCase() + str.substring(1);
What does the charAt() method do?
It retrieves the character at a specified index in a string.
How do you find the length of a string?
Use the length() method, e.g., str.length()
How do you read input from a user in Java?
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
How do you implement a simple calculator using switch?
switch (operator) {
case ‘+’: result = a + b; break;
case ‘-‘: result = a - b; break;
// Add cases for *, /.
}
What is the purpose of Double.parseDouble()?
It converts a string to a double.
How can you safely divide two numbers to avoid a divide-by-zero error?
Check if the denominator is zero before dividing:
if (b != 0) result = a / b;
Write a method to perform addition of two numbers.
int add(int a, int b) {
return a + b;
}
How do you read a file line by line in Java?
Scanner fileScanner = new Scanner(new File(“filename.txt”));
while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
}
How do you write to a file in Java?
PrintWriter writer = new PrintWriter(“output.txt”);
writer.println(“Hello”);
writer.close();
How do you reverse the lines of a file?
Store lines in a list, reverse it with Collections.reverse(), then write back to a file.
What is an ArrayList, and why use it for dynamic data?
An ArrayList is a resizable array. Use it when the size of the array is not fixed.
Write a snippet to filter lines containing a specific word from a file.
while (sc.hasNextLine()) {
String line = sc.nextLine();
if (line.contains(“word”)) {
System.out.println(line);
}
}
Write a base case for a recursive factorial function.
if (n == 0) return 1;
How does recursion differ from iteration?
Recursion uses function calls, while iteration uses loops.
Write a recursive Fibonacci function.
int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
What is a common issue with recursion for large input sizes?
Stack overflow due to too many recursive calls.
What is the difference between direct and indirect recursion?
Direct recursion occurs when a function calls itself, while indirect recursion occurs when two or more functions call each other in a cycle.
How does the Sieve of Eratosthenes work?
Mark multiples of each prime number starting from 2, and remaining unmarked numbers are primes.
Write a snippet to implement the Sieve algorithm.
boolean[] primes = new boolean[n + 1];
Arrays.fill(primes, true);
for (int p = 2; p * p <= n; p++) {
if (primes[p]) {
for (int i = p * p; i <= n; i += p)
primes[i] = false;
}
}
What is Euclid’s algorithm for GCD?
Use the formula:
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
Why is the Sieve of Eratosthenes efficient?
It has a time complexity of O(n log log n) and avoids redundant calculations.
How do you calculate the GCD iteratively?
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;