Friday, 17 October 2014

C Linux interview questions and answers

(1)What will be output if you will execute following program by gcc compiler in Linux?

#include<stdio.h>
int main(){
    int a=5;
    printf("%d %d %d",a++,a++,++a);
    return 0;
}

Output:
In LINUX GCC compiler
7 6 8
In TURBO C
7 6 6

Hints: In Turbo c parameter is passed from right to left in printf function but not in the Linux.


(2)What will be output if you will execute following program by gcc compiler in Linux?

#include<stdio.h>
int main(){
    int a=5,b=10,c=15,d=20;
    printf("%d %d %d");
        return 0;
}

Output:
In LINUX GCC compiler
Garbage values
In TURBO C
5 10 15

Hints: Local variables stores in the stack.

(3) What will be output if you will execute following program by gcc compiler in Linux?

#include<stdio.h>
int main(){
    int i=5,j=5,y;
    int x=++i + ++i + ++i;
    y=++j + ++j + ++j;
    printf("%d  %d  %d %d",x,y,i,j);
    return 0;
}

Output:
In LINUX GCC compiler
22  22  8  8
In TURBO C
21  24  8  8

(4) What will be output if you will execute following program by gcc compiler in Linux?

#include<stdio.h>
int main(){
        int  near *p;
        int  far *q;
        int  huge *r;
        printf("%d  %d  %d",sizeof(p),sizeof(q),sizeof(r));
        return 0;
}

Output:
In LINUX GCC compiler
Compilation error
In TURBO C
2  4  4

Note: In Linux there is not any concept of near, far and huge pointers

(5) What will be output if you will execute following program by gcc compiler in Linux?

#include<stdio.h>
int main(){
    char *p;
    int *q;
    float **r;
    printf("%d  %d  %d",sizeof(p),sizeof(q),sizeof(r));
    return 0;
}

Output:
In LINUX GCC compiler
4  4  4
In TURBO C
2  2  2

Hints: size of any type of pointer in Linux is 4 and in turbo c is 2.

(6) What will be output if you will execute following program by gcc compiler in Linux?

#include<stdio.h>
int main(){
    short int a=5;
    int b=5;
    long int c=5l;
    float d=5.0f;
    double e=5.0;
    long double f=5.0L;
    char g='5';
    printf("Size of short int: %d\n",sizeof(a));
    printf("Size of int: %d\n",sizeof(b));
    printf("Size of long int: %d\n",sizeof(c));
    printf("Size of float: %d\n",sizeof(d));
    printf("Size of double: %d\n",sizeof(e));
    printf("Size of long double: %d\n",sizeof(f));
    printf("Size of char: %d\n",sizeof(g));
        return 0;
}

Output:
In LINUX GCC compiler
Size of short int: 2
Size of int: 4
Size of long int: 4
Size of float: 4
Size of double: 8
Size of long double: 12
Size of char: 1
In TURBO C
Size of short int: 2
Size of int: 2
Size of long int: 4
Size of float: 4
Size of double: 8
Size of long double: 10
Size of char: 1

(7) What will be output if you will execute following program by gcc compiler in Linux?

#include<stdio.h>
    int main(){
        int a=300;
    char *p=(char *)&a;
        printf("%d\n",*p);
    printf("%d",*++p);
        return 0;
}

Output:
In LINUX GCC compiler
44
1
In TURBO C
44
1

(8) What will be output if you will execute following program by gcc compiler in Linux?

#include<stdio.h>
int main(){
    char c='A';
    printf("%d   %d",sizeof(c),sizeof('A'));
    return 0;
}

Output:
In LINUX
1  4
In TURBO C
1  2

(9) What will be output if you will execute following program by gcc compiler in Linux?

#include<stdio.h>
int main(){
    enum color{RED,BLUE,GREEN=-2,YELLOW,PINK};
    printf("%d  %d",BLUE,PINK);
    return 0;
}

Output:
In LINUX GCC compiler
1 0
In TURBO C
1 0

(10) What will be output if you will execute following program by gcc compiler in Linux?

#include<stdio.h>
int main(){
    char c=127;
    printf("%d",++c);
    printf("  %d",++c);
    return 0;
}

Output:
In LINUX GCC compiler
-128 -127
In TURBO C
-128 -127
Hints: char data type cyclic property.

(11) What will be output if you will execute following program by gcc compiler in Linux?

#include"stdio.h"
struct info1{
    char *title;
    long int size;
    double grade;
}hero1;
union info2{
        char *title;
        long int size;
        double grade;
}hero2;
int main(){
    printf("Size of structure: %d\n",sizeof(hero1));
    printf("Size of union:  %d",sizeof(hero2));
    return 0;
}

Output:
In LINUX GCC compiler
Size of structure: 16
Size of union:  8
In TURBO C
Size of structure: 14
Size of union:  8

(12) What will be output if you will execute following program by gcc compiler in Linux?

#define size(x) (char *)(x+1)-(char *)x
#include<stdio.h>
int main(){
    long int *p;
    long double *q;
        printf("Size of long int: %d\n",size(p));
    printf("Size of long double: %d",size(q));
        return 0;
}

Output:
In LINUX GCC compiler
Size of long int: 4
Size of long double: 12
In TURBO C
Size of long int: 4
Size of long double: 10

(13) What will be output if you will execute following program by gcc compiler in Linux?

#include<stdio.h>
int main(){
      int i=2,j=5,k=3;
      int a=i&&j>=k;
      printf("%d",a);
      return 0;
}

Output:
In LINUX GCC compiler
1
In TURBO C
1
Hints: Any conditional or relational operator returns 1 if condition is true otherwise it returns 0.



No comments:

Post a Comment