Monday, January 5, 2009

C programming Question with outputs



53. For the following C program

void main()

{

unsigned char c;

for(c=0;c!=256;c=c+2)

printf("%d",c);

getch();

}

 


(a) 127
(b) 128
(c) 256
(d) infinitely
Ans: (d)

54. For the following program

int i;
i=0;
repeat
i=i+1; <====== PASCAL PROGRAM
print i;
until(i<10)
end

No. of times the loop is executed?

55. For the following program

Convert (int A,var ,int B;int c)
{A=10;
B=4-;
C=120;
}

Convert (inta,b,c)
{ <====== PASCAL PROGRAM
a=1;
b=4;
c=12;
}


56. For the following program

Procedure A
Begin
--------
end <====== PASCAL PROGRAM

Procedure B No. Of errors in the program ?(1,2,3,none)

Begin
-----------
end


57. For the following program

int i;
i=2;
i++;
if(i==4)
{printf(i=4);
}
else
{printf(i=3);
}

Output of the program ?

a) 4
b) 3
c) unpredictable
d) none

Ans. (b)

58. What is FAT?.

a) File Allocation Table
b) File Access Table
c) FDD Allocation Table
d) None of the above

Ans. (a)

. What does the following program print?
                            #include  
                            int sum,count;
                            void main(void)
                            {<>                               for(count=5;sum+=--count;)
                                printf("%d",sum);
                            }
        a. The pgm goes to an infinite loop                    b. Prints 4791010974                        c. Prints 4791001974
        d. Prints 5802112085                                      e. Not sure

2. What is the output of the following program?
                            #include
                            void main(void)
                            {
                                int i;<>                               for(i=2;i<=7;i++)
                                printf("%5d",fno());
                            }
                            fno()
                            {
                                staticintf1=1,f2=1,f3;
                               return(f3=f1+f2,f1=f2,f2=f3);
                            }
        a. produce syntax errors                    b. 2 3 5 8 13 21 will be displayed                    c. 2 2 2 2 2 2 will be displayed
        d. none of the above                          e. Not sure

3. What is the output of the following program?
                            #include
                            void main (void)
                            {
                                int x = 0x1234;
                                int y = 0x5678;
                                x = x & 0x5678;
                                y = y | 0x1234;
                                x = x^y;
                                printf("%x\t",x);
                                x = x | 0x5678;
                                y = y & 0x1234;
                                y = y^x;
                                printf("%x\t",y); 
                            }
        a. bbb3   bbb7                                        b. bbb7   bbb3                                            c. 444c   4448
        d. 4448   444c                                        e. Not sure

4. What does the following program print?
                            #include
                            void main (void)
                            {
                                int x;
                                x = 0;
                                if (x=0)
                                printf ("Value of x is 0");
                                else
                                printf ("Value of x is not 0");
                            }
        a. print value of x is 0                b. print value of x is not 0                    c. does not print anything on the screen
        d. there is a syntax error in the if statement                                           e. Not sure

5. What is the output of the following program?
                            #include
                            #include
                            int foo(char *);
                            void main (void)
                            {
                                char arr[100] = {"Welcome to Mistral"};
                                foo (arr);
                            }
                            foo (char *x)
                            {
                                printf ("%d\t",strlen (x));
                                printf ("%d\t",sizeof(x));
                                return0;
                            }
        a. 100  100                        b. 18  100                       c. 18  18                    d. 18  2                        e. Not sure

6. What is the output of the following program?
                            #include
                            display()
                            {
                                printf ("\n Hello World");
                                return 0;
                            }
                            void main (void)
                            {
                                int (* func_ptr) ();
                                func_ptr = display;
                                printf ("\n %u",func_ptr);
                                (* func_ptr) (); 
                            }
        a. it prints the address of the function display and prints Hello World on the screen
        b. it prints Hello World two times on the screen
        c. it prints only the address of the fuction display on the screen 
        d. there is an error in the program                                e. Not sure

7. What is the output of the following program?
                            #include
                             void main (void)
                            {
                                int i = 0;
                                char ch = 'A';
                                do 
                                    putchar (ch);
                                while(i++ < 5 || ++ch <= 'F');
                            }
        a. ABCDEF will be displayed                                                                    b. AAAAAABCDEF will displayed 
        c. character 'A' will be displayed infinitely                    d. none                    e. Not sure

8. What is the output of the following program?
                            #include
                            #define sum (a,b,c) a+b+c
                            #define avg (a,b,c) sum(a,b,c)/3
                            #define geq (a,b,c) avg(a,b,c) >= 60
                            #define lee (a,b,c) avg(a,b,c) <= 60
                            #define des (a,b,c,d) (d==1?geq(a,b,c):lee(a,b,c))
                            void main (void)
                            {
                                int num = 70;
                                char ch = '0';
                                float f = 2.0;
                                if des(num,ch,f,0) puts ("lee..");
                                else puts("geq...");
                            }
        a. syntax error                            b. geq... will be displayed                                c. lee.. will be displayed
        d. none                                       e. Not sure

9. Which of the following statement is correct?
        a. sizeof('*') is equal to sizeof(int)                                                                b. sizeof('*') is equal to sizeof(char)
        c. sizeof('*') is equal to sizeof(double)                    d. none                          e. Not sure

10. What does the following program print?
                            #include
                            char *rev(int val);
                            void main(void)
                            {
                                extern char dec[];
                                printf ("%c", *rev);
                            }
                            char *rev (int val)
                            {
                                char dec[]="abcde";
                                return dec;
                            }
        a. prints abcde                                                                                        b. prints the address of the array dec
        c. prints garbage, address of the local variable should not returned            d. print a                    e. Not sure

11. What does the following program print?
                            void main(void)
                            {
                                int i;
                                static int k;
                                if(k=='0')
                                    printf("one");
                                else if(k== 48)
                                    printf("two");
                                else
                                    printf("three");
                            }
        a. prints one                                                b. prints two                                         c. prints three
        d. prints one three                                       e. Not sure

12. What does the following program print?
                            #include
                            void main(void)
                            {
                                enum sub
                                {
                                    chemistry, maths, physics
                                };
                                struct result
                                {
                                    char name[30];
                                    enum sub sc;
                                };
                                struct result my_res;
                                strcpy (my_res.name,"Patrick");
                                my_res.sc=physics;
                                printf("name: %s\n",my_res.name);
                                printf("pass in subject: %d\n",my_res.sc);
                            }
        a. name: Patrick                                    b. name: Patrick                                    c. name: Patrick
            pass in subject: 2                                   pass in subject:3                                   pass in subject:0
        d. gives compilation errors                     e. Not sure

13. What does 
                            printf("%s",_FILE_); and printf("%d",_LINE_); do?
        a. the first printf prints the name of the file and the second printf prints the line no: of the second printf in the file
        b. _FILE_ and _LINE_ are not valid parameters to printf function
        c. linker errors will be generated                    d. compiler errors will be generated                        e. Not sure

14. What is the output of the following program?
                            #include
                            void swap (int x, int y, int t)
                            {
                                t = x;
                                x = y;
                                y = t;
                                printf ("x inside swap: %d\t y inside swap : %d\n",x,y);
                            }
                            void main(void)
                            {
                                int x;
                                int y;
                                int t;
                                x = 99;
                                y = 100;
                                swap (x,y,t);
                                printf ("x inside main:%d\t y inside main: %d",x,y);
                            }
a. x inside swap : 100    y inside swap : 99    x inside main : 100    y inside main : 99
b. x inside swap : 100    y inside swap : 99    x inside main : 99      y inside main : 100
c. x inside swap : 99      y inside swap : 100    x inside main : 99     y inside main : 100
d. x inside swap : 99      y inside swap : 100    x inside main : 100    y inside main : 99
e. Not sure

15. Consider the following statements:
                 i) " while loop " is top tested loop                 ii) " for loop " is bottom tested loop
                iii) " do - while loop" is top tested loop          iv) " while loop" and "do - while loop " are top tested loops.
    Which among the above statements are false?
        a. i only                        b. i & ii                        c. iii & i                        d. ii, iii & iv                        e. Not sure

16. Consider the following piece of code:
                            char *p = "MISTRAL";
                            printf ("%c\t", *(++p));
                            p -=1;
                            printf ("%c\t", *(p++));
    Now, what does the two printf's display?
        a. M M                        b. M I                          c. I M                           d. M S                             e. Not sure


17. What does the following program print?
                            #include
                            struct my_struct
                            {
                                int p:1;
                                int q:1;
                                int r:6;
                                int s:2;
                            };
                            struct my_struct bigstruct;
                            struct my_struct1
                            {
                                char m:1;
                            };
                            struct my_struct1 small struct;
                            void main (void)
                            {
                                printf ("%d %d\n",sizeof (bigstruct),sizeof (smallstruct));
                            }
        a. 10  1                        b. 2  2                        c. 2  1                        d. 1  1                        e. Not sure

18. Consider the following piece of code:
                            FILE *fp;
                            fp = fopen("myfile.dat","r");
    Now fp points to
        a. the first character in the file.
        b. a structure which contains a char pointer which points to the first character in the file.
        c. the name of the file.                                d. none of the above.                                    e. Not sure.

19. What does the following program print?
                            #include
                            #define SQR (x) (x*x)
                            void main(void)
                            {
                                int a,b=3;
                                a = SQR (b+2);
                            }
        a. 25                            b. 11                            c. 17                                d. 21                            e. Not sure.

20. What does the declaration do?
                            int (*mist) (void *, void *);
        a. declares mist as a function that takes two void * arguments and returns a pointer to an int.
        b. declares mist as a pointer to a function that has two void * arguments and returns an int.
        c. declares mist as a function that takes two void * arguments and returns an int.
        d. there is a syntax error in the declaration.                                        e. Not sure.


21. What does the following program print?
                            #include
                            void main (void)
                            {
                                int mat [5][5],i,j;
                                int *p;
                                p = & mat [0][0];
                                for (i=0;i<5;i++)
                                    for (j=0;j<5;j++)
                                        mat[i][j] = i+j;
                                printf ("%d\t", sizeof(mat)); <>                               i=4;j=5;
                                printf( "%d", *(p+i+j));
                            }
        a. 25  9                        b. 25  5                        c. 50  9                        d. 50  5                        e. Not sure

22. What is the output of the following program?
                            #include
                            void main (void)
                            {
                                short x = 0x3333;
                                short y = 0x4321;
                                long z = x;
                                z = z << 16;
                                z = z | y;
                                printf("%1x\t",z);
                                z = y;
                                z = z >> 16;
                                z = z | x;
                                printf("%1x\t",z);
                                z = x;
                                y = x && y;
                                z = y;
                                printf("%1x\t",z);
                            }
        a. 43213333     3333     1                    b. 33334321     4321     4321                    c. 33334321     3333     1
        d. 43213333     4321     4321              e. Not sure

23. What is the output of the following program?
                            #include
                            void main (void)
                            {
                                char *p = "Bangalore";
                                #if 0
                                    printf ("%s", p);
                                #endif
                            }
        a. syntax error #if cannot be used inside main function                                b. prints Bangalore on the screen
        c. does not print anything on the screen
        d. program gives an error "undefined symbol if"                                          e. Not sure

24. If x is declared as an integer, y is declared as float, consider the following expression:
                            y = *(float *)&x;
    Which one of the following statments is true?
        a. the program containing the expression produces compilation errors;
        b. the program containing the expression produces runtime errors;
        c. the program containing the expression compiles and runs without any errors;
        d. none of the above                                        e. Not sure

25. What is the return type of calloc function?
        a. int *                                b. void *                                        c. no return type: return type is void
        d. int                                   e. Not sure

1.Max value of SIGNED int


2. A long C program is given -- try to be familiar with few of the concepts listed below

int *num={10,1,5,22,90};
main()
{
int *p,*q;
int i;
p=num;
q=num+2;
i=*p++;
print the value of i, and q-p, and some other operations are there.
}
how the values will change?

 

3. One pointer diff is given like this:

int *(*p[10])(char *, char*)

Explain the variable assignment

 

4. char *a[4]={"jaya","mahe","chandra","buchi"};
    What is the value of sizeof(a) /sizeof(char *)

 

5. For the following C program

void fn(int *a, int *b)
{int *t;
t=a;
a=b;
b=t;
}

main()
{int a=2;
int b=3;
fn(&a,&b);
printf("%d,%d", a,b);
}

What is the output?

a) Error at runtime
b) Compilation error
c) 2 3
d) 3 2

 

6. For the following C program

#define scanf "%s is a string"
main()
{printf(scanf,scanf);
}

What is the output.

Ans. %s is string is string

 

7. For the following C program

{char *p="abc";
char *q="abc123";
while(*p=*q)
print("%c %c",*p,*q);
}

a) aabbcc
b) aabbcc123
c) abcabc123
d) infinate loop

 

8. What is the value of the following:

printf("%u",-1)

a) -1
b) 1
c) 65336

 

9. For the following C program

#define void int
int i=300;
void main(void)
{int i=200;
{int i=100;
print the value of i;}
print the value of i;}

What is the output?

 

10. For the following C program

int x=2;
x=x<<2;
printf("%d ",x);

Ans. 8


11. For the following C program

int a[]={0,0X4,4,9}; /*some values are given*/
int i=2;
printf("%d %d",a[i],i[a]);

What is the value?

 

SATYM PLACEMENT QUESTION WITH SOLUTION on c

 

Which of following operator can't be overloaded.

==
b) ++
c) ?!
d) <=


2. For the following C program

#include
main()
{printf("Hello World");}

The program prints Hello World without changing main() ,the output should
be

intialisation
Hello World
Desruct

The changes should be

a) IOstream operator<<(iostream os, char*s)
     os<<'intialisation'<<(Hello World)<b)
c)
d) none of the above

6. For the following C program:

swap(int x,y)
{ int temp;
temp=x;
x=y;
y=temp;}

main()
{intx=2;y=3;
swap(x,y);}

After calling swap, what are the values x & y?

18. For the following C program

struct base {int a,b;
base();
int virtual function1();}

struct derv1:base
{int b,c,d;
derv1()
int virtual function1();}

struct derv2 : base
{int a,e;
}
base::base()
{a=2;b=3;
}
derv1::derv1()
{b=5;
c=10;d=11;}
base::function1()
{return(100);
}
derv1::function1()
{
return(200);
}

main()
base ba;
derv1 d1,d2;
printf("%d %d",d1.a,d1.b)

Output of the program is:

a)a=2;b=3;
b) a=3; b=2;
c) a=5; b=10;
d) none


19. For the above program answer the following q's

main()
base da;
derv1 d1;
derv2 d2;
printf("%d %d %d",da.function1(),d1.function1(),d2.function1());

Output is:

100,200,200;
b) 200,100,200;
c) 200,200,100;
d) None of the above


20. For the following C program

struct {
int x;
int y;
}abc;

x cannot be accessed by the following

1)abc-->x;
2)abc[0]-->x;
3)abc.x;
4)(abc)-->x;

a )1, 2, 3
b) 2 & 3
c) 1 & 2
d) 1, 3, 4

 

 

SATYAM  INTERVIEW QUESTION WITH SOLUTION on c

 

IBM PLACEMENT QUESTION WITH SOLUTION on c

Ibm

 

 

20. Output of the following program is

main()
{int i=0;
for(i=0;i<20;i++)
{switch(i)
case 0:i+=5;
case 1:i+=2;
case 5:i+=5;
default i+=4;
break;}
printf("%d,",i);
}
}

0,5,9,13,17
b) 5,9,13,17
c) 12,17,22
d) 16,21
e) Syntax error

Ans. (d)


21. What is the ouptut in the following program

main()
{char c=-64;
int i=-32
unsigned int u =-16;
if(c>i)
{printf("pass1,");
if(cprintf("pass2");
else
printf("Fail2");
}
else
printf("Fail1);
if(iprintf("pass2");
else
printf("Fail2")
}

Pass1,Pass2
b) Pass1,Fail2
c) Fail1,Pass2
d) Fail1,Fail2
e) None of these

Ans. (c)

24. What will the following program do?

void main()
{
int i;
char a[]="String";
char *p="New Sring";
char *Temp;
Temp=a;
a=malloc(strlen(p) + 1);
strcpy(a,p); //Line number:9//
p = malloc(strlen(Temp) + 1);
strcpy(p,Temp);
printf("(%s, %s)",a,p);
free(p);
free(a);
} //Line number 15//

Swap contents of p & a and print:(New string, string)
b) Generate compilation error in line number 8
c) Generate compilation error in line number 5
d) Generate compilation error in line number 7
e) Generate compilation error in line number 1

Ans. (b)


25. In the following code segment what will be the result of the function,

value of x , value of y
{unsigned int x=-1;
int y;
y = ~0;
if(x == y)
printf("same");
else
printf("not same");
}

same, MAXINT, -1
b) not same, MAXINT, -MAXINT
c) same , MAXUNIT, -1
d) same, MAXUNIT, MAXUNIT
e) not same, MAXINT, MAXUNIT

Ans. (a)


27. What will be the result of the following program ?

char *gxxx()
{static char xxx[1024];
return xxx;
}

main()
{char *g="string";
strcpy(gxxx(),g);
g = gxxx();
strcpy(g,"oldstring");
printf("The string is : %s",gxxx());
}

The string is : string
b) The string is :Oldstring
c) Run time error/Core dump
d) Syntax error during compilation
e) None of these

Ans. (b)


28. What will be result of the following program?

void myalloc(char *x, int n)
{x= (char *)malloc(n*sizeof(char));
memset(x,\0,n*sizeof(char));
}

main()
{char *g="String";
myalloc(g,20);
strcpy(g,"Oldstring");
printf("The string is %s",g);
}

The string is : String
b) Run time error/Core dump
c) The string is : Oldstring
d) Syntax error during compilation
e) None of these

32. What will be the result of the following program?

main()
{char p[]="String";
int x=0;
if(p=="String")
{printf("Pass 1");
if(p[sizeof(p)-2]=='g')
printf("Pass 2");
else
printf("Fail 2");
}
else
{
printf("Fail 1");
if(p[sizeof(p)-2]=='g')
printf("Pass 2");
else
printf("Fail 2");
}
}

Pass 1, Pass 2
b) Fail 1, Fail 2
c) Pass 1, Fail 2
d) Fail 1, Pass 2
e) syntax error during compilation


33. Which of the choices is true for the mentioned declaration ?

const char *p;
and
char * const p;

You can't change the character in both
b) First : You can't change the characterr & Second : You can;t change the pointer
c) You can't change the pointer in both
d) First : You can't change the pointer & Second : You can't chanage the character
e) None

15. Given the following c program

func(int *i, int*j)
{*i=*i * *i;
  *j=*j* *j;
}

main()
{ int i = 5, j = 2;
   func(&i,&j);
   printf("%d %d", i, j);}

What is the output?

9. For the following C program

void insert(key,r)
typekey key,data array r;
{extern int n;
if(n>=max) /*error table if full */
else r[n++].k=key;
}

This on executing, enables a

Basic sequential search
b) Binary search
c) Interpolation search
d) None


10. Find the outpur of the following C program

void f(char *p)
{p=(char *) malloc(6);
strcpy(p,"hello");
}

void main( )
{char *P="bye";
f(p);
printf("%s',p);
}

6. What is the fallacy in the following program segment ?

int *f1()
{
int a=5;
return &a;
}
f()
int *b=f1()
int c=*b;
}

7. Give the C language equivalents of the following
a)Function returning an int pointer
b)Function pointer returning an int pointer
c)Function pointer returning an array of integers
d)Array of function pointer returning an array of integers

8. Find the fallacy in the following program segment?

int a;
short b;
b=a;


9. Define function ? Explain arguments in functions ?


10. How does C pass variables to a function ?

11. Explain the following program segment.
f(){
int *b;
*b=2;
}
9. Find the output for the following C program

int array[4][4] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
for (i=2;i<0;i--)
 for(j=2;j<=0;j--)
   printf("%d", arr[i][j]);

10. Find the output for the following C program

#include
void main()
{int i,x,sum=0;
int arr[6]=[1,2,3,4,5,6]
for (i=0;i<4;i++)
sum+ = func(arr[i]);
printf("%d", sum);
}

func(int x)
{ int val,x;
val = 2;
return(x+ val++);
}

28. For the following C progralm

int d=0;
   for(int i=0;i<31;i++)
    for(int j=0;j<31;j++)
     for(int k=0;k<31;k++)
     if (((i+j+k) % 3)==0)
    d=d+1;

Find value of d

 

 


No comments:

Blog List