Test Flashcards

1
Q

How do you check if a number is divisible by 7?

A

Use if (num % 7 == 0) to check divisibility.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does the modulus operator (%) do?

A

It returns the remainder of a division.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Write a for loop to iterate over numbers 1 to 100.

A

for (int i = 1; i <= 100; i++) {
System.out.println(i);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can you skip even numbers in a loop?

A

Use if (i % 2 == 0) continue; inside the loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the output of System.out.println(10 % 3);?

A

1, as 10 divided by 3 leaves a remainder of 1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you reverse a string using StringBuilder?

A

String reversed = new StringBuilder(original).reverse().toString();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you convert a string to uppercase?
A: Use str.toUpperCase().

A

Write a snippet to capitalize the first letter of a string.
A:

String capitalized = str.substring(0, 1).toUpperCase() + str.substring(1);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does the charAt() method do?

A

It retrieves the character at a specified index in a string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you find the length of a string?

A

Use the length() method, e.g., str.length()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you read input from a user in Java?

A

Scanner sc = new Scanner(System.in);
String input = sc.nextLine();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you implement a simple calculator using switch?

A

switch (operator) {
case ‘+’: result = a + b; break;
case ‘-‘: result = a - b; break;
// Add cases for *, /.
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the purpose of Double.parseDouble()?

A

It converts a string to a double.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can you safely divide two numbers to avoid a divide-by-zero error?

A

Check if the denominator is zero before dividing:

if (b != 0) result = a / b;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Write a method to perform addition of two numbers.

A

int add(int a, int b) {
return a + b;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you read a file line by line in Java?

A

Scanner fileScanner = new Scanner(new File(“filename.txt”));
while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you write to a file in Java?

A

PrintWriter writer = new PrintWriter(“output.txt”);
writer.println(“Hello”);
writer.close();

17
Q

How do you reverse the lines of a file?

A

Store lines in a list, reverse it with Collections.reverse(), then write back to a file.

18
Q

What is an ArrayList, and why use it for dynamic data?

A

An ArrayList is a resizable array. Use it when the size of the array is not fixed.

19
Q

Write a snippet to filter lines containing a specific word from a file.

A

while (sc.hasNextLine()) {
String line = sc.nextLine();
if (line.contains(“word”)) {
System.out.println(line);
}
}

20
Q

Write a base case for a recursive factorial function.

A

if (n == 0) return 1;

21
Q

How does recursion differ from iteration?

A

Recursion uses function calls, while iteration uses loops.

22
Q

Write a recursive Fibonacci function.

A

int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}

23
Q

What is a common issue with recursion for large input sizes?

A

Stack overflow due to too many recursive calls.

24
Q

What is the difference between direct and indirect recursion?

A

Direct recursion occurs when a function calls itself, while indirect recursion occurs when two or more functions call each other in a cycle.

25
Q

How does the Sieve of Eratosthenes work?

A

Mark multiples of each prime number starting from 2, and remaining unmarked numbers are primes.

26
Q

Write a snippet to implement the Sieve algorithm.

A

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;
}
}

27
Q

What is Euclid’s algorithm for GCD?

A

Use the formula:

int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}

28
Q

Why is the Sieve of Eratosthenes efficient?

A

It has a time complexity of O(n log log n) and avoids redundant calculations.

29
Q

How do you calculate the GCD iteratively?

A

while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;