Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

C++ Institute CLA-11-03 Exam - Topic 2 Question 4 Discussion

What happens if you try to compile and run this program?#include int main (int argc, char *argv[]) {int i = 2;int d= i
B) The program outputs 4
A) The program outputs 0
C) The program outputs 1
D) The program outputs 2
E) Compilation fails

C++ Institute CLA-11-03 Exam - Topic 2 Question 4 Discussion

Actual exam question for C++ Institute's CLA-11-03 exam
Question #: 4
Topic #: 2
[All CLA-11-03 Questions]

What happens if you try to compile and run this program?

#include

int main (int argc, char *argv[]) {

int i = 2;

int d= i << 2;

d /= 2;

printf ("%d", d) ;

return 0;

}

Choose the right answer:

Show Suggested Answer Hide Answer
Suggested Answer: B

The program outputs 4 because the expression i << 2 performs a left shift operation on the binary representation of i, which is 00000010, by two bits, resulting in 00001000, which is equivalent to 8 in decimal. Then, the expression d /= 2 performs a division assignment operation, which divides d by 2 and assigns the result back to d, resulting in 4. The printf function then prints the value of d as a decimal integer using the %d format specifier.

Reference = CLA -- C Certified Associate Programmer Certification, [C Essentials 2 - (Intermediate)], C Bitwise Operators, C Assignment Operators


Contribute your Thoughts:

0/2000 characters

Currently there are no comments in this discussion, be the first to comment!


Save Cancel