Syntax Practice Flashcards

1
Q

Write a loop that prints out every key in the Map foo.

A

for (char c : freq.keySet()) {
System.out.println(“key: “ + c);
}

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

Define a ListNode class
- It will have a public int value
- It will have a public ListNode next
- Define a constructor that sets the value

A

public class ListNode {
int val;
ListNode next;
ListNode (int val) {
this.val=val;
}
}

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

Get a value from a HashMap foo with the key ‘b’.

A

foo.get(‘b’);

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

Write an if statement that prints “Hello” if the Map foo contains the character ‘a’.

A

if (freq.containsKey(‘a’)) {
System.out.println(“Hello”);

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

Write a function “print” for an int array “foo.”
- The function should print out each value.

A

public static void print (int[] array) {
for (int item : array) {
System.out.print(item + “ - “);
}
}

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

Write a function “fibonacci”
- The function can be iterative
- The function will take in a size of the fibonacci seq
- The function will return a new array of the fibonacci seq for those values

A

public static int[] fibonacci (int size) {
int[] result = new int[size];
for (int i = 0; i < size; i++) {
if (i == 0 || i == 1) {
result[i] = i;
} else {
result[i] = result[i-1] + result[i-2];
}
}
return result;
}

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

Iterate over int array foo and set foo[i] = i + 1.

A

for (int i = 0; i < foo.length; i++) {
foo[i] = i + 1;
}

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

Write a function print
- The function can be static
- The function will take in an ListNode head
- The function will print out every value

A

public static void print (ListNode head) {
while (head != null) {
System.out.print(head.val + “ “);
head = head.next;
}
}

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

Write a function “sum”
- The function will take in an int array “arr”
- The function will return the sum of all values

A

public static int sum (int[] array) {
int result = 0;
for (int item : array) {
result += item;
}
return result;
}

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

Declare an int Stack foo.

A

Stack<Integer> foo = new Stack<>();</Integer>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. Write a function “getLength”
    - The function can be static
    - The function will take in a ListNode head
    - The function will return the length of the LinkedList
A

public static int getLength (ListNode head) {
int length = 0;
while (head != null) {
length++;
head= head.next;
}
return length;
}

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

Declare a char -> int HashMap foo.

A

HashMap<Character,Integer> foo = new HashMap<>();

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

Insert a char-int key-value pair into a HashMap called foo.

A

foo.put(‘a’,3);

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

Write a loop that prints out every value in the Map foo.

A

for (char c : foo.keySet()) {
System.out.println(“value: “ + foo.get(c));
}

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

Declare a new array foo of type int and length 5.

A

int[] foo = new int[5];

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

Write a function “reverse”
- The function will take in an int array “arr”
- The function will return a new array

A

public static int[] reverse (int[] array) {
int n = array.length;
int[] result = new int[n];
for (int i = 0; i < n; i++) {
result[i] = array[n - 1 - i];
}
return result;
}

17
Q

Declare a new array bar of type int and length foo.length * 2.

A

int[] bar = new int[foo.length*2];

18
Q

Write an if statement that prints “World” if the value for the character ‘a’ is greater than 5

A

if (freq.get(‘a’) > 5) {
System.out.println(“World”);
}

19
Q
  1. Write a function “contains”
    - The function can be static
    - The function will take in an ListNode head
    - The function will take in an int searchValue
    - The function will return true if the LinkedList contains the value
A

public static boolean contains (ListNode head, int searchValue) {
while (head != null) {
if (head.val == searchValue) {
return true;
}
head = head.next;
}
return false;
}

20
Q

Write a function to print a map
- The function can be static
- The function should take in a map
- The function should print out “{key} -> {value}”

A

public static void printMap (Map<Character,Integer> map) {
for (char
key : map.keySet()) {
System.out.println(key + “ -> “ + map.get(key));
}
}

21
Q

Take an existing key from HashMap foo and add 1 to its value. If the key is not in foo set it’s value to 1.

A

freq.put(‘b’,freq.getOrDefault(‘b’,0)+1);

22
Q

Write a function “indexOf”
- The function can be static
- The function will take in an int array “arr”
- The function will take in an int “searchValue”
- The function will return the index of the value, or -1 if not found

A

public static int indexOf (int[] array, int target) {
int result = -1;
for (int i = 0; i < array.length; i++) {
if ( array[i] == target) {
result = i;
return result;
}
}
return result;
}

23
Q

Write a binary search function
- The function must find the index of a target number in an int array
- If the number is not found, return -1

A

public static int binarySearch (int[] nums, int target) {
int firstIndex = 0;
int lastIndex = nums.length-1;

    while (firstIndex <= lastIndex) {
        int middleIndex = (firstIndex + lastIndex) / 2;
        if (nums[middleIndex] == target) {
            return middleIndex;
        }
        if (nums[middleIndex] < target) {
            firstIndex = middleIndex + 1;
        }
        if (nums[middleIndex] > target) {
            lastIndex = middleIndex - 1;
        }
    }
    return -1;
}
24
Q

Write a print function for a singly linked list.
- The function will take in a ListNode head
- The function should print out the value of every node in the list
- ListNode definition:
public static class ListNode {
int val;
ListNode next;
}

A

public static void print (ListNode head) {
while (head != null) {
System.out.print(head.val + “ “);
head = head.next;
}
System.out.println();
}

25
Q

Write a “contains” function for a singly linked list.
- The function will take in a ListNode head
- The function will take in an int searchValue
- The function will return true if the linked list contains the value

A

public static boolean contains (ListNode head, int searchValue) {
while (head != null) {
if (head.val == searchValue) {
return true;
}
head = head.next;
}
return false;
}

26
Q

Write a “getLength” function for a singly linked list.
- The function will take in a ListNode head
- The function will return the number of nodes in the linked list

A

public static int getLength (ListNode head) {
int length = 0;
while (head != null) {
length++;
head= head.next;
}
return length;
}

27
Q

Declare a String Builder called foo.

A

StringBuilder foo = new StringBuilder();

28
Q

Given a String str, return the character that exists at index 4.

A

str.charAt(4);

29
Q

Write a function that converts a String str into an array of characters.

A

public static char[] stringToCharArray (String str) {
char[] result = new char[str.length()];
for (int i = 0; i < str.length(); i++) {
result[i] = str.charAt(i);
}
return result;
}

30
Q

Write a function that reverses a given Linked List

A

public static ListNode reverseLinkedList (ListNode head) {
ListNode prev = null;
ListNode current = head;
while (current != null) {
ListNode nextNode = current.next;
current.next = prev;
prev = current;
current = nextNode;
}
return prev;
}

31
Q
A