Question | Answer |
What's the use of a switch statement | You can compare the value of a variable with multiple values |
Give an example of a switch statement | int score = 100; switch (score){ case 10: System.out.println(10); break; case 20: System.out.println(10); break; default: System.out.print("default"); break; } |
How many default statements can a switch statement define? | Only one, at the end |
What happens when the variable value matches TUE | When the code control enter the label matching Tue in the switch construct, it will execute all the code until it encounters a break statement or reaches the end of the switch statement |
The difference between the if-else-if-else construct and a switch | The if-else-if-else construct evaluate all conditions until it finds a match. A switch construct compares the argument passed to it with its labels |
Which argument types are accepted by a switch statement? | |
The value of a case label must be... | Compile-time constant value; that is, the value should be known at the time of code compilation |
For the value of a case label, you can use expressions with variables only if.. | the variables you used in a expression are marked final and are initialized at the declaration |
Case values should be... | assignable to the argument passes to the switch statement. And null isn't allowd |
A switch statement without a break | |
Define a nested ArrayList and iterate through it | ArrayList<ArrayList<String>> nestedArrList = new ArrayList<>(); for(ArrayList<String> arrElement : nestedArrList) for(String element : arrElement) System.out.print(element); |
What happens when you try to modify the value of the loop variable in an enhanced for loop | If you iterating through a: a collection of primitive values: manipulation of the loop variable never change the value, because the primitive values are passed by value to the loop variable a collection of objects: The value is passed by reference to the loop variable. Therefore, if the value of the loop variable is manipulated by executing method on it, the modified values will be reflected in the collection of objects being iterated |
Can you initialize an Array or modify its element through an enhanced for loop? | NO |
Why can't you remove or delete an the elements of a collection trough enhanced for loop ? | Because the for loop hides the iterator used to iterate through the element, you can't use the method remove |
Is it possible to iterate over multiple collections or arrays the same for-each loop? | No, because you can't define multiple looping variables in a for-each loop |
differences between do-while and while loop | do-while loop executes the code at least once, even if the condition evaluates to false. The do-while evaluates the termination condition after executing the statements, WHEREAS the while loop evaluates the termination condition before executing its statements |
What is the output of this code? String[] programmers = {"Outer", "Inner"}; for (String outer : programmers) { for (String inner : programmers) { if (inner.equals("Inner")) break; System.out.print(inner + ":"); } } | Outer:Outer |
What is the output of this code? String[] programmers = {"Paul", "Shreya", "Selvan", "Harry"}; for (String name : programmers) { if (name.equals("Shreya")) continue; System.out.println(name); } | Paul Selvan Harry |
Compare for and while loops | For loop: when you know the number of iterations While loop: when you don't know the number of iterations |
continue statement in a loop | the continue statement is used to skip the remaining steps in the current iteration and start the next loop iteration |
continue statement in a nested loop | it exits the current iteration of the inner loop |
Difference between break an continue | |
On which types of statement can a label be added? | - a block statement{} - All looping statements (for,enhanced for while, do-while) Conditional constructs(if and switch statements) -Expressions -Assignments -return statements -try blocks -throws statements |
What is the output of this code: String[] programmers = {"Outer", "Inner"}; outer: for (String outer : programmers) { for (String inner : programmers) { if (inner.equals("Inner")) break outer; System.out.print(inner + ":"); } } | Outer: |
What is the output of this code: |
Want to create your own Flashcards for free with GoConqr? Learn more.