0% found this document useful (0 votes)
4 views5 pages

Operators MCQ

The document contains a series of Java programming questions and their respective answers, focusing on concepts such as type casting, arithmetic operations, and conditional expressions. Each question includes an explanation of the reasoning behind the correct answer. The document serves as a quiz or study guide for understanding Java programming behavior and syntax.

Uploaded by

Naveen A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Operators MCQ

The document contains a series of Java programming questions and their respective answers, focusing on concepts such as type casting, arithmetic operations, and conditional expressions. Each question includes an explanation of the reasoning behind the correct answer. The document serves as a quiz or study guide for understanding Java programming behavior and syntax.

Uploaded by

Naveen A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

1. What will be the output of the following?

public class Test {


public static void main(String[] args) {
System.out.println(10 > 9 == true);
}
}
A) true
B) false
C) Compilation Error
D) Runtime Exception

✅ Answer: A) true
💡 Explanation:

10 > 9 returns true


true == true results in true

2.What is the output?

public class Test {


public static void main(String[] args) {
System.out.println(5 == 5.0);
}
}
A) true
B) false
C) Compilation Error
D) Runtime Exception

✅ Answer: A) true
💡 Explanation:

5 (int) is automatically widened to 5.0 (double) before comparison.

3.What will be the result of System.out.println(10 != 10.0);?


A) true
B) false
C) Compilation Error
D) Runtime Exception

✅ Answer: B) false
💡 Explanation:

10 != 10.0 becomes false since both values are equal since 10 (int) is being
widened to double before comparision.

4. Which of the following is NOT allowed in type casting?


A) double d = 100;
B) int i = (int) 3.14;
C) short s = (short) 123456;
D) boolean b = (boolean) 1;

✅ Answer: D) boolean b = (boolean) 1;


💡 Explanation:
Boolean cannot be typecasted from numbers.

5. What will be the output?

public class Test {


public static void main(String[] args) {
int a = 10;
a *= 2 + 3;
System.out.println(a);
}
}
A) 20
B) 50
C) 30
D) Compilation Error

✅ Answer: B) 50
💡 Explanation:

a *= 2 + 3 → a = a * (2 + 3) → 10 * 5 = 50.

6. What happens when we use x /= 0; in Java?


A) Compilation Error
B) ArithmeticException
C) 0
D) NaN

✅ Answer: B) ArithmeticException
💡 Explanation:

Division by zero throws ArithmeticException.

7. What will be the output of the following?

public class Test {


public static void main(String[] args) {
System.out.println(10 > 9 == 9 < 10);
}
}
A) true
B) false
C) Compilation Error
D) Runtime Exception

✅ Answer: A) true
💡 Explanation:

10 > 9 → true
9 < 10 → true
true == true → true

8. What will be printed?

public class Test {


public static void main(String[] args) {
System.out.println(100L == 100.0);
}
}
A) true
B) false
C) Compilation Error
D) Runtime Exception

✅ Answer: A) true
💡 Explanation:

100L (long) is converted to double before comparison, making 100.0 == 100.0

9. What will be the output?

public class Test {


public static void main(String[] args) {
int x = 5, y = 10;
int result = (x > y) ? x++ : --y;
System.out.println(result + " " + x + " " + y);
}
}
A) 5 6 10
B) 9 6 9
C) 9 5 9
D) 9 5 10

✅ Answer: C) 9 5 9
💡 Explanation:

Since x > y is false, --y executes (10 → 9).


result = 9
x remains unchanged (5)

10. What will be printed?

public class Test {


public static void main(String[] args) {
System.out.println(3 > 2 ? (2 > 1 ? "Yes" : "No") : "No");
}
}
A) Yes
B) No
C) Compilation Error
D) Runtime Exception

✅ Answer: A) Yes
💡 Explanation:

Since 3 > 2 is true, the second ternary expression (2 > 1 ? "Yes" : "No") executes.
Since 2 > 1 is also true, "Yes" is printed.

11. What will be printed?


java
Copy
Edit
public class Test {
public static void main(String[] args) {
byte b = 50;
b = b * 2;
System.out.println(b);
}
}
A) 100
B) Compilation Error
C) Runtime Exception
D) -106

✅ Answer: B) Compilation Error


💡 Explanation:

b * 2 results in int.

12. What will be the output?

public class Test {


public static void main(String[] args) {
int a = 5;
a += a -= a * a;
System.out.println(a);
}
}
A) -15
B) 5
C) 10
D) Compilation Error

✅ Answer: A) -15
💡 Explanation:

a -= a * a → 5 - (5 * 5) = 5 - 25 = -20
a += -20 → -20 + 5 = -15
Associativity is from Right -> Left

13. What happens here?

public class Test {


public static void main(String[] args) {
int x = 3;
int y = ++x + x++ + --x + x--;
System.out.println(y);
}
}
A) 15
B) 16
C) 14
D) Compilation Error

✅ Answer: C) 14
💡 Explanation:

++x → x = 4
x++ → 4 (but x becomes 5)
--x → 4
x-- → 4 (but x becomes 3)
4 + 4 + 4 + 2 = 14

14. What will be printed?

public class Test {


public static void main(String[] args) {
int i = 5;
System.out.println(i++ * --i);
}
}
A) 20
B) 25
C) Compilation Error
D) Runtime Exception

✅ Answer: B) 25
💡 Explanation:

i++ returns 5, but i becomes 6


--i makes i = 5 again
5 * 5 = 25

15. What will be the output?

public class Test{


public static void main(String[] args){
byte b = 5;
b+=5;
System.out.println(b);
}
}
A) 10
B) Compilation Error
c) 5
D) Runtime Error

You might also like