techpreparation-homepage

Home  Interview Questions  Certifications  Aptitude Questions  Tutorials  Placement Papers  Search  Resume  Soft Skills  Video  Forum  Blog


Technical Interview Questions

C++ Interview Questions
C# Interview Questions
C Interview Questions
                              .........More

Aptitude Interview Questions
C ++ Aptitude Questions
C Aptitude Questions 1
                              .........More

Tutorials
C Tutorial
C++ Tutorial
                              .........More

Programming Source Codes
C/C++ Source Codes
C# Source Codes
                              .........More

Online Quiz
C Online Quiz
C++ Online Quiz
                              .........More

Soft Skills
Communication Skills
Leadership Skills
                              .........More

 

 

  

C Aptitude Questions and Answers



Predict the output or error(s) for the following:
100.  main()
{
                        int a=10,*j;
            void *k;
                        j=k=&a;
            j++; 
                        k++;
            printf("\n %u %u ",j,k);
}

Answer:
Compiler error: Cannot increment a void pointer

Explanation:
Void pointers are generic pointers and they can be used only when the type is not known and as an intermediate address storage type. No pointer arithmetic can be done on it and you cannot apply indirection operator (*) on void pointers.

101.    Printf can be implemented by using  __________ list.

Answer:
Variable length argument lists

102.     char *someFun()
            {
            char *temp = “string constant";
            return temp;
            }
            int main()
            {
            puts(someFun());
            }

Answer:
string constant

Explanation:
            The program suffers no problem and gives the output correctly because the character constants are stored in code/data area and not allocated in stack, so this doesn’t lead to dangling pointers.

103.     char *someFun1()
            {
            char temp[ ] = “string";
            return temp;
            }
            char *someFun2()
            {
            char temp[ ] = {‘s’, ‘t’,’r’,’i’,’n’,’g’};
            return temp;
            }
            int main()
            {
            puts(someFun1());
            puts(someFun2());
            }

Answer:
Garbage values.

Explanation:
            Both the functions suffer from the problem of dangling pointers. In someFun1() temp is a character array and so the space for it is allocated in heap and is initialized with character string “string”. This is created dynamically as the function is called, so is also deleted dynamically on exiting the function so the string data is not available in the calling function main() leading to print some garbage values. The function someFun2() also suffers from the same problem but the problem can be easily identified in this case.

104.  There were 10 records stored in “somefile.dat” but the following program printed 11 names. What went wrong?
void main()

{
struct student
{         
char name[30], rollno[6];
}stud;
FILE *fp = fopen(“somefile.dat”,”r”);
while(!feof(fp))
 {
                        fread(&stud, sizeof(stud), 1 , fp);
puts(stud.name);
}
}

Explanation:
fread reads 10 records and prints the names successfully. It will return EOF only when fread tries to read another record and fails reading EOF (and returning EOF). So it prints the last record again. After this only the condition feof(fp) becomes false, hence comes out of the while loop.

 Page Numbers :   1      2      3      4      5      6      7      8      9      10      11      12

Have a Question ? post your questions here. It will be answered as soon as possible.

Check C Aptitude Questions for more C Aptitude Interview Questions with Answers

Check Placement Papers for more IT Companies Paper