Programming Essentials in C: Chapter 5 Assignment (CLA) Exam Answers

How to find: Press "Ctrl + F" in your browser and type key terms to locate the specific exam item or explanation. If the item is not listed on this page, please search for it in our comprehensive IT Exam Items Repository.

1. What happens if you try to compile and run this program?

    #include <stdio.h> 
    int main(void) { 
        char s[10] = "ABCDE", *p = s + 3; 

        printf("%d", p[1] - p[-1]); 
        return 0; 
    }
  • the program outputs 1
  • the program outputs 4
  • the program outputs 2
  • the program outputs 3

2. What happens if you try to compile and run this program?

    #include <stdio.h> 
    int main(void) { 
        char s[] = "ABCDE", *p = s + 5; 

        printf("%d", p[-1] - *(p - 4)); 
        return 0; 
    }

 

  • the program outputs 3
  • the program outputs 1
  • the program outputs 4
  • the program outputs 2

3. What happens if you try to compile and run this program?

    #include <stdio.h> 
    int main(void) { 
        char *p = "12345", *q = p - 10; 

        printf("%d", q[14] - q[13]); 
        return 0; 
    }

 

  • the program outputs 2
  • the program outputs 1
  • the program outputs 3
  • the program outputs 4

4. What happens if you try to compile and run this program?

    #include <stdio.h> 
    int main(void) { 
        int t[] = { 1, 2, 3, 4, 5 }, *p = t; 

        *p++; 
        (*p)++; 
        *p++; 
        printf("%d",p[-1]); 
        return 0; 
    }

 

  • the program outputs 1
  • the program outputs 4
  • the program outputs 2
  • the program outputs 3

5. What happens if you try to compile and run this program?

    #include <stdio.h> 
    int main(void) { 
        int t[2][2] = { 1, 2, 4, 8 }; 
        int s = 0, i, j; 

        for(i = 2; i; i -= 2) 
        	for(j = 1; j < 2; j += 2) 
        		s += t[i - 1][j]; 
        printf("%d",s); 
        return 0; 
    }

 

  • the program outputs 8
  • the program outputs 2
  • the program outputs 1
  • the program outputs 4

6. What happens if you try to compile and run this program?

    #include <stdio.h> 
    int main(void) { 
        int t[1][2][2] = { { { 1, 2 }, { 3, 4 } } }; 
        int s = 0, i, j, k; 

        for(i = 1; i > 0; i -= 2) 
        	for(j = 1; j < 2; j += 2) 
        		for(k = 0; k < 3; k += 3) 
        		s += t[k][i - 1][j]; 
        printf("%d",s); 
        return 0; 
    }

 

  • the program outputs 2
  • the program outputs 3
  • the program outputs 4
  • the program outputs 1

7. What happens if you try to compile and run this program?

    #include <stdio.h> 
    #include <stdlib.h> 
    int main(void) { 
        int *p = (int *)malloc(2 * sizeof(int)); 
        *p = 2; 
        *(p + 1) = *(p) - 1; 
        *p = p[1]; 
        printf("%d",*p); 
        free(p); 
        return 0; 
    }
  • the program outputs 3
  • the program outputs 1
  • the program outputs 2
  • the program outputs 4

8. What happens if you try to compile and run this program?

    #include <stdio.h> 
    #include <stdlib.h> 
    int main(void) { 
        int t[] = { 8, 4, 2, 1 }; 
        int *p = (int *)malloc(sizeof(t)); 
        int i; 
        for(i = 0; i < 4; i++) 
        	p[3 - i] = t[i]; 
        printf("%d",*(p + 2)); 
        free(p); 
        return 0; 
    }
  • the program outputs 3
  • the program outputs 1
  • the program outputs 2
  • the program outputs 4

9. What happens if you try to compile and run this program?

    #include <stdio.h> 
    #include <stdlib.h> 
    int main(void) { 
        int i,j; 
        int **p = (int **)malloc(2 * sizeof(int *)); 
        p[0] = (int *)malloc(2 * sizeof(int)); 
        p[1] = p[0]; 
        for(i = 0; i < 2; i++) 
        	for(j = 0; j < 2; j++) 
        		p[i][j] = i + j; 
        printf("%d",p[0][0]); 
        return 0; 
    }
  • the program outputs 1
  • the program outputs 4
  • the program outputs 2
  • the program outputs 3

10. What happens if you try to compile and run this program?

    #include <stdio.h> 
    #include <stdlib.h> 
    int main(void) { 
        int i,j; 
        int **p = (int **)malloc(2 * sizeof(int *)); 
        p[0] = (int *)malloc(2 * sizeof(int)); 
        p[1] = (int *)malloc(2 * sizeof(int)); 
        for(i = 0; i < 2; i++) 
        	for(j = 0; j < 2; j++) 
        		p[i][j] = i + j; 
        printf("%d",p[0][0]); 
        return 0; 
    }
  • the program outputs 0
  • the program outputs 2
  • the program outputs 3
  • the program outputs 1

11. What happens if you try to compile and run this program?

    #include <stdio.h> 
    int main(void) { 
        int *t[10]; 
        int (*u)[10]; 
        printf("%d",sizeof(t) != sizeof(u)); 
        return 0; 
    }
  • the program outputs 0
  • the program outputs 4
  • the program outputs 1
  • the program outputs 16

12. What happens if you try to compile and run this program?

    #include <stdio.h> 
    int main(void) { 
        int *(t[10]); 
        int *u[10]; 
        printf("%d",sizeof(t) != sizeof(u)); 
        return 0; 
    }
  • the program outputs 3
  • the program outputs 0
  • the program outputs 1
  • the program outputs 2

13. What happens if you try to compile and run this program?

    #include <stdio.h> 
    struct S { 
        int S; 
    }; 
    int main(void) { 
        struct S S; 
        S.S = sizeof(struct S) / sizeof(S); 
        printf("%d",S.S); 
        return 0; 
    }
  • the program outputs 3
  • the program outputs 1
  • the program outputs 4
  • the program outputs 2

14. What happens if you try to compile and run this program?

    #include <stdio.h> 
    #include <string.h> 
    struct Q { 
        char S[3]; 
    }; 
    struct S { 
        struct Q Q; 
    }; 
    int main(void) { 
        struct S S = { '\0', '\0','\0' }; 
        S.Q.S[0] = 'A'; 
        S.Q.S[1] = 'B'; 
        printf("%d",strlen(S.Q.S)); 
        return 0; 
    }
  • the program outputs 4
  • the program outputs 1
  • the program outputs 2
  • the program outputs 3

15. What happens if you try to compile and run this program?

    #include <stdio.h> 
    #include <string.h> 
    struct Q { 
        char S[3]; 
    }; 
    struct S { 
        struct Q Q; 
    }; 
    int main(void) { 
        struct S S = { '\0', '\0','\0' }; 
        S.Q.S[0] = 'A'; 
        S.Q.S[2] = 'B'; 
        printf("%d",strlen(S.Q.S)); 
        return 0; 
    }
  • the program outputs 3
  • the program outputs 4
  • the program outputs 2
  • the program outputs 1

16. What happens if you try to compile and run this program?

    #include <stdio.h> 
    struct Q { 
        int a,b,c; 
    }; 
    struct S { 
        int a,b,c; 
        struct Q Q; 
    }; 
    int main(void) { 
        struct Q Q = { 3,2,1 }; 
        struct S S = { 4,5,6 }; 
        S.Q = Q; 
        printf("%d",S.b - S.Q.b); 
        return 0; 
    }
  • the program outputs 1
  • the program outputs 4
  • the program outputs 2
  • the program outputs 3

17. What happens if you try to compile and run this program?

    #include <stdio.h> 
    #include <stdlib.h> 
    struct S { 
        int 	  a; 
        struct S *b; 
    }; 
    int main(void) { 
        struct S *x = (struct S*) malloc(sizeof(struct S)); 
        struct S *y = (struct S*) malloc(sizeof(struct S)); 
        x->a = 2; 
        x->b = y; 
        y->a = 4; 
        y->b = x; 
        printf("%d",x->b->b->b->a); 
        free(x); free(y);
        return 0; 
    }
  • the program outputs 1
  • the program outputs 4
  • the program outputs 3
  • the program outputs 2

18. What happens if you try to compile and run this program?

    #include <stdio.h> 
    #include <stdlib.h> 
    struct S { 
        int 	  a; 
        struct S *b; 
    }; 
    int main(void) { 
        struct S *x = (struct S*) malloc(sizeof(struct S)); 
        struct S *y = (struct S*) malloc(sizeof(struct S)); 
        struct S *p; 
        x->a = 2; x->b = y; 
        y->a = 4; y->b = x; 
        p = x; 
        p = p->b->b->b->b; 
        printf("%d",p->a); 
        return 0; 
    }
  • the program outputs 2
  • the program outputs 4
  • the program outputs 3
  • the program outputs 1

19. What happens if you try to compile and run this program?

    #include <stdio.h> 
    struct S { 
        int a[2]; 
    }; 
    int main(void) { 
        struct S S[2]; 
        int i; 
        for(i = 0; i < 2; i++) 
        	S[i].a[1-i] = 4 * !i; 
        printf("%d",S[0].a[1]); 
        return 0; 
    }
  • the program outputs 2
  • the program outputs 4
  • the program outputs 1
  • the program outputs 3

20. What happens if you try to compile and run this program?

    #include <stdio.h> 
    struct S { 
        char *p; 
    }; 
    int main(void) { 
        char *p = "abcd"; 
        struct S S[2]; 
        int i; 
        for(i = 0; i < 2; i++) 
        	S[i].p = p + i; 
        printf("%c",S[1].p[0]); 
        return 0; 
    }
  • the program outputs a
  • the program outputs d
  • the program outputs b
  • the program outputs c
Subscribe
Notify of
guest

2 Corrections & Clarifications