What is the output for the program? typedef enum type{t1, t2, t3,}t2; main(){t2 x; x=1; printf("%s",x);}
Answers
Answer:
C Structure & Union
123
Question 1 WRONG
#include‹stdio.h›
int main()
{
struct site
{
char name[] = "GeeksQuiz";
int no_of_pages = 200;
};
struct site *ptr;
printf("%d ", ptr->no_of_pages);
printf("%s", ptr->name);
getchar();
return 0;
}
Run on IDE
A
200 GeeksQuiz
B
200
Runtime Error
Compiler Error
C Structure & Union
Discuss it
Question 1 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.
Question 2
Assume that size of an integer is 32 bit. What is the output of following program?
#include<stdio.h>
struct st
{
int x;
static int y;
};
int main()
{
printf("%d", sizeof(struct st));
return 0;
}
Run on IDE
A
4
B
8
C
Compiler Error
D
Runtime Error
C Structure & Union
Discuss it
Question 3
struct node
{
int i;
float j;
};
struct node *s[10];
Run on IDE
The above C declaration define 's' to be (GATE CS 2000)
A
An array, each element of which is a pointer to a structure of type node
B
A structure of 2 fields, each field being a pointer to an array of 10 elements
C
A structure of 3 fields: an integer, a float, and an array of 10 elements
D
An array, each element of which is a structure of type node.
C Structure & Union
Discuss it
Question 4
Consider the following C declaration
struct {
short s[5];
union {
float y;
long z;
}u;
} t;
Run on IDE
Assume that objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement for variable t, ignoring alignment considerations, is (GATE CS 2000)
A
22 bytes
B
14 bytes
C
18 bytes
D
10 bytes
C Structure & Union
Discuss it
Question 5
#include<stdio.h>
struct st
{
int x;
struct st next;
};
int main()
{
struct st temp;
temp.x = 10;
temp.next = temp;
printf("%d", temp.next.x);
return 0;
}
Run on IDE
A
Compiler Error
B
10
C
Runtime Error
D
Garbage Value
C Structure & Union
Discuss it
Question 6
Which of the following operators can be applied on structure variables?
A
Equality comparison ( == )
B
Assignment ( = )
C
Both of the above
D
None of the above
C Structure & Union
Discuss it
Question 7
union test
{
int x;
char arr[8];
int y;
};
int main()
{
printf("%d", sizeof(union test));
return 0;
}
Run on IDE
Predict the output of above program. Assume that the size of an integer is 4 bytes and size of character is 1 byte. Also assume that there is no alignment needed.
A
12
B
16
C
8
D
Compiler Error
C Structure & Union
Discuss it
Question 8
union test
{
int x;
char arr[4];
int y;
};
int main()
{
union test t;
t.x = 0;
t.arr[1] = 'G';
printf("%s", t.arr);
return 0;
}
Run on IDE
Predict the output of above program. Assume that the size of an integer is 4 bytes and size of character is 1 byte. Also assume that there is no alignment needed.
A
Nothing is printed
B
G
C
Garbage character followed by 'G'
D
Garbage character followed by 'G', followed by more garbage characters
E
Compiler Error
C Structure & Union
Discuss it
Question 9
# include <iostream>
# include <string.h>
using namespace std;
struct Test
{
char str[20];
};
int main()
{
struct Test st1, st2;
strcpy(st1.str, "GeeksQuiz");
st2 = st1;
st1.str[0] = 'S';
cout << st2.str;
return 0;
}
Run on IDE
A
Segmentation Fault
B
SeeksQuiz
C
GeeksQuiz
D
Compiler Error
C Structure & Union
Discuss it
Question 10
Predict the output of following C program
#include<stdio.h>
struct Point
{
int x, y, z;
};
int main()
{
struct Point p1 = {.y = 0, .z = 1, .x = 2};
printf("%d %d %d", p1.x, p1.y, p1.z);
return 0;
}
Run on IDE
A
Compiler Error
B
2 0 1
C
0 1 2
D
2 1 0
C Structure & Union
Discuss it