Java Interview Questions With Answers – Part 5: Java Operators

[1][2][3][4][5] – Here’s part 5 of some Java 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.

Java-Operators-Interview-Questions

The series includes more than 200 questions, so stay tuned for more.

Q61. What is a Modulus Operator?

A61. Modulus operator is represented by % and returns the remainder of division operation.

Q62. Show the usage of Modulus Operator with an example program.

A62. Here are two code snippets that returns the remainder using Modulus operator,

// Code snippet 1

int z = 27;
System.out.println(“z mod 5:” + z % 5);

Output: z mod 5: 2

 

// Code snippet 2

double a = 9.85;
System.out.println(“a mod 5:” + a % 5);

Output: a mod 5: 4.85

Q63. What are Compound Assignment Operators?

A63.  The operator that combines Arithmetic operator with assignment is known as 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

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);

   }

}

Output

Value of z: 75
Value of y: 1
Value of x: 13

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 that 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.

public class Sample {
public static void main(String args[]) {int val1 = 10;
int val2 = 20;
int val3 = 30;
int val4 = 40;

int result1;
int result2;
int result3;
int 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);

}

}

 

Output

Value 1 = 11
Value 2 = 21
Value 3 = 29
Value 4 = 39
Result 1 = 11
Result 2 = 20
Result 3 = 29
Result 4 = 40

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 fulfils the following two conditions,

  • 1 bit if both operands are 1,
  • 0 for all others

Example,

13      1101

6   &0110

——-

4       0100

[1][2][3][4][5]

Comments

comments

thegenericwhiz@gmail.com'

GW Editorial Staff

Editorial Staff at Generic Whiz.

3 thoughts on “Java Interview Questions With Answers – Part 5: Java Operators

Leave a Reply

Your email address will not be published. Required fields are marked *