Java Questions & Answers – Arithmetic Operators

This section of our 1000+ Java MCQs focuses on Arithmetic Operators of Java Programming Language.

1. Which of the following can be operands of arithmetic operators?
a) Numeric
b) Boolean
c) Characters
d) Both Numeric & Characters
View Answer

Answer: d
Explanation: The operand of arithmetic operators can be any of numeric or character type, But not boolean.

2. Modulus operator, %, can be applied to which of these?
a) Integers
b) Floating – point numbers
c) Both Integers and floating – point numbers
d) None of the mentioned
View Answer

Answer: c
Explanation: Modulus operator can be applied to both integers and floating point numbers.

3. With x = 0, which of the following are legal lines of Java code for changing the value of x to 1?

advertisement
advertisement
   1. x++;
   2. x = x + 1;
   3. x += 1;
   4. x =+ 1;

a) 1, 2 & 3
b) 1 & 4
c) 1, 2, 3 & 4
d) 3 & 2
View Answer

Answer: c
Explanation: Operator ++ increases value of variable by 1. x = x + 1 can also be written in shorthand form as x += 1. Also x =+ 1 will set the value of x to 1.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

4. Decrement operator, −−, decreases the value of variable by what number?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: a
Explanation: None.

5. Which of these statements are incorrect?
a) Assignment operators are more efficiently implemented by Java run-time system than their equivalent long forms
b) Assignment operators run faster than their equivalent long forms
c) Assignment operators can be used only with numeric and character data type
d) None of the mentioned
View Answer

Answer: d
Explanation: None.
advertisement

6. What will be the output of the following Java program?

  1.     class increment 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             double var1 = 1 + 5; 
  6.             double var2 = var1 / 4;
  7.             int var3 = 1 + 5;
  8.             int var4 = var3 / 4;
  9.             System.out.print(var2 + " " + var4);
  10.  
  11.         } 
  12.     }

a) 1 1
b) 0 1
c) 1.5 1
d) 1.5 1.0
View Answer

Answer: c
Explanation: None
output:

advertisement
$ javac increment.java
$ java increment
1.5 1

7. What will be the output of the following Java program?

  1.     class Modulus 
  2.     {
  3.         public static void main(String args[]) 
  4.         {    
  5.              double a = 25.64;
  6.              int  b = 25;
  7.              a = a % 10;
  8.              b = b % 10;
  9.              System.out.println(a + " "  + b);
  10.         } 
  11.     }

a) 5.640000000000001 5
b) 5.640000000000001 5.0
c) 5 5
d) 5 5.640000000000001
View Answer

Answer: a
Explanation: Modulus operator returns the remainder of a division operation on the operand. a = a % 10 returns 25.64 % 10 i:e 5.640000000000001. Similarly b = b % 10 returns 5.
output:

$ javac Modulus.java
$ java Modulus
5.640000000000001 5

8. What will be the output of the following Java program?

  1.     class increment 
  2.     {
  3.         public static void main(String args[]) 
  4.         {        
  5.              int g = 3;
  6.              System.out.print(++g * 8);
  7.         } 
  8.     }

a) 25
b) 24
c) 32
d) 33
View Answer

Answer: c
Explanation: Operator ++ has more preference than *, thus g becomes 4 and when multiplied by 8 gives 32.
output:

$ javac increment.java
$ java increment
32

9. Can 8 byte long data type be automatically type cast to 4 byte float data type?
a) True
b) False
View Answer

Answer: a
Explanation: Both data types have different memory representation that’s why 8-byte integral data type can be stored to 4-byte floating point data type.

10. What will be the output of the following Java program?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {    
  5.              int a = 1;
  6.              int b = 2;
  7.              int c;
  8.              int d;
  9.              c = ++b;
  10.              d = a++;
  11.              c++;
  12.              b++;
  13.              ++a;
  14.              System.out.println(a + " " + b + " " + c);
  15.         } 
  16.     }

a) 3 2 4
b) 3 2 3
c) 2 3 4
d) 3 4 4
View Answer

Answer: d
Explanation: None.
output:

$ javac Output.java
$ java Output
3 4 4

Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.