ProgrammingHardHCL · Tech Mahindra pattern
Assume 32-bit int. What is printed?
Assume 32-bit int. What is printed?
int permissions = 0;
#define READ 4
#define WRITE 2
#define EXEC 1
permissions |= READ | WRITE;
printf("%d ", permissions);
printf("%d ", (permissions & READ) != 0);
permissions &= ~WRITE;
printf("%d", permissions);- A. 6 1 4
- B. 6 0 4
- C. 7 1 5
- D. 6 1 2
Correct answer: A. 6 1 4
Setting READ(4)|WRITE(2) gives 6 (0110). Checking (permissions & READ) != 0 tests just bit 2 (the '4' bit), which is set, giving 1. Clearing WRITE with &= ~WRITE removes the '2' bit specifically, leaving just READ: 4.
More C Programming questions
- What happens when this runs? c int main(void) { int arr[3]; for (int i = 0; i < 3; i++) { printf("%d ", arr[i]); } }
- What happens when this runs? c void f(int p) { free(p); p = 10; } int main(void) { int x = malloc(sizeof(int)); f(x); printf("done"); }
- What happens when this runs? c int compute(int a, int b) { int result; if (a > b) { result = a - b; } return result; } int main(void) {…
- What happens when this runs? c int makeArray(void) { int local[10]; for (int i = 0; i < 10; i++) local[i] = i; return local; } int…
- What happens when this runs? c int arr[5] = {1, 2, 3, 4, 5}; int p = arr; int val = p[-1]; printf("%d", val);
- What happens when this runs? c int global; int main(void) { printf("%d", global); }