• Post author:
  • Post category:Blog
  • Reading time:2 mins read
  • Post last modified:June 12, 2024

What is the value of the var variable after the execution of the following snippet of code:

int var = 2;
var = 4
var = var + 6;
var = var + var;
  • 20
  • 24
  • 22
  • 10
Explanation & Hints:

Let’s analyze the code snippet and the operations performed on the variable var to determine its final value:

  1. int var = 2; initializes var with the value 2.
  2. var = 4; changes the value of var to 4.
  3. var = var + 6; adds 6 to the current value of var (which is 4), making it 10.
  4. var = var + var; adds the current value of var to itself, so 10 + 10 = 20.

Therefore, after the execution of the code snippet, the value of var is 20. Hence, the correct answer is 20.

For more Questions and Answers:

Introduction to computer programming, variables, and comments Module 1 Test Answers Full 100%

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments