Geeksquiz | Structure & Union
来源:互联网 发布:手机网络棋牌作弊器 编辑:程序博客网 时间:2024/06/02 04:01
Structure & Union
-----------
Structure & Union
Question 1
CORRECT
#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;
}
200 GeeksQuiz
200
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>
struct
st
{
int
x;
static
int
y;
};
int
main()
{
printf
(
"%d"
,
sizeof
(
struct
st));
return
0;
}
4
8
Compiler Error
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
struct
node
{
int
i;
float
j;
};
struct
node *s[10];
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.
Discuss it
Question 4
WRONG
Consider the following C declarationAssume 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)
struct
{
short
s[5];
union
{
float
y;
long
z;
}u;
} t;
22 bytes
14 bytes
18 bytes
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>
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;
}
Compiler Error
10
Runtime Error
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?
Equality comparison ( == )
Assignment ( = )
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
union
test
{
int
x;
char
arr[8];
int
y;
};
int
main()
{
printf
(
"%d"
,
sizeof
(
union
test));
return
0;
}
12
16
8
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
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;
}
Nothing is printed
G
Garbage character followed by 'G'
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>
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;
}
Segmentation Fault
SeeksQuiz
GeeksQuiz
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%.
Your score is 56%.
0 0
- Geeksquiz | Structure & Union
- structure union 主要区别
- Structure 与 Union 比较?
- Geeksquiz | Array
- Structure and Union有什么区别?(转)
- Disjoint-Set Data Structure (Union Find Algorithm)
- Geeksquiz | Dynamic Memory Allocation
- Geeksquiz | Macro & Preprocessor
- Variant Records: The equivalent to the C-union structure
- structure
- error: request for member 'nameLabel' in something not a structure or union 的原因
- error: request for member ‘on_url’ in something not a structure or union
- 编译报request for member '***' in something not a structure or union的原因,以及解决方法
- request for member 'flags' in something not a structure or union
- JNI_OnLoad编译错误:request for member 'GetEnv' in something not a structure or union
- CCARRAY_FOREACH出现Member reference base type 'cocos2d::CCObject *' is not a structure or union错误
- JNI遇到error: request for member 'FindClass' in something not a structure or union
- C语言程序编译错误 request for member 'num' in something not a structure or union
- 秋叶语录总结提炼
- js访问jsf的SelectOneRadio组件方式
- ASP.NET MVC 视图(五)
- R文件丢失解决方案
- 正则表达式语法 http://www.cnblogs.com/light169/archive/2006/10/18/532347.html
- Geeksquiz | Structure & Union
- java项目打jar包
- WIP 投料报 Invalid Serial Number
- Linux内核读写信号量实现
- wiki 1917 深海机器人问题
- hdu1106排序
- java web的并发处理
- 40_显示来电归属地,监听来电状态的服务
- 字幕基础:字幕介绍、字幕种类及常见格式