NEW
Font size
WorksheetsHackharbor 3.0 Day 4:C
Total questions: 30
Worksheet time: 15mins
What is the key difference between a structure and a union in C?
Structure shares memory among members, union does not
Union shares memory among members, structure does not
Structure allows only one member, union allows multiple
Both allocate memory exactly the same way
What does a struct say to a union at a party?
“I like my space, you share yours”
“Youʼre occupying my memory!”
“Letʼs combine and confuse the compiler!”
“Letʼs be friends forever!”
Which operator is used to access members of a structure variable in C?
meow
. (dot)
&
*
Which of the following keywords is used to define a user-defined data type combining different data types?
class
union
struct
typedef
What will happen if you assign a value to one member of a union and then assign another value to a different member?
Both values get stored separately
The latter assignment overwrites the first value
Compiler error
Both values remain unchanged
Why are structure members always calm?
Because they each have their own space to chill
They work too hard to care
They donʼt share memory, so no conflicts
They meditate regularly
In a union, what is the size of the memory block allocated?
Sum of all membersʼ sizes
Size of the smallest member
Size of the largest member
None of the above
Why did the union break up with the structure?
They fought over memory space
The structure was too possessive with data
Union wanted to share but structure kept everything separated
Because union was always changing personalities!
Which file mode opens a file for writing only and creates the file if it does not exist?
r
w
a
r+
If a file could talk, what would it say when closed?
“Thanks for the RAM!”
“Donʼt forget to fclose me!”
“Iʼm done, go to sleep!”
“Hey coder, save me!”
Which function is used to close a file in C?
fclose()
closefile()
file_close()
close()
Which function reads formatted input from a file?
fread()
fscanf()
fgets()
freadf()
A file pointer walks into a bar and orders a drink. The bartender says:
"Sorry, Iʼm not a pointer!"
"Hereʼs to reading and writing!"
"You look lost, where you wanna go?"
"Do you want to rewind or seek?"
If you try to read a file that doesnʼt exist, the file would:
Cry with tears of NULL pointers
Shout “Error!”
Just silently ignore you
Call your mom
Which function writes formatted output to a file?
fwrite()
fprintf()
fputs()
fprintf_s()
What does fseek(fp, 0, SEEK_SET) do?
Moves file pointer to end
Moves file pointer to beginning
Moves file pointer 0 bytes from current position
None of the above
What will happen if you try to read beyond the end of a file?
Error occurs
Returns EOF or 0
File pointer resets
File is deleted
How can you reset the file pointer back to the start of the file?
fseek(fp, 0, SEEK_END
rewind(fp)
ftell(fp)
fclose(fp)
If a structure contains an array member, how do you assign a string to that array?
person.name = "Alice";
strcpy(person.name, "Alice");
person.name.str = "Alice";
memcopy(person.name, "Alice");
What is the purpose of typedef when used with structs?
To make a new data type alias
To declare an integer
To release memory
To write a function prototype
What happens to other membersʼ values in a union when you store data in one member?
Remain unchanged
Undefined / overwritten
Increase in size
Compiler throws error
Selecting the wrong mode in fopen, for example opening a file with "r" when you intend to write, will:
Allow writing anyway
Result in runtime error
Create the file automatically
Fail to open the file (return NULL
Which of the following is true about nested structures?
A structure cannot contain another structure
Nested structures share same memory space
A structure can include another structure as a member
Nested structures require a union
Whatʼs the minimum number of members a structure can have?
0
1
2
Unlimited but must have 3 minimum
Which of these snippets properly declares a union?
union { int a; char b; };
struct union { int a; char b; };
unite { int a; char b; };
union Data() { int a; float b; };
Which function is best to copy strings to a structure member array?
strcpy()
memcpy()
strcat()
strcpy_s()
What file mode should be used to append to a file without deleting its previous content?
w
a
r
r+
If you want to read an entire line including spaces from a file, which function is preferable?
fscanf()
fgets()
fread()
fprintf()
Which of the following can NOT be done with a union?
Store multiple types but only one at a time
Store multiple members simultaneously with independent values
Reduce memory usage when storing alternative data
Share memory space for different data types
Which file mode would a sneaky hacker use to sneak in quietly?
"r" – reading quietly
"w" – leaving a trace
"a" – sneaking in to add notes at the end
"r+" – best of both worlds!
