Interview Java Q's Flashcards

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
Q

How do you calculate a power?

A

Math.pow(base, exponent)

26
Q

How do you calculate a square root?

A

Math.sqrt(number)

27
Q

How do you create a LocalDate object for a specific date?

A

LocalDate date = LocalDate.of(year, month, day);

28
Q

How do you get the day of week from a LocalDate?

A

DayOfWeek day = date.getDayOfWeek();

29
Q

How do you parse a date string with a specific format?

A

LocalDate parsed = LocalDate.parse(dateString, DateTimeFormatter.ofPattern(“dd/MM/yyyy”));

30
Q

How do you get a currency formatter for a specific locale?

A

NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.US);

31
Q

How do you format a number with specific decimal places?

A

String.format(“%.2f”, number)

32
Q

How do you throw an exception with a custom message?

A

throw new Exception(“Your message here”);

33
Q

How do you write a basic try-catch block?

A

try { // code that may throw exception } catch (Exception e) { // handle exception }

34
Q

How do you sort an array?

A

Arrays.sort(array)

35
Q

How do you convert an array to a List?

A

Arrays.asList(array)

36
Q

How do you check if a number is odd using bitwise operations?

A

number & 1

37
Q

How do you check if a number is a power of 2 using bitwise operations?

A

(n & (n - 1)) == 0

38
Q

How do you sort a List in natural order?

A

Collections.sort(list)

39
Q

How do you sort a List in reverse order?

A

list.sort(Comparator.reverseOrder())

40
Q

How do you check if Scanner has an integer input available?

A

scanner.hasNextInt()

41
Q

How do you safely parse a String to Integer with exception handling?

A

try { Integer.parseInt(stringValue); } catch (NumberFormatException e) { // handle invalid number }

42
Q

How do you implement a two-pointer technique for a sorted array?

A

int left = 0; int right = array.length - 1; while (left < right) { // Process elements from both ends left++; right–; }

43
Q

How do you find an element in a sorted array using binary search?

A

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
Q

How do you implement the sliding window pattern?

A

int windowStart = 0; for (int windowEnd = 0; windowEnd < array.length; windowEnd++) { // Add element to window while (/* condition */) { // Remove element from window windowStart++; } }

45
Q

How do you traverse a linked list using fast and slow pointers?

A

ListNode slow = head; ListNode fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; }

46
Q

How do you create and use a HashMap for counting elements?

A

HashMap<ElementType, Integer> map = new HashMap<>(); map.put(element, map.getOrDefault(element, 0) + 1);

47
Q

How do you iterate over HashMap entries?

A

for (Map.Entry<K, V> entry : map.entrySet()) { K key = entry.getKey(); V value = entry.getValue(); }

48
Q

How do you find all pairs in an array that sum to a target in O(n) time?

A

HashSet<Integer> set = new HashSet<>(); for (int num : array) { if (set.contains(target - num)) { // Found pair } set.add(num); }</Integer>

49
Q

How do you find the majority element using Boyer-Moore algorithm?

A

int count = 0; int candidate = 0; for (int num : array) { if (count == 0) candidate = num; count += (num == candidate) ? 1 : -1; }

50
Q

How do you check if a string is a palindrome efficiently?

A

int left = 0; int right = s.length() - 1; while (left < right) { if (s.charAt(left++) != s.charAt(right–)) return false; } return true;

51
Q

How do you implement a character frequency counter?

A

int[] freq = new int[26]; // for lowercase letters for (char c : s.toCharArray()) { freq[c - ‘a’]++; }

52
Q

What is the difference between ArrayList and LinkedList?

A

ArrayList: O(1) access, O(n) insertion/deletion. LinkedList: O(n) access, O(1) insertion/deletion at known position

53
Q

What is the time complexity of HashMap operations?

A

Average case: O(1) for put/get/remove/containsKey. Worst case: O(n) if many collisions