Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

ข้อสอบ MCQ: หน่วยความจำและกระบวนการคอมไพล์ใน C (Memory Allocation & Compilation)

ชุดคำถาม MCQ เกี่ยวกับ Memory Allocation และ Compilation Process ในภาษา C

คำถาม 1: Output ของโค้ด Union + malloc

union my_union { int i; float f; char c; };
union my_union* u = malloc(sizeof(union my_union));
u->f = 20.60f;
printf("%f", u->f);

ตัวเลือก: A) Garbage Value B) 20.600000 C) Syntax Error D) 20.6

คำตอบ: B - Union ใช้ memory เดียวกัน สำหรับ float 20.60f จะพิมพ์ 20.600000

คำถาม 2: ลำดับขั้นตอนการคอมไพล์ที่ถูกต้อง

ตัวเลือก:

  • A) Assembler, Compiler, Preprocessor, Linking
  • B) Compiler, Assembler, Preprocessor, Linking
  • C) Preprocessor, Compiler, Assembler, Linking ✓
  • D) Assembler, Compiler, Linking, Preprocessor

คำอธิบาย: 1) Preprocessor (รวม header, macro) 2) Compiler (แปลงเป็น assembly) 3) Assembler (assembly → object code) 4) Linker (link object files)

คำถาม 3: #include ถูกแทนที่ด้วยไฟล์ header เมื่อไหร่?

ตัวเลือก: A) Linking B) Preprocessing ✓ C) Execution D) Editing

คำอธิบาย: ขั้นตอน Preprocessing จะนำโค้ดจากไฟล์ใน #include มารวม

คำถาม 4: วัตถุประสงค์ของ fflush()

ตัวเลือก: A) Flush all/specified streams ✓ B) Flush specified only C) Flush I/O buffer D) Invalid function

คำอธิบาย: fflush() ล้าง output buffer ส่งข้อมูลไป console ใช้กับ output stream

คำถาม 5: หาจุดผิดของโค้ด malloc + strcpy

char* ptr;
*ptr = (int*)malloc(30);
strcpy(ptr, "ABC");

ตัวเลือก: A) strcpy error B) *ptr = (int*)malloc(30) ✓ C) free error D) No error

คำอธิบาย: *ptr = (int*)malloc(30) ผิด เพราะ ptr เป็น char* แต่ cast เป็น int* แล้ว dereference (*ptr) จะทำให้ integer from pointer without cast