structure programs in c

structure questions in c

1. What will be the output of the following program. ?

#include‹ stdio.h ›
#include< conio.h >
int
main()
{
struct str
{
char name[] = “programmingknow”;
int no_of_pages = 300;
};
struct str *ptr;
printf(“%d “, ptr->no_of_pages);
printf(“%s”, ptr->name);
getch();
return 0;
}

(a) 300 programmingknow
(b) 300
(c) Compiler Error
(d) Runtime Error

Show Answer

Answer: (c)
Explanation: When we declare a structure or union, we actually declare a new data type suitable for our purpose. So we cannot initialize values as it is not a variable declaration but a data type declaration.

2.Assume that size of an integer if it is 32 bit. What is the output of following program?

#include
struct
struc
{
int a;
static int b;
};
int main()
{
printf(“%d”, sizeof(struct struc));
return 0;
}

a. 4
b. 10
c. Compiler error
d. Runtime error

Show Answer

Answer: (C)
Explanation: In C, struct and union types cannot have static members. In C++, struct types are allowed to have static members, but union cannot have static members in C++ also.

3. What is the output of this Program.?

#include
struct
student
{
char *fname;
};
void main()
{
struct student s, m;
s.fname = “st”;
m = s;
printf(“%s%s”, s.fname, m.fname);
}

a) Compile time error
b) Nothing
c) Junk values
d) st st

Show Answer

Answer: d
Explanation: None.

4. Presence of code like “s.t.b = 10” indicates?

a) Syntax Error
b) Structure
c) double data type
d) An ordinary variable name

Show Answer

Answer: b
Explanation: None.

5. An Array elements always stored in ___________ memory location ?

a. Sequential
b. Sequential and rendom
c. Random
d. Non of these

Show Answer

The correct option is (a)
Explanation:

6. What will be print after execution of the program?

#include< stdio.h >
int
main()
{
int num[10]={4,5,6,7,8};
printf(“%d”,num[5]);
return 0;
}

a. Garbage value
b. 0
c. 6
d. 8

Show Answer

The correct option is (b)
Explanation:
When an array is initialized at the time of declaration then the remaining elements of the array is initialized to 0 by the compiler default.