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


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

048.
049.
i++; // sets i to 11
The decrement operator is represented by two minus signs. It
decrements its operand by 1. The following code demonstrates
this:
int i = 10;
i::; // sets i to 9
Explain the instanceOf operator.
Answer:
The instanceOf operator can be used to check if an object is
an instance of another class. It returns a boolean value. If both
the objects being compared are of same type it returns true and
otherwise it returns false. The following code demonstrates this:
String str = “Hello”;
str instanceof String //returns true
String str = null;
str instanceof String //returns false
Explain the ternary operator with a code sample.
Answer:
The ternary operator is also called as a conditional operator. It is
a short cut operator for the if–else statement. Its syntax is as
follows:
condition?TruePath:FalsePath
It evaluates a condition and if true, it executes code after the “?”
symbol. If false, it executes the code after the “–” symbol. The
following code demonstrates this:


int iValue1 = 100;
int iValue2 = (iValue1 == 100) ? 200: 100; //Line 1
Here, the iValue==100 condition is evaluated. Since is it true,
the value 200 will be assigned to the variable iValue2.



050.
a.
b.
CHAPTER
05
Control Statements
Name the decision statements used in Java.
Answer:
Decision statements evaluate an expression and based on the
result, perform the execution. There are two decision statements
in Java. They are:
if: The if else is a conditional branch statement. It tests a
condition and if the condition is true, execution follows a
certain path and if not, it follows a different path. It has the
following syntax:
if(condition){
//some code here
}
else { // this part is optional
//some code here
}
switch: The switch statement is a multi–way branch
statement. It can be used instead of using several if else
statements. The expression specified within the switch
statement is evaluated and matched with every case value.
Once a match is found, the code within the body of that case
statement is executed. It has the following syntax:
switch(expression) {


051.
a.
b.
case value1:
//some code
break; //optional
case value2:
//some code
break; //optional
…..
case valuen:
//some code
break; //optional
default:
//some code
break; //optional
}
}

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   17   18   19   20   21   22   23   24   ...   171




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