









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A comprehensive set of review questions and answers for a midterm exam in csc 300: data structures i. It covers key concepts related to arrays, linked lists, and basic data types. The questions are designed to test understanding of data structure fundamentals, including array declaration, initialization, manipulation, and the differences between arrays and linked lists. Valuable for students preparing for their midterm exam in data structures.
Typology: Exams
1 / 16
This page cannot be seen from the preview
Don't miss anything!
Which of the following is the correct way to declare and initialize a data fields as an array of 10 Strings? - Correct: ✔✔private String[] names = new String[10]; What does the modulus operator (%) do? - Correct: ✔✔Returns the division remainder What would result in adding 1 to an int variable "x"? - Correct: ✔✔x++ x = x + 1 x += 1 Examine the following code example, and then answer the questions below: String[] names = new String[10]; - Correct: ✔✔This code creates an array of 10 String references called "names", each of which has the value of null. Examine the code below and indicate the purpose of this code below: (assume the array parameters "a" and "b" are always of equal size) public static boolean x(double[] a, double[] b) {
for (int i = 0; i < a.length; i++) { if (a[i] != b[i]) return false; } return true; } - Correct: ✔✔This method returns true if the contents of the 2 arrays are equal. An individual element of an "int" array can be set to null. i .e., nums[2] = null; - Correct: ✔✔False Examine the following and select the best description of what it does below: byte[] values = new byte[] { 0, 55, 32, 112, 270, 130}; int g = Integer.MAX_VALUE; for (int i = 0; i < values.length; i++) { if (values[i] < g) { g = values[i]; } }
b--; } - Correct: ✔✔This code reverses the contents of the array "nums". Examine the following code and indicate below its purpose: (assume it compiles and executes properly) int[] s = new int[] { ... 10 integer values go here ...}; int c = 0; for (int i = 0; i < s.length; i++) { c += s[i]; } int d = c/s.length; if (d < 60) System.out.println(true); else System.out.println(false); - Correct: ✔✔The code determines if the average of a set of grades in the "s" array results in a failing grade. Examine the following code and indicate below its purpose: int[] values = new int[10]; - Correct: ✔✔This code creates an array of 10 integers called "values", each of which has the value of 0.
Examine the following code and indicate below its purpose: double[] values = new double[] {3.14, 6.02, 7.07, 2.12}; double a = 0; for (int i = 0; i < values.length; i++) { a += values[i]; } double b = a / values.length; System.out.println(b); - Correct: ✔✔This code will calculate and print the average of the values in the array "values". Examine the following code and indicate below its purpose: String[] names = new String[0]; System.out.println(names[0]); - Correct: ✔✔This code will result in an ArrayIndexOutOfBoundsException Examine the following code and select the best description of what it does below: Assume a class Node exists, containing a public Node reference called "next" (which refers to the next Node in the list). Also assume a Node
Examine the below Linked List of "Node" objects referred to by a variable called "first". The Node class contains a public double called "value" and a public Node reference called "next" (which refers to the next Node in the list). "x" is a Node reference that points ot the last Node element in the list. 11.0 | --> - 21.2 | --> 31.0 | --> 41.0 | --> 76.6 | X - Correct: ✔✔first = x; would result in a single element list, with a node containing the value 76.6. x.next = first; would result in circularly linked list (a cycle where the end of the list points back to the beginning of the list). first.next = x; would result in a 2 element list, containing the values 11. and 76.6. Examine the linked list diagram. Assume a class Node exists, containing a public String called "item" and a public Node reference called "next" (which refers to the next Node in the list). The Node constructor accepts and sets the "item" and "next" data fields. Also assume a Node reference exists called "first" that points to the beginning of the linked list. "A"| --> "B" | --> "C" | --> "D" | --> "E" | X
What code snippet below would insert a new Node containing "AA" at the beginning of the list? - Correct: ✔✔Node nn = new Node("AA", first);first = nn; Examine the following and select the best description of what this code will do: Assume a class Node exists, containing a public String called "item" and a public Node reference called "next" (which refers to the next Node in the list). Also assume a Node reference exists called "first" that points to the beginning of the linked list. Node walker = first; while (walker != null) { System.out.println(walker.item); } - Correct: ✔✔This code results in an "infinite-loop", continually printing the first list element (one entry per output line). Examine the linked list diagram below. Assume a class Node exists, containing a public String called "item" and a public Node reference called "next" (which refers to the next Node in the list). The Node constructor accepts and sets the "item" and "next" data fields.
Examine the linked list diagram and the code below, and then select the best description of what the code does below: Assume a class Node exists, containing a public String called "item" and a public Node reference called "next" (which refers to the next Node in the list). The Node constructor accepts and sets the "item" and "next" data fields. Also assume a Node reference exists called "first" that points to the beginning of the linked list, and a Node reference exists called "walker", as shown below. first walker "A"| --> "B" | --> "C" | --> "D" | --> "E" | X Node nn = new Node("AA", walker.next); walker.next = nn; - Correct: ✔✔This code shown inserts a Node containing "AA" between the Nodes containing "C" and "D". Examine the below diagram showing a Linked List of "Node" objects referred to by a variable called "first". The Node class contains a public double called "value" and a public Node reference called "next" (which refers to the next Node in the list). Then indicate which of the statements are true:
first 11.0 | --> - 21.2 | --> 31.0 | --> 41.0 | --> 76.6 | X first = first.next; will result in a NullPointerException. first.next = null; is invalid syntax and will result in a compiler error. first = first.next; will cut off the first element in the list, resulting in "first" being a Linked List containing the last four elements of the original list. first.next = null; will cut off the elements after "first", resulting in "first" being a Linked List containing one Node object. - Correct: ✔✔first = first.next; will cut off the first element in the list, resulting in "first" being a Linked List containing the last four elements of the original list. first.next = null; will cut off the elements after "first", resulting in "first" being a Linked List containing one Node object. Assume a class Node exists, containing a public Node reference called "next" (which refers to the next Node in the list). Also assume a Node reference exists called "first" that points to the beginning of the linked list.
What is the storage requirement for a long? What is its range of numbers? - Correct: ✔✔8 bytes,
What is a null? Can primitive types be null? - Correct: ✔✔Null is the default value for reference types. It signifies that the variable contains nothing. Primitive types cannot be null. What is true about 'for' loops? - Correct: ✔✔1. They use a counter to track iterations
The minimum size variable type to use to hold a 4-digit year value (a whole number) is... - Correct: ✔✔short The method that is executed when you "run" a program in Java is defined as... - Correct: ✔✔public static void main(String[] args) Java Strings are... - Correct: ✔✔objects Which of these is a well-formed Java class name? - Correct: ✔✔TankerTruck The 3 components of a "for" loop's definition are: - Correct: ✔✔(initialization; condition; increment/decrement)