Chapter 3 Conditionals & Loops in Java CS 109

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Examine the following code. What will the result of the code be, given the break statement? for(int i = 0; i < 15; i++) { if(i == 5) { System.out.println("bye: " + i); break; } System.out.println(i); }

0 1 2 3 4 bye: 5 the code will break and stop processing the loop

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

1 4 divides into 17 four times

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

What does the for loop require?

A counter (i), a limit, and a setting to either increment or decrement the value

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) { case 1: setValue (23); break; case 2: setValue(15); break; default: System.out.println("Error"); break; }

An error will display

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

Conditional statement

Examine the following code. What control statement would fit best in the else block (the what goes here? question)? for( int beancounter = 0; bean counter <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

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

Each time the main loop is run

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

End

What does the Java if statement:

Evaluate whether an expression is true or false

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

Indefinite

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

Nested

for (int i=1; i<=5; i++) { for (int j =1; j<0; j++) { }

No

Examine the following code. Is there anything that will prevent it from processing? for (int =1; i <= 3; i++) { for (int j =1; i <=3; j++ { } }

No, but it could run forever

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

How many times is any Do-While loop executed?

One or more

Which of the following is NOT a category for operators?

Operational

Category for operators

Relational, logical, arithmetic

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

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

True

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

default

Which is the correct syntax?

for (i=0; i<10; i++) { new_value+=i; }

In the loop, when the value is "4", jump directly to the next value. for (int i = 0; i < 10; i++) { if (i == 4) { ______; } System.out.println(i); }

for (int i = 0; i < 10; i++) { if (i == 4) { CONTINUE ; } System.out.println(i); } use the continue clause

Stop the loop if i is 5. for (int i = 0; i < 10; i++) { if (i == 5) { _____; } System.out.println(i); }

for (int i = 0; i < 10; i++) { if (i == 5) { BREAK ; } System.out.println(i); } This stops the loop after it runs 5 times

Use a for loop to print "Yes" 5 times: ____(int i = 0; i < 5; ___) { System.out.println(____); }

for(int i = 0; i < 5; i++) { System.out.println("Yes"); } we have to have the addition counter i++ so then the loop knows once i<5 then it will stop printing "Yes"

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

if(s1.equals("green")){}

Examine the following code:switch (menuOption) { case 1: System.out.println("Menu option 1"); break; case 2; setPrice (15) break; default: break; }

int

What's missing in this loop? int i = 1; ______(i < 6) { System.out.println(i); _____ ; }

int i = 1; WHILE(i < 6) { System.out.println(i); i++; } we have to use the while loop in order for it to cycle until variable i gets to 6. the i++ is the counter

Which data types are allowed in a Java switch statement?

string, int, short, char

What is three things a Java application needs?

the main () method, some comments, and the class definition

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

break

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

break


Kaugnay na mga set ng pag-aaral

Clin Ortho Practical 2 - Wrist thoracic lumbar pelvis

View Set

Financial Accounting - Learning Objective #13

View Set

Optimum Theory of population Class 9, Unit 3, Demography

View Set

2.13 Unit 2 Test | Constitutional Underpinning

View Set

20-1: The heart is a four-chambered organ that pumps blood through the systemic and pulmonary circuits

View Set