C Structures Set 1

Ques: 1 Which of the following is a properly defined struct?
A. struct {int a;} 
B. struct a_struct {int a;}
C. struct a_struct int a;
D. struct a_struct {int a;};
Description
Answer: D

Ques: 2 What will be the output of the following program?
#include<stdio.h>
int main()
{
    struct result
    {
     int marks;
     char grade;
    };
    struct result a,b;
    a.marks=30;
    a.grade='b';
    b=a;
    printf("%d ",b.marks);
    printf("%c",b.grade);
    return 0;
}
A. Error because struct object cannot be copied
B. garbage garbage
C. 30 b
D. None of these
Description
Answer: C
We can assign values of structure variable to another structure variable , if both variables are defined of same type.

Ques: 3 What will be the output of the following program?
#include<stdio.h>
int main(void)
{
    struct student stu1={"Raju", 10},stu2={"Raju",12};
    if(stu1 == stu2)
     printf("Same\n");
    else
     printf("Not same\n");
    return 0;
}
A. Same
B. Not same  
C. Error
D. None of these
Description
Answer: C
Unary, relational,arithmetic,bitwise operators are not allowed with structure variables. Although they can be used with the members(if member itself is not a structure).

Ques: 4 What will be the output of the following program?
#include<stdio.h>
struct tagname{int i; char c;};
void increa(struct tagname);
int main(void)
{
    struct tagname var = {2,'d'};
    increa(var);
    printf("%d ",var.i);
    printf("%c",var.c);
    return 0;
}
void increa(struct tagname var)
{
    var.i++;
    var.c++;
}
A. 3 e
B. l value required
C. 2 d
D. None of these
Description
Answer: C
Whenever we send structure variable as a argument to a function a copy of variable is made inside the called function and all changes are done on that copy.Any change thus made is not visible in the calling function.

Ques: 5 What will be the output of the following program?
#include<stdio.h>
struct {int i; char c;}var = {2,'s'};
void func(struct {int i; char c;} v);
int main(void)
{ 	
    func(var);
    return 0;
}
void func(struct {int i; char c;} v)
{
    printf("%d   %c\n",v.i++,v.c++);
}
A. 2 s
B. 3 t
C. lvalue required 
D. Error
Description
Answer: D
This program will flash error because structure cannot be used in this way as an argument to a function. 

Ques: 6 What will be the output of the following program?
#include<stdio.h>
typedef struct tag{int i; char c;}tagname;   
int main(void)
{
    tagname v1={1,'A'};			
    tagname v2={2,'B'};
    printf("%d  %c  %d  %c\n",v1.i,v1.c,v2.i,v2.c);
    return 0;
}
A. Error at tag v2(struct keyword absent)
B. 1 A 2 B
C. Error(typedef cannot be used with structure)
D.  None of these.
Description
Answer: B
The program works fine and 1 A 2 B gets printed. When a typedef is used with structure,  struct keyword can be omitted and its tagname can be used to create structure variable.

Ques: 7 What will be the output of the following program?
#include<stdio.h>
struct
{
    int n;
    char c;
};
int main()
{
    struct c;
    c.n = 10;
    printf("%d\n", c.n);
}
A. Compile time error
B. 10
C. Undefined behaviour
D. Segmentation fault
Description
Answer: A
Note the declaration of structure. Structure variable can be created in two ways – with definition of structure or by using structure tagname.

Ques: 8 What will be the output of the following program?
#include<stdio.h>
struct
{
    int n;
    char c;
} a;
int a  = 10;
int main()
{
    a.n = 10;
    printf("%d %d\n", a.n, a);
}
A. 10 10
B. depends on compiler 
C. compile time error
D. None of these
Description
Answer: C
Since a is declared as a structure variable we cannot define a new integer variable a. Hence it is a compile time error

Ques: 9 What will be the output of the following program?
#include<stdio.h>
struct
{
    int n;
    char c;
} a;
int n  = 10;
int main()
{
    a.n = 10;
    printf("%d %d\n", a.n, n);
}
A. Error
B. 10 10
C. Compiler dependent
D. None of these
Description
Answer: B
The code works fine this time because n is a structure member here.

Ques: 10 What will be the output of the following program?
struct
{
    char ch;
    int num; //int 4 byte
}var;
int main()
{    
    int l=sizeof(var);
    printf("%d",l);
    return 0;
}
A. 5 bytes
B. 9 bytes
C. 8 bytes
D. Depends on machine
Description
Answer: D
Size of this structure on most of the machines would be 8 bytes because there will be hole of 3 bytes in between.Some machines store integers only at addresses which are multiple of 4 this creates a hole of 3 bytes in between to satisfy the alignment requirements.


comments powered by Disqus