C Programming – Character Pointers and Functions

C Programming – Character Pointers and Functions

Question 1
What is the output of this C code?
        #include 
        int main()
        {
        char *str = "hello, world\n";
        char *strc = "good morning\n";
        strcpy(strc, str);
        printf("%s\n", strc);
        return 0;
        }
A
hello, world
B
Crash/segmentation fault
C
Undefined behaviour
D
Run time error
Question 2
What is the output of this C code?
        #include 
        int main()
        {
        char *str = "hello world";
        char strc[] = "good morning india\n";
        strcpy(strc, str);
        printf("%s\n", strc);
        return 0;
        }
A
hello world
B
hello worldg india
C
Compile time error
D
Undefined behaviour
Question 3
What is the output of this C code?
        #include 
        int main()
        {
        char *str = "hello, world!!\n";
        char strc[] = "good morning\n";
        strcpy(strc, str);
        printf("%s\n", strc);
        return 0;
        }
A
hello, world!!
B
Compile time error
C
Undefined behaviour
D
Segmenation fault
Question 4
What is the output of this C code?
        #include 
        int main()
        {
        char *str = "hello, world\n";
        str[5] = '.';
        printf("%s\n", str);
        return 0;
        }
A
hello. world
B
hello, world
C
Compile error
D
Segmentation fault
Question 5
What is the output of this C code?
        #include 
        int main()
        {
        char str[] = "hello, world";
        str[5] = '.';
        printf("%s\n", str);
        return 0;
        }
A
hello. world
B
hello, world
C
Compile error
D
Segmentation fault
Question 6
What is the output of this C code?
        #include 
        int main()
        {
        char *str = "hello world";
        char strary[] = "hello world";
        printf("%d %d\n", sizeof(str), sizeof(strary));
        return 0;
        }
A
11 11
B
12 12
C
4 12
D
4 11
Question 7
What is the output of this C code?
        #include 
        int main()
        {
        char *str = "hello world";
        char strary[] = "hello world";
        printf("%d %d\n", strlen(str), strlen(strary));
        return 0;
        }
A
11 11
B
12 11 .
C
11 12
D
x 11 where x can be any positive integer
Question 8
What is the output of this C code?
    #include 
    void f(char *k)
    {
    k++;
    k[2] = 'm';
    printf("%c\n", *k);
    }
    void main()
    {
    char s[] = "hello";
    f(s);
    }
A
l
B
e
C
h
D
o
Question 9
What is the output of this C code?
    #include 
    void fun(char *k)
    {
    printf("%s", k);
    }
    void main()
    {
    char s[] = "hello";
    fun(s);
    }
A
hello
B
Run time error
C
Nothing
D
h
Question 10
Comment on the output of this C code?
    #include 
    int main()
    {
    char *str = "This" //Line 1
    char *ptr = "Program\n"; //Line 2
    str = ptr; //Line 3
    printf("%s, %s\n", str, ptr); //Line 4
    }
A
Memory holding “this” is cleared at line 3
B
Memory holding “this” loses its reference at line 3
C
You cannot assign pointer like in Line 3
D
Output will be This, Program
Question 11
 What type initialization is needed for the segment “ptr[3] = ’3′;” to work?
A
char *ptr = “Hello!”;
B
char ptr[] = “Hello!”;
C
Both (a) and (b)
D
None of the mentioned
Question 12
The syntax for constant pointer to address (i.e., fixed pointer address) is:

A
const *
B
* const
C
const *
D
Both (a) and (c)
Question 13
Comment on the output of this C code?
    #include 
    int add(int a, int b)
    {
    return a + b;
    }
    int main()
    {
    int (*fn_ptr)(int, int);
    fn_ptr = add;
    printf("The sum of two numbers is: %d", (int)fn_ptr(2, 3));
    }
A
Compile time error, declaration of a function inside main.
B
Compile time error, no definition of function fn_ptr.
C
Compile time error, illegal application of statement fn_ptr = add.
D
No Run time error, output is 5.
Question 14
The correct way to declare and assign a function pointer is done by:    (Assuming the function to be assigned is “int multi(int, int);”)
A
int (*fn_ptr)(int, int) = multi;
B
int *fn_ptr(int, int) = multi;
C
int *fn_ptr(int, int) = &multi;
D
Both (b) & (c)
Question 15
Calling a function f with a an array variable a[3] where a is an array, is equivalent to
A
f(a[3])
B
f(*(a + 3))
C
f(3[a])
D
All of the mentioned
Question 16
What is the output of this C code?
    #include 
    void f(char *k)
    {
    k++;
    k[2] = 'm';
    }
    void main()
    {
    char s[] = "hello";
    f(s);
    printf("%c\n", *s);
    }
A
h
B
e
C
m
D
o
Question 17
What is the output of this C code?
    #include 
    void main()
    {
    char s[] = "hello";
    s++;
    printf("%c\n", *s);
    }
A
Compile time error
B
h
C
e
D
o


Check Your Answers:

  1. B
  2. A
  3. C
  4. D
  5. A
  6. C
  7. A
  8. B
  9. A
10. B
11. B
12. B
13. D
14. A
15. D
16. A
16. A


SHARE

Unknown

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment