WorksheetsC Structures and Unions
Total questions: 84
Worksheet time: 1hrs 24mins
Pick the best statement for the below program: #include "stdio.h" int main() { struct {int a[2];} arr[] = {{1},{2}}; printf("%d %d %d %d", arr[0].a[0], arr[0].a[1], arr[1].a[0], arr[1].a[1]); return 0; }
Compile error because arr has been defined using struct type incorrectly. First struct type should be defined using tag and then arr should be defined using that tag.
Compile error because apart from definition of arr, another issue is in the initialization of array of struct i.e. arr[].
Compile error because of initialization of array of struct i.e. arr[].
No compile error and it'll print 1 2 0 0
No compile error and it'll print 1 0 2 0
Choose the correct output from the options given below:
#include
200 GeeksQuiz
200
Runtime Error
Compiler Error
Pick the best statement for the below program snippet: struct {int a[2];} arr[] = {1,2};
No compile error and it'll create array arr of 2 elements. Each of the element of arr contain a struct field of int array of 2 elements. arr[0].a[0] would be 1 and arr[1].a[0] would be 2.
No compile error and it'll create array arr of 2 elements. Each of the element of arr contain a struct field of int array of 2 elements. arr[0].a[0] would be 1 and arr[0].a[1] would be 2. The second element arr[1] would be ZERO i.e. arr[1].a[0] and arr[1].a[1] would be 0.
No compile error and it'll create array arr of 1 element. Each of the element of arr contain a struct field of int array of 2 elements. arr[0].a[0] would be 1 and arr[0].a[1] would be 2.
None of the above
Which of the following operators can be applied on structure variables?
Equality comparison ( == )
Assignment ( = )
Both of the above
None of the above
Consider the following C declaration struct { short s[5]; union { float y; long z; } u; } t; 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)
22 bytes
14 bytes
18 bytes
10 bytes
Consider the following C declaration struct { short s[5]; union { float y; long z; } u; } t; Assume that the 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 consideration, is
22 bytes
18 bytes
14 bytes
10 bytes
Consider the following declaration : struct addr { char city[10]; char street[30]; int pin; }; struct { char name[30]; int gender; struct addr locate; } person, *kd = &person; Then (*(kd -> name + 2) can be used instead of
person.name + 2
kd -> (name + 2)
*((*kd).name + 2)
either (A) or (B), but not (C)
Pick the best statement for the below program:
#include
Compile error due to incorrect syntax of initialization.
No compile error and it'll print "0 100".
Pick the best statement for the below program:
#include
Compile error because struct type (containing two fields of dissimilar type i.e. an int and a char) has been mentioned along with definition of myVar of that struct type.
Compile error because of incorrect syntax of initialization of myVar. Basically, member of operator (i.e. dot .) has been used without myVar.
Compile error for not only B but for incorrect order of fields in myVar i.e. field c has been initialized first and then field i has been initialized.
No compile error and it'll print 100 A.
Pick the best statement for the below program: #include "stdio.h" int main() { struct {int a[2], b;} arr[] = {[0].a = {1}, [1].a = {2}, [0].b = 1, [1].b = 2}; printf("%d %d %d and", arr[0].a[0], arr[0].a[1], arr[0].b); printf("%d %d %d\n", arr[1].a[0], arr[1].a[1], arr[1].b); return 0; }
Compile error because struct type (containing two fields i.e. an array of int and an int) has been specified along with the definition of array arr[] of this struct type.
Compile error because of incorrect syntax for initialization of array arr[].
No compile error and two elements of arr[] would be defined and initialized. Output would be "1 0 1 and 2 0 2".
No compile error and two elements of arr[] would be defined and initialized. Output would be "1 X 1 and 2 X 2" where X is some garbage random number.
Assume that size of an integer is 32 bit. What is the output of following program?
#include
4
8
Compiler Error
Runtime Error
In the following program snippet, both s1 and s2 would be variables of structure type defined as below and there won't be any compilation issue. typedef struct Student { int rollno; int total; } Student; Student s1; struct Student s2;
TRUE
FALSE
Anyone of the following can be used to declare a node for a singly linked list and "NODEPTR nodePtr;" can be used to declare pointer to a node using any of the following /* First declaration */ typedef struct node { int data; struct node *nextPtr; } * NODEPTR; /* Second declaration */ struct node { int data; struct node * nextPtr; }; typedef struct node * NODEPTR;
TRUE
FALSE
Anyone of the followings can be used to declare a node for a singly linked list. If we use the first declaration, "struct node * nodePtr;" would be used to declare pointer to a node. If we use the second declaration, "NODEPTR nodePtr;" can be used to declare pointer to a node. /* First declaration */ struct node { int data; struct node * nextPtr; }; /* Second declaration */ typedef struct node{ int data; NODEPTR nextPtr; } * NODEPTR;
TRUE
FALSE
Predict the output of following C program
#include
Compiler Error
2 0 1
0 1 2
2 1 0
Consider the following C program: 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; } What is the output?
Segmentation Fault
SeeksQuiz
GeeksQuiz
Compiler Error
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; } Predict the output of the 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.
Nothing is printed
G
Garbage character followed by 'G'
Garbage character followed by 'G', followed by more garbage characters
Compiler Error
union test { int x; char arr[8]; int y; }; int main() { printf("%d", sizeof(union test)); return 0; } Predict the output of the 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.
12
16
8
Compiler Error
Choose the correct output from the options given below:
#include
Compiler Error
10
Runtime Error
Garbage Value
struct node { int i; float j; }; struct node *s[10]; The above C declaration define 's' to be
An array, each element of which is a pointer to a structure of type node
A structure of 2 fields, each field being a pointer to an array of 10 elements
A structure of 3 fields: an integer, a float, and an array of 10 elements
An array, each element of which is a structure of type node
Which of the following are themselves a collection of different data types?
String
Struct
char
all
Which operator connects the structure name to its member name?
-
=
.
>>
Which of the following cannot be a structure member?
another structure
array
Function
None
Which of the following structure declaration will throw an error?
struct temp{ } s;
main(){}
struct temp{ };
struct temp s;
main(){ }
struct temp s;
struct temp{ };
main(){ }
None
What is the output of this C code?
void main()
{
struct student
{
int no;
char name[20];
};
struct student s;
s.no = 8;
printf("%d", s.no);
}
Nothing
Compile error
8
Junk
Size of a union is determined by size of the.
First member in the union
Last member in the union
C. Biggest member in the union
Sum of the sizes of all members
Assuming size of long int is 4 bytes, what will be the output for:
typedef long long int L = 24;
printf("Size of long long int is :");
printf("%d",sizeof(L));
24
4
8
Error
What will be the output
Name:
Subject:
Percentage: 86.50000
Name:Raju
Subject:
Percentage: 86.50000
Name: Raju
Subject: Maths
Percentage: 86.50000
Error
Memory allocated for structure members are always contigious
True
False
What do you understand by:
void update(struct student *s);
update is a function that takes an argument as pointer to structure variable and returns nothing
update is a method that takes structure type variable as parameter
Error, we cannot pass structure variable as pointer to a function
update takes pointer argument but it cannot return void
What will be the size of the following structure? #include
5
11
41
44
What is the size of the bit field?
1 to 18 bits
1 to 16 bits
1 to 8 bits
1 to 24 bits
Number of bytes in memory taken by the below structure is Struct test { int k; char c; };
Multiple of integer size
integer size+ character size
Depends on the platform
Multiple of word size
Which of the following share a similarity in syntax? 1. Union, 2. Structure, 3. Arrays and 4. Pointers
3 and 4
1 and 2
1 and 3
1, 3 and 4
Any C Program must contain
Must contain at least one function.
Need not contain any function.
Needs input data.
None of the above
Use of functions
Helps to avoid repeating a set of statements many times.
Enhances the logical clarity of the program.
Helps to avoid repeated programming across programs
Makes the debugging task easier.
All of the above
What is function?
Function is a block of statements that perform some specific task.
Function is the fundamental modular unit. A function is usually designed to perform a specific task.
Function is a block of code that performs a specific task. It has a name and it is reusable.
All of the above
What is the output:
10
9
11
12
which is the formal parameter?
10
i
which is the actual parameter?
10
i
which are the actual arguments/parameters?
n1 and n2
sum and result
a and b
which are the formal arguments/parameters?
n1 and n2
sum and result
a and b
final value of n1 and n2?
1 and 3
2 and 4
struct.salary
structjob.salary
sJob.salary
What will be the output?
#include<stdio.h>
struct Point {
int x, y; };
int main() {
struct Point p = {10, 20};
printf("%d %d", p.x, p.y);
return 0; }
0 0
10 0
10 20
Compilation error
What is the size of the following structure (assuming 4-byte int)?
struct A {
int x;
char y; };
5
8
6
4
Which of these is a correct structure declaration?
struct { int a; } obj;
struct obj { int a }
struct obj = { int a; }
structure obj { int a; }
What will this program output?
#include<stdio.h>
struct Student {
char name[10];
int age; };
int main() {
struct Student s = {"Raj", 21};
struct Student t = s;
printf("%s %d", t.name, t.age);
return 0; }
Raj 21
Garbage values
Compilation error
Segmentation fault
Can we compare structure variables using == operator directly?
Yes
No
Only for int members
Only if typedef is used
Output of the following program?
#include<stdio.h>
struct Test {
int a; float b; };
int main() {
struct Test t1 = {10, 20.5};
struct Test t2 = t1;
t2.a = 15;
printf("%d %.1f", t1.a, t2.b);
return 0; }
15 20.5
10 20.5
10 0.0
Garbage
What is the output?
#include<stdio.h>
struct A {
int a;
struct B { int b; } inner; };
int main() {
struct A x = {5, {10}};
printf("%d %d", x.a, x.inner.b);
return 0; }
5 10
10 5
Compilation error
Garbage
What will this print?
#include<stdio.h>
struct A { int arr[3]; };
int main() {
struct A a = {{1, 2, 3}};
printf("%d", a.arr[1]);
return 0; }
1
2
3
Garbage
What is the output of this code?
#include<stdio.h>
struct A { int x; };
int main() {
struct A a = {10};
struct A b = {20};
if (a.x == b.x)
printf("Equal");
else
printf("Not Equal");
return 0; }
Equal
Not Equal
Compilation error
Garbage
What will this output?
#include<stdio.h>
union Data {
int i;
float f; };
int main() {
union Data d;
d.i = 10;
d.f = 2.5;
printf("%d", d.i);
return 0; }
10
0
Garbage value
2
What is the key difference between structure and union?
Structures are faster than unions
Structure members share memory
Union uses more memory
Union members share memory
Size of this union?
union U {
int a;
char b[4];
};
4
8
5
Compiler dependent
What is the output of this union code?
#include<stdio.h>
union U {
int x;
char y; };
int main() {
union U u;
u.x = 512;
printf("%d", u.y);
return 0; }
0
512
2
Garbage
Choose the correct one about typedef and structures
typedef is mandatory for structures
typedef creates a new structure
typedef gives a new name to existing type
typedef is used only for pointers
Output of this program?
#include<stdio.h>
struct S { int a; };
int main() {
struct S arr[2] = {{1}, {2}};
printf("%d", arr[1].a);
return 0; }
1
2
Compilation error
Garbage
Which one is valid syntax for structure inside structure?
struct A { struct B b; };
struct A { B b; };
struct A { B b; }; struct B { int x; };
struct A { struct B; };
What happens here?
#include<stdio.h>
struct A { int x; };
int main() {
struct A a = {10};
struct A b = a;
b.x = 20;
printf("%d %d", a.x, b.x);
return 0; }
20 20
10 20
10 10
Compilation error
Which of these is true about arrays of structures?
Structure must be pointer to use array
Only int members allowed
You can create arrays of structures
Arrays inside structures not allowed
What is the output?
#include<stdio.h>
struct Book {
char name[10];
int price; };
int main() {
struct Book b[] = {{"C", 200}, {"Java", 300}};
printf("%s %d", b[1].name, b[1].price);
return 0; }
Java 300
C 200
Java 200
Compilation error
In structure initialization, uninitialized members are:
Assigned garbage
Automatically initialized to 0
Not allowed
Assigned -1
What happens if you pass a structure by value to a function?
Address is passed
Error
Copy is made
It’s converted to union
Output of this program?
#include<stdio.h>
struct S { int a; };
void fun(struct S s) {
s.a = 50; }
int main() {
struct S s = {10};
fun(s);
printf("%d", s.a);
return 0; }
50
10
0
Garbage
What is true about nested structures?
Structures can't be nested
A structure can’t contain itself directly
Nested structures can't be initialized
Members of inner structures can't be accessed
What is the output? #include union Test { int i; char c; }; int main() { union Test t; t.c = 'A'; printf("%d", t.i); return 0; }
65
Garbage
0
Compilation error
Which gives more memory flexibility: structure or union?
Structure
Union
Both same
None
Which among the following is never possible in C when members in a structure are same as that in a union?
//Let P be a structure
//Let Q be a union
a.sizeof(P) is greater than sizeof(Q)
b. sizeof(P) is equal to sizeof(Q)
c. sizeof(P) is less than to sizeof(Q)
d. None of the above
Which of the following are themselves a collection of different data types?
String
Structure
Integer
None of these
Which of the following cannot be a structure member?
Another structure
Array
Function
None of these
What is the output of this C code?
#include <stdio.h>
struct student
{
int no; char name[20];
};
void main()
{ student s;
s.no = 8;
printf("hello");
}
Hello
Nothing
Compilation error
Which of the following share a similarity in syntax?
1. Union, 2. Structure, 3. Arrays and 4. Pointers
1 & 2
3&4
1&3
2&3
For what minimum value of x in a 32-bit Linux OS would make the size of s equal to 8 bytes?struct temp
{
int a : 13;
int b : 8;
int c : x;
}s;
4
8
12
16
Which operator connects the structure name to its member name?
-
.
=>
@
What will be the output of the following C 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);
}
Alan alan newton
Newton alan alan
Alan newton alan
Compile time error
What will be the output of the below coding #include <stdio.h>
struct temp
{
int a;
} s;
void func(struct temp s)
{
s.a = 10;
printf("%d\t", s.a);
}
main()
{
func(s);
printf("%d\t", s.a);
}
10,garbage value
Garbage value,10
0,10
10,0
Size of the following union (assume size of int=2, size of float=4 and size of char=1);
union ABC
{
int a;
float b;
char c;
};
2
4
1
8
What is wrong with the following code?
struct Person{ char name; struct Person *Mother, *Father; }Anita;
The ; should appear after the } and Anita be defined later
name should be defined as an array
There is no error in the code
struct Person Mother, Father; must be defined as struct Person Mother, Father;
What will be the output of the following code?
struct { int a;
double d;
float cp;
}s;
void main()
{
printf("%d\t%d\t%d\t%d", sizeof(s.a), sizeof(s.d), sizeof(s.cp), sizeof(s));
}
4,8,4,24
4,4,8,8
4,8,24,8
4,4,4,28
What is important difference between structure & union?
There is no difference
Union takes less memory then structure
Union woks faster then structure
Structure takes less memory then union
Is it necessary that all elements of structure should be different
Yes
No
Which of the following accesses a variable in structure b?
b->var
b.var
b*var
b-var
