Aptitude questions and answers in c

C programming aptitude questions and answers

Aptitude questions in interview


Total question: 32
Time: 60 minute
Total marks: 64

1. Find out the error if any, in the program.

#include<stdio.h>
int main(){
    int i=1;
    switch(i){
         case 1: printf("\radioactive cats have 18 half-lives");
         break;
         case 1*2+4: printf("\bottle for rent -inquire within");
         break;
    }
    return 0;
}

2. Find out the error, if any, in the following program.

#include<stdio.h>
int main(){
    int a=10,b;
    a= 5 ? b=100 : b=200;
    printf("\n%d",b);
    return 0;
}

3. In the following code, in which order the functions would be called?

a= f1(23,14)*f2(12/4)+f3();
a) f1, f2, f3
b) f3, f2, f1
c) The order may vary from compiler to compiler
d) None of the above

4. What would be the output of the following program?

#include<stdio.h>
int main(){
    int i=4;
    switch(i){
         default:
             printf("\n A mouse is an elephant built by the Japanese");
         case 1:
             printf(" Breeding rabbits is a hair raising experience");
             break;
         case 2:
             printf("\n Friction is a drag");
             break;
         case 3:
             printf("\n If practice make perfect, then no body's perfect");
    }
    return 0;
}

a) A mouse is an elephant built by the Japanese
b) Breeding rabbits is a hare raising experience
c) All of the above
d) None of the above

5. What is the output of the following program?

#include<stdio.h>
#define SQR(x) (x*x)
int main(){
    int a,b=3;
    a= SQR(b+2);
    printf("%d",a);
    return 0;
}
a) 25
b) 11
c) error
d) garbage value

6. In which line of the following, an error would be reported?

#include<stdio.h>
1. #define CIRCUM(R) (3.14*R*R);
2. int main()
3. {
4.       float r=1.0,c;
5.       c= CIRCUM(r);
6.       printf("\n%f",c);
7.       if(CIRCUM(r))==6.28)
8.           printf("\nGobbledygook");
9.       return 0;
10. }

a) line 1
b) line 5
c) line 6
d)line 7

7. What is the type of the variable b in the following declaration?

#define FLOATPTR float*
FLOATPTR a,b;

a) float
b) float pointer
c) int
d) int pointer

8. In the following code:

#include"stdio.h";
int main(){
    FILE *fp;
    fp= fopen("trial","r");
}

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.

9. We should not read after a write to a file without an intervening call to fflush(), fseek() or rewind() "TRUE/FALSE" ?

10. If the program (myprog) is run from the command line as myprog 1 2 3 , What would be the output?

#include<stdio.h>
int main(int argc, char *argv[]){
    int i;
    for(i=0;i<argc;i++)
         printf("%s",argv[i]);
    return 0;
}

a) 1 2 3
b) C:\MYPROG.EXE 1 2 3
c) MYP
d) None of the above

11. If the following program (myprog) is run from the command line as myprog 1 2 3, what would be the output?

#include<stdio.h>
int main(int argc, char *argv[]){
    int i,j=0;
    for(i=0;i<argc;i++)
         j=j+ atoi(argv[i]);
    printf("%d",j);
    return 0;
}

a) 1 2 3
b) 6
c) error
d) "123"

12. If the following program (myprog) is run from the command line as :
myprog monday tuesday wednesday thursday
What would be the output?

#include<stdio.h>
int main(int argc, char *argv[]){
    while(--argc <0)
         printf("%s",*++argv);
    return 0;
}

a) myprog monday tuesday wednesday thursday
b) monday tuesday wednesday thursday
c) myprog tuesday thursday
d) None of the above

13. In the following code, is p2 an integer or an integer pointer?

typedef int* ptr
ptr p1,p2;

14. Point out the error in the following program

#include<stdio.h>
int main(){
    const int x;
    x=128;
    printf("%d",x);
    return 0;
}

15. What would be the output of the following program?

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

a) 128
b) Garbage value
c) Error
d) 0

16. What is the difference between the following declarations?

const char *s;
char const *s;

17. What is the difference between the following declarations?

const char *const s;
char const *const s;

18. What would be the output of the following program?

#include<stdio.h>
int main(){
    char near * near *ptr1;
    char near * far *ptr2;
    char near * huge *ptr3;
    printf("%d %d %d",sizeof(ptr1),sizeof(ptr2),sizeof(ptr3));
    return 0;
}

a) 1 1 1
b) 1 2 4
c) 2 4 4
d) 4 4 4

19. If the following program (myprog) is run from the command line as:
myprog friday tuesday Sunday
What would be the output?

#include<stdio.h>
int main(int argc, char*argv[]){
    printf("%c",**++argv);
    return 0;
}

a) m
b) f
c) myprog
d) friday

20. If the following program (myprog) is run from the command line as:
myprog friday tuesday sunday
What would be the output?

#include<stdio.h>
int main(int argc, char *argv[]){
    printf("%c",*++argv[1]);
    return 0;
}

a) r
b) f
c) m
d) y

21. If the following program (myprog) is run from the command line as:
myprog friday tuesday sunday
What would be the output?

#include<stdio.h>
int main(int argc, char *argv[]){
    while(sizeof argv)
         printf("%s",argv[--sizeof argv]);
    return 0;
}

a) myprog friday tuesday sunday
b) myprog friday tuesday
c) sunday tuesday friday myprog
d) sunday tuesday friday

22. Find out the error in the following program.

#include<stdio.h>
int main(){
    int a=10;
    void f();
    a=f();
    printf("\n%d",a);
    return 0;
}
void f(){
    printf("\nHi");
}

23. In the following program how would you print 50 using p?

#include<stdio.h>
int main(){
    int a[]={10, 20, 30, 40, 50};
    char *p;
    p= (char*) a;
}

24. Would the following program compile?

#include<stdio.h>
int main(){
    int a=10,*j;
    void *k;
    j=k=&a;
    j++;
    k++;
    printf("\n%u%u",j,k);
}

a) Yes
b) No, the format is incorrect
c) No, the arithmetic operation is not permitted on void pointers
d) No, the arithmetic operation is not permitted on pointers

25. According to ANSI specifications which is the correct way of declaring main() when it receives command line arguments?

a) main(int argc, char *argv[])
b)
main(argc,argv)
int argc;
char *argv[];
c)
main(){
    int argc; char *argv[];
}
d) None of the above

26. What error would the following function give on compilation?

f(int a, int b){
    int a;
    a=20;
    return a;
}

a) Missing parenthesis in the return statement
b) The function should be declared as int f(int a, int )
c) Re declaration of a
d) None of the above

27. Find out the error in the following program.

#include<stdio.h>
int main(){
    const char *fun();
    *fun()='A';
    return 0;
}
const char *fun(){
    return "Hello";
}

28. What would be the output of the following program?

#include<stdio.h>
int main(){
    const int x=5;
    int *ptrx;
    ptrx=&x;
    *ptrx=10;
    printf("%d",x);
    return 0;
}

a) 5
b) 10
c) Error
d) Garbage value

29. A switch statement cannot include

a) Constants as arguments
b) Constant expression as arguments
c) String as an argument
d) None of the above
30. How long the following program will run?

#include<stdio.h>
int main(){
    printf("\nSonata Software");
    main();
    return 0;
}

a) Infinite loop
b) Until the stack overflows
c) All of the above
d) None of the above

31. On combining the following statements, you will get

char*p;
p=malloc(100);

a) char *p= malloc(100)
b) p= (char*)malloc(100)
c) All of the above
d) None of the above

32. What is the output of the following program?

#include<stdio.h>
int main(){
    int n=5;
    printf("\nn=%*d",n,n);
    return 0;
}

a) n=5
b) n=5
c) n= 5
d) Error

Solution:

1. No error. Constant expression like 1*2+4 is acceptable in cases of a switch.

2. Lvalue required in function main(). The second assignment should be written in parenthesis as follows:
a= 5 ? b = 100 : (b=200);

3. c) The order may vary from compiler to compiler

4. c) All of the above

5. b) 11

6. d)line 7

7. a) float

8. b) A structure which contains a "char" pointer which points to the first character in the file.

9. True

10. b) C:\MYPROG.EXE 1 2 3

11. b) 6

12. b) monday tuesday wednesday Thursday

13. Integer pointer

14. x should have been initialized where it is declared.

15. a) 128

16. No difference

17. No difference

18. c) 2 4 4
19. b) f

20. b) f

21. c) sunday tuesday friday myprog

22. The program is trying to collect the value of a "void" function into an integer variable.

23. printf("\n%d",*((int*)p+4));

24. c) No, the arithmetic operation is not permitted on void pointers

25. a) main(int argc, char *argv[])

26. c) Re declaration of a

27. fun() returns to a "const char" pointer which cannot be modified

28. b) 10

29. c) string as an argument

30. b) Until the stack overflows

31. a) char *p= malloc(100)

32. c) n= 5



Aptitude questions for c

16 comments:

  1. What is meant by Lvalue?? Please explain..

    ReplyDelete
  2. when we try to increment a constant value.....we get a lvalur error

    ReplyDelete
  3. THANX for dis superb blog......u r helping us a lot..

    ReplyDelete
  4. superb blog....helping me a lot..

    ReplyDelete
  5. Very nice blog.... Like it..

    ReplyDelete
  6. Thnx a lot for helping ..

    ReplyDelete
  7. u r blog has given a new way of learning c language.. thanks a lot...

    ReplyDelete
  8. I think 4th answer is wrong
    it prints th default and case 1 because of no break; after default
    so in this question answer is a and b both

    ReplyDelete
  9. main()
    {
    int i;
    i = 10;
    printf("%d\t",5,6);
    printf("%d", i , i++);
    }
    pls give output of this program and explain??/

    ReplyDelete
    Replies
    1. here first printf only one format specifier so 5 is print and second printf stmnt , printf stmt read value from right to left and print left to right so first post increment means first use and then increment so i++is 10 and after incremnt i is 11

      Delete
  10. Even I think 4th ans s a and b.

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. shutdown code will clearly why .let to save explain fully for my mail

    ReplyDelete
  13. great job!!!!

    ReplyDelete