Core java interview Questions You'll Most Likely Be Asked (Second Edition)


Output: 50 50 50 ... Give some examples of “for–each” loop


Download 1.52 Mb.
Pdf ko'rish
bet26/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   22   23   24   25   26   27   28   29   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

Output:
50
50
50
...
Give some examples of “for–each” loop.
Answer:
The following are some examples of for–Each loop:
Example 1:
Integer [] integerArray = {10, 100, 1000};
for(Integer integer: integerArray) {
// do something here
}
Example 2:
int [] iValue = {10, 100, 1000};
for(int iValue1: iValue) {
// do something here
}
Example 3:


063.
064.
Electronics [] electronics = {new Camera(), new
Laptop()};
for(Electronics e: electronics) {
// do something here
}
Example 4:
List numbers = Arrays.asList(4,10,12);
for(int num: numbers) {
// do something here
}
What will be the output of the following code?
for(int iValue = 0; iValue < 10; iValue++) {
}
System.out.println(iValue);
Answer:
This code will not compile. This is because the scope of the
variable iValue is only within the for loop and not beyond the
for loop.
What will happen when you execute the code below? If it
compiles fine, what will be the output?
int iValue = 10;
switch(iValue) {
case 5: System.out.println(“5”);
case 8: System.out.println(“5”);
default: System.out.println(“This is Default”);


case 9: { System.out.println(“9”); break; }
case 11: System.out.println(“11”);
}
Answer:
This code compiles fine and displays the output below:
This is Default
9
Although case 10 is not available, “default” gets executed. Since
there is no break statement, the case statement following default
also gets executed and prints 9. The code does not print 11 since
there is a break statement after case 9.




Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   22   23   24   25   26   27   28   29   ...   171




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling