C Programming MCQ Question and Answer

By Priyanshu Vaish|Updated : November 1st, 2022

C Programming MCQ Questions and Answers are available here for candidates actively preparing for the placement. The C Programming MCQ Question and Answer may provide a brief overview of the questions you can expect to see in the GATE and other related exams.

C Programming MCQs PDF

Objective-based C Programming MCQ questions and answers can help you better understand the C programming better. Five major questions are listed here based on the most recent GATE CSE syllabus. Try to obtain the whole response to each MCQ in C programming that is provided in this article.

Download Complete C Programming and Data Structures Formula Notes PDF

Table of Content

C Programming MCQs with Answer

1. Consider the C program:

#include <stdio.h>

int main()

{

printf(7 + "BYJUS’SEXAMPREP");

return 0;

}

What is the output of the above code?

  1. BYJUS'SEXAMPREP
  2. EXAMPREP
  3. 5BYJUS'SEXAMPREP
  4. Compiler error

Answer: B. EXAMPREP

Solution

The stdio.h header file defines the library function printf. Through the expression 7 + "BYJUS'SEXAMPREP," the compiler adds 7 to the string's base address. The standard library function is then given the string "EXAMPREP" as an argument.

2. What is the output of the program?

#include <stdio.h>

int main()

{

int i = 3;

printf("%d", (++i)++);

return 0;

}

  1. 3
  2. 4C
  3. 5
  4. Compile-time error

Answer: D. Compile-time error

Solution

Prefix and postfix operators in the C language require l-value to operate and return r-value. When the expression (++i)++ is used, the value of the variable i (an l-value) is increased, and an r-value is returned. When the compiler tries to post-increment the value of an r-value, it produces the error "l-value needed."

3. What is the output of the program?

#include <stdio.h>

#if X == 3

#define Y 3

#else

#define Y 5

#endif

int main()

{

printf("%d", Y);

return 0;

}

  1. 3
  2. 5
  3. 3 or 5 depending on the value of X
  4. Compile-time error

Answer: B. 5

Solution

The absence of macro X initially causes the output to appear to be a compile-time error. In C, a macro's default value is set to 0 if the pre-processor does not define it. The control then shifts to the conditional else clause, where the number 5 is written.

4. Consider the following pseudocode:

x:=1;

i:=1;

while (x ≤ 500)

begin

x:=2x ;

i:=i+1;

end

What is the value of i at the end of the pseudocode?

  1. 4
  2. 5
  3. 6
  4. 7

Answer: B. 5

Solution

The program executes as:

In the first iteration: x = 2 and i = 2

In the second iteration: x = 4 and i = 3

In the third iteration: x = 16 and i = 4

In the fourth iteration: x = 16*16 and i = 5

In the fifth iteration: Condition incorrect.
So, option (B) is correct.

5. Consider the code

#include “stdio.h”

int main()

{

printf(“BYJU’SEXAMPREP”);

main();

return 0;

}

Which of the following statement is correct?

  1. We can't use main() inside main(), and the compiler will catch it by showing a compiler error.
  2. BYJU'SEXAMPREP would be printed 2147483647 times which is (2 to the power 31) - 1.
  3. BYJU'SEXAMPREP would be printed only once. Because when main() is used inside main(), it's ignored by the compiler at run time. This is to make sure that main() is called only once.
  4. BYJU'SEXAMPREP would be printed until stack overflow happens for this program.

Answer: D. BYJU'SEXAMPREP would be printed until stack overflow happens for this program.

Solution

Firstly, calling main() from another function is not restricted. Therefore main() can recursively call. However, this section does not state the recursion's explicit termination condition. So, after printing BYJU'SEXAMPREP, main() would call main(). This will continue until the program's stack is filled. Please be aware that a running program's internal stack records the calling function sequence—that is, the order in which one function is called another—so that control can be returned when the called function completes its work. Because of this, main() in this program would keep calling main() until the entire stack had been used or until a stack-overflow had occurred.

6. Consider the following program segment.

Consider the following program segment

The output of the above program will be ____. (Assume that the object of data type int occupies 2 bytes).

  1. 1
  2. 0
  3. 3
  4. 2

Answer: C. 3

Solution

• Store the string in an array and this array will be pointed by the pointer str.

Consider the following program segment.

• Now, pass the content of the pointer str that is 100 (base address of the array) to function Byjus().

Consider the following program segment. 2

Now,

• while *++P1 will execute until '\0'. so P1 will be pointing " 106 " at the end of while loop and P2 is pointing to location "100".

• The program will return P1 - P2 : It will return the number of elements between P1 and P2 of integer type (2 Byte).

Hence

Consider the following program segment. 3

7. The ‘i' value printed by the below program is _________

The ‘i' value printed by the below program is

  1. 1
  2. 0
  3. 5
  4. 2

Answer: D. 2

Solution

As per operator precedence and associativity, the order of evaluation is as follows:

1) 13/6 = 2

2) 8 * 3 = 24

3) 24/4 = 6

4) 6/2 = 3

5) 5 << 2 = = 5 * 22 = 5 × 4 = 20

6) 20 >> 3 = = 20/23 = 20/8 = 2

8. Consider the above program segment.

Consider the above program segment.

The size of (union) is ______ Bytes. (Note: Integer take 2 bytes)

  1. 100
  2. 120
  3. 105
  4. 90

Answer: B. 120

Solution

Union structure
int x[20] = 40 bytes.
char s[10] = 10 bytes.

Struct union

x[20]20 Bytes
⇒S[20]40 Bytes

s1 size = 60 Bytes, s2 size = 60 Bytes

“Union” structure size = 120 Bytes

9. What will be the output of the following code?

What will be the output of the following code?

  1. 1, 4
  2. 0, 5
  3. 1, 5
  4. 0, 4

Answer: A. 1, 4

Solution

Here, we have initialized two variables, and we have an uninitialized variable incr.

a = 6; b = 4

Now, incr = (a > b) → is evaluated to true, and true OR anything is true. So, here due to the short circuit, the statement b++ is not executed.

So, incr = 1 and b = 4

Hence, the correct answer is option A.

10. What will be the output of the program?

#include<stdio.h>
int main()

{

int arr[2][2][2] = {10, 2, 3, 4, 5, 6, 7, 8};

int *p, *q;

p = arr[1][1][1];

q = (int *) arr;

printf(“%d %d \n”, *p, *q);

return 0;

}

  1. 8, 10
  2. 10, 2
  3. 8, 1
  4. Garbage values

Answer: A. 8, 10

Solution

(I) Construct a 3-D array according to the given data :

What will be the output of the program?

(II) p = arr [1] [1] [1]: ‘p’ contains the address of 2nd, 2-D array, 2nd row’s, 2nd column’s address that is the address of element 8.

(II) q = (int *) arr: here (int *) type cast array into integer type that is a one-dimensional array. So, q contains the address of element 10.

(III) *p = 8 and *q = 10

Hence, the output will be 8, 10.

11. Consider the following code:

Consider the following code

What is in the place of x if the above C code prints the output as 32?

  1. b * c
  2. b * c – 2
  3. b + c * 2
  4. None

Answer: C. b + c * 2

Solution

For the output 32.

If we take case 2, it is executed until the break comes. So, the output is 322, which we do not require.

Therefore, we choose case 8.

we get output as 32.

Now, we check option which will give 8 as:

A. b * c = 2 * 3 = 6 (false)

B. b * c – 2 = 6 – 2 = 4 (false)

C. b + c * 2 = 2 + 3 * 2 = 8 (True)

Hence, option C is correct.

12. Consider the following program:

#include<stdio.h>

int main()

{

int i=5;

printf(“%d”, i = ++i == 6);

return 0;

}
What is the output of the above code?

  1. 4
  2. 6
  3. 2
  4. 1

Answer: D. 1

Solution

The expression i = ++i == 6 is treated as:

i = (++i) == 6

As ‘==’ has higher precedence then ‘=’ operator

So, i = (6==6)

i = 1

Hence, option D is correct.

13. Consider the following C function:

int f(int n)

{

static int result = 0;

if(n <= 1)

return 1;

else

result += f(n/3);

return result;

}

What is the output of the above function if n = 6 is passed?

  1. 3
  2. 1
  3. 2
  4. 0

Answer: C. 2

Solution

Consider the following C function

The value returned is 2.

Hence option C is correct.

14. Consider the program.

#include<stdio.h>

static int i;

static float j;

int main ()

{

int a ;

auto float b;

printf(“%d %f %d %f”, i, j, a, b);

return 0;

}

  1. 0 0 0 0
  2. 0 0.000000 Garbage 0.000000
  3. 0 0.000000 Garbage Garbage
  4. 0 0 Garbage Garbage

Answer: C. 0 0.000000 Garbage Garbage

Solution

Although, by default, all the variables are auto. Hence the garbage variable is printed.

The static variables have the default value zero.

Therefore, option C is correct.

15. Consider the program:

#include<stdio.h>

int main()

{

int y;

static char x = ‘A’;

x++;

y =++x;

printf(“%d %d”, x, y);

return 0;

}

What is the output?

  1. Garbage 67
  2. Error
  3. 67 Garbage
  4. 67 67

Answer: D. 67 67

Solution

char x = ‘A’

ASCII value of ‘A’ is 65.

x++; → 66.

y = ++x → y = 67

x = 67

So, the value printed by the code is 67 67.

16. What will be output when the C code is executed?

#include<stdio.h>

void main()

{

int offset = 1;

switch (offset << 2 + offset )

{

default: printf (“BYJU’S”);

case 4: printf(“EXAM”);

case 5: printf(“Prep”);

case 8: printf(“GATE”);

}

}

  1. BYJU’SEXAMPrepGATE
  2. GATE
  3. EXAM
  4. Prep.

Answer: B. GATE

Solution

The left shift operator has less priority than the ‘+’ operator

so,

= [offset <<(2 + offset)]

= [ offset <<(2 + 1)]

= (1 << 3)

= 1 × 23 = 8
Now, value 8 is passed to switch(), which will match case 8.

Therefore case 8 is executed and print “GATE” as output.

17. Consider the below code.

void main()

{

int a = 3.0 * 4 % 2 - 3/2.0;

printf(“%d”, a);

}

What is the output?

  1. 0
  2. 1.00
  3. 1
  4. Compiler error

Answer: D. Compile error

Solution

MOD (%) operator has the following rules, which are as follows:

⇒ Both operands must be of integer(int) type.

⇒ Sign of the result is the same as the first operand.

For example

+ve % +ve → + ve

+ve % -ve → +ve.

-ve % +ve → -ve.

• If the operation is between integer and integer, the result is an integer.

• If the operation is between integer and float, the result is float.

• The priority/precedence of *, %, / is the same.

a = {[(3.0 * 4) % 2] – (3/2.0)}.

a = [(12.0 % 2) – 3/2.0]

• Both the operator of the mod is not integer type.

Therefore, it gives compiler errors.

18. What will be the output of the C program?

int main()

{

int a = 2, b = 2, c = 0, d = 2, m;

m = a++ b++ c++ || d++;

printf(“%d %d %d %d %d”, a, b, c, d, m);

return 0;

}

  1. Compiler error
  2. 33131
  3. Same garbage value
  4. 33130

Answer: B. 33131

Solution

a = 2, b = 2 , c = 0 , d = 2

The evaluation order of expression is as below:

m = [(a++ b++) c++]||d++

m = [(2 2) c++]||d++

m = 1 0||d++

m =0||2 : True
So, 1 will be updated in 'm' i.e. m=1.

We are using the post-increment operator so it will increment the value to a, b, c, and d after statement evaluation.
a= 3
b=3
c=1
d=3

So, the output is 33131

Therefore, option B is correct.

19. What is the output of the following code?

#include<stdio.h>

void main()

{

int x = 1, y = 1;

for(;y; printf(“%d%d”, x, y))

y =x++<=5;

}

  1. 21341516170
  2. 2131416170
  3. 213141516170
  4. 21314151

Answer: C. 213141516170

Solution

x = 1 y = 1

The for-loop condition is true.

So,

y = 1<=5 (True)

y = 1

x = 2

the output printed is 21.

Again, run for loop for y = 1 x = 2

the output printed is 2131

Similarly, for the next values of x and y until the y value becomes 0.

Therefore, the final value of the output is 21314151670

20. Consider the code.

#include<stdio.h>

int main ()

{

int a;

printf(“%d”, sizeof 1==sizeof 4) ;

return 0;

}

  1. 0
  2. 4
  3. 1
  4. Compaction error

Answer: C. 1

Solution

Sizeof is the unary operator, and the parentheses are optional with constant, variable and expression but mandatory with the data type.

Therefore, option C is correct.

21. Consider the following C code. Find the output.

Consider the following C code. Find the output.

  1. 5
  2. 12
  3. 8
  4. None

Answer: C. 8

Solution

i = 0, j = 0 ⇒ j = 2

i = 1, j = 1 ⇒ j = 3

i = 2, j = 2 ⇒ j = 4

i = 3, j = 3 ⇒ j = 5

i = 4, j = 4 ⇒ condition fails as j < 4

i + j ⇒ 4 + 4 = 8

So, the output is 8.

22. Consider the following program:

Consider the following program

What is the output of the above program by using static scoping?

  1. 10,7
  2. 10,10
  3. 7,10
  4. 7,7

Answer: B. 10, 10

Solution

In static scope, if local variable is not available then it will take the value from the most recent ancestor variable. So whenever main is calling f() and g() , then in both cases value of 'b' in f() is 5.

So, B is the correct answer.

23. Consider the C code:

#include <stdio.h>

int fun(int n)

{

if (n == 4)

return n;

else return 2*fun(n+1);

}

int main()

{

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

return 0;

}

The output of the given code is ________.

  1. 4
  2. 8
  3. 16
  4. Run time error

Answer: C. 16

Solution

Fun(2) = 2 * Fun(3) and Fun(3) = 2 * Fun(4) ....(i)

Fun(4) = 4 ......(ii)

From equation (i) and (ii),

Fun(2) = 2 * 2 * Fun(4)

Fun(2) = 2 * 2 * 4

Fun(2) = 16.

So, C is the correct answer.

24. What is the output of the following C-code?

What is the output of the following C-code

  1. D
  2. E
  3. C
  4. None of the above

Answer: B. E

Solution

a is the array

"ECENT" is the string constant

ptr=a// ptr will start pointing to the array a.

strcpy(a, ptr)// both pointers pointing to the same array, so DECENT is replaced by DECENT

*ptr = D + 1 // ASCII + 1

So the answer is E.

25. Consider the following:

Consider the following

What is the output of the above program?

  1. It prints garbage value
  2. It prints the address of ‘a’
  3. Compile Time Error
  4. Run Time Error

Answer: C. Compile Time Error

Solution

For register variables, we cannot apply the address operator so it will generate compile time error.

Hence answer is option C.

☛ Related Questions:

Comments

write a comment

Follow us for latest updates