Java operators quiz 1 contains 10 single and multiple choice questions. Java operators quiz 1 questions are designed in such a way that it will help you understand Java Operators. 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) {
int i = 0;
int j = i++ + ++i;
System.out.println( j );
}
}
Correct answer.
Option 3 is the correct choice. The i++ is a post increment which means that the value will be used first and incremented later, while ++i is a pre-increment which means that the value will be incremented first and used later. The statement i++ + ++i will be evaluated as (0) + ++(1) = 2.
Incorrect answer.
Option 3 is the correct choice. The i++ is a post increment which means that the value will be used first and incremented later, while ++i is a pre-increment which means that the value will be incremented first and used later. The statement i++ + ++i will be evaluated as (0) + ++(1) = 2.
Question 2 of 10
2. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args) {
int i = 0;
int j = ++i + i++;
System.out.println( j );
}
}
Correct answer.
Option 2 is the correct choice. The i++ is a post increment which means that the value will be used first and incremented later, while ++i is a pre-increment which means that the value will be incremented first and used later. The statement ++i + i++ will be evaluated as 1 + (1)++ = 1 + 1 = 2.
Incorrect answer.
Option 2 is the correct choice. The i++ is a post increment which means that the value will be used first and incremented later, while ++i is a pre-increment which means that the value will be incremented first and used later. The statement ++i + i++ will be evaluated as 1 + (1)++ = 1 + 1 = 2.
Question 3 of 10
3. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args) {
int i = 100;
int j = 10;
System.out.println( i%j );
}
}
Correct answer.
Option 2 is the correct choice. The % is a modulus operator, means it returns remainder of division operation. The number 100 is divisible by 10 which yields remainder 0. The result of 10 % 3 will be 1 because if you divide 10 by 3, the remainder will be 1 ( 3 * 3 = 9 which leaves us 1).
Incorrect answer.
Option 2 is the correct choice. The % is a modulus operator, means it returns remainder of division operation. The number 100 is divisible by 10 which yields remainder 0. The result of 10 % 3 will be 1 because if you divide 10 by 3, the remainder will be 1 ( 3 * 3 = 9 which leaves us 1).
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) {
int i = 0;
int j = 1;
if(!i && j)
System.out.println("1");
else
System.out.println("2");
}
}
Correct answer.
Option 3 is the correct choice. The ! operator is a unary logical complement operator which inverts the value of a boolean.
For example if value of boolean variable b is true, !b will make it false. The ! operator requires operand to be of type boolean. So code will give compilation error “The operator ! is undefined for the argument type(s) int”.
Incorrect answer.
Option 3 is the correct choice. The ! operator is a unary logical complement operator which inverts the value of a boolean.
For example if value of boolean variable b is true, !b will make it false. The ! operator requires operand to be of type boolean. So code will give compilation error “The operator ! is undefined for the argument type(s) int”.
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) {
boolean b1 = true, b2 = false, b3 = true;
System.out.println( b1&&b2&&!b3 );
}
}
Correct answer.
Option 2 is the correct choice. The && operator operates on boolean. It returns true if and only if both of the operands are true.
The ! operator is a complement operator. So if the operand is true, it makes it false. If the operand is false, it makes it true. The statement b1&&b2&&!b3 will be evaluated like (true && false && !(true) ) which will equal to (true && false && true) = false.
Incorrect answer.
Option 2 is the correct choice. The && operator operates on boolean. It returns true if and only if both of the operands are true.
The ! operator is a complement operator. So if the operand is true, it makes it false. If the operand is false, it makes it true. The statement b1&&b2&&!b3 will be evaluated like (true && false && !(true) ) which will equal to (true && false && true) = false.
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, j = 12, k = 1;
k += i++ - --j;
System.out.println(k);
}
}
Correct answer.
Option 4 is the correct choice. The expression i++ – –j will be evaluated like 10 – 11 = -1. The result is again added to the value of k because of “k +=”. So 1 + (-1) = 0 will be assigned to k.
Incorrect answer.
Option 4 is the correct choice. The expression i++ – –j will be evaluated like 10 – 11 = -1. The result is again added to the value of k because of “k +=”. So 1 + (-1) = 0 will be assigned to k.
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 = 10;
i++;i++;++i;
int j = i++;
System.out.println(j);
}
}
Correct answer.
Option 3 is the correct choice. All three increments of the variable i are done before the final value is assigned to the variable j. The value of i after 3 increments will be 13. However, the value of j will be 13 as well because the value of i is assigned to j first and incremented later due to the post increment operator.
Incorrect answer.
Option 3 is the correct choice. All three increments of the variable i are done before the final value is assigned to the variable j. The value of i after 3 increments will be 13. However, the value of j will be 13 as well because the value of i is assigned to j first and incremented later due to the post increment operator.
Question 8 of 10
8. Question
What will happen when you compile and run the following code?
Option 4 is the correct choice. The arithmetic operation automatically widens operands of byte, short and char types to int value. The b1 + 10 expression will be widened to int type which cannot be assigned back to the byte type without an explicit cast.
In normal scenarios, a = a + b and a += b are similar, however there is a small difference. The += operator also does the casting if necessary. In above code, b2 += 10 will be executed as b2 = (byte) (b2 + 10). Since it already handles cast for you, there will be no compilation error for that statement.
Incorrect answer.
Option 4 is the correct choice. The arithmetic operation automatically widens operands of byte, short and char types to int value. The b1 + 10 expression will be widened to int type which cannot be assigned back to the byte type without an explicit cast.
In normal scenarios, a = a + b and a += b are similar, however there is a small difference. The += operator also does the casting if necessary. In above code, b2 += 10 will be executed as b2 = (byte) (b2 + 10). Since it already handles cast for you, there will be no compilation error for that statement.
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 = 19;
int j = -5;
System.out.println( i%j );
}
}
Correct answer.
Option 1 is the correct choice. The % modulus operator returns remainder of the division operation. If the first operand of the modulus operator is positive, the remainder is also positive.
Hence, the code will print 4 when run.
Incorrect answer.
Option 1 is the correct choice. The % modulus operator returns remainder of the division operation. If the first operand of the modulus operator is positive, the remainder is also positive.
Hence, the code will print 4 when run.
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 = -21;
int j = 4;
System.out.println( i%j );
}
}
Correct answer.
Option 2 is the correct choice. The % modulus operator returns remainder of the division operation. If the first operand of the modulus operator is negative, the remainder is also negative.
So the code will print -1 when run.
Incorrect answer.
Option 2 is the correct choice. The % modulus operator returns remainder of the division operation. If the first operand of the modulus operator is negative, the remainder is also negative.