Java Unit 5-

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

Consider the given snippet: int num = 0; while (++num<5) System.out.print(num); Choose the correct option inside the while loop condition so that the output will be: 1234

++num<5

An infinite loop is a loop that runs forever, or until you press Control + C. How do we ensure that our while loop will not create an infinite loop?

. We need to put a code inside the while loop that will change the condition from true to false in order for us the stop the while loop.

Given the code snippet: for (int i = 0; i <= 6; i++) { if (i == 4) continue; System.out.print(i); } What will be the output?

012356

Given the code snippet: int num = 0; while (num <= 6) { System.out.print(num); num += 2; } What is the output?

0246

Given the snippet: int num = 0; do { System.out.print(num); num += 3; }while (num <= 10); What will be the output?

0369

Given the code snippet: for (int i = 1; i <= 10; i++) { if (i == 3) break; if (i == 5) break; if (i == 7) break; System.out.print(i); } What will be the output?

12

Given the code snippet: int counter = 1; while(counter <= 3) { System.out.print(counter); counter++; } What is the output of the given code snippet?

123

Given the code snippet: int num = 1; do { System.out.print(num); ++num; }while (num < 5); What will be the output?

1234

Given the snippet below: int num = 1; while (num <= 10) { if (num == 5) break; System.out.print(num++); } What is the output?

1234

Given the code snippet: for (int i = 1; i <= 10; i++) { if (i % 3 == 0) continue; System.out.print(i); } What will happen if we compile and run the code?

12457810

Given the snippet below: int j = 1; do { if (j % 3 == 0) { j++; continue; } System.out.print(j); j++; } while(j <= 10); What will be the output?

12457810

Given the code snippet: for (int i = 1; i <= 10; i+=3) { System.out.print(i); } What will be the output if we run the given snippet?

14710

Given the code snippet: int number=20; switch(number){ case 10: System.out.println("10"); case 20: System.out.println("20"); case 30: System.out.println("30"); default: System.out.println("Not in 10, 20 or 30"); } What will be the output?

20

What will be the output of the given code snippet? int num = 0; while (num < 5) num++; System.out.print(num);

5

Given the Java code snippet below: int a = 10, b = 20, c = 30; if ((a + b) >= c) { System.out.print("A"); if (a > b) System.out.print("B"); else System.out.print("C"); } else System.out.print("D"); What is the output?

AC

Which of the following statement is TRUE about Control Flow Statements?

Allows you to provide direction or control on the flow of programming in your code.

What happens when there is no break keyword implemented at the end of each case?

Fall-through will happen, which means that the program will still execute the remaining cases even if there has already been a match.

In a do-while loop, the condition is evaluated first and if it returns true then the statements inside the do-while loop execute. When the condition returns false, the control comes out of loop and jumps to the next statement after the loop body.

False

In creating labels, the outer loop MUST be called "outer:", while the inner loop MUST be called "inner:". Label names MUST be based according to the order of the loops.

False

Learning loops is IMPORTANT and is REQUIRED in ALL Java programs.

False

The important point to note when using a do-while loop is that we need to use an increment or decrement statement inside the do-while loop so that the loop variable gets changed on each iteration, and at some point the condition will ALWAYS return TRUE. This way, if the condition is ALWAYS TRUE we can end the condition of the do-while loop.

False

Unlike the while and the do-while loops, the for loop CANNOT create an infinite loop.

False

We can also implement switch-case statements on boolean variables e.g. variables that contain either true or false.

False

Given the Java code snippet below: if( population > 10000 ){ System.out.println("This place is a city"); } What should be the values of the variable population to have an output of: This place is a city? Select one:

Greater than 10000

Consider the two code snippets: // code snippet #1 make_sandwich(); make_sandwich(); make_sandwich(); make_sandwich(); make_sandwich(); // code snippet #2 int numberOfPreparedSandwiches = 1; while(numberOfPreparedSandwiches <= 5) { make_sandwich(); numberOfPreparedSandwiches = numberOfPreparedSandwiches + 1; } Which of the following statements is NOT TRUE?

If we need to create 500 sandwiches, it is better to use code snippet #1.

Given the code snippet: outer: for (int i = 1; i < 5; i++) { switch(i) { case 1 : System.out.print("One"); case 2 : System.out.print("Two"); case 3 : System.out.print("Three"); continue outer; case 4 : System.out.print("Four"); } } System.out.print("End of loop"); What will be the output?

OneTwoThreeTwoThreeThreeFourEnd of loop

Given the code snippet: int num = 5; if (num == 7){ System.out.println("Option 1") }else{ System.out.println("Option 2") } What will be the output?

Option 2

What is the purpose of an IF Statement?

Provides decision making feature into our program based on conditions.

Given the code snippet: String str = "Silicon"; switch(str) { case "Silicon": System.out.print("S"); break; case "Valley" : System.out.print("V"); break; case "High" : System.out.print("H"); break; case "School" : System.out.print("S"); } Choose among the list of options below a snippet code that will produce the same output as the given snippet above.

String str = "Silicon"; switch(str) { case "Silicon": case "School" : System.out.print("S"); break; case "Valley" : System.out.print("V"); break; case "High" : System.out.print("H"); }

The following describes the while loop in Java, EXCEPT?

The code block inside the loop will execute at least once even if the condition is false.

Given the code snippet: int i = 0; for( ; i < 5 ; ) { System.out.print(i); i++; } Describe what happens when the code is executed and run?

The code will compile and run with the output of: 01234

Which of the following statements is NOT TRUE about the for loop.

The for loop allows us to run the loop body at least once, regardless of the value of the condition.

Given the code snippet: int num = 10; do { System.out.print(num); } while (num = 11); What will happen if the given snippet is executed?

The snippet will cause a compilation error.

Given the code snippet: for(int i=4; i < 4; i++) { System.out.println("i = " + i); } Describe the output of the snippet.

The snippet will compile and run with no output.

A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.

True

A switch-case statement (with breaks included) will execute the code within the matching case constant only.

True

In programming, loops are important to learn in order for us to do a lot of repetitive things.

True

In switch-case statements, the break keyword tells the program to skip all the remaining cases. The next statement that will run is the one outside the switch block.

True

Labels are optional identifiers placed before the loop. Labels must be followed by a colon (:), then the loop body.

True

The Java while loop is used to iterate a part of the program several times. If the number of iteration is not fixed, it is recommended to use the while loop. The loop body will execute as long as the condition is true.

True

The break statement can be used to terminate a case in the switch statement. Once encountered within the switch statement, the code terminates the switch block and jumps outside the switch-block and runs the statement after the said block.

True

The continue statement is mostly used inside loops. Whenever it is encountered inside a loop, control directly jumps to the beginning of the loop for the next iteration, skipping the execution of statements inside loop's body for the current iteration.

True

The important point to note when using while loop is that we need to use increment or decrement statement inside the while loop, so that the loop variable gets changed on each iteration, and at some point the condition returns false. This way we can end the execution of the while loop, otherwise the loop would execute indefinitely.

True

There are two types of continue statements, unlabeled continue statement and the labeled continue statement.

True

When the break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement outside the loop.

True

You can use If/else Statements to check if numbers are greater than or less than other numbers.

True

Given the Java code snippet below: public static void main(String[] args){ int myAge = 18; if (myAge < 18 ) System.out.println("You cannot enter the museum."); if (++myAge > 18 ) System.out.println("You can enter the museum."); } What is the output of the program?

You can enter the museum.

The for loop contains three expressions; match the description of all three expressions with their purpose and description. It is called the increment/decrement expression and it is invoked after each iteration of the loop. _________ It is called the initialisation expression and it is only executed once, when the loop begins- Expression 2 It is called the termination expression (or the boolean expression) and it is only executed at the beginning of each loop. If the termination expression (boolean expression) evaluates to false, program execution exits the for loop.- Epression 2

a. ?-not- espression 1 b. expression 2 c. expression 2

The continue statement can be used in do-while , but not in switch-case.

a. do-while b. switch-case

Given the output: 54321 Complete the code snippet below so that the loop will print the given output: for (int i = 5 i>0;i--)System.out.print(i);

a. i>0 b.i--

The do-while loop is similar to the while loop, however, there is a difference between them: In the while loop, the condition is evaluated before the execution of loop's body, but in the __________loop, the condition is evaluated after the execution of loop's body.

a. while b. ?-not- while switch-case

Suppose you are working with loops. It is sometimes desirable to skip some statements inside the loop or terminate the loop immediately without checking the test expression. We can use the keyword break to terminate the loop immediately.

break

Given an integer variable foo, what is the correct syntax of implementing a case statement that tests if foo is equal to 4?

case 4:

There may be unique circumstances in which loop iteration skipping is required, and the continue statement provides a structured way to do just that.

continue

If an infinite loop ran, we terminate it by pressing

control +c

Given the code snippet: int foo = 25; switch(foo){ case 25: System.out.println("Awesome!"); break; case 26: System.out.println("Cool!"); break; case 27: System.out.println("Terrific!"); break; default: System.out.println("Wonderful!"); } // end of switch What will be the output?

d. Awesome!

Which of the following Java constructs can you NOT use the break statement in?

d. if-else condition

Given a variable foo with a value of 4 and a switch-case statement tests, if a variable is equal to 1, 2, and 3 respectively, and If foo is passed onto the switch-case statement, which case will be executed?

default

The ____________loop in Java allows us to execute a loop body at least once, irrespective of the conditions.

do-while

In creating a Java code, if the number of iteration is fixed, it is recommended to use the for loop

for

The for loop in Java allows you to repeat a certain code segment a specific number of times.

for

Complete the given code snippet to show an infinite loop using a for loop statement: for(;;) { System.out.print("I will run forever.."); }

for(;;)

Given that the minimum age for getting a driver's license is 18. If my age now is 14, can I get my driver's license? Construct an if-else condition for this statement.

int age = 14; if (age == 18) System.out.println("You can apply for a driver's license"); else System.out.println("You cannot apply for a driver's license");

Given the output: 12345 Which of the following do-while loops WILL NOT produce the given output?

int num = 1; do { System.out.print(num); }while (++num < 5);

Given that the minimum passing score for the Java test is 75. Choose the correct code snippet that will check if you pass the test considering that your score is 82.

int score = 82; if (score >= 75) System.out.println("You pass"); else System.out.println("You fail");

If I want to get the output: 54321 Which of the following code snippets should I use?

int x = 5; do { System.out.print(x--); } while (x >= 1);

If I wanted to get a desired output of: 54321 Which of the following code snippets WILL NOT give me the said output?

int x = 5; do { System.out.print(x--); } while (x >= 1);

In programming, a loop repeats a portion of a code a set number of times until the process is complete.

loop

Given the code snippet: for (int i = 1; i <= 10; i++) { if (i % 3 == 0) break; System.out.print(i); } What will be the output?

not- 12457810

Given the code snippet below: for (int i = 0; i < 2; i++) { for (int j = 10; j < 12; j++) { if (i < j) break; System.out.println("Inside for loop j"); } System.out.println("Inside for loop i"); } System.out.println("Outside for loop i"); What will be the output of the code?

not- Inside for loop j Inside for loop i Outside for loop i

Which of the following statements below is NOT TRUE about the for loop statement?

not- The expressions are contained inside the set of parenthesis located directly after the Java keyword: for.

Which of following code snippets will produce an infinite loop?

not- int num = 1; while(num != 3) { System.out.println(num); ++num; }

When you require something to be done once before continuing it in a loop, you use the_______loop.

not- switch-case for

An infinite loop is a type of loop that executes forever. Which of the following code snippet is NOT an infinite loop?

not- while (!false) { System.out.println("I'm NOT an infinite loop, am I?"); } -or- a. int num = 1; while(num++ < 3) { System.out.println(num); }

Given the snippet: String str; outer: for (int i = 1; i < 5; i++) { switch(i) { case 1 : str = "One"; break; case 2 : str = "Two"; case 3 : str = "Three"; break outer; case 4 : str = "Four"; } System.out.print(str); } What will be the output?

one

Which of the following statements is NOT a sample loop in Java?

repeat until loop

Given a variable foo, what is the correct syntax of implementing a switch-case statement with this variable? Select one:

switch(foo)

Given the Java code snippet below: int superPower = 5; boolean myCondition = superPower > 5; if (myCondition) System.out.println("Give 'em a blast"); else System.out.println("you need to charge up."); What is the output?

you need to charge up.


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

Chapter 5 The Integumentary System

View Set

Strategic & Entrepreneurial Thinking Midterm

View Set

Med-Surg Chapter 61: Managements of Patients With Dermatologic Problems

View Set

Radiographic Pathology Ch 4 Skeletal System

View Set

Chapter 3 - Inflammation, the Inflammatory Response, and Fever

View Set