CSE 110 Exam tips for exam 3
When you are not writing a complete program (in the short answer questions for example), you can assume everything you need is properly imported. So, if the question has you create a Scanner to take user input, you do not need to write "______ ______ ______ ______" and can just go right to declaring and initializing your Scanner.
import java.util.Scanner;
legal index values
0 is the first and the last index is 1 less than the length
What does this print? ArrayList str1 = new ArrayList(); str1.add(1); str1.add(2); str1.add(3); for(int i = 0; i < str1.size(); i++) { str1[i] = 0; } System.out.println("Array list " + str1); }
0,0,0
What does this print out? ArrayList str1 = new ArrayList(); str1.add(1); str1.add(2); str1.add(3); ArrayList str2 = new ArrayList(str1); System.out.print(" " + str2);
1,2,3
What does this print? ArrayList str1 = new ArrayList(); str1.add(1); str1.add(2); str1.add(3); System.out.println(" Array List before " + str1); str1.removeAll(str1); System.out.println (" Array List after " + str1);
Array List before 1,2,3 Array List after 0,0,0
how to declare an array
Array str1 = new ArrayList();
how to declare two arrays and compare them to see of they are the same
Array str1 = new ArrayList(); Array str2 = new ArrayList(); System.out.println(" "+str1.equals(str2));
fill an array with zeros
ArrayList str1 = new ArrayList(); str1.add(1); str1.add(2); str1.add(3); for(int i = 0; i < str1.size(); i++) { str1[i] = 0; } System.out.println("Array list " + str1); }
copy one array list to another and print the second array
ArrayList str1 = new ArrayList(); str1.add(1); str1.add(2); str1.add(3); ArrayList str2 = new ArrayList(str1); System.out.print(" " + str2);
How do you remove all the elements from an array?
ArrayList str1 = new ArrayList(); str1.add(1); str1.add(2); str1.add(3); System.out.println(" Array List before " + str1); str1.removeAll(str1); System.out.println (" Array List after " + str1);
When the instructions say "_____ ______ ____ ______ ______" or something similar, this means those are declared and initialized elsewhere in the program. Do not declare or initialize them yourself and do not provide values for them. You are using these variables as they already are. You do not need to know the exact value of a variable in order to use it.
Given three ints values called
Unless the instructions specifically say that there is an error in the code, or is outright asking if the code will compile and run correctly, all code provided to you is intended to compile and run as expected. This means if you see what you think is an error that would stop it from running, you had better be very certain before simply answering the question with "__ ____ ______ __ _____ __ __ ______" or something similar. If you say that, and you're wrong, you won't get any points. If you think you see an error, make note of it, but still answer the question as if it's not a problem (if at all possible). The graders will let you know if you were right or not and why.
It won't compile so there is no output
Whether you are writing a complete program or not, you do not need to provide input prompts to the user. For example, if you are accepting two int values, there is no need to print out "_____ __ __ ___ _____" before each input statement. You also do not need to add anything to explain your output. So, if you are displaying the sum of the two ints, you do not need to put "___ ___ ___" in front of that output.
Type in an int value, The sum is:
Unless the instructions say specifically "________ _ _______ _____ ________" then you only need to write the lines needed to complete the task. So, you do not need to write "public static void main (String[] args)" for example. You can assume all of that is there.
Write a complete Java program...
bounds error happens when
an illegal index value is used
Write a method that reverses the sequence of elements in an array.
boolean found = false; int low = 0; int high = values.length - 1; int pos = 0; while (low <= high && !found) { pos = (low + high) / 2; // Midpoint of the subsequence if (values[pos] == searchedNumber) { found = true; } else if (values[pos] < searchedNumber) { low = pos + 1; } // Look in second half else { high = pos - 1; } // Look in first half } if (found) { System.out.println("Found at position " + pos); } else { System.out.println("Not found. Insert before position " + pos); }
True or false? Elements of different columns in a two-dimensional array can have different types.
false, a 2d array cannot have two types of elements
True or false? method cannot return a two-dimensional array.
false, a method can return a 2d array
True or false? Arrays cannot contain strings as elements.
false, a string array can be declared.
True or false? Two-dimensional arrays always have the same number of rows and columns.
false, when declaring a 2d array the column and rows are declared individually
write code that prints an array backwards
public static void main(String[] args) { final int length = 10; double[] arr = new double[length]; int currentSize = 0; Scanner scan = new Scanner(System.in); while(in.hasNextDouble() && currentSize <arr.length) { arr[currentSize] = in.nextDouble(); currentSize++; } for( int i = currentSize - 1 ; i> = 0 ; i--) { system.out.println(" " + arr[i]); } }
how do you reverse an array?
public static void reverseArray(int[] arr) { int lastIndex=arr.length-1; int temp; int mid; if(lastIndex%2 != 0) { mid = (lastIndex+1)/2; } else { mid=lastIndex/2; } for(int i = 0; i { temp = arr[i]; arr[i]=arr[lastIndex]; arr[lastIndex]=temp; lastIndex-- } } }
what is the output for this and why? Array str1 = new ArrayList(); Array str2 = new ArrayList(); str2.add(2); Str2.add(1); str2.add(3); System.out.println(" "+str1.equals(str2));
the output would be false. .equals sequentially matches each element and at the first mismatch boolean false is returned.
True or false? A method cannot change the number of columns of an argument that is a two-dimensional array.
true, a method can manipulate the number of columns
True or false? A method cannot change the length of an array argument.
true, the length of an array cannot be modified by a method
True or false? All elements of an array are of the same type.
true, the types of the elements are declared when the string is declared.