0% found this document useful (0 votes)
35 views24 pages

MCQS

Uploaded by

vaibhavmng537
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views24 pages

MCQS

Uploaded by

vaibhavmng537
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MCQS-

1.What is the output of the following code?

int arr[5] = {1, 2, 3};

printf("%d", arr[4]);

A) 3
B) 0
C) Garbage value
D) Compilation error
Answer: C

2. What does this expression compute: x & (-x)?

A) Clears lowest set bit


B) Sets highest set bit
C) Isolates lowest set bit
D) Inverts all bits
Answer: C

3. What is the base case in the following recursive function?

int factorial(int n) {

if (n <= 1)

return 1;

return n * factorial(n - 1);

A) n == 0
B) n <= 1
C) n == 1
D) No base case
Answer: B

4.What is the index of the last element in an array declared as int arr[10];?
A) 10
B) 9
C) 0
D) Undefined
Answer: B

5. How do you check if the nth bit is set in integer x?

A) x & n
B) x | (1 << n)
C) x & (1 << n)
D) x ^ (1 << n)
Answer: C

6. How many times is the hello printed?

void printHello(int n) {

if (n == 0) return;

printf("hello\n");

printHello(n - 1);

Called as: printHello(3);

A) 2
B) 3
C) 4
D) Infinite
Answer: B

7.
Which of the following correctly initializes all elements to zero?

A) int arr[5] = {};


B) int arr[5];
C) int arr[5] = {0};
D) int arr[5] = (0);
Answer: C
8. What does x ^ y ^ x equal?

A) x
B) y
C) 0
D) x + y
Answer: B

9. (Recursion)
What will be the output of the following?

int f(int n) {

if (n == 1) return 1;

return n + f(n - 1);

printf("%d", f(3));

A) 6
B) 5
C) 3
D) 1
Answer: A

10. (Array)
Which of the following will give a segmentation fault?

int arr[3] = {1, 2, 3};

A) arr[0]
B) arr[2]
C) arr[3]
D) arr[1]
Answer: C

11. (Array)
What will be the output?

int arr[] = {1, 2, 3, 4};


printf("%d", *(arr + 2));

A) 1
B) 2
C) 3
D) 4
Answer: C

12. (Bit Manipulation)


What does x << 2 do? A) Divides x by 2
B) Multiplies x by 2
C) Multiplies x by 4
D) Shifts bits to the right
Answer: C

13. (Recursion)
What will be the result of the following function?

int mystery(int n) {

if (n == 0) return 0;

return 1 + mystery(n / 2);

Call: mystery(8); A) 4
B) 3
C) 2
D) 1
Answer: B

14. (Array)
Which of the following best describes int *p = arr; where arr is an array? A) Invalid
B) p points to the entire array
C) p points to the first element
D) p copies the array
Answer: C
15. (Bit Manipulation)
How do you turn off the kth bit of number x? A) x = x | (1 << k)
B) x = x & ~(1 << k)
C) x = x ^ (1 >> k)
D) x = x << k
Answer: B

16. (Recursion)
What is the output?

void recurse(int n) {

if (n == 0) return;

recurse(n - 1);

printf("%d ", n);

recurse(3);

A) 3 2 1
B) 1 2 3
C) 0 1 2 3
D) 1 2
Answer: B

17. (Array)
Which statement is true about arrays in C? A) Array bounds are checked
B) Array name stores address of last element
C) Array elements are stored in heap
D) Array name is a constant pointer
Answer: D

18. (Bit Manipulation)


Which bitwise operator flips all bits? A) ^
B) |
C) &
D) ~
Answer: D
19. (Recursion)
What does this function return?

int sum(int n) {

if (n <= 0) return 0;

return n + sum(n - 1);

Call: sum(5); A) 5
B) 10
C) 15
D) 20
Answer: C

20. (Array)
What is the time complexity of finding an element in an unsorted array? A) O(log n)
B) O(n)
C) O(1)
D) O(n log n)
Answer: B

21. What does the bitwise AND operator '&' do in C?

· A) Sets each bit to 1

· B) Sets each bit to 0

· C) Sets bit to 1 if both bits are 1

· D) Sets bit to 0 if both bits are 1

Answer: C

22.Which operator is used for bitwise OR in C?

· A) &&

· B) ||

· C) |
· D) &

Answer: C

23. What is the result of 5 & 3?

· A) 1

· B) 3

· C) 7

· D) 5

Answer: B

24.Which operator is used to invert all bits in C?

· A) !

· B) ~

· C) ^

· D) &

Answer: B

25. What does the '<<' operator do in C?

· A) Left shift

· B) Right shift

· C) Bitwise AND

· D) Bitwise OR

Answer: A

26.What is recursion in C?

· A) Function calling itself

· B) Looping

· C) Memory allocation
· D) Bitwise operation

Answer: A

27.What is the base case in recursion?

· A) An infinite loop

· B) A stopping condition

· C) A function call

· D) None of these

Answer: B

28.Which of the following problems is best suited for recursion?

· A) Factorial

· B) Loop printing

· C) Array input

· D) Bitwise shift

Answer: A

29.What happens if base case is missing in recursion?

· A) Stack overflow

· B) Program ends

· C) No error

· D) Runs faster

Answer: A

30.Recursion uses which memory structure?

· A) Queue
· B) Heap

· C) Stack

· D) Array

Answer: C

31.Which operator is used to access the value at the address stored in a pointer?

· A) &

· B) *

· C) ->

· D) %

Answer: B

32.Which of the following is not a valid storage class in C?

· A) auto

· B) static

· C) register

· D) volatile

Answer: D

33.What is the output of `printf("%d", sizeof('A'));` in C?

· A) 1

· B) 2

· C) 4

· D) Depends on compiler

Answer: C

34.Which header file is required for using `malloc()` function in C?

· A) stdlib.h
· B) stdio.h

· C) malloc.h

· D) memory.h

Answer: A

35.Which loop is guaranteed to execute at least once?

· A) for

· B) while

· C) do-while

· D) None of the above

Answer: C

1. What is the index number of the first element of an array in C?

A) 0

B) 1

C) -1

D) It depends on the compiler

Answer: A

2. Which of the following is the correct way to declare an array?

A) int arr[];

B) int arr[5];

C) arr{5};

D) array int arr;

Answer: B

3. What is the output of this code?


int arr[] = {10, 20, 30};
printf("%d", arr[1]);

A) 10
B) 20

C) 30

D) 0

Answer: B

4. What is the size of the array `int arr[10];` in bytes if int = 4 bytes?

A) 10

B) 40

C) 4

D) 20

Answer: B

5. Which of the following accesses the last element of `int arr[5];`?

A) arr[5]

B) arr[4]

C) arr[6]

D) arr[0]

Answer: B

6. Which function is used to get the number of elements in an array?

A) length(arr)

B) size(arr)

C) sizeof(arr)/sizeof(arr[0])

D) count(arr)

Answer: C

7. What will be the output of the code below?


int arr[] = {1, 2, 3, 4, 5};
printf("%d", sizeof(arr));

A) 5
B) 10

C) 20

D) 4

Answer: C

8. What will `arr[5]` return if array `arr[5] = {1, 2, 3, 4, 5};`?

A) 0

B) 5

C) Garbage

D) Compilation Error

Answer: C

9. What is the output?


int arr[3] = {10, 20};
printf("%d", arr[2]);

A) 10

B) 0

C) Garbage

D) Compilation error

Answer: B

10. In a 2D array `int a[3][4];`, how many elements are there?

A) 7

B) 12

C) 3

D) 4

Answer: B

11. Which of the following initializes all elements of array to 0?

A) int arr[5] = {};


B) int arr[5] = {0};

C) int arr[] = {0, 0, 0, 0, 0};

D) Both B and C

Answer: D

12. What is the output of this code?


int arr[] = {10, 20, 30};
printf("%d", *arr);

A) 10

B) 20

C) 30

D) Error

Answer: A

13. Accessing an element outside the bounds of an array:

A) Causes segmentation fault always

B) Is undefined behavior

C) Gives 0

D) Is a syntax error

Answer: B

14. Which of the following correctly accesses the second row, third column of `int arr[2][3]`?

A) arr[2][3]

B) arr[3][2]

C) arr[1][2]

D) arr[2][1]

Answer: C

15. Which statement is true about arrays in C?

A) The size of the array is determined at runtime


B) Arrays can grow dynamically

C) Arrays are zero-indexed

D) Arrays can be of variable type

Answer: C

16. What is the correct way to declare and initialize an array?

A) int a[5] = {1, 2, 3};

B) int a[] = (1, 2, 3);

C) int a[3] = 1, 2, 3;

D) int a[3] = {1 2 3};

Answer: A

17. Which is valid about multidimensional arrays?

A) int a[][] = {{1, 2}, {3, 4}};

B) int a[2][] = {{1, 2}, {3, 4}};

C) int a[][2] = {{1, 2}, {3, 4}};

D) int a[2][2] = {1, 2, 3, 4};

Answer: C and D

18. What is the value of `*(arr + 2)` if `arr = {10, 20, 30, 40}`?

A) 20

B) 30

C) 10

D) 40

Answer: B

19. Which expression gives the address of the first element of an array `arr`?

A) &arr[0]

B) arr
C) &arr

D) All of the above

Answer: D

20. What is the time complexity to access any element in an array by index?

A) O(n)

B) O(log n)

C) O(1)

D) O(n^2)

Answer: C

21. What is the result of 5 & 3?

• A) 7

• B) 1

• C) 0

• D) 2
Answer: B

22. Which operator is used to set a bit?

• A) &

• B) |

• C) ^

• D) ~
Answer: B

23. What is the result of 6 >> 1?

• A) 3

• B) 6

• C) 12

• D) 1
Answer: A
24. Which of the following clears the least significant bit?

• A) x & (x - 1)

• B) x | (x - 1)

• C) x ^ (x - 1)

• D) x >> 1
Answer: A

25. How to check if a number is a power of 2?

• A) x % 2 == 0

• B) x & (x - 1) == 0

• C) x | (x - 1) == 0

• D) x == 2
Answer: B

26. What is the output of printf("%d", 1 << 3);?

• A) 8

• B) 3

• C) 6

• D) 9
Answer: A

27. Which bitwise operation is used to toggle bits?

• A) &

• B) |

• C) ^

• D) ~
Answer: C

28. How many bits are set in binary 1011?

• A) 2

• B) 3

• C) 4
• D) 1
Answer: B

29. What is the output of ~5 in 8-bit binary?

• A) 11111010

• B) 00000101

• C) 11111110

• D) 00000010
Answer: A

30. What is the binary representation of 9?

• A) 1001

• B) 1100

• C) 1010

• D) 0110
Answer: A

31. What is recursion?

• A) A function calling itself

• B) A loop inside function

• C) Goto statement

• D) External function call


Answer: A

32. What is the base case in recursion?

• A) Where loop ends

• B) Where function stops calling itself

• C) Where error occurs

• D) None
Answer: B

33. What happens if base case is missing in recursion?

• A) Stack overflow

• B) Output is zero
• C) Compiler error

• D) Nothing
Answer: A

34. Which of the following uses recursion?

• A) Binary Search

• B) Bubble Sort

• C) Selection Sort

• D) Insertion Sort
Answer: A

35. What is the output of factorial(3)?

• A) 6

• B) 3

• C) 9

• D) 0
Answer: A

36. Which is not a valid bitwise operator?

• A) &

• B) |

• C) &&

• D) ^
Answer: C

37. Bitwise XOR of a number with itself is?

• A) 1

• B) 0

• C) Same number

• D) -1
Answer: B

38. Which operator has highest precedence in C?

• A) &
• B) ^

• C) |

• D) ~
Answer: D

39. What does x << n do?

• A) Divides x by 2^n

• B) Multiplies x by 2^n

• C) Adds n to x

• D) Subtracts n from x
Answer: B

40. What is the size of int in C (typically)?

• A) 2 bytes

• B) 4 bytes

• C) 8 bytes

• D) 1 byte
Answer: B

41. Which condition is true for even number in bitwise?

• A) n & 1 == 1

• B) n & 1 == 0

• C) n | 1 == 0

• D) n ^ 1 == 1
Answer: B

42. Which recursion is used in Fibonacci sequence?

• A) Linear

• B) Tail

• C) Tree

• D) Direct
Answer: C

43. What is tail recursion?


• A) Recursive call at end

• B) Recursive call at beginning

• C) No recursion

• D) Loop recursion
Answer: A

44. Bitmasking is mainly used in:

• A) Searching

• B) Sorting

• C) Optimization

• D) Subset generation
Answer: D

45. What is the output of printf("%d", 1 ^ 1);?

• A) 1

• B) 0

• C) 2

• D) 11
Answer: B

46. What does recursion always require?

• A) Loop

• B) Array

• C) Base case

• D) Pointer
Answer: C

47. How to swap two numbers without temp variable?

• A) Using *, /

• B) Using XOR

• C) Using loop

• D) Not possible
Answer: B
48. Which of these is NOT a base case in recursion?

• A) if (n == 0)

• B) if (n == 1)

• C) return n;

• D) while(n > 0)
Answer: D

49. How many times will a tail recursive function execute?

• A) Until condition fails

• B) Forever

• C) Twice

• D) Depends on OS
Answer: A

50. Which of these reduces recursive function to iteration?

• A) Recursion stack

• B) Tail call optimization

• C) Bit masking

• D) Loops
Answer: B

51. What is the output?

int fun(int n) {

if(n == 0) return 1;

return n * fun(n - 1);

printf("%d", fun(3));

A) 6
B) 9
C) 3
D) 24
Answer: D
52. What is the output of the following?

int rec(int n) {

if (n <= 0)

return 0;

else

return rec(n - 1) + n;

int main() {

printf("%d", rec(3));

return 0;

A) 3
B) 6
C) 5
D) 1
Answer: B

53. What is the output?

int mystery(int n) {

if (n == 0)

return 1;

return 2 * mystery(n - 1);

int main() {

printf("%d", mystery(3));

A) 6
B) 8
C) 4
D) 2
Answer: B

54.Which function below is tail-recursive?

A) int fact(int n) {

if (n == 0) return 1;

return n * fact(n - 1);

b) int fact_helper(int n, int result) {

if (n == 0) return result;

return fact_helper(n - 1, result * n);

c) int print(int n) {

printf("%d\n", n);

return print(n - 1);

d) D) None of the above


Answer: B

55.What is the base case for computing Fibonacci numbers recursively?

int fib(int n) {

if (n <= 1)

return n;

return fib(n-1) + fib(n-2);

A) n == 2
B) n <= 0
C) n <= 1
D) n == 3
Answer: C

56. What is the output?

void printFun(int test) {


if (test < 1)

return;

else {

printf("%d ", test);

printFun(test - 1);

printf("%d ", test);

int main() {

printFun(3);

return 0;

A) 3 2 1 1 2 3
B) 1 2 3 3 2 1
C) 3 2 1
D) 1 2 3
Answer: A

You might also like