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

Java Class 9 Worksheet on Operators

Uploaded by

sidshah925
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)
124 views2 pages

Java Class 9 Worksheet on Operators

Uploaded by

sidshah925
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

Java ICSE Class 9 Worksheet

Increment and Decrement Operators

1. Output-Based Questions

Q1.

int a = 5;

[Link](a++);

[Link](a);

Output: ___________

Q2.

int b = 10;

[Link](++b);

[Link](b);

Output: ___________

Q3.

int x = 7, y = x++;

[Link]("x = " + x + ", y = " + y);

Output: ___________

Q4.

int p = 4, q = ++p + p++;

[Link]("p = " + p + ", q = " + q);

Output: ___________

2. True or False

Q5. Post-increment means the value is increased after it is used.

Ans: ___________

Q6. ++a is post-increment.

Ans: ___________
Java ICSE Class 9 Worksheet
Increment and Decrement Operators

3. Fill in the blanks

Q7. int a = 6; int b = ___;

If b should get the value 7, using post-increment on a, fill the blank.

Q8. After executing:

int a = 3;

int b = ++a;

a = ___, b = ___

4. Predict the Output

Q9.

int i = 5;

int j = i++ + ++i;

[Link]("i = " + i + ", j = " + j);

Output: ___________

Q10.

int x = 10;

x = x++ + ++x;

[Link](x);

Output: ___________

Common questions

Powered by AI

Initially, 'a' is set to 3. The pre-increment operation '++a' increases 'a' to 4, and then assigns this new value to 'b'. Thus, both 'a' and 'b' end up being 4 after execution.

The statement is false. '++a' represents a pre-increment operation, meaning the variable 'a' is incremented first and then used, unlike a post-increment which is 'a++'.

Initially, 'i' is 5. In the expression 'i++ + ++i', the 'i++' uses the value 5 and increments 'i' to 6. The '++i' then increments 'i' to 7 before returning it as part of the expression. Hence, 'j' equals 5 + 7, resulting in 12. After all operations, 'i' is left as 7.

The final value printed first is 5 because the post-increment operator 'a++' returns the current value of 'a' before incrementing. The subsequent println outputs 6, which is the incremented value of 'a'. Therefore, the final values are 5 and 6 respectively.

The pre-increment operator '++b' first increments the value of 'b' before it's used. Therefore, the first output is 11. The second System.out.println(b) also outputs 11, as 'b' remains unchanged after the first increment and use.

Initially, 'x = 7' and 'y' is set to 'x++'. In this case, 'y' is assigned the current value of 'x' (which is 7) before 'x' is incremented. After the post-increment operation, 'x' becomes 8. Hence, the printed values are: x = 8, y = 7.

The pre-increment operator '++p' increases the value of 'p' from 4 to 5. Then 'q = 5 + 5', as 'p++' uses the value 5 before post-incrementing. After this, 'p' becomes 6. Thus, 'q' is 10, and the final values are: p = 6, q = 10.

Initially, 'x' is 10. In the expression 'x++ + ++x', the 'x++' uses 10 but increments 'x' to 11 after evaluation. Then '++x' increments it to 12. Therefore, the result 'x' receives is 10 + 12, which evaluates to 22. As a result, the final value of 'x' becomes 22.

This statement is true because, in a post-increment operation, the current value is used or returned first, and then the increment is applied. This behavior can be exemplified by how 'x++' functions compared to '++x'.

To assign 'b' the value 7 using a post-increment operator, the variable 'b' should be declared as: int b = a++. This would initially assign the current value of 'a' (which is 6) to 'b', and then increment 'a' to 7. Since the end goal is to have 'b' equal 7, the operation needs additional context as the post increment alone in one step won't achieve this without logic to add the increment prior.

You might also like