Quizsummary
0 of 12 questions completed
Questions:
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
Information
Java operators quiz 3 contains 12 single and multiple choice questions. Java operators quiz 3 questions are designed in such a way that it will help you understand how to use Java Operators, their 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 12 questions were answered correctly.
Time has elapsed
Your score is 0 out of 0, (0)
Average score  
Your score 
Category Score
 Operators 0%
Review Answers
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 Answered
 Review
 Question 1 of 12
1. Question
What will happen when you compile and run the following code?
public class Test{ public static void main(String[] args) { int i = 5, j = 8, k = 0; if(i > j && i++ < 10 && (k += i + j) > 10){ System.out.print("true"); } System.out.print(i); System.out.print(j); System.out.print(k); } }
Correct answer.Option 2 is the correct choice. The code uses multiple logical AND conditions. The expression will be evaluated from left to right.
Since the first condition i > j is false, other two conditions will not be even evaluated as && operator is a short circuit operator. So i is not incremented and k is not assigned the sum of i and j. They remain as is. The code will not go inside the if and 5, 8, 0 will be printed in the respective print statements.
Incorrect answer.Option 2 is the correct choice. The code uses multiple logical AND conditions. The expression will be evaluated from left to right.
Since the first condition i > j is false, other two conditions will not be even evaluated as && operator is a short circuit operator. So i is not incremented and k is not assigned the sum of i and j. They remain as is. The code will not go inside the if and 5, 8, 0 will be printed in the respective print statements.
 Question 2 of 12
2. Question
What will happen when you compile and run the following code?
public class Test{ public static void main(String[] args) { int i = 1, j = 1, k = 1; if( i++ > 1 && j++ < 2  k++ == 1 ) System.out.print(1); else System.out.print(2); System.out.print(i); System.out.print(j); System.out.print(k); } }
Correct answer.Option 1 is the correct choice. The && (logical AND) has higher precedence than  (logical OR) operator. So if condition will be evaluated like “(i++ > 1 && j++ < 2)  k++ == 1”.
The i++ > 1 condition is false because it is a post increment (1 > 1). The value of i becomes 2 after this condition. Since the first expression is false, j++ < 2 will not be evaluated and j remains 1. Thus first part of the if condition (i++ > 1 && j++ < 2) becomes false.
Now false  k++ == 1 will be evaluated. Again  is a short circuit operator. That means if the first condition is true, second will not be evaluated. But here, the first condition is false, so k++ == 1 will be evaluated which is true due to post increment (1 == 1). After this condition, k becomes 2. The whole if condition is thus false  true results in true.
The code will go inside the if statement and prints 1 followed by 212, values of i, j and k respectively.
Incorrect answer.Option 1 is the correct choice. The && (logical AND) has higher precedence than  (logical OR) operator. So if condition will be evaluated like “(i++ > 1 && j++ < 2)  k++ == 1”.
The i++ > 1 condition is false because it is a post increment (1 > 1). The value of i becomes 2 after this condition. Since the first expression is false, j++ < 2 will not be evaluated and j remains 1. Thus first part of the if condition (i++ > 1 && j++ < 2) becomes false.
Now false  k++ == 1 will be evaluated. Again  is a short circuit operator. That means if the first condition is true, second will not be evaluated. But here, the first condition is false, so k++ == 1 will be evaluated which is true due to post increment (1 == 1). After this condition, k becomes 2. The whole if condition is thus false  true results in true.
The code will go inside the if statement and prints 1 followed by 212, values of i, j and k respectively.
 Question 3 of 12
3. Question
What will happen when you compile and run the following code?
public class Test{ public static void main(String[] args) { int i = 50, j = 10; int k = i + 10 / j; System.out.println(k); } }
Correct answer.Option 3 is the correct choice. The / division operator has higher precedence than + operator. So the expression i + 10 / j will be evaluated as i + (10/j). So k will be 50 + 1 = 51 (since j is 10 so 10/j = 10/10 = 1).
Incorrect answer.Option 3 is the correct choice. The / division operator has higher precedence than + operator. So the expression i + 10 / j will be evaluated as i + (10/j). So k will be 50 + 1 = 51 (since j is 10 so 10/j = 10/10 = 1).
 Question 4 of 12
4. Question
What will happen when you compile and run the following code?
public class Test{ public static void main(String[] args) { int i = 12, j = 3; int k = i / j * 2; System.out.println(k); } }
Correct answer.Option 1 is the correct choice. The * multiplication and / division operators have same precedence. Hence, the expression will be evaluated from left to right according to the associativity.
The expression will be evaluated as (i / j) * 2 = (12/3) * 2 = 8.
Incorrect answer.Option 1 is the correct choice. The * multiplication and / division operators have same precedence. Hence, the expression will be evaluated from left to right according to the associativity.
The expression will be evaluated as (i / j) * 2 = (12/3) * 2 = 8.
 Question 5 of 12
5. Question
The && operator always evaluates both operands.
Correct answer.False is the correct choice. The && operator is known as a short circuit operator. It does not evaluate the second operand if the first one on the left side is false (because result is always going to be false no matter what is the second expression, false && false and false && true both are false).
Incorrect answer.False is the correct choice. The && operator is known as a short circuit operator. It does not evaluate the second operand if the first one on the left side is false (because result is always going to be false no matter what is the second expression, false && false and false && true both are false).
 Question 6 of 12
6. Question
What will happen when you compile and run the following code?
public class Test{ public static void main(String[] args){ int i = 0; System.out.println(~i); } }
Correct answer.Option 4 is the correct choice. The ~ operator is a unary bitwise complement operator. It flips the bits, i.e. makes all 0 to 1 and all 1 to 0.
Java int is a 32 bit, so 0 in binary is “00000000 00000000 00000000 00000000”. When you apply complement operator ~ to it, it makes the value as “11111111 11111111 11111111 11111111” that is 1 in decimal.
Incorrect answer.Option 4 is the correct choice. The ~ operator is a unary bitwise complement operator. It flips the bits, i.e. makes all 0 to 1 and all 1 to 0.
Java int is a 32 bit, so 0 in binary is “00000000 00000000 00000000 00000000”. When you apply complement operator ~ to it, it makes the value as “11111111 11111111 11111111 11111111” that is 1 in decimal.
 Question 7 of 12
7. Question
What will happen when you compile and run the following code?
public class Test{ public static void main(String[] args){ int i = 2; System.out.println(i << 3); } }
Correct answer.Option 4 is the correct choice. The << operator is a signed left shift operator. It shifts the bits to the left side by specified number of times by inserting 0s to the right (the same number of bits from the left side are discarded).
The decimal number 2 is “00000000 00000000 00000000 00000010” in 32 bit binary. When you apply left shift by 3, the number becomes “00000000 00000000 00000000 00010000”. As you can see, 3 zeros are inserted from left side, which gives us 16.
Short cut to calculate value of the left shift:
i << n = i * (2^n) where i is the number, and n is the bits needed to left shift.For example,
2 << 3 is equal to 2 * (2^3).
2^3 is 2 * 2 * 2
which gives us 2 * 8 = 16.Likewise, 8 << 4 is equal to 8 * 2 ^ 4
2 ^ 4 = 2 * 2 * 2 * 2 = 16
so the result will be 8 * 16 = 128.Incorrect answer.Option 4 is the correct choice. The << operator is a signed left shift operator. It shifts the bits to the left side by specified number of times by inserting 0s to the right (the same number of bits from the left side are discarded).
The decimal number 2 is “00000000 00000000 00000000 00000010” in 32 bit binary. When you apply left shift by 3, the number becomes “00000000 00000000 00000000 00010000”. As you can see, 3 zeros are inserted from left side, which gives us 16.
Short cut to calculate value of the left shift:
i << n = i * (2^n) where i is the number, and n is the bits needed to left shift.For example,
2 << 3 is equal to 2 * (2^3).
2^3 is 2 * 2 * 2
which gives us 2 * 8 = 16.Likewise, 8 << 4 is equal to 8 * 2 ^ 4
2 ^ 4 = 2 * 2 * 2 * 2 = 16
so the result will be 8 * 16 = 128.  Question 8 of 12
8. Question
What will happen when you compile and run the following code?
public class Test{ public static void main(String[] args){ int i = 32; System.out.println(i >> 3); } }
Correct answer.Option 3 is the correct choice. The >> operator is a bitwise signed right shift operator. It shifts the bits to the right side by specified number. However, unlike the left shift << operator, it extends the most significant bit instead of 0s.
The decimal number 32 is “00000000 00000000 00000000 00100000” in 32 bit binary, shifting it right by 3 positions will give us “00000000 00000000 00000000 00000100” which is 4 in decimal. Since the most significant bit was 0 (the first bit from the left side), it was extended in the left side.
Let’s looks at the negative number 16 which in binary representation is “11111111 11111111 11111111 11110000”. When we apply right shift to it using >> operator by 2, it becomes “11111111 11111111 11111111 11111100” which is 4 in decimal. As you can see, instead of inserting 0s at the left, >> operator replicated the most significant bit which was 1.
As with the left shift <<, signed right shift >> operator divides the number by power of 2. For example, 16 >> 2 is equivalent to 16 / (2 ^ 2) which is equal to 16/4 = 4.
Incorrect answer.Option 3 is the correct choice. The >> operator is a bitwise signed right shift operator. It shifts the bits to the right side by specified number. However, unlike the left shift << operator, it extends the most significant bit instead of 0s.
The decimal number 32 is “00000000 00000000 00000000 00100000” in 32 bit binary, shifting it right by 3 positions will give us “00000000 00000000 00000000 00000100” which is 4 in decimal. Since the most significant bit was 0 (the first bit from the left side), it was extended in the left side.
Let’s looks at the negative number 16 which in binary representation is “11111111 11111111 11111111 11110000”. When we apply right shift to it using >> operator by 2, it becomes “11111111 11111111 11111111 11111100” which is 4 in decimal. As you can see, instead of inserting 0s at the left, >> operator replicated the most significant bit which was 1.
As with the left shift <<, signed right shift >> operator divides the number by power of 2. For example, 16 >> 2 is equivalent to 16 / (2 ^ 2) which is equal to 16/4 = 4.
 Question 9 of 12
9. Question
What will happen when you compile and run the following code?
public class Test{ public static void main(String[] args){ int i = 32; System.out.println(i>>>3); } }
Correct answer.Option 5 is the correct choice. The code uses unsigned right shift >>> operator. Difference between >> and >>> is that the >>> operator inserts 0 to the left when shifting the bits to right, whereas >> operator replicates the most significant bit (the left most bit) to shift the bits to the right.
The decimal number 32 is “11111111 11111111 11111111 11100000” in 32bit binary. When we apply >>> shift operator by 3, it becomes “00011111 11111111 11111111 11111100” which in decimal is 536870908. As you can see, the zeros were inserted from the left and last 3 bits from right were dropped.
Incorrect answer.Option 5 is the correct choice. The code uses unsigned right shift >>> operator. Difference between >> and >>> is that the >>> operator inserts 0 to the left when shifting the bits to right, whereas >> operator replicates the most significant bit (the left most bit) to shift the bits to the right.
The decimal number 32 is “11111111 11111111 11111111 11100000” in 32bit binary. When we apply >>> shift operator by 3, it becomes “00011111 11111111 11111111 11111100” which in decimal is 536870908. As you can see, the zeros were inserted from the left and last 3 bits from right were dropped.
 Question 10 of 12
10. Question
What will happen when you compile and run the following code?
public class Test{ public static void main(String[] args){ int i = 10, j = 5; System.out.println(i & j); } }
Correct answer.Option 3 is the correct choice. The & is bitwise AND operator. It can work with boolean as well numbers, so there will be no compilation error.
The & operator compares the values bit by bit. If both of the bits are 1, it returns 1 otherwise 0. The decimal number 10 is “00000000 00000000 00000000 00001010” in 32bit binary and decimal number 5 is “00000000 00000000 00000000 00000101” in 32bit binary.
00000000 00000000 00000000 00001010
&
00000000 00000000 00000000 00000101
=
00000000 00000000 00000000 00000000As you can see, the values are compared bit by bit. The last bit of first number is compared with the last bit of the second number, if both bits are 1, the result bit is 1, otherwise the result bit is 0. All bits are compared in the same manner. As given above, the result is 0.
Incorrect answer.Option 3 is the correct choice. The & is bitwise AND operator. It can work with boolean as well numbers, so there will be no compilation error.
The & operator compares the values bit by bit. If both of the bits are 1, it returns 1 otherwise 0. The decimal number 10 is “00000000 00000000 00000000 00001010” in 32bit binary and decimal number 5 is “00000000 00000000 00000000 00000101” in 32bit binary.
00000000 00000000 00000000 00001010
&
00000000 00000000 00000000 00000101
=
00000000 00000000 00000000 00000000As you can see, the values are compared bit by bit. The last bit of first number is compared with the last bit of the second number, if both bits are 1, the result bit is 1, otherwise the result bit is 0. All bits are compared in the same manner. As given above, the result is 0.
 Question 11 of 12
11. Question
What will happen when you compile and run the following code?
public class Test{ public static void main(String[] args){ int i = 5, j = 2; System.out.println(i  j); } }
Correct answer.Option 1 is the correct choice. The  is a bitwise OR operator. It can work with boolean as well numbers, so there will be no compilation error.
The  operator compares the values bit by bit. If one of the bits is 1, result bit is 1, otherwise 0. The decimal number 5 is “00000000 00000000 00000000 00000101” in 32bit binary and decimal number 2 is “00000000 00000000 00000000 00000010” in 32bit binary.
00000000 00000000 00000000 00000101

00000000 00000000 00000000 00000010
=
00000000 00000000 00000000 00000111As you can see, the values are compared bit by bit. The last bit of first number is compared with the last bit of the second number, if one of the both bits is 1, the result bit is 1, otherwise the result bit is 0. All bits are compared in the same manner. As given above, the result is “00000000 00000000 00000000 00000111” which is 7 in decimal.
Incorrect answer.Option 1 is the correct choice. The  is a bitwise OR operator. It can work with boolean as well numbers, so there will be no compilation error.
The  operator compares the values bit by bit. If one of the bits is 1, result bit is 1, otherwise 0. The decimal number 5 is “00000000 00000000 00000000 00000101” in 32bit binary and decimal number 2 is “00000000 00000000 00000000 00000010” in 32bit binary.
00000000 00000000 00000000 00000101

00000000 00000000 00000000 00000010
=
00000000 00000000 00000000 00000111As you can see, the values are compared bit by bit. The last bit of first number is compared with the last bit of the second number, if one of the both bits is 1, the result bit is 1, otherwise the result bit is 0. All bits are compared in the same manner. As given above, the result is “00000000 00000000 00000000 00000111” which is 7 in decimal.
 Question 12 of 12
12. Question
What will happen when you compile and run the following code?
public class Test{ public static void main(String[] args){ int i = 5, j = 3; System.out.println(i ^ j); } }
Correct answer.Option 3 is the correct choice. The ^ is bitwise XOR (exclusive OR) operator. It can work with boolean as well numbers, so there will be no compilation error.
The ^ operator compares the values bit by bit. The result bit is 1, if both of the bits are different, 0 otherwise. The decimal number 5 is “00000000 00000000 00000000 00000101” in 32bit binary and decimal number 2 is “00000000 00000000 00000000 00000011” in 32bit binary.
00000000 00000000 00000000 00000101
&
00000000 00000000 00000000 00000011
=
00000000 00000000 00000000 00000110As you can see, the values are compared bit by bit. The last bit of first number is compared with the last bit of the second number, if both bits are different (if the first bit is 1 and second is 0 or the first bit is 0 and second is 1) result is 1, otherwise the result bit is 0. All bits are compared in the same manner. As given above, the result is “00000000 00000000 00000000 00000110” which is 6 in decimal.
Incorrect answer.Option 3 is the correct choice. The ^ is bitwise XOR (exclusive OR) operator. It can work with boolean as well numbers, so there will be no compilation error.
The ^ operator compares the values bit by bit. The result bit is 1, if both of the bits are different, 0 otherwise. The decimal number 5 is “00000000 00000000 00000000 00000101” in 32bit binary and decimal number 2 is “00000000 00000000 00000000 00000011” in 32bit binary.
00000000 00000000 00000000 00000101
&
00000000 00000000 00000000 00000011
=
00000000 00000000 00000000 00000110As you can see, the values are compared bit by bit. The last bit of first number is compared with the last bit of the second number, if both bits are different (if the first bit is 1 and second is 0 or the first bit is 0 and second is 1) result is 1, otherwise the result bit is 0. All bits are compared in the same manner. As given above, the result is “00000000 00000000 00000000 00000110” which is 6 in decimal.