Structures And Unions C Programming Interview Questions- Basics Viva Tutorials

1.A bit field is
a) A pointer variable in a structure.
b) One bit or a set of adjacent bits within a word
c) A pointer variable in a union
d) Not used in C
ANSWER: B

2.Union differs from structure in the following way
a) All members are used at a time
b) Only one member can be used at a time
c) Union cannot have more members
d) Union initialized all members as structure
ANSWER: B

3.What type of structure is created by the following definition?
struct first { . . . ; struct second *s};
struct second { . . . ; struct first *f};
a) Nested structure
b) Self-referential structure
c) Invalid structure
d) Structured structure
ANSWER: B

4.Identify the wrong syntax
a) typedef struct { member declaration; } NAME; NAME V1, V2; 
b) typedef struct tag{ member declaration; } NAME; NAME V1, V2; 
c) typedef struct { member declaration; } NAME; NAME V1, V2; 
d) typedef struct tag { member declaration; } NAME; NAME V1, V2; 
ANSWER: D

5.The changes made in the members of a structure are available in the calling function if
a) pointer to structure is passed as argument
b) structure variable is passed
c) the member other then pointer type are passed as argument
d) both option a and c
ANSWER: A

6.About structure which of the following is true.
1. Structure members are aligned in memory depending on their data type.
2. The size of a structure may not be equal to the sum of the size of its members.
a) Only option 1
b) Only option 2
c) Both option 1 and 2
d) Neither option 1 nor 2
ANSWER: C

7.Referring to the code Below, which of the following will make the speed equal to 200?
struct car
{
int speed;
car type[10];
} vehicle;
struct car *ptr;
ptr = &vehicle;
a) ( *ptr).speed = 200.
b) ( *ptr) ->speed = 200.
c) *ptr.speed = 200.
d) &ptr.speed = 200.
ANSWER: A

8.The statement statmentN
struct date
{
int day;
int month;
int year;
};
main()
{
struct date *d;
. . . .
++d -> day; /*statmentN */
. . . .
}
a) Increments the pointer to point month
b) Increment the value of day
c) Increment d by sizeof( struct date)
d) None
ANSWER: B

9.Consider the following structure.
struct numname
{
int no;
char name[25];
};
struct numname n1[] = {
{12, “Raja”},
{15, Selvan},
{18, Prema},
{21, “Naveen”}
};
The output of the following statement would be:
printf(“%d, %d”,n1[2].no, ( *( n1 + 2)).no);
a) 18, ASCII value of p
b) 18, 18
c) 18, ASCII value of r
d) 18, ASCII value of e
ANSWER: B

10.What is the output of the following program?
struct x x
{
int a;
long b;
} s;
union y y
{
int a;
long b;
} u;
print sizeof( s ) and sizeof( u ) if sizeof( int ) = 4 and sizeof( long ) = 4.
a) sizeof( s ) = 8, sizeof( u ) = 4.
b) sizeof( s ) = 4, sizeof( u ) = 4.
c) sizeof( s ) = 4, sizeof( u ) = 8.
d) sizeof( s ) = 8, sizeof( u ) = 8.
ANSWER: A

11.Whether the below code is correct or wrong?
struct list
{
int x;
struct list *next;
} *head;
head.x = 100;
a) Use (*head).x = 100
b) Use ( head*).x = 100
c) Use head ->x = 100
d) None 
ANSWER: C

12.What is the output of the program?
#include <stdio.h>
main()
{
struct s1 { int i ;};
struct s2 { int i ;};
struct s1 st1;
struct s2 st2;
st1.i = 5;
st2 = st1;
printf(“ %d”, st2.i);
}
a) 5
b) 1004
c) Syntax error
d) None 
ANSWER: C

13.For the following declaration 
union x {
char ch;
int I;
double j;
} u_var;
What is the value of sizeof( u_var)?
a) Same as sizeof( int )
b) Same as sizeof( double)
c) Same as sizeof( char )
d) None 
ANSWER: B

14.What is the output of the following program?
#include <stdio.h>
typedef struct NType
{
int I;
char c;
long x;
} NewType;
main()
{
NewType *c;
c = ( NewType *) malloc( sizeof(NewType));
c-> = 100;
c->c = ‘C’;
( *c).x = 100L;
printf(“(%d %c %4Ld)”,c->I, c->c, c->x);
}
a) 100 100 100L
b) 100 C 100
c) 100 100 C
d) None
ANSWER: B

15.The size of the following union, where an int occupies 4 bytes of memory is
union arc
{
char x;
int y;
char ax[8];
}aha;
a) 16 byte
b) 13 byte
c) 8 byte
d) 4 byte
ANSWER: C

16.Which of the following is the correct way to increment the variable ”ptr” to point to the next member of the array from the sample above?
union rainbow
{
int a [5];
float x [5];
};
union rainbow color [20];
void *ptr = color;
a) ptr = ptr + sizeof( rainbow.a);
b) ptr = ( void*)((union rainbow*) ptr + 1);
c) ptr = ptr + sizeof( *ptr);
d) ++(int*)ptr;
ANSWER: B

17. What is the size of ptr1 and ptr2?
struct x
{
int j;
char k[ 100];
unsigned I;
};
int *ptr1;
struct x *ptr2;
a) Same depending on the model used
b) 2, 104
c) 2, undefined for memory is not allocated
d) 2, 4
ANSWER: A

18.Which of these are valid declaration?
i) union { int I; int j;};
ii) union u_tag { int I; int j;} u;
iii) union { int I; int j; FILE *K};
iv) union { int I; int j;} u;
a) All are correct
b) Option (i), (ii),and(iv)
c) Option (ii) and (iv)
d) Option (ii)only
ANSWER: C

19.You cannot access x by the following.
struct
{
int x;
int y;
}abc;
1. abc -> x
2. abc[0] ->x
3. abc.x
4. (abc) ->x
a) Option 1,2 and 4
b) Option 2 and 3
c) Option 1 and 3
d) Option 1,3 and 4
ANSWER: A

20. struct customer *ptr = malloc( sizeof( struct customer));
Given the sample allocation for the pointer ”ptr” found, which statement would be used to reallocate ptr to be an array of 10 element?
a) ptr = realloc( ptr, 10 * sizeof( sizeof customer));
b) ptr = realloc( ptr, 9 * sizeof( struct customer) );
c) realloc(ptr, 9 * sizeof(struct customer));
d) realloc( ptr, 10 * sizeof( struct customer));
ANSWER: A

Related

Unions 7262960261579924589

Post a Comment

SPAMMING will not be Appreciated.

emo-but-icon

Hot in week

Recent

Comments

Our Channel

Contact Us

Name

Email *

Message *

item