Aptitude Test On C-programming (Pointers And Strings) Set -b

15 Questions | Attempts: 103
Share

SettingsSettingsSettings
Aptitude Quizzes & Trivia

Questions and Answers
  • 1. 

    Which of the following function is used to find the first occurrence of a given string in another string?

    • A.

      Strnset()

    • B.

      Strchr()

    • C.

      Strrchr()

    • D.

      Strstr()

    Correct Answer
    D. Strstr()
  • 2. 

    What would be the equivalent pointer expression for referring the array element a[i][j][k][l]

    • A.

      ((((a+i)+j)+k)+l)

    • B.

      *(*(*(*(a+i)+j)+k)+l)

    • C.

      (((a+i)+j)+k+l)

    • D.

      *((a+i)+j+k+l)

    Correct Answer
    B. *(*(*(*(a+i)+j)+k)+l)
  • 3. 

    What will be the output of the program ?   int main() {     char p[] = "%d\n";     p[1] = 'c';     printf(p, 65);     return 0; }

    • A.

      A

    • B.

      C

    • C.

      A

    • D.

      65

    Correct Answer
    C. A
    Explanation
    char p[] = "%d\n"; The variable p is declared as an array of characters and initialized with string "%d".
    p[1] = 'c'; Here, we overwrite the second element of array p by 'c'. So array p becomes "%c".
    printf(p, 65); becomes printf("%c", 65);
    Therefore it prints the ASCII value of 65. The output is 'A'.

    Rate this question:

  • 4. 

    What will be the output of the program ?   int main() {     int i;     char a[] = "\0";     if(printf("%s", a))         printf("The string is empty\n");     else         printf("The string is not empty\n");     return 0; }

    • A.

      No Output

    • B.

      The string is empty

    • C.

      The string is not empty

    • D.

      0

    Correct Answer
    C. The string is not empty
    Explanation
    The function printf() returns the number of characters printed on the console.
    char a[] = "\0"; The variable a is declared as an array of characters and it initialized with "\0". It denotes that the string is empty.
    if(printf("%s", a)) The printf() statement does not print anything, so it returns '0'(zero). Hence the if condition is failed.
    In the else part it prints "The string is not empty".

    Rate this question:

  • 5. 

    What will be the output of the program ?   int main() {     int i=3, *j, k;     j = &i;     printf("%d\n", i**j*i+*j);     return 0; }

    • A.

      30

    • B.

      27

    • C.

      9

    • D.

      3

    Correct Answer
    A. 30
    Explanation
    j=&i implies *j=i;
    *j=3
    i**j=3*3=9
    i**j*i=9*3=27
    i**j*i+*j=27+3=30

    Rate this question:

  • 6. 

    What will be the output of the program ?   int main() {     void*vp;     char ch=74, *cp="JACK";     int j=65;     vp=&ch;     printf("%c", *(char*)vp);     vp=&j;     printf("%c", *(int*)vp);     vp=cp;     printf("%s", (char*)vp+2);     return 0; }

    • A.

      JCK

    • B.

      J65K

    • C.

      JACK

    • D.

      JAK

    Correct Answer
    C. JACK
    Explanation
    Pointer always store integer value so cp will store the memory address of location where string "jack " is stored.

    vp = &ch; Will store address of ch in vp so while we print content in printf it will print asccii value of 74 i.e "J"

    vp = &j; It will assign address of j to vp again it will print ascii value of 65 as "A"

    vp = cp; In this step cp is pointing to memory locatioon where string jack is stored and we r incrementing it by two so it will point to "C"
    from sring "JACK" and since we hava given %S in printf so it will print content from c onward ie "CK"

    Rate this question:

  • 7. 

    What will be the output of the program ?   int main() {     int i, a[] = {2, 4, 6, 8, 10};     change(a, 5);     for(i=0; i<=4; i++)         printf("%d, ", a[i]);     return 0; } change(int *b, int n) {     int i;    for(i=0; i<n; i++)      *(b+1) = *(b+i)+5; }

    • A.

      3, 1, -1, -3, -5

    • B.

      7, 9, 11, 13, 15

    • C.

      2, 15, 6, 8, 10

    • D.

      2, 4, 6, 8, 10

    Correct Answer
    C. 2, 15, 6, 8, 10
    Explanation
    *(b+1) = *(b+i)+5;
    1. i = 0 => *(b+1) ie 4 is replaced by *(b+0)+5 ie 2+5
    2. i = 1 => *(b+1) ie 4 is replaced by *(b+1)+5 ie 4+5
    3. i = 2 => *(b+1) ie 4 is replaced by *(b+2)+5 ie 6+5
    4. i = 3 => *(b+1) ie 4 is replaced by *(b+3)+5 ie 8+5
    5. i = 4 => *(b+1) ie 4 is replaced by *(b+4)+5 ie 10+5

    Rate this question:

  • 8. 

    What will be the output of the program ? int main() { char *str; str = "%s"; printf(str, "K\n"); return 0; }

    • A.

      %s

    • B.

      Error

    • C.

      K

    • D.

      No Output

    Correct Answer
    C. K
    Explanation
    printf(str, "K\n"); is replaced with printf("%s" , "K\n");
    (since str = "%s";)

    So it will print K.

    Rate this question:

  • 9. 

    What will be the output of the program ? int main() {     printf(5+"Good Morning\n");   return 0; }

    • A.

      Morning

    • B.

      Good Morning

    • C.

      Good

    • D.

      M

    Correct Answer
    A. Morning
    Explanation
    printf(5+"Good Morning\n"); It skips the 5 characters and prints the given string.
    Hence the output is "Morning"

    Rate this question:

  • 10. 

    What will be the output of the program ? int main() {     char str[] = "India\0\is Best\0";     printf("%d\n", strlen(str));     return 0; }

    • A.

      5

    • B.

      10

    • C.

      11

    • D.

      6

    Correct Answer
    A. 5
    Explanation
    The function strlen returns the number of characters in the given string.
    Therefore, strlen(str) becomes strlen("India") contains 5 characters. A string is a collection of characters terminated by '\0'.
    The output of the program is "5"

    Rate this question:

  • 11. 

    What will be the output of the program ? int main() {     char str1[] = "Hello";     char str2[] = "Hello";     if(str1 == str2)         printf("Equal\n");     else         printf("Unequal\n");     return 0; }

    • A.

      Unequal

    • B.

      Equal

    • C.

      Error

    • D.

      None of above

    Correct Answer
    A. Unequal
    Explanation
    if(str1 == str2) here the address of str1 and str2 are compared. The addresses of both variables are not same. Hence the if condition is failed.
    At the else part it prints "Unequal".

    Rate this question:

  • 12. 

    Trace the output. int main() {     char str[] = "basic";     char *s = str;     printf("%s ", s++ +3);     printf("%s",s);     return 0; }

    • A.

      C basic

    • B.

      C c

    • C.

      Ic asic

    • D.

      Asic sic

    Correct Answer
    C. Ic asic
    Explanation
    printf("%s ", s++ +3); -> ic since (s++ +3) and s++ is post increment but +3 just print string from 'i'. It ll not increment the pointer to 3.
    printf("%s",s); here after incrementing (s++) It ll print ''asic''.

    Rate this question:

  • 13. 

    If the size of integer is 4bytes, What will be the output of the program? int main() { int arr[] = {12, 13, 14, 15, 16}; printf("%d, %d, %d\n", sizeof(arr), sizeof(*arr), sizeof(arr[0])); return 0; }

    • A.

      10, 2, 4

    • B.

      20, 4, 4

    • C.

      20, 2, 2

    • D.

      16, 2, 2

    Correct Answer
    B. 20, 4, 4
  • 14. 

    If a variable is a pointer to a structure, then which of the following operator is used to access data members of the structure through the pointer variable?

    • A.

      '.'

    • B.

      '&'

    • C.

      '*'

    • D.

      '->'

    Correct Answer
    D. '->'
  • 15. 

    Are the expression *ptr++ and ++*ptr are same?

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    *ptr++ increments the pointer and not the value,
    ++*ptr increments the value being pointed by ptr .

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 21, 2022
    Quiz Edited by
    ProProfs Editorial Team
  • Aug 25, 2011
    Quiz Created by
    Mayurkothawade
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.