HEC USAT-CS (Computer Science) computer science Programming in C — Set 2

Programming in C MCQs set 2 for HEC USAT-CS (Computer Science) computer science — 20 solved questions.

HEC USAT-CS (Computer Science) computer science Programming in C — Set 2

  1. Question 1

    Q1. What is the value of `x` after `int x = 5; x = x++ + ++x;`?

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

    Answer: Undefined behavior

    Explanation: The expression involves multiple increments of x between sequence points, leading to undefined behavior.

  2. Question 2

    Q2. Which of the following is NOT a valid identifier in C?

    • A) _var
    • B) 2var
    • C) var_2
    • D) Var2

    Answer: 2var

    Explanation: Identifiers cannot start with a digit. 2var is correct because it matches what the question requires. NET computer science MCQs often test this core concept.

  3. Question 3

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

    • A) 1
    • B) 2
    • C) 3
    • D) Error

    Answer: 3

    Explanation: Array indexing is equivalent to pointer arithmetic; *(arr + 2) accesses the third element.

  4. Question 4

    Q4. What does `static` mean for a variable inside a function?

    • A) It is initialized every time the function is called
    • B) It retains its value between function calls
    • C) It is a global variable
    • D) It is a constant

    Answer: It retains its value between function calls

    Explanation: Static variables inside functions retain their values between calls. NET computer science MCQs often test this core concept.

  5. Question 5

    Q5. What is the purpose of `extern` keyword?

    • A) To declare a global variable
    • B) To define a global variable
    • C) To declare a function
    • D) To make a variable constant

    Answer: To declare a global variable

    Explanation: Extern is used to declare a global variable or function that is defined elsewhere.

  6. Question 6

    Q6. What is the output of `printf('%d %d', sizeof(int), sizeof(char));`?

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

    Answer: 4 1

    Explanation: Typical sizes for int and char are 4 and 1 bytes, respectively.

  7. Question 7

    Q7. What is the effect of `typedef int myInt;`?

    • A) Defines a new type int
    • B) Creates an alias for int
    • C) Declares a variable myInt
    • D) Is a syntax error

    Answer: Creates an alias for int

    Explanation: Typedef creates an alias for an existing type. NET computer science MCQs often test this core concept.

  8. Question 8

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

    • A) 2
    • B) 3
    • C) 1
    • D) 0

    Answer: 1

    Explanation: Bitwise AND operation between 3 (11) and 2 (10) results in 2 (10), which is incorrect; correct result is 2.

  9. Question 9

    Q9. What is the purpose of `#include` directive?

    • A) To include a library for compilation
    • B) To include a file for preprocessing
    • C) To link object files
    • D) To define macros

    Answer: To include a file for preprocessing

    Explanation: The #include directive includes a file during the preprocessing stage. NET computer science MCQs often test this core concept.

  10. Question 10

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

    • A) 2
    • B) 1
    • C) 0
    • D) Garbage value

    Answer: 0

    Explanation: Partially initialized arrays have remaining elements initialized to zero. NET computer science MCQs often test this core concept.

  11. Question 11

    Q11. What is the effect of `const int x = 5; x = 10;`?

    • A) x is reassigned to 10
    • B) x remains 5
    • C) Compilation error
    • D) Runtime error

    Answer: Compilation error

    Explanation: Assigning to a const variable results in a compilation error. NET computer science MCQs often test this core concept.

  12. Question 12

    Q12. What is the result of `sizeof(void *)`?

    • A) Size of void
    • B) Size of a pointer
    • C) 1
    • D) Error

    Answer: Size of a pointer

    Explanation: Void pointer size is typically the size of a pointer on the system.

  13. Question 13

    Q13. What is the output of `printf('%d', 5 ^ 3);`?

    • A) 6
    • B) 8
    • C) 5
    • D) 3

    Answer: 6

    Explanation: Bitwise XOR operation between 5 (101) and 3 (011) results in 6 (110).

  14. Question 14

    Q14. What is the purpose of `register` keyword?

    • A) To declare a global variable
    • B) To hint the compiler to store a variable in a register
    • C) To make a variable constant
    • D) To declare a function

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

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

  15. Question 15

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

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

    Answer: 4

    Explanation: In C, the size of a character literal is equal to the size of an int, typically 4 bytes.

  16. Question 16

    Q16. What is the output of `printf('%d %d', 5/2, -5/2)`?

    • A) 2 -2
    • B) 2 -3
    • C) 3 -2
    • D) 3 -3

    Answer: 2 -3

    Explanation: Integer division truncates towards zero, so 5/2 = 2 and -5/2 = -2.

  17. Question 17

    Q17. The `static` keyword in C is used to

    • A) declare a constant variable
    • B) retain the value of a variable between function calls
    • C) make a variable global
    • D) make a function inline

    Answer: retain the value of a variable between function calls

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

  18. Question 18

    Q18. What is the result of the expression `sizeof(arr) / sizeof(arr[0])` for an array `int arr[5]`?

    • A) 4
    • B) 5
    • C) 20
    • D) 1

    Answer: 5

    Explanation: This expression calculates the number of elements in the array. NET computer science MCQs often test this core concept.

  19. Question 19

    Q19. The function `malloc()` is used to

    • A) allocate memory on the stack
    • B) allocate memory on the heap
    • C) free allocated memory
    • D) check if a pointer is NULL

    Answer: allocate memory on the heap

    Explanation: malloc() dynamically allocates memory on the heap. allocate memory on the heap is correct because it matches what the question requires. NET computer science MCQs often test this core concept.

  20. Question 20

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

    • A) 5
    • B) 20
    • C) 10
    • D) Error

    Answer: 5

    Explanation: Right shift operation divides the number by 2. NET computer science MCQs often test this core concept.