OCA Java SE 7: Loop Constructs

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

True or False: The enhanced for loop can be used to initialize an array and modify its elements.

False.

True or False: You can add labels to declarations of variables.

False.

True or False: calling remove on a list in a for each loop will result in a concurrent modification exception.

False.

True or False: The enhanced for loop can be used to delete the elements of a collection.

False. An iterator would be needed to set elements or delete them.

True or False: This is a valid for declaration: for(int i = 0, int j = 10; j > 5; i++, j--) {

False. multiple declarations of variables are not allowed. "int i = 0, j=10;" would compile, however.

What is the behavior of a break statement?

It is used to exit—or break out of—the for, for-each, do, and do-while loops and the switch construct.

What is the behavior of the continue statement?

The continue statement is used to skip the remaining steps in the current iteration and start with the next loop iteration. The continue statement works with the for, for-each, do, and do-while loops.

What is the format of a label?

The label name followed by a colon: "myLabel:"

What are the three types of statements a for loop defines?

The three types of statements are separated by semicolons (;): initialization statements, termination condition, and update clauses

True or False: A for loop initialization block executes only once. A for loop can declare and initialize multiple variables in its initialization block, but the variables that it declares should be of the same type.

True.

True or False: A while loop is used to keep executing a set of statements until the condition that it uses evaluates to false. This loop checks the condition before it starts the execution of the statement.

True.

True or False: The enhanced for loop can't be used to iterate over multiple collections or arrays in the same loop.

True.

True or False: The enhanced for loop is also called the for-each loop.

True.

True or False: When you use the break statement with nested loops, it exits the inner loop.

True.

True or False: When you use the continue statement with nested loops, it exits the current iteration of the inner loop.

True.

True or False: You can use a labeled break statement to exit an outer loop.

True.

True or False: You can use a labeled continue statement to skip the iteration of the outer loop.

True.

True or False: calling add on a list in a for each loop will result in a concurrent modification exception.

True.

True or False: A do-while loop is used to keep executing a set of statements until the condition that it uses evaluates to false. This loop checks the condition after it completes the execution of all the statements in its loop body.

True. A do-while loop will execute its body at least once.

True or False: The definition of any of the three for loop statements is optional.

True. For example, for (;;); and for (;;){} are valid code for defining a for loop. Also, defining any one of these statements is also valid code.

True or False: The update block is usually used to increment or decrement the value of the variables that are defined in the initialization block.

True. It can also execute multiple other statements, including method calls.

True or False: The termination condition is evaluated once, for all the iterations, before the statements defined within the body of the loop are executed.

True. The for loop terminates when the termination condition evaluates to false.

How many lines of code are allowed between a do-while construct?

Unlimited if curly-braces are used. Otherwise, only one line of code.

Is it possible to have no expressions in a for statement, for(;;)?

Yes. However, if there are any statements after such a for block and the for block does not contain a break statement, there will be an "Unreachable code" compilation error: for(;;) { } // Compiler error if this uncommented System.out.println("foo");

Does a labeled break or continue need to be exist inside the loop where the label is declared.

Yes. This is a gotcha on nested loop exams. Compilation will fail if the break/continue is declared outside of where the loop was declared.

Where in code and a label be used?

You can add labels to a code block defined using braces, {}, all looping statements (for , enhanced for loop, while, do-while), conditional constructs (if and switch statements), expressions and assignments, return statements, try blocks, and throws statements.


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

Financial Management- Exam 1 Review Rutgers

View Set

Exam 1 Sample & Homework Questions

View Set

Health, Wellness, and Health Disparities

View Set

Linear Algebra Exam 2 Concept Problems

View Set

BIOL 262 Spinal Cord & Nerves of Extremities HW

View Set

Module A Agency and Property Disclosure

View Set