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


Which of the following lines of code will compile and


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

Which of the following lines of code will compile and
which will cause an error?
int iValue=5;
while (iValue) {} //Line 1
while (false) {} //Line 2
while (iValue = 5) {} //Line 3
while (iValue == 5) {} //Line 4
Answer:
Line 1 will cause an error because an integer value cannot be
passed to a while loop. It requires a boolean value.
Line 2 compiles fine since the boolean value false is used in
the while loop
Line 3 causes a compilation error since the expression
iValue=5 does not evaluate to a boolean value, it is an
assignment statement


054.
Line 4 compiles file since the expression iValue==5 evaluates
to a boolean value.
What would be the output if boolA is false and boolB is
true?
public void foo( boolean boolA, boolean boolB) /* Line 1
*/
{
if(boolA ) {/* Line 3 */
System.out.println(“X”); /* Line 5 */
}
else if(boolA && boolB) {/* Line 7 */
System.out.println( “X && Y”); /* Line 9 */
}
else { /* Line 11 */
if (!boolB ){ /* Line 13 */
System.out.println( “notY”); /* Line 15 */
}
else{ /* Line 17 */
System.out.println( “In here” ); /* Line 19 */
}
}
}
Answer:
Output: In here
Since boolA is false, line 5 will never execute and X will not be
printed. Since boolA is false line 7 will also not execute and so X
&& Y will also not be printed. The else block at line 11 will be
entered. Since boolB is true, the if statement at line 13 is not


055.
executed. So, the else statement at line 13 is executed which
prints In here.
What will be the output of the following program?
public class SwitchDemo
{
final static short caseVal = 2;
public static void main(String [] abc)
{
for (int iterNo=0; iterNo < 3; iterNo ++) {
switch (iterNo) {
case caseVal– System.out.print(“a “);
case caseVal–1– System.out.print(“b “);
case caseVal–2– System.out.print(“c “);
}
}
}
}

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   19   20   21   22   23   24   25   26   ...   171




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