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


What happens when you compile and execute the code


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

What happens when you compile and execute the code
below? What will be the output?
for(int iValue=0; iValue<5; iValue++) {
System.out.println(iValue); //line 1
continue; //line 2
System.out.println(iValue + 1);
}
Answer:
The code compiles and on executing it displays the below output:
0
1
2
3
4
After the Sysout statement at line 1, the continue statement at
line 2 gets executed. This skips the rest of the body of the loop
and transfers control to the start of the loop.
What happens when you compile and execute the code
below? What will be the output?
for(int iValue=0; iValue<5; iValue++) {
System.out.println(iValue);
break;


060.
061.
System.out.println(iValue + 1);
}
Answer:
The code compiles and on executing it displays the following
output:
0
It prints just 0. This is because the break statement stops
executing the next statement in the loop and transfers control out
of the loop.
What happens when you compile and execute the code
below? What will be the output?
for (;;) {
System.out.println(“Will this get printed?”);
}
Answer:
The above code compiles fine. However, it executes an infinite
loop since there is no loop control variable and no condition to
check. So, this code will keep on printing the output “Will this
get printed?”.
What happens when you compile and execute the code
below? What will be the output?
int iValue = 50;
for (;iValue < 100;) {
System.out.println(iValue);
}


062.
Answer:
In the above code, there is no initialization statement and
iteration statement, it only specifies a condition. So, the variable
iValue is never incremented. So, this becomes an infinite loop;
it infinitely prints the value “50”.

Download 1.52 Mb.

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




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