JavaExam.md --- ### 1. Abstract Data Types (ADTs) #### Easy Which ADT uses a Last-In-First-Out (LIFO) policy? * Array * Stack * Queue * LinkedList --- Describe a real-life scenario where a stack data structure could be used effectively. How would push, pop, and peek operations correspond to actions in this scenario? --- Explain how a queue data structure might be used in the management of print jobs on a printer. How would the enqueue, dequeue, and peek operations be utilized? --- What are some advantages and disadvantages of using a LinkedList over an Array in certain scenarios? Give an example of a scenario where a LinkedList would be a better choice. #### Hard Implement a basic queue using only a stack data structure. The queue should support `enqueue`, `dequeue`, and `peek` operations. ```java import java.util.Stack; public class QueueUsingStacks { //ADD STUFF HERE public void enqueue(int x) { //TODO } public int dequeue() { //TODO } public int peek() { //TODO } } ``` ### 2. Binary Trees #### Easy In a Binary Search Tree, if all nodes to the right contain larger values, then all nodes to the left of the parent node contain...: A. Smaller values B. Larger values C. Equal values D. Random values --- What is the maximum height for a binary tree that has `n` nodes. #### Hard Question: --- What is the minimum height for a binary tree that has `n` nodes. --- Write a recursive function that checks whether a binary tree is a valid binary search tree. ![example](https://i.imgur.com/6q77hW5.png) --- What is the worst-case time complexity of searching for a value in a binary search tree? A. O(log n) B. O(n) C. O(1) D. O(n log n) ### 3. Recursion/Iteration #### Easy Question: How many base cases can you have for a recursive function? A. Exactly 1 B. Infinite C. Any finite number greater than 0 D. 0 or 1 --- Write both an iterative and recursive function to compute the factorial of a given non-negative integer. --- Write both an iterative and recursive function `power2` where `power2(n) = 2 ^ n` that is `2 * 2 * 2 ...` `n` times. --- Which of the following problems can be solved using recursion? A. Computing factorials B. Traversing a tree C. Performing a binary search D. All of the above ### Hard: This is an inorder traversal ```java class BinaryTree { Node root; void inorderTraversal(Node node) { if (node == null) { return; } /* first recur on left child */ inorderTraversal(node.left); /* then print the data of node */ System.out.print(node.value + " "); /* now recur on right child */ inorderTraversal(node.right); } // Wrapping function void inorderTraversal() { inorderTraversal(root); } } ``` Rewrite it using an iterative method. --- Write a java method that takes a string that can contain three types of brackets, which are `(), [], {}`. It should return whether they are matching. For instance, `((()))` and `({[][]})` are matching, but `())`, `([(])`, `)(` and `({)}` are not. --- In general, how do you transform recursive method into an iterative one? ### 4. Stream (lambda expression) #### Easy Write a method using Java 8 Streams that calculates the product of all numbers in a list. --- Implement a method using Java 8 Streams to count the number of strings in a list that have more than 5 characters. --- Implement a method using Java 8 Streams that takes a list of integers and returns a new list containing only the even numbers. --- What does the `map` operation do on a Stream in Java 8? A. Filters elements based on a condition B. Transforms the elements of the stream C. Reduces the elements to a single summary element D. Limits the number of elements in the stream --- Consider the following Java code snippet: ``` List<String> names = Arrays.asList("John", "Jane", "Jill", "James"); names.stream().filter(name -> name.startsWith("J")).count(); ``` What will be the output of the above code? A. 4 B. 3 C. 2 D. 1 --- What does this code do ? ```java import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { List<String> words = Arrays.asList("Apple", "Banana", "Cherry", "Blueberry", "Elderberry"); List<String> sortedWords = words.stream() .sorted(Comparator.comparing(String::length)) .collect(Collectors.toList()); System.out.println(sortedWords); } } ``` #### Hard Implement a method using Java 8 Streams that takes a list of numbers and returns the list sorted in ascending order, but without any duplicate numbers. --- Look at this code. ```java import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Main { public static class Fruit implements Comparable<Fruit> { private String name; public Fruit(String name) { this.name = name; } public String getName() { return name; } @Override public int compareTo(Fruit otherFruit) { // Reverse order by name return __________ // REPLACE HERE } @Override public String toString() { return this.name; } } public static void main(String[] args) { List<Fruit> fruits = Arrays.asList( new Fruit("Apple"), new Fruit("Banana"), new Fruit("Cherry"), new Fruit("Blueberry"), new Fruit("Elderberry")); List<Fruit> sortedFruits = fruits.stream() .sorted() .collect(Collectors.toList()); System.out.println(sortedFruits); } } ``` How do you change the `compareTo` method so the output is `[Elderberry, Cherry, Blueberry, Banana, Apple]` ### 5. Hashing (HashMap) #### Easy Question: In a HashMap, data is stored using: A. Key-Value pairs B. Queues C. Stacks D. Arrays #### Hard Question Given a string S, use hash maps to make a function `f` return all characters in the string that occur at least 5 times. For example `f("aaaaaaaaaaaabaaaaaaacaaaaadeeeffffffff")` will return `"af"`. It doesn't matter if you return `"af"` or `"fa"`. Implement `f`. ### XML #### Easy Question What does XML stand for? A. eXtensible Markup Language B. eXtensible Madeup Language C. eXample Machine Learning D. Xenophobic Mario Luigi --- Which of the following is a self-closing tag in XML? A. `<tag> </tag>` B. `<tag>` C. `<tag/>` D. `</tag>` # Conclusion Blame errors on ChatGPT. JavaExam.md --- ### 1. Abstract Data Types (ADTs) #### Easy Which ADT uses a Last-In-First-Out (LIFO) policy? * Array * Stack * Queue * LinkedList --- Describe a real-life scenario where a stack data structure could be used effectively. How would push, pop, and peek operations correspond to actions in this scenario? --- Explain how a queue data structure might be used in the management of print jobs on a printer. How would the enqueue, dequeue, and peek operations be utilized? --- What are some advantages and disadvantages of using a LinkedList over an Array in certain scenarios? Give an example of a scenario where a LinkedList would be a better choice. #### Hard Implement a basic queue using only a stack data structure. The queue should support `enqueue`, `dequeue`, and `peek` operations. ```java import java.util.Stack; public class QueueUsingStacks { //ADD STUFF HERE public void enqueue(int x) { //TODO } public int dequeue() { //TODO } public int peek() { //TODO } } ``` ### 2. Binary Trees #### Easy In a Binary Search Tree, if all nodes to the right contain larger values, then all nodes to the left of the parent node contain...: A. Smaller values B. Larger values C. Equal values D. Random values --- What is the __maximum__ height for a binary tree that has `n` nodes. #### Hard Question: --- What is the __minimum__ height for a binary tree that has `n` nodes. --- Write a recursive function that checks whether a binary tree is a valid binary search tree. ![example](https://i.imgur.com/6q77hW5.png) --- What is the worst-case time complexity of searching for a value in a binary search tree? A. O(log n) B. O(n) C. O(1) D. O(n log n) ### 3. Recursion/Iteration #### Easy Question: How many base cases can you have for a recursive function? A. Exactly 1 B. Infinite C. Any finite number greater than 0 D. 0 or 1 --- Write both an iterative and recursive function to compute the factorial of a given non-negative integer. --- Write both an iterative and recursive function `power2` where `power2(n) = 2 ^ n` that is `2 * 2 * 2 ...` `n` times. --- Which of the following problems can be solved using recursion? A. Computing factorials B. Traversing a tree C. Performing a binary search D. All of the above ### Hard: This is an inorder traversal ```java class BinaryTree { Node root; void inorderTraversal(Node node) { if (node == null) { return; } /* first recur on left child */ inorderTraversal(node.left); /* then print the data of node */ System.out.print(node.value + " "); /* now recur on right child */ inorderTraversal(node.right); } // Wrapping function void inorderTraversal() { inorderTraversal(root); } } ``` Rewrite it using an iterative method. --- Write a java method that takes a string that can contain three types of brackets, which are `(), [], {}`. It should return whether they are matching. For instance, `((()))`, `()(){}[]` and `({[][]})` are matching, but `())`, `([(])`, `)(` and `({)}` are not. --- In general, how do you transform recursive method into an iterative one? ### 4. Stream (lambda expression) #### Easy Write a method using Java 8 Streams that calculates the product of all numbers in a list. --- Implement a method using Java 8 Streams to count the number of strings in a list that have more than 5 characters. --- Implement a method using Java 8 Streams that takes a list of integers and returns a new list containing only the even numbers. --- What does the `map` operation do on a Stream in Java 8? A. Filters elements based on a condition B. Transforms the elements of the stream C. Reduces the elements to a single summary element D. Limits the number of elements in the stream --- Consider the following Java code snippet: ``` List<String> names = Arrays.asList("John", "Jane", "Jill", "James"); names.stream().filter(name -> name.startsWith("J")).count(); ``` What will be the output of the above code? A. 4 B. 3 C. 2 D. 1 --- What does this code do ? ```java import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { List<String> words = Arrays.asList("Apple", "Banana", "Cherry", "Blueberry", "Elderberry"); List<String> sortedWords = words.stream() .sorted(Comparator.comparing(String::length)) .collect(Collectors.toList()); System.out.println(sortedWords); } } ``` #### Hard Implement a method using Java 8 Streams that takes a list of numbers and returns the list sorted in ascending order, but without any duplicate numbers. --- Look at this code. ```java import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Main { public static class Fruit implements Comparable<Fruit> { private String name; public Fruit(String name) { this.name = name; } public String getName() { return name; } @Override public int compareTo(Fruit otherFruit) { // Reverse order by name return __________ // REPLACE HERE } @Override public String toString() { return this.name; } } public static void main(String[] args) { List<Fruit> fruits = Arrays.asList( new Fruit("Apple"), new Fruit("Banana"), new Fruit("Cherry"), new Fruit("Blueberry"), new Fruit("Elderberry")); List<Fruit> sortedFruits = fruits.stream() .sorted() .collect(Collectors.toList()); System.out.println(sortedFruits); } } ``` How do you change the `compareTo` method so the output is `[Elderberry, Cherry, Blueberry, Banana, Apple]` ### 5. Hashing (HashMap) #### Easy Question: In a HashMap, data is stored using: A. Key-Value pairs B. Queues C. Stacks D. Arrays #### Hard Question Given a string S, use hash maps to make a function `f` return all characters in the string that occur at least 5 times. For example `f("aaaaaaaaaaaabaaaaaaacaaaaadeeeffffffff")` will return `"af"`. It doesn't matter if you return `"af"` or `"fa"`. Implement `f`. ### XML #### Easy Question What does XML stand for? A. eXtensible Markup Language B. eXtensible Madeup Language C. eXample Machine Learning D. Xenophobic Mario Luigi --- Which of the following is a self-closing tag in XML? A. `<tag> </tag>` B. `<tag>` C. `<tag/>` D. `</tag>` # Conclusion Blame errors on ChatGPT.