Java operators quiz 2 contains 10 single and multiple choice questions. Java operators quiz 2 questions are designed in such a way that it will help you understand Java Operators, precedence and associativity. At the end of the quiz, result will be displayed along with your score and Java operators quiz answers.
There is no time limit to complete the quiz. Click Start Quiz button to start the Java operators quiz online.
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading...
You must sign in or sign up to start the quiz.
You have to finish following quiz, to start this quiz:
Result
0 out of 10 questions were answered correctly.
Time has elapsed
Your score is 0 out of 0, (0)
Average score
Your score
Category Score
Operators0%
Review Answers
1
2
3
4
5
6
7
8
9
10
Answered
Review
Question 1 of 10
1. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args) {
long l1 = 30000;
long l2 = 10000;
short s = 0;
s += l1 + l2;
System.out.println(s);
}
}
Correct answer.
Option 1 is the correct choice. The += operator handles the casting implicitly. That means s += l1 + l2 will be evaluated as s = (short) (s + l1 + l2). Hence there will be no compilation error.
However, range of the short data type in Java is -32768 to 32767. The result of l1 + l2 will be 40000 which is above the range of the short. Hence, value of variable s will be 40000 – 65536 = -25536.
Incorrect answer.
Option 1 is the correct choice. The += operator handles the casting implicitly. That means s += l1 + l2 will be evaluated as s = (short) (s + l1 + l2). Hence there will be no compilation error.
However, range of the short data type in Java is -32768 to 32767. The result of l1 + l2 will be 40000 which is above the range of the short. Hence, value of variable s will be 40000 – 65536 = -25536.
Question 2 of 10
2. Question
What will happen when you compile and run the following code?
class One{
}
public class Test{
static One one;
public static void main(String[] args) {
System.out.println(one instanceof One);
}
}
Correct answer.
Option 2 is the correct choice. Since there is no object created and assigned to the static reference one, it will be initialized with default value null.
Expression “null instanceof ANY_CLASS” always returns false. So the code will print false when run.
Incorrect answer.
Option 2 is the correct choice. Since there is no object created and assigned to the static reference one, it will be initialized with default value null.
Expression “null instanceof ANY_CLASS” always returns false. So the code will print false when run.
Question 3 of 10
3. Question
Which of the following line will print true if written at line 15?
interface Inter{}
class One{}
class Two extends One{}
class Three extends Two implements Inter{}
public class Test{
public static void main(String[] args) {
One one = new Two();
Two two = new Three();
Three three = new Three();
Inter i = new Three();
//your code here
}
}
Correct answer.
All of the above is the correct choice.
Object reference one points to the object of class Two which is subclass of class One, hence option 1 and 2 prints true. Object reference two points to the object of class Three which is indirect subclass of class One, hence option 3 prints true.
Object reference i points to the object of class Three which implements interface inter, so option 4 prints true. Class Three is also indirect subclass of the class One, so option 5 prints true.
Object reference three points to the object of class Three which implements the interface inter and is a direct subclass of class Two and indirect subclass of class One, so options 6, 7 and 8 prints true.
Incorrect answer.
All of the above is the correct choice.
Object reference one points to the object of class Two which is subclass of class One, hence option 1 and 2 prints true. Object reference two points to the object of class Three which is indirect subclass of class One, hence option 3 prints true.
Object reference i points to the object of class Three which implements interface inter, so option 4 prints true. Class Three is also indirect subclass of the class One, so option 5 prints true.
Object reference three points to the object of class Three which implements the interface inter and is a direct subclass of class Two and indirect subclass of class One, so options 6, 7 and 8 prints true.
Question 4 of 10
4. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args) {
long l = 20;
System.out.println(l instanceof Object);
}
}
Correct answer.
Option 3 is the correct choice. The instanceof operator does not work with primitive operands. The code will give compilation error “Incompatible conditional operand types long and Object”.
Incorrect answer.
Option 3 is the correct choice. The instanceof operator does not work with primitive operands. The code will give compilation error “Incompatible conditional operand types long and Object”.
Question 5 of 10
5. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args) {
int i = 0;
if( i = 0 )
System.out.println(true);
else
System.out.println(false);
}
}
Correct answer.
Option 3 is the correct choice. The if statement needs boolean as a condition while i = 0 returns int. The code will give compilation error “Type mismatch: cannot convert from int to boolean”.
In Java, the “==” operator is used to compare values and it returns boolean. The “=” operator is an assignment operator.
Incorrect answer.
Option 3 is the correct choice. The if statement needs boolean as a condition while i = 0 returns int. The code will give compilation error “Type mismatch: cannot convert from int to boolean”.
In Java, the “==” operator is used to compare values and it returns boolean. The “=” operator is an assignment operator.
Question 6 of 10
6. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args) {
int i = 10;
if( i =< 15.5 )
System.out.println(true);
else
System.out.println(false);
}
}
Correct answer.
Option 3 is the correct choice. The right syntax to check whether any value is less than or equal to another value is “<=" not "=<". The code will give compilation error.
Incorrect answer.
Option 3 is the correct choice. The right syntax to check whether any value is less than or equal to another value is “<=" not "=<". The code will give compilation error.
Question 7 of 10
7. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args) {
int i = 0, j = 1;
if( i && j )
System.out.println(1);
else
System.out.println(2);
}
}
Correct answer.
Option 3 is the correct choice. Both of the operands of the && operator (short circuit and operator) must be of type boolean. It does not work with any other types. Hence, the code will give compilation error “The operator && is undefined for the argument type(s) int, int”.
Incorrect answer.
Option 3 is the correct choice. Both of the operands of the && operator (short circuit and operator) must be of type boolean. It does not work with any other types. Hence, the code will give compilation error “The operator && is undefined for the argument type(s) int, int”.
Question 8 of 10
8. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args) {
float[] f = {1.3f, 1.4f, 1.5f};
System.out.println( f instanceof Object );
}
}
Correct answer.
Option 1 is the correct choice. Java arrays are objects. Object class is the super class of all the Java classes. Hence, the code will print true when executed.
Incorrect answer.
Option 1 is the correct choice. Java arrays are objects. Object class is the super class of all the Java classes. Hence, the code will print true when executed.
Question 9 of 10
9. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int i = 10;
int j = 20;
if( i + j < 15 & i++ > 10 )
System.out.print(i);
else
System.out.print(i);
int x = 10;
int y = 20;
if( x + y < 15 && x++ > 10 )
System.out.print(x);
else
System.out.print(x);
}
}
Correct answer.
Option 2 is the correct choice. The & (bitwise AND) operator evaluates both of the operands and if they both are true, result is true.
The && (logical AND/short circuit AND) operator evaluates first operand, if it is false, the second operand is not evaluated. If the first operand is true, the second operand will be evaluated and if it is also true, the result is true otherwise false.
Here, i + j is 30 which is not less than 15. The second expression is i++ > 10. Since i++ is post increment, 10 will be used for comparison. 10 is not greater than 10 so it will also return false. However, value of i will be incremented after that statement so 11 will be printed.
In the second part, x + y is 30 which is not less than 15. Since the first expression is false, x++ > 10 is never evaluated and hence i is not incremented. So 10 will be printed. This is the reason && is also called short circuit operator, because it does not evaluate the second expression at all if first is false.
Incorrect answer.
Option 2 is the correct choice. The & (bitwise AND) operator evaluates both of the operands and if they both are true, result is true.
The && (logical AND/short circuit AND) operator evaluates first operand, if it is false, the second operand is not evaluated. If the first operand is true, the second operand will be evaluated and if it is also true, the result is true otherwise false.
Here, i + j is 30 which is not less than 15. The second expression is i++ > 10. Since i++ is post increment, 10 will be used for comparison. 10 is not greater than 10 so it will also return false. However, value of i will be incremented after that statement so 11 will be printed.
In the second part, x + y is 30 which is not less than 15. Since the first expression is false, x++ > 10 is never evaluated and hence i is not incremented. So 10 will be printed. This is the reason && is also called short circuit operator, because it does not evaluate the second expression at all if first is false.
Question 10 of 10
10. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int i = 1;
int j = 2;
int k = i + 10 * i++ - j * 5 + --j;
System.out.println(k);
}
}
Correct answer.
Option 1 is the correct choice. You need to understand associativity and operator precedence in order to answer this question.
The “i + 10 * i++ – j * 5 + –j” expression will be evaluated as given below.
1) Since * and / have higher precedence than + and -, the expression will be evaluated as “1 + (10 * i++) – (2 * 5) + –j”
2) The i++ will be evaluated and multiplication will be done like “1 + (10 * 1) – (2 * 5) + –j”. The variable i is incremented using post increment so 1 will be used for multiplication and after that it becomes 2.
3) The –j will be evaluated and the expression will become “1 + 10 – 10 + 1”.
4) Since + and – both have same precedence, expression will be evaluated from left to right due to associativity, so it will be executed like (((1 + 10) – 10) + 1) which gives us 2.
Incorrect answer.
Option 1 is the correct choice. You need to understand associativity and operator precedence in order to answer this question.
The “i + 10 * i++ – j * 5 + –j” expression will be evaluated as given below.
1) Since * and / have higher precedence than + and -, the expression will be evaluated as “1 + (10 * i++) – (2 * 5) + –j”
2) The i++ will be evaluated and multiplication will be done like “1 + (10 * 1) – (2 * 5) + –j”. The variable i is incremented using post increment so 1 will be used for multiplication and after that it becomes 2.
3) The –j will be evaluated and the expression will become “1 + 10 – 10 + 1”.
4) Since + and – both have same precedence, expression will be evaluated from left to right due to associativity, so it will be executed like (((1 + 10) – 10) + 1) which gives us 2.