Java Operators Practice
Arithmetic, relational, logical, assignment and miscellaneous operators in Java.
1
Simple Calculator
Easy
Read two integers A and B and print their sum, difference, product and integer quotient (A / B), each on its own line in that order.
Input Format
A single line with two integers A and B.
Output Format
Four lines: sum, difference, product, quotient.
Sample Test Cases
|
Sample Input 1
10 3 |
Sample Output 1
13 7 30 3 |
|
Sample Input 2
8 2 |
Sample Output 2
10 6 16 4 |
|
Sample Input 3
1 1 |
Sample Output 3
2 0 1 1 |
|
Sample Input 4
100 10 |
Sample Output 4
110 90 1000 10 |
|
Sample Input 5
7 2 |
Sample Output 5
9 5 14 3 |
Constraints
1
Explanation
Use +, -, *, / in order.
2
Even or Odd
Easy
Read an integer N and print "Even" if it is even, otherwise "Odd".
Input Format
A single integer N.
Output Format
"Even" or "Odd".
Sample Test Cases
|
Sample Input 1
7 |
Sample Output 1
Odd |
|
Sample Input 2
12 |
Sample Output 2
Even |
|
Sample Input 3
0 |
Sample Output 3
Even |
|
Sample Input 4
-3 |
Sample Output 4
Odd |
|
Sample Input 5
-2 |
Sample Output 5
Even |
Constraints
-10^6
Explanation
N % 2 == 0 means even. Java % on negatives gives -1 or 0.
3
Modulo Magic
Easy
Read two integers A and B and print A % B.
Input Format
A single line with two integers A and B.
Output Format
A single integer: A % B.
Sample Test Cases
|
Sample Input 1
10 3 |
Sample Output 1
1 |
|
Sample Input 2
17 5 |
Sample Output 2
2 |
|
Sample Input 3
10 10 |
Sample Output 3
0 |
|
Sample Input 4
1 7 |
Sample Output 4
1 |
|
Sample Input 5
1000000000 7 |
Sample Output 5
6 |
Constraints
1
Explanation
The remainder operator % gives the remainder of A divided by B.
4
Square and Cube
Easy
Read an integer N and print N squared and N cubed on one line.
Input Format
A single integer N.
Output Format
Two integers: N^2 and N^3, space-separated.
Sample Test Cases
|
Sample Input 1
3 |
Sample Output 1
9 27 |
|
Sample Input 2
5 |
Sample Output 2
25 125 |
|
Sample Input 3
1 |
Sample Output 3
1 1 |
|
Sample Input 4
0 |
Sample Output 4
0 0 |
|
Sample Input 5
1000 |
Sample Output 5
1000000 1000000000 |
Constraints
1
Explanation
n*n and n*n*n.
5
Division and Modulus
Easy
Read two integers A and B and print the integer quotient and remainder of A divided by B on one line.
Input Format
A single line with two integers A and B.
Output Format
Two integers: quotient then remainder, space-separated.
Sample Test Cases
|
Sample Input 1
17 5 |
Sample Output 1
3 2 |
|
Sample Input 2
9 3 |
Sample Output 2
3 0 |
|
Sample Input 3
10 2 |
Sample Output 3
5 0 |
|
Sample Input 4
100 7 |
Sample Output 4
14 2 |
|
Sample Input 5
1 1 |
Sample Output 5
1 0 |
Constraints
1
Explanation
quotient = a / b; remainder = a % b.
1
Absolute Difference
Medium
Read two integers A and B and print |A - B| using Math.abs().
Input Format
A single line with two integers A and B.
Output Format
A single integer: |A - B|.
Sample Test Cases
|
Sample Input 1
10 3 |
Sample Output 1
7 |
|
Sample Input 2
-5 5 |
Sample Output 2
10 |
|
Sample Input 3
7 7 |
Sample Output 3
0 |
|
Sample Input 4
-3 -9 |
Sample Output 4
6 |
|
Sample Input 5
1000000000 -1000000000 |
Sample Output 5
2000000000 |
Constraints
-10^9
Explanation
int diff = Math.abs(a - b);
2
Swap Without Temp
Medium
Read two integers A and B and print them swapped (B then A) WITHOUT using a third variable (use arithmetic).
Input Format
A single line with two integers A and B.
Output Format
A single line: B then A, space-separated.
Sample Test Cases
|
Sample Input 1
5 10 |
Sample Output 1
10 5 |
|
Sample Input 2
1 2 |
Sample Output 2
2 1 |
|
Sample Input 3
100 1 |
Sample Output 3
1 100 |
|
Sample Input 4
7 7 |
Sample Output 4
7 7 |
|
Sample Input 5
-3 4 |
Sample Output 5
4 -3 |
Constraints
-10^9
Explanation
a = a + b; b = a - b; a = a - b.
3
Compound Interest (Simple)
Medium
Read principal P, rate R (percent per year) and time T (years) and print the simple interest as an integer. It is guaranteed P*R*T is divisible by 100.
Input Format
A single line with three integers P, R, T.
Output Format
A single integer: (P * R * T) / 100.
Sample Test Cases
|
Sample Input 1
1000 5 2 |
Sample Output 1
100 |
|
Sample Input 2
2000 10 1 |
Sample Output 2
200 |
|
Sample Input 3
5000 8 5 |
Sample Output 3
2000 |
|
Sample Input 4
1 100 1 |
Sample Output 4
1 |
|
Sample Input 5
10000 3 3 |
Sample Output 5
900 |
Constraints
1
Explanation
interest = (P * R * T) / 100 using integer division.
4
Perimeter and Diagonal
Medium
Read two integers L and B and print the perimeter and the diagonal rounded DOWN to the nearest integer. diagonal = floor(sqrt(L*L + B*B)).
Input Format
A single line with two integers L and B.
Output Format
Two integers: perimeter and floor(diagonal), space-separated.
Sample Test Cases
|
Sample Input 1
3 4 |
Sample Output 1
14 5 |
|
Sample Input 2
6 8 |
Sample Output 2
28 10 |
|
Sample Input 3
1 1 |
Sample Output 3
4 1 |
|
Sample Input 4
5 12 |
Sample Output 4
34 13 |
|
Sample Input 5
10 10 |
Sample Output 5
40 14 |
Constraints
1
Explanation
perimeter = 2*(L+B); diagonal = (int)Math.sqrt(L*L + B*B).
5
Last Two Digits
Medium
Read a positive integer N and print its last two digits as a two-digit number (with a leading zero if needed).
Input Format
A single integer N.
Output Format
A single string: the last two digits.
Sample Test Cases
|
Sample Input 1
1234 |
Sample Output 1
34 |
|
Sample Input 2
500 |
Sample Output 2
00 |
|
Sample Input 3
100 |
Sample Output 3
00 |
|
Sample Input 4
109 |
Sample Output 4
09 |
|
Sample Input 5
987654321 |
Sample Output 5
21 |
Constraints
100
Explanation
int lastTwo = n % 100; System.out.printf("%02d", lastTwo).
1
Maximum of Two Without if
Hard
Read two integers A and B and print the larger one WITHOUT using if/else, ternary or Math.max. Use arithmetic only.
Input Format
A single line with two integers A and B.
Output Format
A single integer: the larger of A and B.
Sample Test Cases
|
Sample Input 1
5 9 |
Sample Output 1
9 |
|
Sample Input 2
-3 -7 |
Sample Output 2
-3 |
|
Sample Input 3
7 7 |
Sample Output 3
7 |
|
Sample Input 4
100 1 |
Sample Output 4
100 |
|
Sample Input 5
-100 -1 |
Sample Output 5
-1 |
Constraints
-10^9
Explanation
max = (A + B + Math.abs(A - B)) / 2.
2
Count Digits (Arithmetic)
Hard
Read a positive integer N and print the number of digits using arithmetic (division by 10 in a loop).
Input Format
A single integer N.
Output Format
A single integer: the digit count.
Sample Test Cases
|
Sample Input 1
12345 |
Sample Output 1
5 |
|
Sample Input 2
7 |
Sample Output 2
1 |
|
Sample Input 3
0 |
Sample Output 3
1 |
|
Sample Input 4
1000000000 |
Sample Output 4
10 |
|
Sample Input 5
999 |
Sample Output 5
3 |
Constraints
1
Explanation
int c = 0; while (n > 0) { c++; n /= 10; }
3
Digit Sum (Single Pass)
Hard
Read a positive integer N and print the sum of its digits using only arithmetic operators.
Input Format
A single integer N.
Output Format
A single integer: the digit sum.
Sample Test Cases
|
Sample Input 1
1234 |
Sample Output 1
10 |
|
Sample Input 2
999 |
Sample Output 2
27 |
|
Sample Input 3
1 |
Sample Output 3
1 |
|
Sample Input 4
1000000000 |
Sample Output 4
1 |
|
Sample Input 5
987654321 |
Sample Output 5
45 |
Constraints
1
Explanation
while (n > 0) { sum += n % 10; n /= 10; }
4
Nearest Multiple
Hard
Read two integers A and B and print the smallest multiple of B that is greater than or equal to A.
Input Format
A single line with two integers A and B.
Output Format
A single integer: ceil(A / B) * B.
Sample Test Cases
|
Sample Input 1
17 5 |
Sample Output 1
20 |
|
Sample Input 2
10 5 |
Sample Output 2
10 |
|
Sample Input 3
1 3 |
Sample Output 3
3 |
|
Sample Input 4
100 7 |
Sample Output 4
105 |
|
Sample Input 5
99 10 |
Sample Output 5
100 |
Constraints
1
Explanation
int m = ((A + B - 1) / B) * B;
5
Percentage Change
Hard
Read original price O and new price N and print the percentage change rounded DOWN. change = ((N - O) * 100) / O (integer division, may be negative).
Input Format
A single line with two integers O and N.
Output Format
A single integer: the percentage change.
Sample Test Cases
|
Sample Input 1
100 120 |
Sample Output 1
20 |
|
Sample Input 2
50 40 |
Sample Output 2
-20 |
|
Sample Input 3
200 150 |
Sample Output 3
-25 |
|
Sample Input 4
10 15 |
Sample Output 4
50 |
|
Sample Input 5
1000 1000 |
Sample Output 5
0 |
Constraints
1
Explanation
int pct = (N - O) * 100 / O;
Competitive MCQs — Java Operators
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score
0/ 33
Q1
What is the output of 10 % 3?
Correct!
Wrong — correct answer is .
% gives the remainder: 10 / 3 leaves remainder 1.
Q2
Which operator increments a variable by 1 after using its current value?
Correct!
Wrong — correct answer is .
x++ uses then increments; ++x increments then uses.
Q3
What is the output of this code?
java
1
2
2
int x = 5;
System.out.println(x > 3 && x < 10);Correct!
Wrong — correct answer is .
Both conditions hold, so true is printed.
Q4
What is the output of this code?
int x = 5;
System.out.println(x > 3 && x < 10);
int x = 5;
System.out.println(x > 3 && x < 10);
Correct!
Wrong — correct answer is .
Both conditions hold, so true is printed.
Q5
Which operator is used for string concatenation?
Correct!
Wrong — correct answer is .
+ concatenates strings in Java.
Q6
What is the value of 15 / 4 in integer arithmetic?
Correct!
Wrong — correct answer is .
Integer division truncates the fraction: 15 / 4 = 3.
Q7
Which is the NOT operator in Java?
Correct!
Wrong — correct answer is .
! negates a boolean. ~ is bitwise NOT on integers.
Q8
What is the output of this code?
java
1
System.out.println(2 + 3 * 4);Correct!
Wrong — correct answer is .
* binds tighter than +: 2 + (3*4) = 14.
Q9
What is the output of this code?
System.out.println(2 + 3 * 4);
System.out.println(2 + 3 * 4);
Correct!
Wrong — correct answer is .
* binds tighter than +: 2 + (3*4) = 14.
Q10
Which operator checks whether two references point to the same object?
Correct!
Wrong — correct answer is .
== on objects compares references; equals() compares content.
Q11
What is the result of this code?
java
1
2
3
2
3
int x = 8;
x += 5;
System.out.println(x);Correct!
Wrong — correct answer is .
x += 5 adds 5 to x, giving 13.
Q12
What is the result of this code?
int x = 8;
x += 5;
System.out.println(x);
int x = 8;
x += 5;
System.out.println(x);
Correct!
Wrong — correct answer is .
x += 5 adds 5 to x, giving 13.
Q13
Which expression tests if a is NOT equal to b (primitives)?
Correct!
Wrong — correct answer is .
!= is the not-equal operator for primitives.
Q14
What is the output of this code?
int x = 5;
System.out.println(x % 2);
int x = 5;
System.out.println(x % 2);
Correct!
Wrong — correct answer is .
5 % 2 leaves remainder 1.
Q15
Which operator is used for logical OR?
Correct!
Wrong — correct answer is .
|| is short-circuit logical OR for booleans; | is bitwise.
Q16
What is the output of this code?
System.out.println(true && false);
System.out.println(true && false);
Correct!
Wrong — correct answer is .
AND is true only when both operands are true.
Q17
What does the ternary operator ? : do?
Correct!
Wrong — correct answer is .
cond ? a : b evaluates to a when cond is true, else b.
Q18
What is the output of this code?
int a = 5, b = 10;
int m = (a > b) ? a : b;
System.out.println(m);
int a = 5, b = 10;
int m = (a > b) ? a : b;
System.out.println(m);
Correct!
Wrong — correct answer is .
a > b is false, so the ternary selects b = 10.
Q19
Which operator shifts bits right and fills with the sign bit?
Correct!
Wrong — correct answer is .
>> is arithmetic right shift; >>> fills with zeros.
Q20
What is the output of this code?
System.out.println(5 & 3);
System.out.println(5 & 3);
Correct!
Wrong — correct answer is .
101 & 011 = 001, which is 1.
Q21
What is the output of this code?
System.out.println(5 | 3);
System.out.println(5 | 3);
Correct!
Wrong — correct answer is .
101 | 011 = 111, which is 7.
Q22
What is the output of this code?
System.out.println(5 ^ 3);
System.out.println(5 ^ 3);
Correct!
Wrong — correct answer is .
XOR of 101 and 011 is 110, which is 6.
Q23
Which operator tests equality for primitives?
Correct!
Wrong — correct answer is .
== compares primitive values; equals() compares objects.
Q24
What is the output of this code?
System.out.println(7 >= 7);
System.out.println(7 >= 7);
Correct!
Wrong — correct answer is .
>= is true when the left side is greater or equal.
Q25
What is the precedence outcome of this code?
System.out.println(true || false && false);
System.out.println(true || false && false);
Correct!
Wrong — correct answer is .
&& binds tighter than ||: true || (false && false) = true.
Q26
Which operator increments and then returns the new value?
Correct!
Wrong — correct answer is .
Pre-increment ++x raises x first, then yields the updated value.
Q27
What is the output of this code?
int x = 5;
int y = ++x;
System.out.println(y);
int x = 5;
int y = ++x;
System.out.println(y);
Correct!
Wrong — correct answer is .
++x increments x to 6 and assigns that value to y.
Q28
What is the output of this code?
int x = 5;
System.out.println(-x);
int x = 5;
System.out.println(-x);
Correct!
Wrong — correct answer is .
Unary minus negates the value.
Q29
Which operator checks type of an object?
Correct!
Wrong — correct answer is .
obj instanceof Type returns true when obj matches that type.
Q30
What is the output of this code?
System.out.println("a" instanceof String);
System.out.println("a" instanceof String);
Correct!
Wrong — correct answer is .
"a" is a String, so instanceof returns true.
Q31
What is the result of 10 % 4?
Correct!
Wrong — correct answer is .
10 divided by 4 leaves remainder 2.
Q32
What is the output of this code?
int x = 10;
System.out.println(x > 5 ? "big" : "small");
int x = 10;
System.out.println(x > 5 ? "big" : "small");
Correct!
Wrong — correct answer is .
10 > 5 is true, so the ternary returns "big".
Q33
Which operator compares object references?
Correct!
Wrong — correct answer is .
== on objects tests reference equality, not content.