08 Nov Java Operators Interview Questions
Here are Java Operators Interview Questions. The level of questions is suitable for beginners as well advanced core Java programmers. This part covers Java Operators, including Modulus operators, compound assignment operators, increment operators, etc.
Q61. What is a Modulus Operator?
A61. Modulus operator is represented by % and returns the remainder of the division operation.
Q62. Show the usage of Modulus Operator with an example program.
A62. Here are two code snippets that return the remainder using the Modulus operator,
// Code snippet 1
1 2 3 4 |
int z = 27; System.out.println("z mod 5:" + z % 5); |
Output
1 2 3 |
z mod 5: 2 |
// Code snippet 2
1 2 3 4 |
double a = 9.85; System.out.println("a mod 5:" + a % 5); |
Output
1 2 3 |
a mod 5: 4.85 |
Q63. What is Compound Assignment Operators?
A63. The operator that combines the Arithmetic operator with assignment is known as the Compound Assignment operator.
For example, x+=10;
Q63. Give an example showing the usage of Compound Assignment Operators?
A63
. Here’s a simple example, with output,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class Sample { public static void main(String args[]) { int z = 50; int y = 9; int x = 20; // Using Compound Assignment Operators z += 25; y %= 4; // Using normal operation x = x -7; System.out.println("Value of z: " + z); System.out.println("Value of y: " + y); System.out.println("Value of x: " + x); } } |
Here’s the output,
Q64. Why compound assignment operators are used since the same operation can be done normally as shown above?
A64. Compound assignment operators are used since they have a short form to do the same work, and they are more efficient than the longer form.
So, z+=25 is used more than z = z + 25 in coding.
Q65. What is the difference between ++x and x++ under increment operators?
A65. In ++x, the value of x will get incremented before it is used in the expression.
In x++, the previous value is used in the expression first, and after that x is modified.
Q66. Explain the above concept of increment operator with code snippets?
A66. 1.)
a = 10;
b = ++a;
In the above case, a is set to 11, since ++a increment before a is assigned to b.
2.)
a = 10;
b = a++;
In the above case, the value of a is assigned to be first before it gets increments by a++.
Q67. Write a complete program explaining the increment and decrement operators in Java?
A67. Here’s the program,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
public class Sample { public static void main(String args[]) { int val1 = 10; int val2 = 20; int val3 = 30; int val4 = 40; int result1, result2, result3, result4; // Increment operator result1 = ++val1; result2 = val2++; // Decrement operator result3 = --val3; result4 = val4--; System.out.println("Value 1 = " + val1); System.out.println("Value 2 = " + val2); System.out.println("Value 3 = " + val3); System.out.println("Value 4 = " + val4); System.out.println("Result 1 = " + result1); System.out.println("Result 2 = " + result2); System.out.println("Result 3 = " + result3); System.out.println("Result 4 = " + result4); } } |
Here’s the output,
Q68. What are Bitwise Logical Operators?
A68. &, |, ^, and ~ are bitwise logical operators.
Q69. What is the role of Bitwise NOT Operator (~)? Give an example?
A69. Bitwise Not Operator inverts all the bits of the operand.
For example, Bit pattern 1101 for the number 13
So, the value of ~1101 = 0010 i.e. all individual bits invert.
Q70. What is Bitwise AND Operator (&)? Give an example.
A70. The Bitwise AND operator fulfills the following two conditions,
- 1 bit if both operands are 1,
- 0 for all others
Here’s an example,
1 2 3 4 5 6 7 8 9 |
13 1101 6 &0110 ------- 4 0100 |
No Comments