C Aptitude Questions
C Aptitude Questions
void main(){
int i = 4, j = 8;
i = i|j & j|i + i|j & j|i - i^j;
j = i|i & j|j + j|j & i|i - j^j;
printf("%d %d %d\n", i|j & j|i, i|j &j|i, i^j);
}
so the expression
i = i|j & j|i + i|j & j|i – i^j; -> i = 4|8 & 8|4 + 4|8 & 8|4 – 4^8; -> 4|8 & 8| 8 |8 & 8| 0 ^8; -> 4 | 8 | 8 | 8 | 0 ^8
next expression
j = i|i & j|j + j|j & i|i – j^j; -> 12|12 & 8|8 + 8|8 & 12|12 – 8^8 -> 12|12 & 8| 16 |8 & 12| 4 ^8 ->
i|j & j|i -> 12|28 & 28|12 -> 12| 28 | 12 -> 12|28 -> 28
`b` has became 0 so there wont be a next iteration so the final value of a would be `21`
3. void main(){
int a[3][4] = {2, 4, 6, 8, 10, 12, 12, 10, 8, 6, 4, 2};
int i = 0, j, k = 13;
while(i < 3){
for(j =0; j < 4; j++){
if(a[i][j] > k)
k = a[i][j];
}
i++;
}
printf("%d\n", k);
}
As the value of k is greater than the values of all elements in the matrix `a` the if block is not executed
so the value of k would be 13 and it does not change
void main(){
int i = 7547;
int k;
k = find(i);
}
Solution:
void main(){
*f() = 12;
printf("%d %d %d", a[0], a[1], a[2]);
}
Solution :
The program begins its execution from the main()
This makes a function call to the function f with return type of an integer pointer
The function returns the reference of a + 1 {a means the a[0]th reference so a + 1 means a[1]’s reference}
so the first line becomes *(a + 1) = 12 -> a[1] = 12
6. void main(){
int n;
for(n = 5; n > 0; n--){
printf("%d", n--);
}
}
Solution:
since the decrement is postfix the value of i is printed first and then i is decremented
7. void main(){
int c[] = {1, 2, 3, 4};
int j, *q = c;
for(j = 0; j<4;j++){
printf("%d", *c);
++q;
}
}
The output is
1111
Explanation:
bonus point : if you are trying to dereferrence *(c + 1) -> this means *(c + 1 *(sizeof(int))
8. #include<string.h>
void main(){
int nf, i, j, c, m;
char str[] = {"Zoho Corporation - Chennai"};
int length = strlen(str);
i = 0, m = 0, c = 0;
Explanation:
`o`This is a program used to count the no of `0` from a word which has the maximum `o` so the ans would
be from the word Corporation which has 3 `o`s
So the output is 3