some tricky c interview questions to practice. Learn c and c++ programming interview skill. predicting output of sample program is way of refreshing your basic concepts.
1.what could be output of below c code?
#include <stdio.h> int main() { printf ("%x" ,-1<<4); return 0; }
output:
fffffff0
Technically left-shifting a negative integer invokes Undefined Behaviour. That means -1<<4 is UB C99 [6.5.7/4] says The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros. If E1 has an unsigned type, the value of the result is E1× 2E2, reduced modulo one more than the maximum value representable in the result type. If E1 has a signed type and nonnegative value, and E1× 2E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is un-defined.
2. write c code for the below problem.
Given a number n, give me the numbers (among 3..5 and an even number of numbers) whose adding would return the original number.
The resulting numbers should be as balanced as possible, meaning that instead of returning 3 and 5, for instance, return 4 and 4. Ex:
7 = 3 + 4
16 = 4 + 4 + 4 + 4 rather than 3 + 5 + 4 + 4
24 = 12 + 12 or 6 + 6 + 6 + 6
3. will the below code compile??
#include <stdio.h> #include <time.h> int f() { return 2; } int main() { static int i = f(); printf("%d\n", i); }
ans: NO, test_1.c:11:5: error: initializer element is not constant static int i = f(); ^ For all static variables (and global variables), the compiler must ensure that they are initialized before main() is called. Even static variables that aren't explicitly set to a value by the programmer, must be initialized to zero or NULL
4. What’s the output of the following program?
#include <stdio.h> void Unknown(int *p, int num); void HardToFollow(int *p, int q, int *num); void Unknown(int *p, int num) { int *q; q = # *p = *q + 2; num = 7; } void HardToFollow(int *p, int q, int *num) { *p = q + *num; *num = q; num = p; p = &q; Unknown(num, *p); } int main() { int *q; int trouble[3]; trouble[0] = 1; q = &trouble[1]; *q = 2; trouble[2] = 3; HardToFollow(q, trouble[0], &trouble[2]); Unknown(&trouble[0], *q); printf("%d %d %d \n", *q,*trouble,*(trouble+2)); return 0; }
5. Write a c program to find size of structure without using sizeof operator?
#include <stdio.h> struct foo { float y; char z; }; int main() { struct foo p; struct foo *q = &p; int size = (char*)(q+1) - (char*)q; printf("%d\n",size); return 0; }
6. How can you find if a variable is allocated in stack or heap?
7. What could be the output of below program?
#include<stdio.h> int main() { printf("%lu\n",sizeof(2.4)); return 0; }
8. Add two numbers without using + or – operator in c.
#include<stdio.h> int add(int, int); int main() { int a,b,sum; printf("enter two numbers:"); scanf("%d %d",&a,&b); sum=add(a,b); printf("%d\n",sum); return 0; } int add(int a, int b) { int sum, carry; if (b == 0) return a; else sum = a ^ b; // add without carrying carry = (a & b) << 1; // carry, but don’t add return add(sum, carry); // recursion }
Ref:
https://www.quora.com/How-do-I-add-two-numbers-without-using-any-operator-C-C++
WE RECOMMEND RELATED POST
The post some tricky c interview questions to practice appeared first on wikistack.