Home »
Java Aptitude Que. & Ans.
Java Operators Aptitude Questions and Answers
Java Operators Aptitude Questions and Answers: This section provides you Java Operators related Aptitude Questions and Answers with multiple choices. Here, You will get solution and explanation of each question.
List of Java Operators Aptitude Questions and Answers
1)
What will be the output of following program ?
class Opr
{
public static void main(String args[])
{
boolean ans=false;
if(ans=true)
System.out.print("Done");
else
System.out.print("Fail");
}
}
- Done
- Fail
- Error
- DoneFail
Correct Answer: 1
Done
in condition ans=true, the value of ans becomes true hence condition is true.
2)
What will be the output of following program?
class Opr
{
public static void main(String args[])
{
int x=5,y;
y= ++x + x++ + --x;
System.out.println(x + "," + y);
}
}
- 6,15
- 6,18
- 6,12
- None of these
Correct Answer: 2
6, 18
In JAVA ++/-- operators evaluates with expression
Rule to evaluate expression with ++/-- : [pre_increment -> expression ->post_increment] Consider the expression,
y=++x + x++ + --x; => y=6 + 6 + 6;
here, ++x evaluates first, value of x will be 6, x++ evaluates after adding starting two terms ++x + x++ [6+6], and then x will be 7 (due to x++), --x will evaluate before adding value in previous result, so expression will solve like 6+6+6.
3)
What will be the output of following program?
class Opr
{
public static void main(String args[])
{
byte a,b;
a=10; b=20;
b=assign(a);
System.out.println(a +","+ b);
}
public static byte assign(byte a)
{
a+=100;
return a;
}
}
- 110, 110
- 10, 110
- 10, 10
- None of these
Correct Answer: 2
10, 110
Here variable a in main and a in assign are different, only value of a (10) will pass into function assign, value of a will remain same, answer will 10, 110.
4)
What will be the output of following program?
class Opr
{
public static void main(String args[])
{
int a,b,result;
a=10; b=20;
result=(b>=a);
System.out.print(result);
}
}
- Error
- 1
- True
- 20
Correct Answer: 1
ERROR: incompatible types.
Consider the expression result=(b>=a); here value of b is largest from a, True will return, and true (boolean) can not assign into integer variable.
5)
What will be the output of following program?
class Opr
{
public static void main(String args[])
{
int a,b,result;
a=10; b=20;
result=(int)(b>=a);
System.out.print(result);
}
}
- Error
- 1
- True
- 20
Correct Answer: 1
ERROR: incompatible types.
Consider the expression result=(int)(b>=a); .boolean is not convertible to int.
Page 2