1. What will be the output of the following JAVA code?
class LWD{
public static void main(String arg[]){
int a=b=c=d=9;
System.out.println(a+" "+b+" "+c+" "+d);
}
}
Output & Explanation:
Compile time error
In the above JAVA code, variable a is declared and initialized with the integer value 9, rest of the variable b, c and d are not declared. Hence it shows can't find the symbol error during compilation time.
In-order to avoid this error, we can declare and initialize the variables as mentioned below:
int a,b,c,d;
a=b=c=d=9; // valid declaration
2. What will be the output of the following JAVA code?
class LWD{
public static void main(String args[])
{
int l,d;
l=9;
d=7;
float l1,d1;
l1=9.0f;
d1=7.0f;
int result[]={l+d,l-d,l*d,l/d,l%d};
float result1[]={l1+d1,l1-d1,l1*d1,l1/d1,l1%d1};
for(int i=0;i<result.length;i++){
System.out.println("Int result & float result1:"+result[i]+" & "+result1[i]);
}
}
}
Output & Explanation:
Int result & float result1: 16 & 16.0
Int result & float result1: 2 & 2.0
Int result & float result1: 63 & 63.0
Int result & float result1: 1 & 1.2857143
Int result & float result1: 2 & 2.0
3. What will be the output of the following JAVA code?
class LWD{
public static void main(String args[]){
int a,b;
a=5;b=6;
int result[]={a & b,a|b ,a^b};
for(int i=0;i<result.length;i++)
System.out.println("result="+result[i]);
}
}
Output & Explanation:
result=4
result=7
result=3
|
A
|
B
|
Bitwise &
|
Bitwise |
|
Bitwise ^
|
|
0
|
0
|
0
|
0
|
0
|
|
0
|
1
|
0
|
1
|
1
|
|
1
|
0
|
0
|
1
|
1
|
|
1
|
1
|
1
|
1
|
0
|
Given that,
|
5 - 0101
|
0101
0110
|
0101
0110
|
0101
0110
|
|
6 - 0110
|
|||
|
Result
|
&0100à4
|
| 0111 à7
|
^ 0011 à3
|
4. What will be the output of the following JAVA code?
class LWD{
public static void main(String args[]){
byte a=1;
a=a+1;
System.out.println(a);
}
}
Output & Explanation:
Compile time error due to incompitable type, lossy conversion from int to byte.
byte+byte=int, integer can't be stored in byte variable.
5. What will be the output of the following JAVA code?
class A{}
class B{}
class LWD extends A{
public static void main(String args[]){
A a=new LWD();
B b=new B();
LWD l=new LWD();
System.out.println(a instanceof LWD);
System.out.println(b instanceof B);
System.out.println(l instanceof LWD);
}
}
Output & Explanation:
false
true
true
instanceof operator used to check the object is comes from the respective class instance or not.
6. What will be the output of the following JAVA code?
class Samsung{
static int i=5;
void disp(){
System.out.println(i--);
System.out.println(i--);
}
}
class Mobile{
public static void main(String arg[]){
Samsung m1=new Samsung();
m1.disp();
Samsung m2=new Samsung();
m2.disp();
}
}
Output & Explanation:
5
4
3
2
Static variable is used to initialize the variable only once in the program. disp() function called with m1 and m2 instance, m1.disp() prints twice the value of i--(i--=5 and i--=4) and being statice variable m2.disp() prints again twice the value of i-- (i-- =3 and i-- =2).
7. What will be the output of the following JAVA code?
class LWD{
public static void main(String L[]){
int x=20,y=15;
if(++x>10 || ++y>15)
x++;
else
y++;
System.out.println(x+" "+y);
}
}
Output & Explanation:
22 15
|
Bitwise &, |
|
Logical
&&, ||
|
|
Both arguments
should be evaluated always.
x & y à Even if x is false, y will also be evaluated.
x | y à Even if x is true, y will also be evaluated.
|
Second argument
evaluation is optional.
x&&y à If x is true then only y will be evaluated.
x || y à If x is false the only y will be evaluated.
|
|
Relatively
performance is low
|
Relatively
performance is high
|
|
Applicable for
integer and Boolean types.
|
Applicable only
for Boolean types but not for integer types.
|
Given that, x=20 and y=15
|
Operator
|
x
|
Y
|
|
&&
|
22
|
16
|
|
||
|
22
|
15
|
|
&
|
22
|
16
|
|
|
|
22
|
16
|
No comments:
Post a Comment