📖 Scenario: You are working on a simple Java program that helps a cashier calculate the number of items left after selling some. You will use unary operators to increase or decrease the count.
🎯 Goal: Build a Java program that uses unary operators to update the count of items sold and items left.
📋 What You'll Learn
Create an integer variable for the initial number of items
Create an integer variable for the number of items sold
Use unary operators to increase and decrease counts
Print the final count of items left
💡 Why This Matters
🌍 Real World
Cashiers and inventory managers often need to update counts quickly when items are sold or restocked.
💼 Career
Understanding unary operators helps in writing concise code for counters, loops, and state changes in many software jobs.
Progress0 / 4 steps
1
Create initial item count
Create an integer variable called items and set it to 10.
Java
Hint
Use int to declare a whole number variable.
2
Create sold items count
Create an integer variable called sold and set it to 0.
Java
Hint
Initialize sold with zero because no items are sold yet.
3
Use unary operators to update sold count
Use the unary increment operator ++ to increase sold by 1.
Java
Hint
The ++ operator adds one to the variable.
4
Calculate and print items left
Use the unary decrement operator -- to decrease items by 1, then print the value of items.
Java
Hint
The -- operator subtracts one from the variable. Use System.out.println(items); to print.
Practice
(1/5)
1.
What does the unary operator ++ do in Java?
easy
A. Negates the value of a variable
B. Increases the value of a variable by 1
C. Decreases the value of a variable by 1
D. Checks if a value is true or false
Solution
Step 1: Understand the meaning of ++
The ++ operator adds 1 to the current value of a variable.
Step 2: Compare with other unary operators
-- subtracts 1, ! flips boolean, and negation changes sign.
Final Answer:
Increases the value of a variable by 1 -> Option B
Quick Check:
++ means add one [OK]
Hint: ++ means add one to a number quickly [OK]
Common Mistakes:
Confusing ++ with --
Thinking ++ negates the value
Mixing ++ with boolean negation !
2.
Which of the following is the correct **postfix** syntax to decrement a variable x by 1 using a unary operator?
int x = 5;
// Your code here
easy
A. x-1;
B. --x;
C. x--;
D. x=!1;
Solution
Step 1: Identify decrement unary operators
Both x-- and --x decrement by 1, but x-1 is an expression, not an operator.
Step 2: Choose correct syntax for decrement
x--; correctly decreases x by 1 after its current use.
Final Answer:
x--; -> Option C
Quick Check:
Use x-- to subtract one [OK]
Hint: Use x-- or --x to subtract one [OK]
Common Mistakes:
Using x-1; which doesn't change x
Confusing ! operator with decrement
Writing invalid syntax like x=!1;
3.
What is the output of the following Java code?
int a = 3;
System.out.println(++a);
System.out.println(a++);
System.out.println(a);
medium
A. 4 4 5
B. 3 4 5
C. 4 3 4
D. 3 3 4
Solution
Step 1: Evaluate ++a
++a increases a before printing, so prints 4 and a becomes 4.
Step 2: Evaluate a++
a++ prints current a (4) then increases it to 5.
Step 3: Print final value of a
After previous steps, a is 5, so prints 5.
Final Answer:
4 4 5 -> Option A
Quick Check:
Prefix ++ adds before print, postfix ++ adds after print [OK]
Hint: Prefix ++ changes before print, postfix ++ after print [OK]
Common Mistakes:
Mixing prefix and postfix effects
Forgetting that ++ changes the variable
Assuming no change in variable after print
4.
Find the error in the following code snippet:
boolean flag = true;
flag = !flag++;
System.out.println(flag);
medium
A. No error, code runs fine
B. Missing semicolon after flag declaration
C. Logical NOT operator ! cannot be used here
D. Cannot apply ++ to a boolean variable
Solution
Step 1: Check variable type and operators
flag is boolean; ++ operator works only on numeric types, not boolean.
Step 2: Identify invalid operation
Using flag++ causes a compile-time error because booleans cannot be incremented.
Final Answer:
Cannot apply ++ to a boolean variable -> Option D
Quick Check:
++ works only on numbers, not booleans [OK]
Hint: ++ cannot be used on boolean variables [OK]
Common Mistakes:
Trying to increment boolean values
Confusing logical NOT ! with increment ++
Assuming code compiles without error
5.
Given the code below, what will be the final value of count after execution?
int count = 0;
for (int i = 0; i < 3; i++) {
count = count++ + ++count;
}
System.out.println(count);