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

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

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

  1. Question 1

    Q1. The `const` keyword in C is used to

    • A) declare a variable that can be modified
    • B) declare a constant variable
    • C) make a function inline
    • D) make a pointer point to a constant

    Answer: declare a constant variable

    Explanation: const keyword is used to declare variables that cannot be modified. NET computer science MCQs often test this core concept.

  2. Question 2

    Q2. What is the output of the expression `5 & 3`?

    • A) 1
    • B) 7
    • C) 5
    • D) 3

    Answer: 1

    Explanation: Bitwise AND operation: 5 (101) & 3 (011) = 1 (001). NET computer science MCQs often test this core concept.

  3. Question 3

    Q3. The `typedef` keyword is used to

    • A) create a new data type
    • B) declare a constant
    • C) allocate memory dynamically
    • D) define a macro

    Answer: create a new data type

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

  4. Question 4

    Q4. What is the output of `printf('%d', 10 % -3)`?

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

    Answer: 1

    Explanation: Modulus operation: 10 % -3 = 1. 1 is correct because it matches what the question requires. NET computer science MCQs often test this core concept.

  5. Question 5

    Q5. The `extern` keyword is used to

    • A) declare a global variable
    • B) declare a function
    • C) define a macro
    • D) allocate memory dynamically

    Answer: declare a global variable

    Explanation: extern is used to declare global variables that are defined elsewhere. NET computer science MCQs often test this core concept.

  6. Question 6

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

    • A) 3
    • B) Garbage value
    • C) Error
    • D) 0

    Answer: Garbage value

    Explanation: Accessing an array out of bounds results in undefined behavior, typically a garbage value.

  7. Question 7

    Q7. The `register` keyword is used to

    • A) declare a variable that is stored in a register
    • B) declare a constant variable
    • C) make a function inline
    • D) allocate memory dynamically

    Answer: declare a variable that is stored in a register

    Explanation: register keyword is a hint to the compiler to store the variable in a register.

  8. Question 8

    Q8. What is the output of `printf('%d', sizeof(void *))`?

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

    Answer: 4

    Explanation: Size of a void pointer is typically equal to the word size of the system, often 4 bytes on 32-bit systems.

  9. Question 9

    Q9. The function `free()` is used to

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

    Answer: free allocated memory

    Explanation: free() is used to deallocate memory that was previously allocated using malloc() or similar functions.

  10. Question 10

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

    • A) -6
    • B) -5
    • C) -4
    • D) Error

    Answer: -6

    Explanation: Bitwise NOT operation: ~5 = -6 (two's complement representation). NET computer science MCQs often test this core concept.

  11. Question 11

    Q11. The `volatile` keyword is used to

    • A) declare a variable that can be modified by external factors
    • B) declare a constant variable
    • C) make a function inline
    • D) allocate memory dynamically

    Answer: declare a variable that can be modified by external factors

    Explanation: volatile keyword informs the compiler that a variable's value can change in ways not explicitly specified by the program.

  12. Question 12

    Q12. Which operator has the highest precedence in C?

    • A) +
    • B) *
    • C) ()
    • D) =

    Answer: ()

    Explanation: Parentheses () have the highest precedence, used for grouping and function calls.

  13. Question 13

    Q13. In C, what is `NULL` typically defined as?

    • A) 0
    • B) (void *)0
    • C) 1
    • D) ((void *)1)

    Answer: (void *)0

    Explanation: NULL is defined as (void *)0, representing a null pointer constant. NET computer science MCQs often test this core concept.

  14. Question 14

    Q14. What is the purpose of `extern` keyword in C?

    • 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 declares a global variable or function that is defined elsewhere. NET computer science MCQs often test this core concept.

  15. Question 15

    Q15. What does `typedef` do?

    • A) Creates a new data type
    • B) Defines a macro
    • C) Declares a variable
    • D) Defines a function

    Answer: Creates a new data type

    Explanation: typedef creates an alias for an existing data type, enhancing code readability.

  16. Question 16

    Q16. How does `switch` statement work?

    • A) Checks all cases until a match is found
    • B) Jumps to the matching case
    • C) Executes all cases
    • D) Always executes default case

    Answer: Jumps to the matching case

    Explanation: switch jumps to the case matching the expression's value, then executes until break.

  17. Question 17

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

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

    Answer: Garbage value

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

  18. Question 18

    Q18. What is the effect of `register` keyword?

    • A) Stores variable in register
    • B) Makes variable global
    • C) Makes variable constant
    • D) Allocates memory on heap

    Answer: Stores variable in register

    Explanation: register hints the compiler to store the variable in a CPU register for faster access.

  19. Question 19

    Q19. What is the behavior of `int a = 10; a = a << 1;`?

    • A) Doubles the value of a
    • B) Halves the value of a
    • C) No change
    • D) Undefined behavior

    Answer: Doubles the value of a

    Explanation: Left shift (<<) effectively doubles the value by shifting bits one place to the left.

  20. Question 20

    Q20. What does `const` mean for a pointer?

    • A) Pointer cannot be changed
    • B) Data pointed to cannot be changed
    • C) Both pointer and data are constant
    • D) It depends on the placement of const

    Answer: It depends on the placement of const

    Explanation: const can be applied to either the pointer, the data it points to, or both, depending on its placement.