quiz 2
What are the ending contents of the array? Choices show elements in index order 0, 1, 2. int[] yearsList = new int[3]; yearsList[0] = 5; yearsList[1] = yearsList[0]; yearsList[0] = 10; yearsList[2] = yearsList[1];
10, 5, 5
How many elements does the array declaration create? int[] scores = new int[10]; scores[0] = 25; scores[1] = 22; scores[2] = 18; scores[3] = 28;
10
Given that integer array x has elements 4, 7, 3, 0, 8, what are the elements after the loop? int i; for (i = 0; i < 4; ++i) { x[i] = x[i + 1]; }
7, 3, 0, 8, 8
How many elements are in the array? int[][] userVals = new int[2][4];
8
Given two integer arrays prevValues and newValues. Which code copies the array prevValues into newValues? The size of both arrays is NUM_ELEMENTS. newValues = prevValues; newValues[] = prevValues[]; newValues[NUM_ELEMENTS] = prevValues[NUM_ELEMENTS]; for (i = 0; i < NUM_ELEMENTS; ++i) { newValues[i] = prevValues[i]; }
for (i = 0; i < NUM_ELEMENTS; ++i) { newValues[i] = prevValues[i]; }
Given x = 4 and y = 8, what are the ending values of x and y? x = y; y = x; x = y;
x = 8, y = 8
Which XXX is needed for enterNumber() to read a value from standard input? public int enterNumber() XXX{ int value; value = System.in.read(); return value; } import java.io throws SystemException nothing is needed throws IOException
throws IOException
What code for XXX, YYY correctly swaps a and b? Choices are written as XXX / YYY. XXX; a = b; YYY; b = tmp; tmp = a / (nothing) tmp = a / tmp = b tmp = b / (nothing) (nothing) / tmp = a
tmp = a / (nothing)
Given an array with values 5, 10, 15, 20, 25, what are the fewest number of swaps needed to reverse the list? 2 3 4 5
2
Which line(s) immediately flush the output buffer? 1 System.out.print("One for the money"); 2 System.out.print("Two for the show\n"); 3 System.out.println("Three to get ready"); 4 System.out.print("And four to go"); 5 System.out.flush();
2, 3, and 5
What is the ending value of the element at index 0? int[] numbers = new int[10]; numbers[0] = 35; numbers[1] = 37; numbers[1] = numbers[0] + 4;
35
Which is true? The FileOutputStream class includes println( ) A PrintWriter object should be closed using close( ) A PrintWriter constructor requires an OutputStream object A FileOutputStream object opens an existing file
A PrintWriter constructor requires an OutputStream object
Which is true? A program must import java.io.system to use System.out System.output.print() only outputs objects of type String The output of println() for an object reference includes all data stored in the object Data written to System.out are placed in a buffer and eventually output
Data written to System.out are placed in a buffer and eventually output
Given two integer arrays, largerArray with 4 elements, and smallerArray with 3 elements. What is the result after this code executes? for (i = 0; i < 4; ++i) { largerArray[i] = smallerArray[i]; } All the elements of largerArray will get copied to smallerArray. Some of the elements of largerArray will get copied to smallerArray. All of the elements of smallerArray will get copied to largerArray. Error: The two arrays are not the same size.
Error: The two arrays are not the same size.
What is output given the user input? >three blind mice System.out.print("Enter info: "); Scanner keyboard = new Scanner(System.in); String info = keyboard.nextLine(); Scanner inputScnr = new Scanner(info); int item1 = inputScnr.nextInt(); String item2 = inputScnr.next(); String item3 = inputScnr.next(); System.out.println(item1 + " " + item2 + " " + item1);
Error: input mismatch exception
What is output? (_ represents a blank space) String name = "Samantha"; System.out.printf("Her name is %6.3s", name); Her name is ___Sam Her name is %6.3s Her name is Sam her name is Samant
Her name is ___Sam
Which XXX / YYY declare an array having MAX_SIZE elements and initializes all elements with -1? final int MAX_SIZE = 4; int[] myNumbers = new int[XXX]; int i; for (i = 0; i < YYY; ++i) { myNumbers[i] = -1; }
MAX_SIZE / MAX_SIZE
Given that integer array x has elements 5, 10, 15, 20, what is the output? int i; for (i = 0; i < 4; ++i) { System.out.print(x[i] + x[i + 1]); } 10, 15, 20, 5 15, 25, 35, 25 15, 25, 35, 20 Out of range access
Out of range access
The following program generates an error. Why? final int NUM_ELEMENTS = 5; int[] userVals = new int[NUM_ELEMENTS]; int i; userVals[0] = 1; userVals[1] = 7; userVals[2] = 4; for (i = 0; i <= userVals.length; ++i) { System.out.println(userVals[i]); } Variable i is never initialized. The array userVals has 5 elements, but only 3 have values assigned. The integer NUM_ELEMENTS is declared as a constant The for loop tries to access an index that is out of the array's valid range.
The for loop tries to access an index that is out of the array's valid range.
What is the error in writeFile( )? public static void writeFile() throws IOException { FileOutputStream foStream = null; PrintWriter outFS = null; foStream = new FileOutputStream(); outFS = new PrintWriter(foStream); outFS.println("Java is my favorite language!"); outFS.flush(); foStream.close(); } The output stream has not been properly opened writeFile( ) should throw PrintException outFS has not been closed foStream has not been flushed
The output stream has not been properly opened
What is output? double pi = 3.14159; System.out.printf("The value of PI is -%4.3f", pi); The value of PI is -%4.3f The value of PI is -3.142 The value of PI is 0003.142 The value of PI is 3.14159
The value of PI is -3.142
Given two arrays, which code will output all the arrays' elements, in the order key, item followed by a newline? int[] keysList = new int[SIZE_LIST]; int[] itemsList = new int[SIZE_LIST]; System.out.println(keysList[SIZE_LIST - 1] + ", " + itemsList[SIZE_LIST - 1]); System.out.println(keysList[SIZE_LIST] + ", " + itemsList[SIZE_LIST]); for (i = 0; i < SIZE_LIST - 1; ++i) { System.out.println(keysList[i] + ", " + itemsList[i]); } for (i = 0; i < SIZE_LIST; ++i) { System.out.println(keysList[i] + ", " + itemsList[i]); }
for (i = 0; i < SIZE_LIST; ++i) { System.out.println(keysList[i] + ", " + itemsList[i]); }
What is the output of this Java program? class Driver { public static void main(String[] args) { int a = 81; int b = 0; for (int c = 3; c <= 5; c++) { b = 0; while (b < c) { b = b + 1; a = a - b; } b = b + a; } System.out.print(b); } } -inputStream.hasNextInt( ) -total != 0 -inFS.hasNextInt( ) -inFS.eof( ) == false
inFS.hasNextInt( )
Which XXX and YYY correctly complete the code to find the maximum score? Choices are in the form XXX / YYY. int[] scores = {43, 24, 58, 92, 60, 72}; int maxScore; maxScore = scores[0]; for (XXX) { if (num > maxScore) { YYY; } } int scores: num / maxScore = num scores != 0 / num = maxScore int num: scores / maxScore = num num < scores / num = maxScore
int num: scores / maxScore = num
Which XXX and YYY correctly complete the code to find the maximum score? Choices are in the form XXX / YYY. int[] scores = {43, 24, 58, 92, 60, 72}; int maxScore; maxScore = scores[0]; for (XXX) { if (num > maxScore) { YYY; } } int scores: num / maxScore = num scores != 0 / num = maxScore int num: scores / maxScore = num num < scores / num = maxScore
int num: scores / maxScore = num
Which statement declares a two-dimensional integer array called myArray with 3 rows and 4 columns? int myArray[3, 4]; int[] myArray = new int[7]; int[][] myArray = new int[3][4]; int[] myArray = new int[4][3];
int[][] myArray = new int[3][4];
What is output given the user input? >3 blind mice System.out.print("Enter info: "); Scanner keyboard = new Scanner(System.in); String info = keyboard.nextLine(); Scanner inputScnr = new Scanner(info); int item1 = inputScnr.nextInt(); String item2 = inputScnr.next(); String item3 = inputScnr.next(); System.out.println(item3 + " " + item1);
mice 3
Which best describes what is output? Assume v is a large array of ints. int i; int s; s = v[0]; for (i = 0; i < N_SIZE; ++i) { if (s > v[i]) { s = v[i]; } } System.out.println(s); first value in v max value in v min value in v last value in v
min value in v
Which assigns the array's first element with 99? int[] myVector = new int[4]; myVector[] = 99; myVector[-1] = 99; myVector[0] = 99; myVector[1] = 99;
myVector[0] = 99;
Which is an invalid access for the array? int[] numsList = new int[5]; int x = 3; numsList[x-3] numsList[0] numsList[x+2] numsList[(2*x) - x]
numsList[x+2]
Which XXX allows information to be written to a file using print( )? FileOutputStream foStream = null; PrintWriter outFS = null; foStream = new FileOutputStream("outfile.txt"); XXX outFS.println("Java is my favorite language!"); outFS = new PrintWriter("outfile.txt"); new PrintWriter(foStream); outFS = PrintWriter.open(foStream); outFS = new PrintWriter(foStream);
outFS = new PrintWriter(foStream);
The enhanced for loop _____ reduces the number of iterations necessary to visit all elements of an array can be used to replace any regular for loop while yielding less code creates a copy of an existing array and iterates through the copy's elements prevents incorrectly accessing elements outside of an array's range
prevents incorrectly accessing elements outside of an array's range
Which XXX inserts userItem into sw until the user enters "Quit"? StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); System.out.println("Enter items (type Exit to quit):"); String userItem = scnr.next(); while (!userItem.equals("Quit")) { XXX userItem = scnr.next(); } System.out.println(sw.toString()); pw.print(userItem); pw.add(userItem); sw = sw + userItem sw.add(userItem);
pw.print(userItem);
What is the error in readFile()? public void readFile(){ FileInputStream fbStream = null; Scanner inFS = null; int value = 0; fbStream = new FileInputStream("data.txt"); inFS = new Scanner(fbStream); value = inFS.nextInt(); System.out.println("Value: " + value); fbStream.close(); } There is a data input mismatch readFile( ) does not throw IOException inFS is not closed The code has no errors
readFile( ) does not throw IOException
Given array scorePerQuiz has 10 elements. Which assigns the 7th element with the value 8? scorePerQuiz = 8; scorePerQuiz[6] = 8; scorePerQuiz[7] = 8; scorePerQuiz[8] = 7;
scorePerQuiz[6] = 8;
Given two arrays, studentNames that maintains a list of student names, and studentScores that has a list of the scores for each student, which XXX and YYY prints out only the student names whose score is above 80? Choices are in the form XXX / YYY. String[] studentNames = new String[NUM_STUDENTS]; int[] studentScores = new int[NUM_STUDENTS]; int i; for (i = 0; i < NUM_STUDENTS; ++i) { if (XXX > 80) { System.out.print(YYY + " "); } }
studentScores[i] / studentNames[i]