Interview Java Q's Flashcards

(53 cards)

1
Q

How do you create a Scanner object to read input from console?

A

Scanner scanner = new Scanner(System.in);

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

What Scanner method reads an entire line as a String?

A

scanner.nextLine()

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

What Scanner method reads the next integer value?

A

scanner.nextInt()

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

How do you print output without a new line in Java?

A

System.out.print()

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

How do you print output with a new line in Java?

A

System.out.println()

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

How do you get the length of a String?

A

string.length()

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

How do you get a character at a specific index in a String?

A

string.charAt(index)

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

How do you extract a portion of a String from start index (inclusive) to end index (exclusive)?

A

string.substring(startIndex, endIndex)

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

How do you convert a String to uppercase?

A

string.toUpperCase()

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

How do you convert a String to lowercase?

A

string.toLowerCase()

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

How do you remove whitespace from beginning and end of a String?

A

string.trim()

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

How do you split a String into an array using a delimiter?

A

string.split(delimiter)

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

How do you convert a String to an integer?

A

Integer.parseInt(string)

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

How do you convert a String to a double?

A

Double.parseDouble(string)

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

How do you convert a number to a String?

A

String.valueOf(number)

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

How do you create an empty ArrayList?

A

ArrayList<Type> list = new ArrayList<>();

ArrayList<String> cars = new ArrayList<String>();

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

How do you add an element to a List?

A

list.add(element)

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

How do you remove an element at a specific index from a List?

A

list.remove(index)

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

How do you get the size of a List?

A

list.size()

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

How do you check if a List contains an element?

A

list.contains(element)

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

How do you get an element at a specific index from a List?

A

list.get(index)

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

How do you find the maximum number between two numbers?

A

Math.max(a, b)

23
Q

How do you find the minimum of two numbers?

A

Math.min(a, b)

24
Q

How do you calculate the absolute value? (ab=+ve number)

A

Math.abs(number)

25
How do you calculate a power?
Math.pow(base, exponent)
26
How do you calculate a square root?
Math.sqrt(number)
27
How do you create a LocalDate object for a specific date?
LocalDate date = LocalDate.of(year, month, day);
28
How do you get the day of week from a LocalDate?
DayOfWeek day = date.getDayOfWeek();
29
How do you parse a date string with a specific format?
LocalDate parsed = LocalDate.parse(dateString, DateTimeFormatter.ofPattern("dd/MM/yyyy"));
30
How do you get a currency formatter for a specific locale?
NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.US);
31
How do you format a number with specific decimal places?
String.format("%.2f", number)
32
How do you throw an exception with a custom message?
throw new Exception("Your message here");
33
How do you write a basic try-catch block?
try { // code that may throw exception } catch (Exception e) { // handle exception }
34
How do you sort an array?
Arrays.sort(array)
35
How do you convert an array to a List?
Arrays.asList(array)
36
How do you check if a number is odd using bitwise operations?
number & 1
37
How do you check if a number is a power of 2 using bitwise operations?
(n & (n - 1)) == 0
38
How do you sort a List in natural order?
Collections.sort(list)
39
How do you sort a List in reverse order?
list.sort(Comparator.reverseOrder())
40
How do you check if Scanner has an integer input available?
scanner.hasNextInt()
41
How do you safely parse a String to Integer with exception handling?
try { Integer.parseInt(stringValue); } catch (NumberFormatException e) { // handle invalid number }
42
How do you implement a two-pointer technique for a sorted array?
int left = 0; int right = array.length - 1; while (left < right) { // Process elements from both ends left++; right--; }
43
How do you find an element in a sorted array using binary search?
int left = 0; int right = array.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (array[mid] == target) return mid; else if (array[mid] < target) left = mid + 1; else right = mid - 1; }
44
How do you implement the sliding window pattern?
int windowStart = 0; for (int windowEnd = 0; windowEnd < array.length; windowEnd++) { // Add element to window while (/* condition */) { // Remove element from window windowStart++; } }
45
How do you traverse a linked list using fast and slow pointers?
ListNode slow = head; ListNode fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; }
46
How do you create and use a HashMap for counting elements?
HashMap map = new HashMap<>(); map.put(element, map.getOrDefault(element, 0) + 1);
47
How do you iterate over HashMap entries?
for (Map.Entry entry : map.entrySet()) { K key = entry.getKey(); V value = entry.getValue(); }
48
How do you find all pairs in an array that sum to a target in O(n) time?
HashSet set = new HashSet<>(); for (int num : array) { if (set.contains(target - num)) { // Found pair } set.add(num); }
49
How do you find the majority element using Boyer-Moore algorithm?
int count = 0; int candidate = 0; for (int num : array) { if (count == 0) candidate = num; count += (num == candidate) ? 1 : -1; }
50
How do you check if a string is a palindrome efficiently?
int left = 0; int right = s.length() - 1; while (left < right) { if (s.charAt(left++) != s.charAt(right--)) return false; } return true;
51
How do you implement a character frequency counter?
int[] freq = new int[26]; // for lowercase letters for (char c : s.toCharArray()) { freq[c - 'a']++; }
52
What is the difference between ArrayList and LinkedList?
ArrayList: O(1) access, O(n) insertion/deletion. LinkedList: O(n) access, O(1) insertion/deletion at known position
53
What is the time complexity of HashMap operations?
Average case: O(1) for put/get/remove/containsKey. Worst case: O(n) if many collisions