Computer Science 109: Introduction to Programming

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Examine the following code. What is the value of the fifth bucket in the conversion variable (conversion[5])?

0. Explanation: The array is initialized, but everything is 0 as no values have been provided.

In the following code, how many times will the for loop process?

13.

What will be the value of the variable tester_value when this loop is finished?

15. Explanation: The counter (i) and the value (tester_value) both start at 0, go until 5, and add i to the value each time; here is the order of the loop: i = 0; tester = 0 i = 1; tester = 1 i = 2; tester = 3 (2+1) i = 3; tester = 6 (3 + 3) i = 4; tester = 10 (6 + 4) i = 5; tester = 15 (10 + 5)

Which of the following declarations is correct for a Java array of three elements?

int[] test = new int [3];

In the code below what gets printed after 2 : 5? class Main { public static void main(String args[]) { int outerLoop = 2; while(outerLoop <5) { int innerLoop = 4; while(innerLoop < 7) { System.out.println(outerLoop + ': ' + innerLoop); innerLoop++; } outerLoop++; } } }

2 : 6. Explanation: The outer loop starts from 2 and goes up to 4. The inner loop starts from 4 and goes to 6. After the first iteration of the outer loop (in this case 2), all of the iterations of the inner loop are printed before the next iteration of the outer loop (3). So after 2: 5 will be printed 2 : 6

Examine the following code. How many times will the nested loop run?

6. Explanation: The nested loop will run at least once for each pass through the main loop. When i is 1, j will run once; When i is 2, j will run twice (since it is set to run as many times as the value of i); when i is 3, j will run three times; for a total of six.

Examine the following code. What is it missing?

A third 'for' loop. Explanation: The third loop is missing. The code in the second loop needs to be in a third. This is where we do the addition and multiplication and build the third matrix.

What is a nested while loop in Java?

A while loop within another while loop. Explanation: A nested while loop in Java is a while statement within another while statement.

Examine the following code. What is the add function doing?

Adding an instance of the Publication class to the ArrayList.

The following code contains an error. What needs to be updated to correct the issue?

Change counter to int. Explanation: The get method only takes an integer value for the parameter. You can't use a double value to access the index.

What type of array is this?

Character. Explanation: This will create an array of characters. One option is to take a string, say a user name, then split it into an array using a special command toCharArray(). Then you are not interfering with the original string but can still work on the array of characters.

Examine the following code. What control statement would fit best in the else block (the what goes here? question)? for(int beancounter = 0; beancounter < 25; beancounter++) { if(beancounter == 23) { break; } else { //what goes here? } }

Continue. Explanation: Because we are using the break if the value is 23, the logical opposite would be to continue processing. In this case it is not necessary, but it does provide a clear view of your intention with this loop.

In a Java Do-While loop, the condition is evaluated at the _____ of the statement.

End.

What will be the result of the following code? int j = 0; while (j < 1000) { j = j - 1; }

Endless loop. Explanation: Look closely at the code in the while statement: It is not incrementing J, but decrementing it. It will always be less than 1000 and the code results in an endless loop. Some compilers may eventually stop and display something like -2147483648, but for all intents and purposes, it is an endless loop.

The Java if statement:

Evaluates whether an expression is true or false.

Examine the following code for an array. Which of the following statements will initialize it? long[ ] phoneNumber;

Explanation. Once the array is declared, you can initialize it with the new keyword and specify the length in the brackets.

Which of these code samples correctly processes a 2-dimensional array?

Explanation: In order to process all cells of the table/matrix, a nested for loop is required to step through the rows and columns.

Which of the following correctly creates a new ArrayList for orders?

Explanation: Since ArrayList is a class, you need to declare it such. You can do this by specifying the class name, the array name you want, and the new keyword to create a new instance of that class.

Which of the following correctly checks to make sure the matrices can be multiplied?

Explanation: The columns in matrix 1 must match the rows in matrix 2.

Examine the following code. Will the code compile and execute?

Explanation: There are a couple of issues here. The code needs another closing brace in order to compile. Also, Java will look at the j loop, but never process it, since j is set to 1, and the limit of j is less than zero. Be sure to check your code over for errors like this before running, as they may not show as errors. The code will might compile, but you may not get the expected results.

What is the common type of loop used to step through an array?

For. Explanation: The for loop is the most common, as the array is fixed length; we can quickly walk through an array with a for loop. By using an integer counter, we always know where we are in the array.

A while loop is considered _____ if you don't know when the condition will be true.

Indefinite Explanation: If we don't know when the roller coaster operator will hit the switch, we call the loop indefinite.

Examine the following code. Why won't it run?

Index 3 is out of range. Explanation: Remember that Java starts at 0 and so the last element will have an index of 2.

Which of the following is a characteristic of an operator?

It is a symbol, it represents an operation, and it is an indicator, all are characteristics of an operator.

What happens when you add an item to an existing ArrayList, without specifying an index?

Java will insert the item at the end of the list.

Examine the following code. What will happen when the code runs?

Java will throw an out of bounds error. Explanation: The array was initialized to have 3 buckets. Trying to add a fourth will result in an out-of-bounds run-time error.

What is the number of elements in a Java array called?

Length.

Which of these will display the size of an array?

Length.

An arithmetic operator performs what type of operation?

Mathematical.

Examine the following code. How many times will the nested loop run?

No, but it could run forever. Explanation: Notice that the code re-uses the i variable within the j loop declaration. This code will work but it will spin because you've caught it within an endless condition that will always be true. Chapter 3 / Lesson 6

Java limits the number of nested for loops to _____

None.

Examine the following code. What is missing?

Semicolon at end of while statement.

Which data types are allowed in a Java switch statement?

String, int, short, char. Explanation: Doubles and floats are not allowed because of precision and rounding issues. However, the switch statement is a very powerful tool for evaluating int, short, char, or Strings.

The _____ statement gives control back to a method or constructor.

Return. Explanation: The return statement can return a value back to the method also; let's say you call a getPay() method from the main method, and want to get back the pay rate. At the end of the getPay() method the statement might look like return payRate;

When multiplying matrices, we start at the first _____ of the first matrix and the first _____ of the second.

Row, column

In Java, length is associated with _____, and size is associated with _____.

an array, an ArrayList.

What is the native Java function that allows the copying of one array to another?

arraycopy. Explanation: It is used in the System class. To copy array newOrders to orders: System.arraycopy(orders, 0, newOrders, 0, orders.length);

Examine the following code. Which of the following statements will retrieve the perfect score (300)?

bowlingScores[1];

The _____ statement tells the computer to skip to the end of the switch statement

break. Explanation: Using break is not required, but it improves processing time, as the computer doesn't have to go through the whole statement.

The following code declares a character array with 3 rows and 5 columns:

char [] [] matrix; matrix = new char[3][5];

Which is the correct syntax?

for(i=0;i<10;i++) { new_value+= i; } Explanation: The for loop requires the counter (i), a limit (i is less than 10), and a setting to either increment or decrement the value. If you are decrementing, the syntax could be: for(i=100; i>-100; i--)

A while loop runs code as long as the condition(s) is/are:

True.

A Java array that would have rows and columns is called a _____ array?

Two-dimensional. Explanation: One- and two-dimensional arrays are the most straightforward to manage and program, as they can be thought of in terms of sequences (1-d) and tables (2-d). When we add more dimensions, things get very confusing very quickly.

Examine the following code. This will create an infinite loop. What can be added to stop this from happening?

Update the variable myValue. Explanation: myValue is never updated, and so the code will go on and on and on. You'll need to update myValue so that at some point the loop stops.

What would be the most practical option for adding values to the following array:

Use a for loop.

Look at the following code. When will the processing stop? for(i=0;i<25;i++)

When i = 25 Explanation: Because the statement is stated with a less-than sign (<), the code will stop when the counter reaches 25--it WILL NOT step into the loop when the value is 25. If the code had read <= 25, it would have gone through the loop one more time, then quit when it hit 26.

Examine the following code. Will the statement being printed ever show up?

Yes, once. Explanation: Even though the counter is zero, and the condition states greater than 0, the condition will process at least once. That is the beauty of the do-while loop: We get at least one trip through the sprinkler.

In programming, which of the following is the symbol for multiply?

* Explanation: In programming, * is the symbol for multiply.

What is the index of 14 in the array orderID?

1. Explanation: Because of how we declared the array, within the brackets, Java knows it has six buckets. Java starts counting at zero, so orderID[0] is 7 and orderID[1] is 14.

If var1 is 17 and var2 is 4, what is the result of the statement, result = var1 % var2; is?

1. Explanation: If var1 is 17 and var2 is 4, the result of the statement, result = var1 % var2; is 1. 4 divides into 17 four times, with a remainder of 1.

How many dimensions does the following array have?

3. Explanation: Each set of brackets indicates a dimensions; plus the name of the array refers to a cube. These are usually 3d. Remember, any array bigger than 2 dimensions is rather tricky to work with.

Examine the following code. After the code processes, what is the index of Jean Valjean, considering that Jane Eyre is the first element in the ArrayList? employees.add("Jane Eyre"); employees.add("Sherlock Holmes"); employees.add("Edmond Dantes"); employees.add("Jean Valjean"); employees.add("Sam Spade");

3. Explanation: Remember Java starts counting at zero! Therefore the fourth bucket is index 3.

What will the value of result be after the following code processes?

356. Explanation: The third bucket is 356 and referenced by the integer value 2 (0, 1, 2). You can have variables in the get method, but they must be an integer.

In the code below what gets printed after 3 : 5? import java.util.*; public class Main { public static void main(String args[]) { int outerLoop = 3; while (outerLoop < 8) { int innerLoop = 5; while(innerLoop < 7) { if(innerLoop == 6) { break; } else { System.out.println(outerLoop + ":" + innerLoop); innerLoop++; } outerLoop++; } } } }

4 : 5. Explanation: Here the inner loop breaks at 6. So it prints only till 5. The outer loop starts at 3 and prints 3 : 5. The next iteration of the inner loop is 6, but at this point the inner loop breaks. So the execution goes to the next iteration of the outer loop 4 and the inner loop 5, and prints 4 : 5.

A Java application should always have three things. Which of these isn't one of them?

A conditional statement. Explanation: A conditional statement is not always included in a Java application. It is possible the Java application will not need a conditional statement. However, the class definition, main() method, and some comments should always be included.

Which of the following is a characteristic of a Java array?

All elements are of the same type, may be of any type, an ordered collection.

In a nested while loop what happens if there is a break in the inner loop?

All iterations of the inner loop are executed till the break statement is encountered. Explanation: A break statement in an inner loop causes the inner loop to stop execution if the condition in the break statement is satisfied.

Which type of statement can contain break statements(s)?

All of these can contain break statements. Explanation: The only statements that contain break are switch, while, do, and for.

Examine the following switch statement that creates a mini-menu. What will happen if the user types in 17?

An error will display. Explanation: The default error will show, Error. Since 17 does not match the allowable input values of 1 or 2, the default command returns the error, and the program continues as normal.

Although we use the term multidimensional arrays, a multidimensional array is really an array of _____

Arrays. Explanation: Java purists will be quick to remind you that a multidimensional array is really just an array of an arrays. A 3D array is an array of array of arrays.

When you use a return statement in Java, where does the control go?

Back to the code that called it. Explanation: The return statement returns control back to the code that called the method.

Which condition tells the system to stop processing the code within the loop?

Break.

A for loop includes a _____, which increases or decreases through each step of the loop.

Counter. Explanation: A for loop can be considered a counting loop since it contains a counter that keeps track of how many times it goes through. Example, i is the counter in this loop statement: for(i=0; i<10; i++); it will be incremented by 1 each time.

What type of initialization is used in the following code?

Default. Explanation: The array is initialized using the default method. It is a 1-dimensional array of doubles, and it will have 50 buckets.

Which command in a Java switch statement is the catch-all for values that don't meet the criteria?

Default. Explanation: Always use default! It doesn't matter how air-tight the code is, there is always opportunity for a value to slip in that doesn't pass the expression and then you will have errors.

What is the function of the following code?

Display the number of elements in the ArrayList.

How often is the inner loop of a nested loop run?

Each time the main loop is run. Explanation: Each time the main loop processes, the nested loop(s) is/are processed. They will continue until the condition is met for them to stop, then the main loop will run again.

What will the output be of this expression if the variable x = 13?

Nothing will happen. Explanation: Nothing will happen when this expression is evaluated. The if statement only executes the expression enclosed in curly braces if the result is true. Since x = 13 and it asks whether x < 12, the print statement will never happen.

How many times is any Do-While loop executed?

One or more Explanation: Because the condition is at the end, the code will run at least once.

A while loop can evaluate _____ condition(s).

One or more. Explanation: While can be used to evaluate multiple conditions. Example, while(j > 0 && i < 0), will run as long as j is greater than zero, and i is less than zero. If this were to be an OR statement, use ||.

Which of the following is NOT a category for operators?

Operational. Explanation: Operational is NOT a category for operators. The term has no meaning with respect to operators. Logical, arithmetic, and relational are all operator categories.

An infinite loop probably wouldn't run forever; instead Java will _____, stopping processing.

Overflow. Explanation: Worst-case scenario the computer or application crashes. Otherwise Java overflows and stops processing. An overflow occurs when the compiler handles a number far too large for it to process.

If it is possible that a get method will try to reach a non-existent index, what should you do?

Place the get method within a try statement, and catch the error in a catch statement. Explanation: It is a good idea to check for the IndexOutOfBoundsException. As in a standard array, Java doesn't like it when you try to access an index that doesn't exist in the array!

Examine the following code. What do you think will happen when it runs?

The code will run forever (infinite loop). Explanation: Look first at the ending condition: The code will always run as long as the beanCounter is less than or equal to 100. Then look in the code: We're printing the variable as long as the counter is less than 50. BOTH conditions are ALWAYS met: This is a classic runaway code/infinite loop.

When does an infinite loop happen?

The condition is always true. Explanation: Infinite loops will happen when the condition is always true. This is mostly a bad thing, but can be used for menus.

What happens if the result of a Java if/else statement evaluates to false?

The else clause is executed.

In a nested while loop, which loop is executed first?

The first iteration of the outer loop. Explanation: In a nest while statement, the first iteration of the outer loop is executed first. Following this the entire inner loop is then executed.

The following code compiles but errors out when it runs. What is causing the failure?

There isn't a fifth index to get from. Explanation: Since there isn't a fifth index (or sixth bucket — Java starts numbering at zero), we can't access it. We will get an out of bounds exception. Instead, wrap the statement in a try and catch block.

In order for matrix multiplication to work, the number of _____ in the first matrix must match the number of _____ in the second matrix.

columns, rows

Examine the following code. Which of the following correctly retrieves the value .000223?

double v = rates.get(1);

If all elements of an array are of the same type, the array is said to be _____.

homogeneous.

Which of these is the correct way to see if the String object s1 equals 'green'?

if (s1.equals("green")) { } Explanation: The correct way to see if the String object s1 equals 'green' is: if (s1.equals("green")) { }. Recall that the Java String object has a method called equals(), and you place what you are comparing between the parentheses of that method.

What is missing from the following code?

import java.util.ArrayList;

The number that references the row/column in a multidimensional array is called a (n) ______

index. Explanation: The index denotes our place in the table. Remember that Java starts at 0. Therefore, table[0][1] is in row 1, column 2

In the following example, menuOption is what data type?

int. Explanation: Although it's possible to use numbers in String or char data types, the most logical option for a simple menu is the int. Although if you are expecting larger values, long isn't a bad idea either. However, in this example, menuOption was declared as an int.

The following code references the fourth row, fifth column, and seventh plane in a 3D array named matrix:

matrix[3][4][6]; Explanation: Remember all values start at 0! The fourth row is really index 3.

A major reason Java doesn't support dynamic array is because of ____

memory usage. Explanation: Think of an array that is three dimensions and starts out small. All of a sudden it needs to grow 500x. Such a need would really throttle back performance because memory would have to be used for this and this is memory you probably need elsewhere in the code!

A for loop inside another for loop is called a _____ loop

nested

After completing the matrix multiplication, the resulting matrix will have the same number of _____ as the first matrix, and the same number of _____ as the second matrix.

rows, columns

ArrayList has the _____ feature to display the length/size of the array:

size Explanation: myArray.size(); will return the size/length of the ArrayList. Since ArrayList is its own class, this is a specific method to that class.

Which is the correct syntax for a while loop?

while (total_panic < 1) { minute++; } Explanation: Parenthesis are required around the condition(s) being tested, as well as brackets: { } to start/end the loop.

Which code sample will result in an infinite while loop?

while(true) { System.out.println("Welcome to the Menu"); } Explanation: This statement will print forever unless you have some way out of the loop, or a statement that is waiting for user input. The other code examples look like they might produce a loop. However the while(count <=10) will stop, the (while i < 0) will not run since i is set to 0; and while(false) will not run since that is an invalid call.


संबंधित स्टडी सेट्स

A&P1 // Chapter 12: Nervous Tissue

View Set

Earth Science Exam 1 Study Guide

View Set

AP English--the Power of Language

View Set

HOSP2040 Human Resources Management in Service Organizations Final Exam Part 1

View Set

fixed-ratio, variable-ratio, fixed-interval, variable-interval, or continuous reinforcement, Psychology - Operant and Classical Conditioning/Shaping & Chaining, Operant Conditioning, Psychology exam 1

View Set

Global 2 Honors Nazi Germany Review

View Set

LVN Term 1 - ATI Infection Control Pretest

View Set