Loading

Interview Questions of operators

Scenario 1: Arithmetic Operation in Payroll

Scenario:


You are building a payroll system that calculates an employee's net salary by subtracting deductions from the gross salary.


Question:

Which Java operators would you use for this calculations ? Write a sample expression. 


Answer:

I would use the subtraction (-) and assignment (=) operators. For example:

double netSalary = grossSalary - deductions;

The - operator performs subtraction, and = assigns the result to netSalary.


Scenario 2: Checking Eligibility with Relational Operators

Scenario:


You need to determine if a candidate's score qualifies them for the next round. The passing score is 60.


Question:

Which operator would you use to check if the candidate's score is at least 60 ? Provide a sample condition.


Answer:

I would use the greater than or equal to (>=) relational operator. For example:

if (score >= 60) {
    // Candidate qualifies
}

The >= operator checks if score is 60 or higher.


Scenario 3: Combining Conditions

Scenario:


You want to check if a user is both an adult ( age>=18) and has accepted terms and conditions.


Question:

Which operator allows you to combine these two boolean conditions in Java ? Write the condition.


Answer:

I would use the logical AND (&&) operator:

if (age >= 18 && hasAcceptedTerms) {
    // Allow access
}

The && operator ensures both conditions are true for the block to execute.


Scenario 4: Using Modulus for Even/Odd Check

Scenario:


You are asked to write a function that determines if a number is even.


Question:

Which operator would you use, and how would you implement this check ?


Answer:

I would use the modulus (%) operator:

boolean isEven = (number % 2 == 0);

The % operator returns the remainder. If it's 0, the number is even.


Scenario 5: Shortcuts with Compound Assignment

Scenario: 


You want to increment a counter by 5 each time a button is clicked.


Question:

Which Java operator lets you do this in a concise way ? Show the code.

Answer:

I would use the compound assignment operator (+=):

counter += 5;

This is equivalent to counter = counter + 5; but shorter and clearer.


Scenario 6: Bitwise Operations

Scenario:


You are optimizing a system for performance and need to turn off a specific bit in an integer flag.


Question:

Which category of Java operators is used for this, and give a sample operation.


Answer:

Bitwise operators are use. For example, to turn off the 2nd bit:

int mask = ~(1 << 1);
flags = flags & mask;

Here, & is the bitwise AND, and ~ is the bitwise NOT.