Chapter 3: Conditionals & Loops in programming

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

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

END

Which of the following is a characteristic of an operator?

IT represents and operation, a symbol and an indicator. All are correct.

Java limits the number of nested for loops to _____

None

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

When i = 25

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

*

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

1 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.

What will be the value of the variable tester_value when this loop is finished? int i; int tester value = 0; for (i = 0; i <= 5: i++) f tester value = tester value + i: }

15 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)

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

In the code below what gets printed after 3 : 5? class nestedwhileLoop( public static void main(String arg|]) ( 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 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.

Examine the following code. How many times will the nested loop run? for (int I = 1; I <= 3; I++) { for (int j = 1; j <= I; j++) { } }

6 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.

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

A conditional statement 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.

What is a nested while loop in Java?

A while loop within another while loop

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

Examine the following switch statement that creates a mini-menu. What will happen if the user types in 17? switch (userInput) f case 1: setValue(23); break; case 2: setValue(15); break; default: System.out.println("Error"); break:

An error will display 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.

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

Back to the code that called it

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

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

Counter 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 will be the result of the following code? int j = 0; while (j < 1000) { j = j - 1; }

Endless loop 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 will loop forever. If more complex statements are left to run infinitely, they can crash computer systems

The Java if statement:

Evaluates whether an expression is true or false

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

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

An arithmetic Operator performs what type of operation?

Mathematical

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

Nested

Examine the following code. Will the code compile and execute? for (int i = 1; i <= 5; i++) { for (int j = 1; j < 0; j++) f

No here 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.

Examine the following code. Is there anything that will prevent it from processing? for (int 1 - 1; 1 <- 3; 1++) ( for (int 1 - 1: 1 <-3: ¡++) f

No, but it could run forever 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.

What will the output be of this expression if the variable x = 13? if (x < 12) { System.out.println('The number was too low');

Nothing will happen 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.

A while loop can evaluate _____ condition(s).

One or more

How many times is any Do-While loop executed?

One or more

Which of the following is NOT a category for operators?

Operational

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

Return

Examine the following code. What is missing? do { //instructions here } while (true)

Semicolon at end of while statement because the condition is at the end, and the condition is really a Java statement, the semicolon is required at the end of this line of code, e.g., while (true);

Examine the following code. What do you think will happen when it runs? int beanCounter - 0; do it beancounter < 50)3 System.out.printin("Bean Counter is : " + beanCounter); 3 while (beancounter <= 100);

The code will run forever (infinite loop) 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

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

The else clause is executed. hen the result of a Java if/else statement evaluates to false, the else clause is executed. Only if there is no else clause does nothing specific happen when the if clause is false.

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

The first iteration of the outer loop

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

True

Examine the following code. This will create an infinite loop. What can be added to stop this from happening? int myValue - 10; while (myValue <= 100) { System.out.println("Your value is: " + myValue):

Update the variable myValue 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.

int counter = 0; do { System.out.println("Will I print?"); } while (counter > 0):

Yes, once 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.

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

break

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

default

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

each time the main loop is run

Which is the correct syntax?

for(i=0;i<10;i++) { new_value+= i; } 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--)

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

if (s1.equals("green")) { } 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.

In the following example, menuOption is what data type? switch (menuoption) { case 1: System.out.println("Menu option 1"); break; cacp ?. setPrice(15); break: default: break;

int 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.

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

overflow

Which data types are allowed in a Java switch statement?

string, int, long, char

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

switch, while, do, and for.

Which is the correct syntax for a while loop?

while (total_panic < 1) { minute++; } 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"); }


Ensembles d'études connexes

Genetics (final) midterms to study (Jason Leonhard)

View Set

Chapter 6 Legal and Ethical Basis for Practice

View Set

LCCW Pediatrics A - Cumulative Final

View Set

Chapter 6: Basic Principles of nutrition

View Set