Geeksquiz | Structure & Union

来源:互联网 发布:手机网络棋牌作弊器 编辑:程序博客网 时间:2024/06/02 04:01

Structure & Union

-----------

Structure & Union

Question 1
CORRECT
#include‹stdio.h›
intmain()
{
    structsite
    {
        charname[] = "GeeksQuiz";
        intno_of_pages = 200;
    };
    structsite *ptr;
    printf("%d ", ptr->no_of_pages);
    printf("%s", ptr->name);
    getchar();
    return0;
}
A
200 GeeksQuiz
B
200
C
Runtime Error
Compiler Error

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
CORRECT
Assume that size of an integer is 32 bit. What is the output of following program?
#include<stdio.h>
structst
{
    intx;
    staticint y;
};
 
intmain()
{
    printf("%d",sizeof(structst));
    return0;
}
A
4
B
8
Compiler Error
D
Runtime Error

Discuss it


Question 2 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.
Question 3
CORRECT
structnode
{
   inti;
   floatj;
};
structnode *s[10];
The above C declaration define 's' to be (GATE CS 2000)
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.

Discuss it


Question 4
WRONG
Consider the following C declaration
struct{
    shorts[5];
    union{
         floaty;
         longz;
    }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
B
14 bytes
18 bytes
D
10 bytes

Discuss it


Question 4 Explanation: 
Short array s[5] will take 10 bytes as size of short is 2 bytes. When we declare a union, memory allocated for the union is equal to memory needed for the largest member of it, and all members share this same memory space. Since u is a union, memory allocated to u will be max of float y(4 bytes) and long z(8 bytes). So, total size will be 18 bytes (10 + 8).
Question 5
CORRECT
#include<stdio.h>
structst
{
    intx;
    structst next;
};
   
intmain()
{
    structst temp;
    temp.x = 10;
    temp.next = temp;
    printf("%d", temp.next.x);
    return0;
}
Compiler Error
B
10
C
Runtime Error
D
Garbage Value

Discuss it


Question 5 Explanation: 
A structure cannot contain a member of its own type because if this is allowed then it becomes impossible for compiler to know size of such struct. Although a pointer of same type can be a member because pointers of all types are of same size and compiler can calculate size of struct
Question 6
WRONG
Which of the following operators can be applied on structure variables?
A
Equality comparison ( == )
Assignment ( = )
C
Both of the above
None of the above

Discuss it


Question 6 Explanation: 
A structure variable can be assigned to other using =, but cannot be compared with other using ==
Question 7
CORRECT
uniontest
{
    intx;
    chararr[8];
    inty;
};
 
intmain()
{
    printf("%d",sizeof(uniontest));
    return0;
}
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
8
D
Compiler Error

Discuss it


Question 7 Explanation: 
When we declare a union, memory allocated for a union variable of the type is equal to memory needed for the largest member of it, and all members share this same memory space. In above example, "char arr[8]" is the largest member. Therefore size of union test is 8 bytes.
Question 8
WRONG
uniontest
{
    intx;
    chararr[4];
    inty;
};
 
intmain()
{
    uniontest t;
    t.x = 0;
    t.arr[1] ='G';
    printf("%s", t.arr);
    return0;
}
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.
Nothing is printed
B
G
C
Garbage character followed by 'G'
D
Garbage character followed by 'G', followed by more garbage characters
Compiler Error

Discuss it


Question 8 Explanation: 
Since x and arr[4] share the same memory, when we set x = 0, all characters of arr are set as 0. O is ASCII value of '\0'. When we do "t.arr[1] = 'G'", arr[] becomes "\0G\0\0". When we print a string using "%s", the printf function starts from the first character and keeps printing till it finds a \0. Since the first character itself is \0, nothing is printed.
Question 9
WRONG
# include <iostream>
# include <string.h>
usingnamespace std;
 
structTest
{
  charstr[20];
};
 
intmain()
{
  structTest st1, st2;
  strcpy(st1.str,"GeeksQuiz");
  st2 = st1;
  st1.str[0] ='S';
  cout << st2.str;
  return0;
}
Segmentation Fault
B
SeeksQuiz
GeeksQuiz
D
Compiler Error

Discuss it


Question 9 Explanation: 
Array members are deeply copied when a struct variable is assigned to another one. See Are array members deeply copied? for more details.
You have completed 9/9 questions .
Your score is 56%.

0 0
原创粉丝点击