Interview Questions
- Puzzle 1 : Guess the output of the puzzle below?
- Puzzle : Guess the output of the puzzle below?
- Guess the output of the puzzle below?
- Guess the output of the puzzle below?
- Guess the output of the puzzle below?
- Guess the output of the puzzle below?
- Guess the output of the puzzle below?
- Guess the output of the puzzle below?
- Guess the output of the puzzle below?
- Guess the output of the puzzle below?
- Guess the output of the puzzle below?
- Guess the output of the puzzle below?
Puzzle 1 : Guess the output of the puzzle below?
#include <stdio.h> int main() { printf("%d", 5 + 3 * 4); return 0; }
Output is shown in the comment below with explanation:
#include <stdio.h> int main() { printf("%d", 5 + 3 * 4); //17 //Executed as 5 + (3 * 4) because * has higher precedence return 0; }
Puzzle : Guess the output of the puzzle below?
#include <stdio.h> int main() { printf("%d\n", 5 * 2 / 10); printf("%d\n", 5 * (2 / 10)); printf("%d\n", (5 * 2) / 10); return 0; }
Output is shown in the comment below with explanation:
#include <stdio.h> int main() { //Executed as 5 + (3 * 4) because * has higher precedence printf("%d\n", 5 * 2 / 10); //1 executed as (5 * 2)/10 printf("%d\n", 5 * (2 / 10)); //0. 2/10 is integer operation. Result is 0. printf("%d\n", (5 * 2) / 10); //1 return 0; }
Guess the output of the puzzle below?
#include <stdio.h> void increment(){ static int staticVariable=5; staticVariable++; printf("staticVariable:%d\n",staticVariable); } int main() { increment(); increment(); }
Output:
staticVariable:6 staticVariable:7
The value of a static variable is retained between function calls.
Guess the output of the puzzle below?
#include <stdio.h> struct Point{ int x; int y; }; void modify(struct Point p) { p.x = 50; } int main() { struct Point p1; p1.x = 20; modify(p1); printf("p1.x:%d",p1.x);//p1.x:20 }
Output is shown in the comment below with explanation:
#include <stdio.h> struct Point{ int x; int y; }; //when this function is invoked, the value of p1 is copied into //a new instance of the structure and it is named as p void modify(struct Point p) { //here the new instance is modified. the original structure is unchanged. p.x = 50; } int main() { struct Point p1; p1.x = 20; modify(p1);//copy of p1 is passed //p1.x is not changed in modify printf("p1.x:%d",p1.x);//p1.x:20 }
Guess the output of the puzzle below?
int main() { int i=10; printf("BEFORE BLOCK i:%d\n",i); { //block int i=15; printf("IN THE BLOCK i:%d\n",i); } printf("AFTER BLOCK i:%d\n",i); }
Output is shown in the comment below with explanation:
int main() { int i=10; printf("BEFORE BLOCK i:%d\n",i);//BEFORE BLOCK i:10 { //block int i=15;//i overshadows the variable in main printf("IN THE BLOCK i:%d\n",i);//IN THE BLOCK i:15 } printf("AFTER BLOCK i:%d\n",i);//AFTER BLOCK i:10 }
Guess the output of the puzzle below?
#include <stdio.h> void swap(int i, int j) //Definition of Function { int temp = i; i = j; j = temp; } int main() { int a=5, b=6; swap(a,b); printf("a:%d,b:%d",a,b); }
Output is shown in the comment below with more explanation: Program above does NOT swap variables because in C, everything is pass by value. The value of variable a,b are copied into new variables i,j and the values of i and j are swapped in the function. However, the original values remain unchanged.
#include <stdio.h> //New variables i and j are created //Their values are modified //Arguments a and b are NOT modified. void swap(int i, int j) //Definition of Function { int temp = i; i = j; j = temp; } int main() { int a=5, b=6; //when the function swap is invoked, //values of a and b are copied to //the variables i and j swap(a,b); printf("a:%d,b:%d",a,b);//a:5,b:6 }
Guess the output of the puzzle below?
#include <stdio.h> void swap(int *pi, int *pj) //Definition of Function { int temp = *pi; *pi = *pj; *pj = temp; } int main() { int a=5, b=6; swap(&a,&b); printf("a:%d,b:%d",a,b); }
Output is shown in the comment below. In the program above, the values a and b are swapped. The address of the variables a and b are passed to the function. So, the function modifies the original values.
#include <stdio.h> void swap(int *pi, int *pj) //Definition of Function { int temp = *pi; *pi = *pj; *pj = temp; } int main() { int a=5, b=6; swap(&a,&b); printf("a:%d,b:%d",a,b);//a:6,b:5 }
Guess the output of the puzzle below?
#include <stdio.h> void modify(int marks[]) //Definition of Function { marks[0] = 100; } int main() { int marks[] = {95,96,97}; modify(marks); printf("first:%d",marks[0]); }
Output is shown in the comment below. When an array is passed, the address of the first element of array is passed. So, the actual array is modified. Value of first (index 0) element is modified to 100.
#include <stdio.h> void modify(int marks[]) //Definition of Function { marks[0] = 100; } int main() { int marks[] = {95,96,97}; modify(marks); printf("first:%d",marks[0]);//first:100 }
Guess the output of the puzzle below?
#include <stdio.h> void main() { int marks[] = {35,45,67}; printf("%d\n",*marks); printf("%d\n",*(marks+1)); printf("%d\n",*(marks+2)); printf("%p\n",marks); }
Output is shown in the comment below: An array variable stores the address of where the first element(index 0) of the array is stored. Hence an array variable is actually a pointer. In the above example marks is similar to a pointer.
Program below shows the different pointer operations that can be done on an array.
int main() { int marks[] = {35,45,67}; printf("%d\n",*marks);//35 printf("%d\n",*(marks+1));//45 printf("%d\n",*(marks+2));//67 printf("%p\n",marks);//0x7fff57a3baac }
Guess the output of the puzzle below?
#include <stdio.h> int main() { char* textptr = "SomethingIsBrewing"; char textarr[] = "SomethingIsBrewing"; printf("sizeof(textptr):%d sizeof(textarr):%d",sizeof(textptr),sizeof(textarr)); }
Output is shown in the comment below with explanation: textptr is a pointer (size : 8 or 4 based on the memory size of the execution environment) and can be changed. textarr is a constant (value cannot be changed). Doing sizeof(textarr) returns the size of the array (19 in the example below. including 1 character for null character at end of array).
#include <stdio.h> int main() { char* textptr = "SomethingIsBrewing"; char textarr[] = "SomethingIsBrewing"; printf("sizeof(textptr):%d sizeof(textarr):%d",sizeof(textptr),sizeof(textarr));//sizeof(textptr):8 sizeof(textarr):19 }
Guess the output of the puzzle below?
#include <stdio.h> #include <stdlib.h> int main() { if(-1) { printf("if(-1) is true"); } }
Output is shown in the comment below : printf in the if condition is executed. -1 is also non zero. So, it is true. Even negative numbers represent a true condition in an if.
#include <stdio.h> #include <stdlib.h> int main() { if(-1) { printf("if(-1) is true");//if(-1) is true } }
Guess the output of the puzzle below?
#include <stdio.h> #include <stdlib.h> int main() { int i=4,j=7; if(i==3)//FIRST IF if (j==6)//SECOND IF printf("i==3 j==6"); else printf("i!=3"); }
No Output. Nothing is printed to the output. The formatting of above program is not the same as the execution. The else part belongs to the second if. The correct formatting of the program is as below.