0% found this document useful (0 votes)
3 views2 pages

Java Operator Basics and Examples

The document explains basic Java operators, including arithmetic, relational, and logical operators. It provides examples of their usage, highlights differences between certain operators, and discusses the consequences of dividing by zero. Key concepts include the distinction between assignment and equality, as well as the behavior of boolean operators.

Uploaded by

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

Java Operator Basics and Examples

The document explains basic Java operators, including arithmetic, relational, and logical operators. It provides examples of their usage, highlights differences between certain operators, and discusses the consequences of dividing by zero. Key concepts include the distinction between assignment and equality, as well as the behavior of boolean operators.

Uploaded by

shaaradagnihotri
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Basic Java Operator Questions / Home work

(By Shaarad Agnihotri)

1. What are arithmetic operators in Java? Give examples.

Arithmetic operators in Java are used to do basic mathematical work like add, substracte,
multiply, divide .Eg.

Operator Name Example Result


+ Addition 5+3 8
- Subtraction 10 - 4 6
* Multiplication 6*2 12
/ Division 15 / 3 5
% Modulus (Remainder) 17 % 5 2

2. What is the difference between / and % operators?

The / calculates the quotient of the division.

30 / 5 = 6

The calculates the remainder of the division.

36 % 5 = 1

3. What is the difference between = and ==?

The = is used to assign a value to a variable. It takes the value on the right-hand side and stores
it in the variable on the left-hand side.

• Example: int age = 30; (The value 30 is assigned to the variable age).

The == is used to check if two values or expressions are equal. It returns a boolean result (true
or false).
• Example: if (age == 30) (This expression evaluates to true if the value of age is 30).

4. Can relational operators be used with boolean values?

Yes, relational operators can be used with boolean values, specifically the equality operators ==
and !=

Other relational operators like <, >, <=, and >=, however, cannot be used with boolean values.

5. What does && and || mean in Java?

&& operator is used for AND means it will show true only if both side conditions are satisfied.

||operator is used for OR means it will show true if anyone of that condition is satisfied.

6. What does the ! operator do?

! operator is a unary operator that is used to reverse the boolean value of its Result

• If the Result is true, ! makes it false.


• If the Result is false, ! makes it true.

Example: ! (5 > 10) evaluates to true, because 5 > 10 is false.

7. What happens if you divide an integer by zero in Java?

If you attempt to divide an integer by zero in Java (e.g., int result = 10 / 0;), a
[Link] will be thrown at runtime. This causes the program to
crash unless the exception is caught and handled. (( This is not my answer I used GPT ))

You might also like