NTS NAT-ICS (Computer Science Track) computer science: Programming in C MCQs

Practice Programming in C MCQs for NTS NAT-ICS (Computer Science Track) computer science — topic-wise sets with solved answers.

NTS NAT-ICS (Computer Science Track) computer science: Programming in C MCQs — sample questions

  1. Question 1

    Q1. What is the output of `printf('%d', sizeof('a'));` in C?

    • A) 1
    • B) 2
    • C) 4
    • D) sizeof(char)

    Answer: 4

    Explanation: In C, character literals are treated as integers, so 'a' is an int, typically 4 bytes on most systems.

  2. Question 2

    Q2. What does `static int x;` imply?

    • A) x is a global variable
    • B) x retains its value between function calls
    • C) x is a constant
    • D) x is not accessible outside the file

    Answer: x retains its value between function calls

    Explanation: Static variables retain their values between function calls, initialized only once. NET computer science MCQs often test this core concept.

  3. Question 3

    Q3. What is the result of `5 / 2` in C?

    • A) 2.5
    • B) 2
    • C) 3
    • D) Compilation Error

    Answer: 2

    Explanation: Integer division truncates the decimal part, resulting in 2. NET computer science MCQs often test this core concept.

  4. Question 4

    Q4. What is the output of `int x = 10; printf('%d', x++);`?

    • A) 10
    • B) 11
    • C) Compilation Error
    • D) Runtime Error

    Answer: 10

    Explanation: Post-increment operator returns the current value before incrementing. NET computer science MCQs often test this core concept.

  5. Question 5

    Q5. What is a dangling pointer?

    • A) Pointer to a freed memory location
    • B) Pointer to a valid memory location
    • C) Null pointer
    • D) Pointer to a local variable

    Answer: Pointer to a freed memory location

    Explanation: Dangling pointer points to memory that has been freed or reused. NET computer science MCQs often test this core concept.

  6. Question 6

    Q6. What is the purpose of `typedef`?

    • A) To define a new data type
    • B) To declare a variable
    • C) To define a function
    • D) To allocate memory

    Answer: To define a new data type

    Explanation: Typedef is used to create an alias for an existing data type.

  7. Question 7

    Q7. What is the output of `printf('%d', 3 && 0);`?

    • A) 1
    • B) 0
    • C) Compilation Error
    • D) Runtime Error

    Answer: 0

    Explanation: Logical AND operation results in 0 because one operand is 0. NET computer science MCQs often test this core concept.

  8. Question 8

    Q8. What does `extern int x;` mean?

    • A) x is defined elsewhere
    • B) x is a local variable
    • C) x is a constant
    • D) x is not accessible

    Answer: x is defined elsewhere

    Explanation: Extern keyword declares a variable defined elsewhere, typically in another file. NET computer science MCQs often test this core concept.

  9. Question 9

    Q9. What is the result of `sizeof(int) / sizeof(int*)` on a 64-bit system?

    • A) 1/2
    • B) 1
    • C) 2
    • D) 4

    Answer: 1/2

    Explanation: On a 64-bit system, sizeof(int) is typically 4 and sizeof(int*) is 8, so the result is 4/8 = 1/2.

  10. Question 10

    Q10. What is a memory leak?

    • A) Memory not being freed after use
    • B) Memory being accessed after being freed
    • C) Memory not being allocated
    • D) Memory being corrupted

    Answer: Memory not being freed after use

    Explanation: Memory leak occurs when dynamically allocated memory is not released back to the system.

  11. Question 11

    Q11. What is the output of `int arr[5] = 1, 2; printf('%d', arr[3]);`?

    • A) 0
    • B) Garbage Value
    • C) 2
    • D) Compilation Error

    Answer: Garbage Value

    Explanation: Uninitialized elements in an array contain garbage values. NET computer science MCQs often test this core concept.

  12. Question 12

    Q12. What is the use of `register` keyword?

    • A) To allocate memory on the heap
    • B) To suggest the compiler to store a variable in a register
    • C) To declare a global variable
    • D) To define a macro

    Answer: To suggest the compiler to store a variable in a register

    Explanation: Register keyword is a hint to the compiler to store a variable in a CPU register for faster access.

  13. Question 13

    Q13. What is the result of `!0 && 5`?

    • A) 1
    • B) 0
    • C) 5
    • D) Compilation Error

    Answer: 1

    Explanation: !0 is 1, and 1 && 5 is 1 because both are non-zero.

  14. Question 14

    Q14. What is the output of `printf('%lu', sizeof('Hello'));`?

    • A) 5
    • B) 6
    • C) sizeof(char)
    • D) Compilation Error

    Answer: 6

    Explanation: String literal 'Hello' includes a null terminator, making its size 6 bytes.

  15. Question 15

    Q15. What is the purpose of `volatile` keyword?

    • A) To optimize variable access
    • B) To inform the compiler that a variable's value can change unexpectedly
    • C) To declare a constant
    • D) To allocate memory on the heap

    Answer: To inform the compiler that a variable's value can change unexpectedly

    Explanation: Volatile keyword tells the compiler to always read a variable's value from memory, not from a register.

  16. Question 16

    Q16. What is the output of `int x = 5; x = x++ + ++x; printf('%d', x);`?

    • A) 11
    • B) 12
    • C) 13
    • D) Undefined Behavior

    Answer: Undefined Behavior

    Explanation: The expression involves multiple side effects on the same variable between sequence points, leading to undefined behavior.

  17. Question 17

    Q17. What is the difference between `calloc()` and `malloc()`?

    • A) calloc() initializes memory to zero
    • B) malloc() is faster
    • C) calloc() is used for arrays
    • D) malloc() is used for structures

    Answer: calloc() initializes memory to zero

    Explanation: calloc() allocates memory and initializes it to zero, unlike malloc(). NET computer science MCQs often test this core concept.

  18. Question 18

    Q18. What is the output of `char str[] = 'Hello'; printf('%s', str + 2);`?

    • A) Hello
    • B) llo
    • C) He
    • D) ll

    Answer: llo

    Explanation: Pointer arithmetic moves the starting point of the string to 'l', so it prints 'llo'.

  19. Question 19

    Q19. What is the result of `sizeof(void*)` on a 64-bit system?

    • A) 4
    • B) 8
    • C) sizeof(int)
    • D) Compilation Error

    Answer: 8

    Explanation: On a 64-bit system, the size of a pointer is typically 8 bytes.

  20. Question 20

    Q20. What is the purpose of `#ifndef` directive?

    • A) To include a header file multiple times
    • B) To prevent multiple inclusions of a header file
    • C) To define a macro
    • D) To undefine a macro

    Answer: To prevent multiple inclusions of a header file

    Explanation: #ifndef directive checks if a symbol is not defined, used to prevent multiple inclusions of the same header file.

Loading...