Tuesday, February 11, 2020

1.7 Structure in C


Session 7
o   Structure
o   Array of Structures
o   Passing struct to function
o   Nested Structure
o   Struct memory allocation
o   Typedef
1.what is the output of this code?
 #include <stdio.h>
    struct student
    {
        char *name;
    };
    struct student s;
    struct student fun(void)
    {
        s.name = "newton";
        printf("%s\n", s.name);
        s.name = "alan";
        return s;
    }
    void main()
    {
        struct student m = fun();
        printf("%s\n", m.name);
        m.name = "turing";
        printf("%s\n", s.name);
    }

a) newton alan alan
b) alan newton alan
c) alan alan newton
d) Compile time error
Answer: a
2.
 #include <stdio.h>
    struct student
    {   char *name;
    };
    void main()
    {
        struct student s, m;
        s.name = "st";
        m = s;
        printf("%s%s", s.name, m.name);
    }
a)      Compile time error
b) Nothing
c) Junk values
d) st st
Answer: d
3.
How will you free the allocated memory ?
remove(var-name);
free(var-name);
delete(var-name);
dalloc(var-name);

Answer: b
4. #include<stdio.h>
 
int main()
{
    struct value
    {
        int bit1:1;
        int bit3:4;
        int bit4:4;
    }bit={1, 2, 13};
 
    printf("%d, %d, %d\n", bit.bit1, bit.bit3, bit.bit4);
    return 0;
}
A.    1, 2, 13
B.     1, 4, 4
C.     -1, 2, -13
D.    -1, -2, -13
Answer: c
Explanation:
Note the below statement inside the struct:
int bit1:1; --> 'int' indicates that it is a SIGNED integer.
For signed integers the leftmost bit will be taken for +/- sign.
If you store 1 in 1-bit field: 

The left most bit is 1, so the system will treat the value as negative number.

The 2's complement method is used by the system to handle the negative values.

Therefore, the data stored is 1. The 2's complement of 1 is also 1 (negative).

Therefore -1 is printed.

If you store 2 in 4-bits field:

Binary 2: 0010 (left most bit is 0, so system will treat it as positive value)

0010 is 2 

Therefore 2 is printed.


If you store 13 in 4-bits field:

Binary 13: 1101 (left most bit is 1, so system will treat it as negative value)

Find 2's complement of 1101: 

1's complement of 1101 : 0010
2's complement of 1101 : 0011 (Add 1 to the result of 1's complement)

0011 is 3 (but negative value)

Therefore -3 is printed.

5.Point out the error
. struct emp
{
    int ecode;
    struct emp *e;
};

A.    Linker error
B.     No error
C.     Error in structure declaration
D.    None of the above
Answer: B
Explanation: this type of declaration is called as self-referential structure. Here *e is pointer to a struct emp.
6. Point out the error, if any in the program
#include<stdio.h>
 
int main()
{
    struct a
    {
        float category:5;
        char scheme:4;
    };
    printf("size=%d", sizeof(struct a));
    return 0;
}
A.    No error
B.     Error: invalid structure member in printf
C.     Error in this float category:5; statement
D.    none of the above
Answer: c
Explanation
Bit field type must be signed int or unsigned int.
The char type: char scheme:4; is also a invalid statement.


7. Point out the error, if any in the program
struct emp
{
    int ecode;
    struct emp e;
};
A.    Error in structure declration
B.     Linker error
C.     No error
D.    None of the above
Answer: A
Explanation:
The structure emp contains a member e of the same type.(i.e) struct emp. At this stage compiler does not know the size of sttructure.

8.Point out the error if any
#include<stdio.h>
#include<string.h>
void modify(struct emp*);
struct emp
{
    char name[20];
    int age;
};
int main()
{
    struct emp e = {"Sanjay", 35};
    modify(&e);
    printf("%s %d", e.name, e.age);
    return 0;
}
void modify(struct emp *p)
{
     p ->age=p->age+2;
}

A.    Error in structure
B.     Error: in prototype declaration unknown struct emp
C.     No error
D.    None of the above
Answer: b
9. one of elements of a structure can be a pointer to the same structure.
A. true
B. false
Answer: A
10. A structure can be nested inside another structure.
A. True                        B. False
Answer: a
11. Which of the following comment about the usage of structures in true?
[A] Storage class can be assigned to individual member
[B] Individual members can be initialized within a structure type declaration
[C] The scope of the member name is confined to the particular structure, within which it is defined
[D] None of above
AnswerC.
Explanation:
Structure is user defined data type which is used to store heterogeneous data under unique name.
12. For accessing a structure element using a pointer,you must use?
[A] Pointer operator (&)
[B] Dot operators(.)
[C] Pointer operator(*)
[D] Arrow operator(->)
Answer: D. Arrow operator(->)
Explanation:
For accessing the structure element using pointers arrow operator is used otherwise dot operator is used.
13. Which operator is used to connect structure name to its member name?
[A] dot operator(.)
[B] logical operator(&&)
[C] pointer operator(&)
[D] Arrow operator(->)
AnswerA. dot operator(.)
14. In C, structure values can be passed as arguments to function by?
[A] passing each member of the structure as an argument of function call
[B] passing copy of the entire structure to the called function
[C] passing structure as an argument using pointer
[D] All of above
AnswerD. All of above
15. A -> B is syntactically correct if?
[A] a and b are structure
[B] a is a structure and b is a pointer to structure
[C] a is a pointer to structure and b is a structure
[D] a is a pointer to structure in which b is a field
Answer: D. a is a pointer to structure in which b is a field
16. Which of the following is a collection of different data types?
[A] String
[B] Array
[C] Structure
[D] Files
Answer: C. Structure
Explanation:
Structure is a user defined data type which contains the variables of different data types.
17. What is the output of the program?
#include<stdio.h>
 
    struct course
    {
        int courseno;
        char coursename[25];
    };
int main()
{
    struct course c[] = { {102, "Java"}, 
                          {103, "PHP"}, 
                          {104, "DotNet"}     };
 
    printf("%d ", c[1].courseno);
    printf("%s\n", (*(c+2)).coursename);
    return 0;
}
A.   103 DotNet
B.   102 Java
C.   103 PHP
D.   104 DotNet
Answer: A
18. User-defined data type can be derived by___________.
A. struct
B. enum
C. typedef
D. All of the mentioned
Answer: D
19. Which operator connects the structure name to its member name?
A. -
B. .
C. Both (b) and (c)
Ansswer: b
20. Which of the following cannot be a structure member?
A. Another structure
B. Function
C. Array
D. None of the mentioned
Answer: B
21. Which of the following structure declaration will throw an error?
A. struct temp{}s;
main(){}
B. struct temp{};
struct temp s;
main(){}

C. struct temp s;
struct temp{};
main(){}

D. None of the mentioned
Answer: d
22.  Number of bytes in memory taken by the below structure is?

struct test
{
int k;
char c;
};
A. Multiple of integer size
B. integer size+character size
C. Depends on the platform
D. Multiple of word size
Answer: b
23. Which of the following data types are accepted while declaring bit-fields?
A. char
B. float
C. double
D. None of the mentioned
Answer: a
24.  In the declaration of bit-fields,
     struct-declarator:
     declarator
     type-specifier declarator opt : constant-expression
     The constant-expression specifies
A. The width of the field in bits.
B. Nothing
C. The width of the field in bytes.
D. Error
Answer: a
25. Bit fields can only be declared as part of a structure.
·         A. false
·         B. true
·         C. Nothing
·         D. Varies
Answer: b
26.  The correct syntax to access the member of the ith structure in the array of structures is?
    Assuming: struct temp
    {
        int b;
    }s[50];
a) s.b.[i];
b) s.[i].b;
c) s.b[i];
d) s[i].b;
Answer: d
27. What is the output of the program?
 #include <stdio.h>
    struct temp
    {
        int a;
        int b;
        int c;
    };
    main()
    {
        struct temp p[] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    }
a) No Compile time error, generates an array of structure of size 3
b) No Compile time error, generates an array of structure of size 9
c) Compile time error, illegal declaration of a multidimensional array
d) Compile time error, illegal assignment to members of structure
Answer: a




28.  What is the correct syntax to declare a function foo() which receives an array of structure in     function?
a) void foo(struct *var);
b) void foo(struct *var[]);
c) void foo(struct var);
d) None of the mentioned
Answer:a
29. What is the output of this C code?
    (Assuming size of int be 4)
    #include <stdio.h>
    struct temp
    {
        int a, b, c;
    } p[] = {0};
    main()
    {
        printf("%d", sizeof(p));
    }
a) 4
b) 12
c) 16
d) Can’t be estimated due to ambigous initialization of array
Answer: b
30.What is the output of this C code?
    #include <stdio.h>
    struct student
    {
        char *name;
    };
    struct student s[2], r[2];
    void main()
    {
        s[0].name = "alan";
        s[1] = s[0];
        r = s;
        printf("%s%s", r[0].name, r[1].name);
    }
a) alan alan
b) Compile time error
c) Varies
d) Nothing
Answer:b

31.What is the output of this C code?
    #include <stdio.h>
    struct student
    {
        char *name;
    };
    void main()
    {
        struct student s[2], r[2];
        s[1] = s[0] = "alan";
        printf("%s%s", s[0].name, s[1].name);
    }
a) alan alan
b) Nothing
c) Compile time error
d) Varies
Answer: c

32.Which of the following are incorrect syntax for pointer to structure?
    (Assuming struct temp{int b;}*my_struct;)
a) *my_struct.b = 10;
b) (*my_struct).b = 10;
c) my_struct->b = 10;
d) Both (a) and (b)
Answer: a

33.Which of the following is an incorrect syntax to pass by reference a member of a structure in      a function?
    (Assume: struct temp{int a;}s;)
a) func(&s.a);
b) func(&(s).a);
c) func(&(s.a));
d) None of the mentioned
Answer: d
34. For the following function call which option is not possible?
    func(&s.a); //where s is a variable of type struct and a is the member of the struct.
a) Compiler can access entire structure from the function.
b) Individual member’s address can be displayed in structure.
c) Individual member can be passed by reference in a function.
d) Both (b) and (c).
       Answer: a




35.Comment on the output of this C code?
    #include <stdio.h>
    struct temp
    {
        int a;
    } s;
    void change(struct temp);
    main()
    {
        s.a = 10;
        change(s);
        printf("%d\n", s.a);
    }
    void change(struct temp s)
    {
        s.a = 1;
    }
a)      Output will be 1
b) Output will be 10
c) Output varies with machine
d) Compile time error
Answer: b
36.What is the output of this C code?
    #include <stdio.h>
    struct student
    {
        char *c;
    };
    void main()
    {
        struct student *s;
        s->c = "hello";
        printf("%s", s->c);
    }
a)      hello
b) Segmentation fault
c) Run time error
d) Nothing
Answer: b
Explanation: A pointer holds an address value... the data residing in a memory location should be assigned to the pointer as 
                    struct student m;
        struct student *s = &m;
        s->c = "hello";
only then the code runs.


37.What is the output of the code?

#include <stdio.h>
    struct student
    {
        char *c;
    };
    void main()
    {
        struct student m;
        struct student *s = &m;
        (*s).c = "hello";
        printf("%s", m.c);
    }
a) Run time error
b) Nothing
c) Varies
d) hello
Answer: d

38.What is the output of this C code?
    #include <stdio.h>
    struct p
    {
        int x[2];
    };
    struct q
    {
        int *x;
    };
    int main()
    {
        struct p p1 = {1, 2};
        struct q *ptr1;
        ptr1->x = (struct q*)&p1.x;
        printf("%d\n", ptr1->x[1]);
    }
a)      Compile time error
b) Segmentation fault/code crash
c) 2
d) 1
Answer: b



39.What is the output of this C code?
    #include <stdio.h>
    struct p
    {
        int x;
        char y;
    };
    int main(){
        struct p p1[] = {1, 92, 3, 94, 5, 96};
        struct p *ptr1 = p1;
        int x = (sizeof(p1) / sizeof(struct p));
        printf("%d %d\n", ptr1->x, (ptr1 + x - 1)->x);
    }
a) Compile time error
b) Undefined behaviour
c) 1 3
d) 1 5
Answer: d

40.What is the output of this C code?
    #include <stdio.h>
    typedef struct student
    {
        char *a;
    }stu;
    void main()
    {
        struct stu s;
        s.a = "hi";
        printf("%s", s.a);
    }
a) Compile time error
b) Varies
c) hi
d) h
Answer: a
Explanation: Struct stu s is undefined behaviour, instead it should be struct student s.



41. What is the output of this C code?
    #include <stdio.h>
    typedef struct student
    {
        char *a;
    }stu;
    void main()
    {
        struct student s;
        s.a = "hey";
        printf("%s", s.a);
    }
a) Compile time error
b) Varies
c) he
d) hey
Answer: d

42. What is the output of this C code?
    #include <stdio.h>
    typedef int integer;
    int main()
    {
        int i = 10, *ptr;
        float f = 20;
        integer j = i;
        ptr = &j;
        printf("%d\n", *ptr);
        return 0;
    }
a) Compile time error
b) Undefined behaviour
c) Depends on the standard
d) 10
Answer: d
Explanation: pointer assignment and printing.

43. What is the output of this C code?
    #include <stdio.h>
    typedef struct p
    {
        int x, y;
    };
    int main()
    {
        p k1 = {1, 2};
        printf("%d\n", k1.x);
    }
a) Compile time error
b) 1
c) 0
d) Depends on the standard
Answer: a
Explanation: error in line 8

44. What is the output of this C code?
    #include <stdio.h>
    typedef struct p
    {
        int x, y;
    }k = {1, 2};
    int main()
    {
        p k1 = k;
        printf("%d\n", k1.x);
    }
a) Compile time error
b) 1
c) 0
d) Depends on the standard
Answer: a
45. What is the output of this C code?
    #include <stdio.h>
    typedef struct p
    {
        int x, y;
    }k;
    int main()
    {
        struct p p = {1, 2};
        k k1 = p;
        printf("%d\n", k1.x);
    }
a) Compile time error
b) 1
c) 0
d) Depends on the standard
Answer:b




 46. What is the output of this C code?
    #include <stdio.h>
    struct student
    {
        char *c;
        struct student *point;
    };
    void main()
    {
        struct student s;
        printf("%d", sizeof(s));
    }
a) 5
b) 9
c) 8
d) 16
Answer: c

47. What is the output of this C code?
    #include <stdio.h>
    struct student
    {
        char *c;
        struct student *point;
    };
    void main()
    {
        struct student s;
        struct student *m = &s;
        printf("%d", sizeof(student));
    }
a) Compile time error
b) 8
c) 5
d) 16
Answer: a
48. What is the output of this C code?
    #include <stdio.h>
    void main()
    {
        struct student
        {
            int no;
            char name[20];
        };
        struct student s;
        no = 8;
        printf("%d", no);
    }
a) Nothing
b) Compile time error
c) Junk
d) 8
Answer: b
Explanation: assignment of value to a structure element is wrong here, thus leading to compile time error.

49. What is the output of this C code?
    #include <stdio.h>
    struct point
    {
        int x;
        int y;
    };
    struct notpoint
    {
        int x;
        int y;
    };
    int main()
    {
        struct point p = {1};
        struct notpoint p1 = p;
        printf("%d\n", p1.x);
    }
a) Compile time error
b) 1
c) 0
d) Undefined
Answer: a

50. What is the output of this C code?
    #include <stdio.h>
    struct student
    {
        char *name;
    };
    struct student fun(void)
    {
        struct student s;
        s.name = "alan";
        return s;
    }
    void main()
    {
        struct student m = fun();
        printf("%s", m.name);
    }
a) Nothing
b) alan
c) Run time error
d) Varies
Answer: b

 Previous                Next

No comments:

Post a Comment