If i = 0, int j= (i++) + (++i) find the value of j
Correct answer: C. j = 2
- A. j = 21
- B. j = 20
- C. j = 2
- D. i = 23
Explanation
The correct option is (c) j = 2.Let's break down the expression to understand how we arrive at the answer:i = 0: This assigns the initial value of 0 to the variable i.(i++): This part uses the post-increment operator. The value of i (which is 0 here) is used in the expression first, and then i is incremented by 1. So, in this case, the value used is 0.(++i): This part uses the pre-increment operator. Here, i is incremented by 1 first (which becomes 1 from the previous step), and then the incremented value (1) is used in the expression.Now, let's evaluate the expression step by step:(i++) + (++i) becomes (0) + (1).Since the post-increment operator uses the value before incrementing, i remains 0 in the first part.The pre-increment operator increments i to 1 before using its value in the second part.Therefore, j = 0 + 1.So, the final value of j is 2.
Report an error
The more specific you are, the faster it gets fixed. A source beats an opinion.
Prefer email? [email protected]
About Programming Basics
Programming basics covers algorithms, flowcharts, pseudocode, variables, constants, data types, operators, input and output, conditional statements and loops. It also introduces functions, arrays, strings, debugging and common programming errors, with attention to the difference between syntax errors, runtime errors and logical errors in a program.
Practise Programming Basics
183 free Programming Basics MCQs from Computer Science and IT, each with the correct answer and an explanation. Unlimited attempts, no account needed.
Discussion
Stuck on an option, or know a faster way to get there? Ask or explain it here.
No comments yet. Be the first to explain this one.
Exams that ask Computer Science and IT questions like this
Computer Science and IT is on 85 papers prepared for on TestUstad, and all of them draw the same bank, so this question is worth knowing for every one of them.
More Programming Basics questions
Which of the following cannot be included in variable and constant names in most programming languages?
This means to increase a value by one
The expression 10 % 3 returns a value of:
A relational expression is false, it has the value of?
Omitting which ending letter will cause the whole program to become comments?
Which of the following is not a valid variable name?