250MB free for everyone.

Prepare C UrSelf

Technical Sathish Welcomes U


Technicalsathish.250free.com
sathi1693@gmail.com

Home C C++ Java ASP .Net
Puzzles Aptitude Verbal Interview FAQ Contact





“C” Questions

1. Base class has some virtual method and derived class has a method with the same name. If we initialize the base class pointer with derived
object,. calling of that virtual method will result in which method being called? 

a. Base method
b. Derived method..

Ans. b

2. For the following C program

#define AREA(x)(3.14*x*x)
main()
{float r1=6.25,r2=2.5,a;
a=AREA(r1);
printf("\n Area of the circle is %f", a);
a=AREA(r2);
printf("\n Area of the circle is %f", a);
}

What is the output?

Ans. Area of the circle is 122.656250
        Area of the circle is  19.625000

3. What do the following statements indicate. Explain.

·         int(*p)[10]

·         int*f()

·         int(*pf)()

·         int*p[10]

Refer to:
-- Kernighan & Ritchie page no. 122
-- Schaum series page no. 323

4.
void main()
{
int d=5;
printf("%f",d);
}

Ans: Undefined

 

 

 


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

Ans: 1,2,3,4


6.
void main()
{
char *s="\12345s\n";
printf("%d",sizeof(s));
}

Ans: 6


7.
void main()
{
unsigned i=1; /* unsigned char k= -1 => k=255; */
signed j=-1; /* char k= -1 => k=65535 */
/* unsigned or signed int k= -1 =>k=65535 */
if(i printf("less");
else
if(i>j)
printf("greater");
else
if(i==j)
printf("equal");
}

Ans: less

 

 

 


8.
void main()
{
float j;
j=1000*1000;
printf("%f",j);
}

1. 1000000
2. Overflow
3. Error
4. None

Ans: 4


9.  How do you declare an array of N pointers to functions returning
     pointers to functions returning pointers to characters?

Ans: The first part of this question can be answered in at least
        three ways:

    1. char *(*(*a[N])())();

    2. Build the declaration up incrementally, using typedefs:

        typedef char *pc;    /* pointer to char */
        typedef pc fpc();    /* function returning pointer to char */
        typedef fpc *pfpc;    /* pointer to above */
        typedef pfpc fpfpc();    /* function returning... */
        typedef fpfpc *pfpfpc;    /* pointer to... */
        pfpfpc a[N];         /* array of... */

    3. Use the cdecl program, which turns English into C and vice
    versa:

        cdecl> declare a as array of pointer to function returning
            pointer to function returning pointer to char
        char *(*(*a[])())()

    cdecl can also explain complicated declarations, help with
    casts, and indicate which set of parentheses the arguments
    go in (for complicated function definitions, like the one
    above).
    Any good book on C should explain how to read these complicated
    C declarations "inside out" to understand them ("declaration
    mimics use").
    The pointer-to-function declarations in the examples above have
    not included parameter type information. When the parameters
    have complicated types, declarations can *really* get messy.
    (Modern versions of cdecl can help here, too.)


10. A structure pointer is defined of the type time . With 3 fields min,sec hours having pointers to intergers.
    Write the way to initialize the 2nd element to 10.


11. In the above question an array of pointers is declared.
    Write the statement to initialize the 3rd element of the 2 element to 10;


12.
int f()
void main()
{
f(1);
f(1,2);
f(1,2,3);
}
f(int i,int j,int k)
{
printf("%d %d %d",i,j,k);
}

What are the number of syntax errors in the above?

Ans: None.


13.
void main()
{
int i=7;
printf("%d",i++*i++);
}

Ans: 56


14.
#define one 0
#ifdef one
printf("one is defined ");
#ifndef one
printf("one is not defined ");

Ans: "one is defined"

 



15.
void main()
{
int count=10,*temp,sum=0;
temp=&count;
*temp=20;
temp=∑
*temp=count;
printf("%d %d %d ",count,*temp,sum);
}

Ans: 20 20 20


16. There was question in c working only on unix machine with pattern matching.


14. what is alloca()

Ans : It allocates and frees memory after use/after getting out of scope


17.
main()
{
static i=3;
printf("%d",i--);
return i>0 ? main():0;
}

Ans: 321


18.
char *foo()
{
char result[100]);
strcpy(result,"anything is good");
return(result);
}
void main()
{
char *j;
j=foo()
printf("%s",j);
}

Ans: anything is good.

 

19.
void main()
{
char *s[]={ "dharma","hewlett-packard","siemens","ibm"};
char **p;
p=s;
printf("%s",++*p);
printf("%s",*p++);
printf("%s",++*p);
}

Ans: "harma" (p->add(dharma) && (*p)->harma)
"harma" (after printing, p->add(hewlett-packard) &&(*p)->harma)
"ewlett-packard"

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);
}
}

a) 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(c printf("pass2");
else
printf("Fail2");
}
else
printf("Fail1);
if(i printf("pass2");
else
printf("Fail2")
}

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

Ans. (c)

22. 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//

a) 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)

23. 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");
}

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

Ans. (a)

24. 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());
}

a) 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)

25.  Find the output for the following C program

main()
{
char *p1="Name";
char *p2;
p2=(char *)malloc(20);
while(*p2++=*p1++);
printf("%s\n",p2);
}

 Ans. An empty string

26.  Find the output for the following C program

main()
{
int x=20,y=35;
x = y++ + x++;
y = ++y + ++x;
printf("%d %d\n",x,y);
}

Ans. 57 94

27.  Find the output for the following C program

main()
{
int x=5;
printf("%d %d %d\n",x,x<<2,x>>2);
}

Ans. 5 20 1

28 Find the output for the following C program

#define swap1(a,b) a=a+b;b=a-b;a=a-b;
main()
{
int x=5,y=10;
swap1(x,y);
printf("%d %d\n",x,y);
swap2(x,y);
printf("%d %d\n",x,y);
}
int swap2(int a,int b)
{
int temp;
temp=a;
b=a;
a=temp;
return;
}

Ans. 10 5


29 Find the output for the following C program

main()
{
char *ptr = "Ramco Systems";
(*ptr)++;
printf("%s\n",ptr);
ptr++;
printf("%s\n",ptr);
}

Ans. Samco Systems


30 Find the output for the following C program

#include
main()
{
char s1[]="Ramco";
char s2[]="Systems";
s1=s2;
printf("%s",s1);
}

Ans. Compilation error giving it cannot be an modifiable 'lvalue'


31 Find the output for the following C program

#include
main()
{
char *p1;
char *p2;
p1=(char *) malloc(25);
p2=(char *) malloc(25);
strcpy(p1,"Ramco");
strcpy(p2,"Systems");
strcat(p1,p2);
printf("%s",p1);
}

 Ans. RamcoSystems


32.  Find the output for the following C program given that
[1]. The following variable is available in file1.c
static int average_float;

Ans. All the functions in the file1.c can access the variable


33.  Find the output for the following C program

# define TRUE 0
some code
while(TRUE)
{
some code
}

Ans. This won't go into the loop as TRUE is defined as 0

34. struct list{
       int x;
      struct list *next;
      }*head;

        the struct head.x =100

       Is the above assignment to pointer is correct or wrong ?

Ans. Wrong

35.What is the output of the following ?

      int i;
      i=1;
      i=i+2*i++;
      printf(%d,i);

Ans. 4

36. FILE *fp1,*fp2;
     
      fp1=fopen("one","w")
      fp2=fopen("one","w")
      fputc('A',fp1)
      fputc('B',fp2)
      fclose(fp1)
      fclose(fp2)
     }

     Find the Error, If Any?

Ans. no error. But It will over writes on same file.

37. What are the output(s) for the following ?

38. #include
      char *f()
      {char *s=malloc(8);
        strcpy(s,"goodbye");
     }

      main()
      {
      char *f();
      printf("%c",*f()='A');     }

 

 

39. #define MAN(x,y) (x)>(y)?(x):(y)
      {int i=10;
      j=5;
      k=0;
      k=MAX(i++,++j);
      printf(%d %d %d %d,i,j,k);
      }

Ans. 10 5 0

40.
void main()
{
int i=7;
printf("%d",i++*i++);
}

Ans: 56

 

 

Top Of Page

C  Questions

 

Note : All the programs are tested under Turbo C/C++ compilers. 

It is assumed that,

Ř      Programs run under DOS environment,

Ř      The underlying machine is an x86 system,

Ř      Program is compiled using Turbo C/C++ compiler.

The program output may depend on the information based on this assumptions (for example sizeof(int) == 2 may be assumed).

 

Predict the output or error(s) for the following:

 

1.      void main()

{

            int  const * p=5;

            printf("%d",++(*p));

}

Answer:

                        Compiler error: Cannot modify a constant value.

Explanation:   

p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".

 

2.      main()

{

            char s[ ]="man";

            int i;

            for(i=0;s[ i ];i++)

            printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);

}

Answer:

                        mmmm

                       aaaa

                       nnnn

Explanation:

s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea. Generally  array name is the base address for that array. Here s is the base address. i is the index number/displacement from the base address. So, indirecting it with * is same as s[i]. i[s] may be surprising. But in the  case of  C  it is same as s[i].

 

3.      main()

{

            float me = 1.1;

            double you = 1.1;

            if(me==you)

printf("I love U");

else

                        printf("I hate U");

}

Answer:

I hate U

Explanation:

For floating point numbers (float, double, long double) the values cannot be predicted exactly. Depending on the number of bytes, the precession with of the value  represented varies. Float takes 4 bytes and long double takes 10 bytes. So float stores 0.9 with less precision than long double.

Rule of Thumb:

Never compare or at-least be cautious when using floating point numbers with relational operators (== , >, <, <=, >=,!= ) . 

 

4.      main()

            {

            static int var = 5;

            printf("%d ",var--);

            if(var)

                        main();

            }

Answer:

5 4 3 2 1

            Explanation:

When static storage class is given, it is initialized once. The change in the value of a static variable is retained even between the function calls. Main is also treated like any other ordinary function, which can be called recursively. 

 

5.      main()

{

             int c[ ]={2.8,3.4,4,6.7,5};

             int j,*p=c,*q=c;

             for(j=0;j<5;j++) {

                        printf(" %d ",*c);

                        ++q;     }

             for(j=0;j<5;j++){

printf(" %d ",*p);

++p;     }

}

 

Answer:

                        2 2 2 2 2 2 3 4 6 5

            Explanation:

Initially pointer c is assigned to both p and q. In the first loop, since only q is incremented and not c , the value 2 will be printed 5 times. In second loop p itself is incremented. So the values 2 3 4 6 5 will be printed.

           

6.      main()

{

            extern int i;

            i=20;

printf("%d",i);

}

 

Answer: 

Linker Error : Undefined symbol '_i'

Explanation:

                        extern storage class in the following declaration,

                                    extern int i;

specifies to the compiler that the memory for i is allocated in some other program and that address will be given to the current program at the time of linking. But linker finds that no other variable of name i is available in any other program with memory space allocated for it. Hence a linker error has occurred .

 

7.      main()

{

            int i=-1,j=-1,k=0,l=2,m;

            m=i++&&j++&&k++||l++;

            printf("%d %d %d %d %d",i,j,k,l,m);

}

Answer:

                        0 0 1 3 1

Explanation :

Logical operations always give a result of 1 or 0 . And also the logical AND (&&) operator has higher priority over the logical OR (||) operator. So the expression  i++ && j++ && k++’ is executed first. The result of this expression is 0    (-1 && -1 && 0 = 0). Now the expression is 0 || 2 which evaluates to 1 (because OR operator always gives 1 except for ‘0 || 0’ combination- for which it gives 0). So the value of m is 1. The values of other variables are also incremented by 1.

 

8.      main()

{

            char *p;

            printf("%d %d ",sizeof(*p),sizeof(p));

}

 

Answer:

                        1 2

Explanation:

The sizeof() operator gives the number of bytes taken by its operand. P is a character pointer, which needs one byte for storing its value (a character). Hence sizeof(*p) gives a value of 1. Since it needs two bytes to store the address of the character pointer sizeof(p) gives 2.

 

9.      main()

{

            int i=3;

            switch(i)

             {

                default:printf("zero");

                case 1: printf("one");

                           break;

               case 2:printf("two");

                          break;

              case 3: printf("three");

                          break;

              } 

}

Answer :

three

Explanation :

The default case can be placed anywhere inside the loop. It is executed only when all other cases doesn't match.

 

10.  main()

{

              printf("%x",-1<<4);

}

Answer:

fff0

Explanation :

-1 is internally represented as all 1's. When left shifted four times the least significant 4 bits are filled with 0's.The %x format specifier specifies that the integer value be printed as a hexadecimal value.

 

11.  main()

{

            char string[]="Hello World";


Let us know if this page contains pornographic, copyrighted, or hate content. 250Free proudly supports TheFreeSite.com