ICSE Class 10 Computer Applications Question 21 of 30

Solved Sample Paper 3 — Question 21

Back to all questions
21
Question

Question 2(i)

Write the values of r and s after the execution of the following code.

int p = 11, q = 21, r, s; 
r = ++q;
s = p++; 
r++;
Answer

After execution of the given code, r = 23 and s = 11.

Explanation
StatementRemarks
int p = 11, q = 21, r, s;p = 11, q = 21, r and s are declared
r = ++q;r = 22 (prefix operator first increments the value and then uses it)
s = p++;s = 11 (postfix operator first uses the value and then increments it)
r++;r = 23 (value of r is incremented by 1)