0% found this document useful (0 votes)
6 views

C Programming MCQ

The document contains 1000 multiple choice questions (MCQs) on C programming, covering various topics such as variable names, data types, functions, and operators. Each question includes options and the correct answer along with explanations. The questions are structured to test knowledge on fundamental concepts of the C programming language.

Uploaded by

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

C Programming MCQ

The document contains 1000 multiple choice questions (MCQs) on C programming, covering various topics such as variable names, data types, functions, and operators. Each question includes options and the correct answer along with explanations. The questions are structured to test knowledge on fundamental concepts of the C programming language.

Uploaded by

l.n.sharma666
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 119

C Programming MCQ (Multiple Choice

Questions)
Here are 1000 MCQs on C Programming (Chapterwise).
1. Who is the father of C language?
a) Steve Jobs
b) James Gosling
c) Dennis Ritchie
d) Rasmus Lerdorf
View Answer
Answer: c
Explanation: Dennis Ritchie is the father of C Programming Language. C programming
language was developed in 1972 at American Telephone & Telegraph Bell Laboratories of
USA.

2. Which of the following is not a valid C variable name?


a) int number;
b) float rate;
c) int variable_count;
d) int $main;
View Answer
Answer: d
Explanation: Since only underscore and no other special character is allowed in a variable
name, it results in an error.

3. All keywords in C are in ____________


a) LowerCase letters
b) UpperCase letters
c) CamelCase letters
d) None of the mentioned
View Answer
Answer: a
Explanation: None.

4. Which of the following is true for variable names in C?


a) They can contain alphanumeric characters as well as special characters
b) It is not an error to declare a variable to be one of the keywords(like goto, static)
c) Variable names cannot start with a digit
d) Variable can be of any length
View Answer
Answer: c
Explanation: According to the syntax for C variable name, it cannot start with a digit.

5. Which is valid C expression?


a) int my_num = 100,000;
b) int my_num = 100000;
c) int my num = 1000;
d) int $my_num = 10000;
View Answer
Answer: b
Explanation: Space, comma and $ cannot be used in a variable name.

6. Which of the following cannot be a variable name in C?


a) volatile
b) true
c) friend
d) export
View Answer
Answer: a
Explanation: volatile is C keyword.
advertisement
ADVERTISEMENT
ADVERTISEMENT

7. What is short int in C programming?


a) The basic data type of C
b) Qualifier
c) Short is the qualifier and int is the basic data type
d) All of the mentioned
View Answer
Answer: c
Explanation: None.

8. Which of the following declaration is not supported by C language?


a) String str;
b) char *str;
c) float str = 3e2;
d) Both “String str;” and “float str = 3e2;”
View Answer
Answer: a
Explanation: It is legal in Java, but not in C language.

9. Which keyword is used to prevent any changes in the variable within a C program?
a) immutable
b) mutable
c) const
d) volatile
View Answer
Answer: c
Explanation: const is a keyword constant in C program.

10. What is the result of logical or relational expression in C?


a) True or False
b) 0 or 1
c) 0 if an expression is false and any positive number if an expression is true
d) None of the mentioned
View Answer
Answer: b
Explanation: None.

11. Which of the following typecasting is accepted by C language?


a) Widening conversions
b) Narrowing conversions
c) Widening & Narrowing conversions
d) None of the mentioned
View Answer
Answer: c
Explanation: None.

12. Where in C the order of precedence of operators do not exist?


a) Within conditional statements, if, else
b) Within while, do-while
c) Within a macro definition
d) None of the mentioned
View Answer
Answer: d
Explanation: None.

13. Which of the following is NOT possible with any 2 operators in C?


a) Different precedence, same associativity
b) Different precedence, different associativity
c) Same precedence, different associativity
d) All of the mentioned
View Answer
Answer: c
Explanation: None.

14. What is an example of iteration in C?


a) for
b) while
c) do-while
d) all of the mentioned
View Answer
Answer: d
Explanation: None.

15. Functions can return enumeration constants in C?


a) true
b) false
c) depends on the compiler
d) depends on the standard
View Answer
Answer: a
Explanation: None.

16. Functions in C Language are always _________


a) Internal
b) External
c) Both Internal and External
d) External and Internal are not valid terms for functions
View Answer
Answer: b
Explanation: None.

17. Which of following is not accepted in C?


a) static a = 10; //static as
b) static int func (int); //parameter as static
c) static static int a; //a static variable prefixed with static
d) all of the mentioned
View Answer
Answer: c
Explanation: None.

18. Property which allows to produce different executable for different platforms in C is
called?
a) File inclusion
b) Selective inclusion
c) Conditional compilation
d) Recursive macros
View Answer
Answer: c
Explanation: Conditional compilation is the preprocessor facility to produce a different
executable.

19. What is #include <stdio.h>?


a) Preprocessor directive
b) Inclusion directive
c) File inclusion directive
d) None of the mentioned
View Answer
Answer: a
Explanation: None.

20. C preprocessors can have compiler specific features.


a) True
b) False
c) Depends on the standard
d) Depends on the platform
View Answer
Answer: a
Explanation: #pragma is compiler specific feature.

21. Which of the following are C preprocessors?


a) #ifdef
b) #define
c) #endif
d) all of the mentioned
View Answer
Answer: d
Explanation: None.

22. The C-preprocessors are specified with _________ symbol.


a) #
b) $
c) ” ”
d) &
View Answer
Answer: a
Explanation: The C-preprocessors are specified with # symbol.

23. How is search done in #include and #include “somelibrary.h” according to C standard?
a) When former is used, current directory is searched and when latter is used, standard
directory is searched
b) When former is used, standard directory is searched and when latter is used, current
directory is searched
c) When former is used, search is done in implementation defined manner and when latter
is used, current directory is searched
d) For both, search for ‘somelibrary’ is done in implementation-defined places
View Answer
Answer: b
Explanation: None.

24. How many number of pointer (*) does C have against a pointer variable declaration?
a) 7
b) 127
c) 255
d) No limits
View Answer
Answer: d
Explanation: None.

25. Which of the following is not possible statically in C language?


a) Jagged Array
b) Rectangular Array
c) Cuboidal Array
d) Multidimensional Array
View Answer
Answer: a
Explanation: None.

26. Which of the following return-type cannot be used for a function in C?


a) char *
b) struct
c) void
d) none of the mentioned
View Answer
Answer: d
Explanation: None.

27. The standard header _______ is used for variable list arguments (…) in C.
a) <stdio.h >
b) <stdlib.h>
c) <math.h>
d) <stdarg.h>
View Answer
Answer: d
Explanation: None.

28. When a C program is started, O.S environment is responsible for opening file and
providing pointer for that file?
a) Standard input
b) Standard output
c) Standard error
d) All of the mentioned
View Answer
Answer: d
Explanation: None.

29. In C language, FILE is of which data type?


a) int
b) char *
c) struct
d) None of the mentioned
View Answer
Answer: c
Explanation: None.

30. What is the sizeof(char) in a 32-bit C compiler?


a) 1 bit
b) 2 bits
c) 1 Byte
d) 2 Bytes
View Answer
Answer: c
Explanation: None.

31. Which of the following is not an operator in C?


a) ,
b) sizeof()
c) ~
d) None of the mentioned
View Answer
Answer: d
Explanation: None.
32. scanf() is a predefined function in______header file.
a) stdlib. h
b) ctype. h
c) stdio. h
d) stdarg. h
View Answer
Answer: c
Explanation: scanf() is a predefined function in "stdio.h" header file.printf and scanf() carry
out input and output functions in C. These functions statements are present in the header
file stdio.h.

33. What is meant by ‘a’ in the following C operation?

fp = fopen("Random.txt", "a");

a) Attach
b) Append
c) Apprehend
d) Add
View Answer
Answer: b
Explanation: None.

34. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int y = 10000;
5. int y = 34;
6. printf("Hello World! %d\n", y);
7. return 0;
8. }

a) Compile time error


b) Hello World! 34
c) Hello World! 1000
d) Hello World! followed by a junk value
View Answer
Answer: a
Explanation: Since y is already defined, redefining it results in an error.
Output:
$ cc pgm2.c
pgm2.c: In function ‘main’:
pgm2.c:5: error: redefinition of ‘y’
pgm2.c:4: note: previous definition of ‘y’ was here

35. What will happen if the following C code is executed?

1. #include <stdio.h>
2. int main()
3. {
4. int main = 3;
5. printf("%d", main);
6. return 0;
7. }

a) It will cause a compile-time error


b) It will cause a run-time error
c) It will run without any error and prints 3
d) It will experience infinite looping
View Answer
Answer: c
Explanation: A C program can have same function name and same variable name.
$ cc pgm3.c
$ a.out
3

36. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. signed char chr;
5. chr = 128;
6. printf("%d\n", chr);
7. return 0;
8. }

a) 128
b) -128
c) Depends on the compiler
d) None of the mentioned
View Answer
Answer: b
Explanation: The range of signed character is from -128 to +127. Since we are assigning a
value of 128 to the variable ‘chr’, the result will be negative. 128 in binary is represented as
“1000 0000” for character datatype. As you can see that the sign bit is set to 1, followed by
7 zeros (0), its final decimal value will be -128 (negative 128).
Output:
$ cc pgm2.c
$ a.out
-128

37. What will be the output of the following C code on a 64 bit machine?

1. #include <stdio.h>
2. union Sti
3. {
4. int nu;
5. char m;
6. };
7. int main()
8. {
9. union Sti s;
10. printf("%d", sizeof(s));
11. return 0;
12. }

a) 8
b) 5
c) 9
d) 4
View Answer
Answer: d
Explanation: Since the size of a union is the size of its maximum data type, here int is the
largest data type. Hence the size of the union is 4.
Output:
$ cc pgm7.c
$ a.out
4

38. What will be the output of the following C function?

1. #include <stdio.h>
2. enum birds {SPARROW, PEACOCK, PARROT};
3. enum animals {TIGER = 8, LION, RABBIT, ZEBRA};
4. int main()
5. {
6. enum birds m = TIGER;
7. int k;
8. k = m;
9. printf("%d\n", k);
10. return 0;
11. }

a) 0
b) Compile time error
c) 1
d) 8
View Answer
Answer: d
Explanation: m is an integer constant, hence it is compatible.
Output:
$ cc pgm5.c
$ a.out
8

39. What will be the output of the following C code?

1. #include <stdio.h>
2. int const print()
3. {
4. printf("Sanfoundry.com");
5. return 0;
6. }
7. void main()
8. {
9. print();
10. }

a) Error because function name cannot be preceded by const


b) Sanfoundry.com
c) Sanfoundry.com is printed infinite times
d) Blank screen, no output
View Answer
Answer: b
Explanation: None.
Output:
$ cc pgm13.c
$ a.out
Sanfoundry.com

40. Will the following C code compile without any error?

1. #include <stdio.h>
2. int main()
3. {
4. for (int k = 0; k < 10; k++);
5. return 0;
6. }

a) Yes
b) No
c) Depends on the C standard implemented by compilers
d) Error
View Answer
Answer: c
Explanation: Compilers implementing C90 do not allow this, but compilers implementing
C99 allow it.
Output:
$ cc pgm4.c
pgm4.c: In function ‘main’:
pgm4.c:4: error: ‘for’ loop initial declarations are only allowed in C99 mode
pgm4.c:4: note: use option -std=c99 or -std=gnu99 to compile your code

41. What will be the final value of x in the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int x = 5 * 9 / 3 + 9;
5. }

a) 3.75
b) Depends on compiler
c) 24
d) 3
View Answer
Answer: c
Explanation: None.

42. What will be the output of the following C code? (Initial values: x= 7, y = 8)

1. #include <stdio.h>
2. void main()
3. {
4. float x;
5. int y;
6. printf("enter two numbers \n");
7. scanf("%f %f", &x, &y);
8. printf("%f, %d", x, y);
9. }

a) 7.000000, 7
b) Run time error
c) 7.000000, junk
d) Varies
View Answer
Answer: c
Explanation: None.

43. What will be the output of the following C code considering the size of a short int is 2,
char is 1 and int is 4 bytes?

1. #include <stdio.h>
2. int main()
3. {
4. short int i = 20;
5. char c = 97;
6. printf("%d, %d, %d\n", sizeof(i), sizeof(c), sizeof(c + i));
7. return 0;
8. }

a) 2, 1, 2
b) 2, 1, 1
c) 2, 1, 4
d) 2, 2, 8
View Answer
Answer: c
Explanation: None.

44. What is the difference between the following 2 C codes?

1. #include <stdio.h> //Program 1


2. int main()
3. {
4. int d, a = 1, b = 2;
5. d = a++ + ++b;
6. printf("%d %d %d", d, a, b);
7. }
1. #include <stdio.h> //Program 2
2. int main()
3. {
4. int d, a = 1, b = 2;
5. d = a++ +++b;
6. printf("%d %d %d", d, a, b);
7. }

a) No difference as space doesn’t make any difference, values of a, b, d are same in both
the case
b) Space does make a difference, values of a, b, d are different
c) Program 1 has syntax error, program 2 is not
d) Program 2 has syntax error, program 1 is not
View Answer
Answer: d
Explanation: None.

45. What will be the output of the following C code snippet?

1. #include <stdio.h>
2. void main()
3. {
4. 1 < 2 ? return 1: return 2;
5. }

a) returns 1
b) returns 2
c) Varies
d) Compile time error
View Answer
Answer: d
Explanation: None.

46. What will be the value of the following assignment expression?

(x = foo())!= 1 considering foo() returns 2

a) 2
b) True
c) 1
d) 0
View Answer
Answer: a
Explanation: None.

47. What will be the output of the following C function?

1. #include <stdio.h>
2. void reverse(int i);
3. int main()
4. {
5. reverse(1);
6. }
7. void reverse(int i)
8. {
9. if (i > 5)
10. return ;
11. printf("%d ", i);
12. return reverse((i++, i));
13. }

a) 1 2 3 4 5
b) Segmentation fault
c) Compilation error
d) Undefined behaviour
View Answer
Answer: a
Explanation: None.

48. What will be the final values of i and j in the following C code?

1.#include <stdio.h>
2.int x = 0;
3.int f()
4.{
5. if (x == 0)
6. return x + 1;
7. else
8. return x - 1;
9.}
10. int g()
11. {
12. return x++;
13. }
14. int main()
15. {
16. int i = (f() + g()) | g(); //bitwise or
17. int j = g() | (f() + g()); //bitwise or
18. }

a) i value is 1 and j value is 1


b) i value is 0 and j value is 0
c) i value is 1 and j value is undefined
d) i and j value are undefined
View Answer
Answer: c
Explanation: None.

49. Comment on the following C statement.

n = 1;
printf("%d, %dn", 3*n, n++);
a) Output will be 3, 2
b) Output will be 3, 1
c) Output will be 6, 1
d) Output is compiler dependent
View Answer
Answer: d
Explanation: None.

50. How many times i value is checked in the following C program?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 0;
5. while (i < 3)
6. i++;
7. printf("In while loop\n");
8. }

a) 2
b) 3
c) 4
d) 1
View Answer
Answer: c
Explanation: None.

51. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 0;
5. do
6. {
7. i++;
8. if (i == 2)
9. continue;
10. printf("In while loop ");
11. } while (i < 2);
12. printf("%d\n", i);
13. }

a) In while loop 2
b) In while loop in while loop 3
c) In while loop 3
d) Infinite loop
View Answer
Answer: a
Explanation: None.

52. What will be the data type returned for the following C function?
1. #include <stdio.h>
2. int func()
3. {
4. return (double)(char)5.0;
5. }

a) char
b) int
c) double
d) multiple type-casting in return is illegal
View Answer
Answer: b
Explanation: None.

53. What is the problem in the following C declarations?

int func(int);
double func(int);
int func(float);

a) A function with same name cannot have different signatures


b) A function with same name cannot have different return types
c) A function with same name cannot have different number of parameters
d) All of the mentioned
View Answer
Answer: d
Explanation: None.

54. Which option should be selected to work the following C expression?

string p = "HELLO";

a) typedef char [] string;


b) typedef char *string;
c) typedef char [] string; and typedef char *string;
d) Such expression cannot be generated in C
View Answer
Answer: b
Explanation: None.

55. What is the meaning of the following C statement?

printf(“%10s”, state);

a) 10 spaces before the string state is printed


b) Print empty spaces if the string state is less than 10 characters
c) Print the last 10 characters of the string
d) None of the mentioned
View Answer
Answer: b
Explanation: None.

56. What are the elements present in the array of the following C code?

int array[5] = {5};

a) 5, 5, 5, 5, 5
b) 5, 0, 0, 0, 0
c) 5, (garbage), (garbage), (garbage), (garbage)
d) (garbage), (garbage), (garbage), (garbage), 5
View Answer
Answer: b
Explanation: None.

57. What will be the output of the following C function when EOF returns?

int fputs(char *line, FILE *fp)

a) ‘�’ character of array line is encountered


b) ‘n’ character in array line is encountered
c) ‘t’ character in array line is encountered
d) When an error occurs
View Answer
Answer: d
Explanation: None.

58. Which part of the program address space is p stored in the following C code?

1. #include <stdio.h>
2. int *p;
3. int main()
4. {
5. int i = 0;
6. p = &i;
7. return 0;
8. }

a) Code/text segment
b) Data segment
c) Bss segment
d) Stack
View Answer
Answer: c
Explanation: None.

59. Which of the following sequences are unaccepted in C language?


a)

#if
#else
#endif

b)

#if
#elif
#endif

c)

#if
#if
#endif

d)

#if
#undef
#endif

View Answer
Answer: c
Explanation: None.

59. Comment on the output of following C code.

1. #include <stdio.h>
2. main()
3. {
4. char *p = 0;
5. *p = 'a';
6. printf("value in pointer p is %c\n", *p);
7. }

a) It will print a
b) It will print 0
c) Compile time error
d) Run time error
View Answer
Answer:d
Output:
$ cc pgm.c
$ a.out
Segmentation fault (core dumped)

60. What is the output of this C code?

1. #include <stdio.h>
2. main()
3. {
4. if (sizeof(int) > -1)
5. printf("True");
6. else
7. printf("False");
8. }

a) True
b) False
View Answer
Answer:b
Output:
$ cc pgm.c
$ a.out
False

61. What is the output of this C code?

1. #include <stdio.h>
2. main()
3. {
4. char *p = "Sanfoundry C-Test";
5. p[0] = 'a';
6. p[1] = 'b';
7. printf("%s", p);
8. }

a) abnfoundry C-Test
b) Sanfoundry C-Test
c) Compile time error
d) Run time error
View Answer
Answer:d
Output:
$ cc pgm.c
$ a.out
Segmentation fault (core dumped)

62. What is the output of this C code?

1. #include <stdio.h>
2. int main()
3. {
4. float f = 0.1;
5. if (f == 0.1)
6. printf("True");
7. else
8. printf("False");
9. }

a) True
b) False
View Answer
Answer: b
Output:
$ cc pgm.c
$ a.out
False

63. What is the output of this C code?

1. #include <stdio.h>
2. main()
3. {
4. int n = 0, m = 0;
5. if (n > 0)
6. if (m > 0)
7. printf("True");
8. else
9. printf("False");
10. }

a) True
b) False
c) No Output will be printed
d) Run Time Error
View Answer
Answer:c

C Programming Questions and Answers –


Variable Names – 1
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Variable Names –
1”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. C99 standard guarantees uniqueness of __________ characters for internal names.
a) 31
b) 63
c) 12
d) 14
View Answer
Answer: b
Explanation: ISO C99 compiler may consider only first 63 characters for internal names.

2. C99 standard guarantees uniqueness of ___________ characters for external names.


a) 31
b) 6
c) 12
d) 14
View Answer
Answer: a
Explanation: ISO C99 compiler may consider only first 31 characters for external names.

advertisement

3. Which of the following is not a valid variable name declaration?


a) int __a3;
b) int __3a;
c) int __A3;
d) None of the mentioned
View Answer
Answer: d
Explanation: None.

4. Which of the following is not a valid variable name declaration?


a) int _a3;
b) int a_3;
c) int 3_a;
d) int _3a
View Answer
Answer: c
Explanation: Variable name cannot start with a digit.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate


Now!

5. Why do variable names beginning with the underscore is not encouraged?


a) It is not standardized
b) To avoid conflicts since assemblers and loaders use such names
c) To avoid conflicts since library routines use such names
d) To avoid conflicts with environment variables of an operating system
View Answer
Answer: c
Explanation: None.

6. All keywords in C are in ____________


a) LowerCase letters
b) UpperCase letters
c) CamelCase letters
d) None of the mentioned
View Answer
Answer: a
Explanation: None.

7. Variable name resolution (number of significant characters for the uniqueness of variable)
depends on ___________
a) Compiler and linker implementations
b) Assemblers and loaders implementations
c) C language
d) None of the mentioned
View Answer
Answer: a
Explanation: It depends on the standard to which compiler and linkers are adhering.

8. Which of the following is not a valid C variable name?


a) int number;
b) float rate;
c) int variable_count;
d) int $main;
View Answer
Answer: d
Explanation: Since only underscore and no other special character is allowed in a variable
name, it results in an error.

9. Which of the following is true for variable names in C?


a) They can contain alphanumeric characters as well as special characters
b) It is not an error to declare a variable to be one of the keywords(like goto, static)
c) Variable names cannot start with a digit
d) Variable can be of any length
View Answer
Answer: c
Explanation: According to the syntax for C variable name, it cannot start with a digit.

C Programming Questions and Answers –


Variable Names – 2
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Variable Names –
2”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. Which is valid C expression?
a) int my_num = 100,000;
b) int my_num = 100000;
c) int my num = 1000;
d) int $my_num = 10000;
View Answer
Answer: b
Explanation: Space, comma and $ cannot be used in a variable name.

2. What will be the output of the following C code?

advertisement

1. #include <stdio.h>
2. int main()
3. {
4. printf("Hello World! %d \n", x);
5. return 0;
6. }
a) Hello World! x;
b) Hello World! followed by a junk value
c) Compile time error
d) Hello World!
View Answer
Answer: c
Explanation: It results in an error since x is used without declaring the variable x.
Output:
$ cc pgm1.c
pgm1.c: In function ‘main’:
pgm1.c:4: error: ‘x’ undeclared (first use in this function)
pgm1.c:4: error: (Each undeclared identifier is reported only once
pgm1.c:4: error: for each function it appears in.)

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate


Now!

3. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int y = 10000;
5. int y = 34;
6. printf("Hello World! %d\n", y);
7. return 0;
8. }
a) Compile time error
b) Hello World! 34
c) Hello World! 1000
d) Hello World! followed by a junk value
View Answer
Answer: a
Explanation: Since y is already defined, redefining it results in an error.
Output:
$ cc pgm2.c
pgm2.c: In function ‘main’:
pgm2.c:5: error: redefinition of ‘y’
pgm2.c:4: note: previous definition of ‘y’ was here

4. Which of the following is not a valid variable name declaration?


a) float PI = 3.14;
b) double PI = 3.14;
c) int PI = 3.14;
d) #define PI 3.14
View Answer
Answer: d
Explanation: #define PI 3.14 is a macro preprocessor, it is a textual substitution.

5. What will happen if the following C code is executed?

1. #include <stdio.h>
2. int main()
3. {
4. int main = 3;
5. printf("%d", main);
6. return 0;
7. }
a) It will cause a compile-time error
b) It will cause a run-time error
c) It will run without any error and prints 3
d) It will experience infinite looping
View Answer
Answer: c
Explanation: A C program can have same function name and same variable name.
$ cc pgm3.c
$ a.out
3

6. What is the problem in the following variable declaration?

float 3Bedroom-Hall-Kitchen?;

a) The variable name begins with an integer


b) The special character ‘-‘
c) The special character ‘?’
d) All of the mentioned
View Answer
Answer: d
Explanation: A variable name cannot start with an integer, along with that the C compiler
interprets the ‘-‘ and ‘?’ as a minus operator and a question mark operator respectively.

7. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int ThisIsVariableName = 12;
5. int ThisIsVariablename = 14;
6. printf("%d", ThisIsVariablename);
7. return 0;
8. }
a) The program will print 12
b) The program will print 14
c) The program will have a runtime error
d) The program will cause a compile-time error due to redeclaration
View Answer
Answer: b
Explanation: Variable names ThisIsVariablename and ThisIsVariableName are both distinct
as C is case sensitive.
Output:
$ cc pgm4.c
$ a.out
14

8. Which of the following cannot be a variable name in C?


a) volatile
b) true
c) friend
d) export
View Answer
Answer: a
Explanation: volatile is C keyword.

C Programming Questions and Answers –


Data Types and Sizes – 2
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Data Types and
Sizes – 2”.

Pre-requisite for C Data Types MCQ set: Video Tutorial on C Data Types.
1. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. float f1 = 0.1;
5. if (f1 == 0.1)
6. printf("equal\n");
7. else
8. printf("not equal\n");
9. }
a) equal
b) not equal
c) output depends on the compiler
d) error
View Answer
Answer: b
Explanation: 0.1 by default is of type double which has different representation than float
resulting in inequality even after conversion.
Output:
$ cc pgm4.c
$ a.out
not equal

advertisement

ADVERTISEMENT

ADVERTISEMENT

2. What will be the output of the following C code?

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate


Now!
1. #include <stdio.h>
2. int main()
3. {
4. float f1 = 0.1;
5. if (f1 == 0.1f)
6. printf("equal\n");
7. else
8. printf("not equal\n");
9. }
a) equal
b) not equal
c) output depends on compiler
d) error
View Answer
Answer: a
Explanation: 0.1f results in 0.1 to be stored in floating point representations.
Output:
$ cc pgm5.c
$ a.out
equal

3. What will be the output of the following C code on a 32-bit machine?

1. #include <stdio.h>
2. int main()
3. {
4. int x = 10000;
5. double y = 56;
6. int *p = &x;
7. double *q = &y;
8. printf("p and q are %d and %d", sizeof(p), sizeof(q));
9. return 0;
10. }
a) p and q are 4 and 4
b) p and q are 4 and 8
c) compiler error
d) p and q are 2 and 8
View Answer
Answer: a
Explanation: Size of any type of pointer is 4 on a 32-bit machine.
Output:
$ cc pgm6.c
$ a.out
p and q are 4 and 4

4. Which is correct with respect to the size of the data types?


a) char > int > float
b) int > char > float
c) char < int < double
d) double > char > int
View Answer
Answer: c
Explanation: char has less bytes than int and int has less bytes than double in any system

5. What will be the output of the following C code on a 64 bit machine?

1. #include <stdio.h>
2. union Sti
3. {
4. int nu;
5. char m;
6. };
7. int main()
8. {
9. union Sti s;
10. printf("%d", sizeof(s));
11. return 0;
12. }
a) 8
b) 5
c) 9
d) 4
View Answer
Answer: d
Explanation: Since the size of a union is the size of its maximum data type, here int is the
largest data type. Hence the size of the union is 4.
Output:
$ cc pgm7.c
$ a.out
4
6. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. float x = 'a';
5. printf("%f", x);
6. return 0;
7. }
a) a
b) run time error
c) a.0000000
d) 97.000000
View Answer
Answer: d
Explanation: Since the ASCII value of a is 97, the same is assigned to the float variable and
printed.
Output:
$ cc pgm8.c
$ a.out
97.000000

7. Which of the data types has the size that is variable?


a) int
b) struct
c) float
d) double
View Answer
Answer: b
Explanation: Since the size of the structure depends on its fields, it has a variable size

C Programming Questions and Answers –


Constants – 1
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Constants – 1”.

Pre-requisite for C Constants MCQ set: Video Tutorial on C Constants.


1. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. enum {ORANGE = 5, MANGO, BANANA = 4, PEACH};
5. printf("PEACH = %d\n", PEACH);
6. }
a) PEACH = 3
b) PEACH = 4
c) PEACH = 5
d) PEACH = 6
View Answer
Answer: c
Explanation: In enum, the value of constant is defined to the recent assignment from left.
Output:
$ cc pgm1.c
$ a.out
PEACH = 5

advertisement

2. What will be the output of the following C code?

Subscribe Now: C Newsletter | Important Subjects Newsletters


1. #include <stdio.h>
2. int main()
3. {
4. printf("C programming %s", "Class by\n%s Sanfoundry", "WOW");
5. }
a)

C programming Class by

WOW Sanfoundry

b) C programming Class by\n%s Sanfoundry


c)

C programming Class by

%s Sanfoundry

d) Compilation error
View Answer
Answer: c
Explanation: This program has only one %s within first double quotes, so it does not read
the string “WOW”.
The %s along with the Sanfoundry is not read as a format modifier while new line character
prints the new line.
Output:
$ cc pgm2.c
$ a.out
C programming Class by
%s Sanfoundry
3. In the following code snippet, character pointer str holds a reference to the string
___________

char *str = "Sanfoundry.com\0" "training classes";


a) Sanfoundry.com
b) Sanfoundry.com\0training classes
c) Sanfoundry.comtraining classes
d) Invalid declaration
View Answer
Answer: b
Explanation: ‘\0’ is accepted as a char in the string. Even though strlen will give length of
string “Sanfoundry.com”, in memory str is pointing to entire string including training classes.

4. What will be the output of the following C code?

1. #include <stdio.h>
2. #define a 10
3. int main()
4. {
5. const int a = 5;
6. printf("a = %d\n", a);
7. }
a) a = 5
b) a = 10
c) Compilation error
d) Runtime error
View Answer
Answer: c
Explanation: The #define substitutes a with 10 without leaving any identifier, which results in
Compilation error.
Output:
$ cc pgm3.c
pgm3.c: In function ‘main’:
pgm3.c:5: error: expected identifier or ‘(’ before numeric constant

5. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int var = 010;
5. printf("%d", var);
6. }
a) 2
b) 8
c) 9
d) 10
View Answer
Answer: b
Explanation: 010 is octal representation of 8.
Output:
$ cc pgm4.c
$ a.out
8

6. What will be the output of the following C function?

1. #include <stdio.h>
2. enum birds {SPARROW, PEACOCK, PARROT};
3. enum animals {TIGER = 8, LION, RABBIT, ZEBRA};
4. int main()
5. {
6. enum birds m = TIGER;
7. int k;
8. k = m;
9. printf("%d\n", k);
10. return 0;
11. }
a) 0
b) Compile time error
c) 1
d) 8
View Answer
Answer: d
Explanation: m is an integer constant, hence it is compatible.
Output:
$ cc pgm5.c
$ a.out
8

7. What will be the output of the following C code?

1. #include <stdio.h>
2. #define MAX 2
3. enum bird {SPARROW = MAX + 1, PARROT = SPARROW + MAX};
4. int main()
5. {
6. enum bird b = PARROT;
7. printf("%d\n", b);
8. return 0;
9. }
a) Compilation error
b) 5
c) Undefined value
d) 2
View Answer
Answer: b
Explanation: MAX value is 2 and hence PARROT will have value 3 + 2.
Output:
$ cc pgm6.c
$ a.out
5

8. What will be the output of the following C code?

1. #include <stdio.h>
2. #include <string.h>
3. int main()
4. {
5. char *str = "x";
6. char c = 'x';
7. char ary[1];
8. ary[0] = c;
9. printf("%d %d", strlen(str), strlen(ary));
10. return 0;
11. }
a) 1 1
b) 2 1
c) 2 2
d) 1 (undefined value)
View Answer
Answer: d

C Programming Questions and Answers –


Constants – 2
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Constants – 2”.

Pre-requisite for C Constants MCQ set: Video Tutorial on C Constants.


1. enum types are processed by _________
a) Compiler
b) Preprocessor
c) Linker
d) Assembler
View Answer
Answer: a
Explanation: None.

2. What will be the output of the following C code?

advertisement

ADVERTISEMENT
ADVERTISEMENT
1. #include <stdio.h>
2. int main()
3. {
4. printf("sanfoundry\rclass\n");
5. return 0;
6. }
a) sanfoundryclass
b)

sanfoundry

class

Subscribe Now: C Newsletter | Important Subjects Newsletters

c) classundry
d) sanfoundry
View Answer
Answer: c
Explanation: r is carriage return and moves the cursor back. sanfo is replaced by class.
Output:
$ cc pgm8.c
$ a.out
classundry

3. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. printf("sanfoundry\r\nclass\n");
5. return 0;
6. }
a) sanfoundryclass
b)

sanfoundry

class

c) classundry
d) sanfoundry
View Answer
Answer: b
Explanation: rn combination makes the cursor move to the next line.
Output:
$ cc pgm9.c
$ a.out
sanfoundry
class

4. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. const int p;
5. p = 4;
6. printf("p is %d", p);
7. return 0;
8. }
a) p is 4
b) Compile time error
c) Run time error
d) p is followed by a garbage value
View Answer
Answer: b
Explanation: Since the constant variable has to be declared and defined at the same time,
not doing it results in an error.
Output:
$ cc pgm10.c
pgm10.c: In function ‘main’:
pgm10.c:5: error: assignment of read-only variable ‘p’

5. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int k = 4;
5. int *const p = &k;
6. int r = 3;
7. p = &r;
8. printf("%d", p);
9. }
a) Address of k
b) Address of r
c) Compile time error
d) Address of k + address of r
View Answer
Answer: c
Explanation: Since the pointer p is declared to be constant, trying to assign it with a new
value results in an error.
Output:
$ cc pgm11.c
pgm11.c: In function ‘main’:
pgm11.c:7: error: assignment of read-only variable ‘p’
pgm11.c:8: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int * const’

6. Which of the following statement is false?


a) Constant variables need not be defined as they are declared and can be defined later
b) Global constant variables are initialized to zero
c) const keyword is used to define constant values
d) You cannot reassign a value to a constant variable
View Answer
Answer: a
Explanation: Since the constant variable has to be declared and defined at the same time,
not doing it results in an error.

7. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int const k = 5;
5. k++;
6. printf("k is %d", k);
7. }
a) k is 6
b) Error due to const succeeding int
c) Error, because a constant variable can be changed only twice
d) Error, because a constant variable cannot be changed
View Answer
Answer: d
Explanation: Constant variable has to be declared and defined at the same time. Trying to
change it results in an error.
Output:
$ cc pgm12.c
pgm12.c: In function ‘main’:
pgm12.c:5: error: increment of read-only variable ‘k’

8. What will be the output of the following C code?

1. #include <stdio.h>
2. int const print()
3. {
4. printf("Sanfoundry.com");
5. return 0;
6. }
7. void main()
8. {
9. print();
10. }
a) Error because function name cannot be preceded by const
b) Sanfoundry.com
c) Sanfoundry.com is printed infinite times
d) Blank screen, no output
View Answer
Answer: b

C Programming Questions and Answers –


Declarations – 1
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Declarations – 1”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. What will be the output of the following C code?

1. #include <stdio.h>
2. void foo(const int *);
3. int main()
4. {
5. const int i = 10;
6. printf("%d ", i);
7. foo(&i);
8. printf("%d", i);
9.
10. }
11. void foo(const int *i)
12. {
13. *i = 20;
14. }
a) Compile time error
b) 10 20
c) Undefined value
d) 10
View Answer
Answer: a
Explanation: Cannot change a const type value.
Output:
$ cc pgm1.c
pgm1.c: In function ‘foo’:
pgm1.c:13: error: assignment of read-only location ‘*i’

advertisement

2. What will be the output of the following C code?

Subscribe Now: C Newsletter | Important Subjects Newsletters


1. #include <stdio.h>
2. int main()
3. {
4. const int i = 10;
5. int *ptr = &i;
6. *ptr = 20;
7. printf("%d\n", i);
8. return 0;
9. }
a) Compile time error
b) Compile time warning and printf displays 20
c) Undefined behaviour
d) 10
View Answer
Answer: b
Explanation: Changing const variable through non-constant pointers invokes compiler
warning.
Output:
$ cc pgm2.c
pgm2.c: In function ‘main’:
pgm2.c:5: warning: initialization discards qualifiers from pointer target type
$ a.out
20

3. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. j = 10;
5. printf("%d\n", j++);
6. return 0;
7. }
a) 10
b) 11
c) Compile time error
d) 0
View Answer
Answer: c
Explanation: Variable j is not defined.
Output:
$ cc pgm3.c
pgm3.c: In function ‘main’:
pgm3.c:4: error: ‘j’ undeclared (first use in this function)
pgm3.c:4: error: (Each undeclared identifier is reported only once
pgm3.c:4: error: for each function it appears in.)

4. Will the following C code compile without any error?


1. #include <stdio.h>
2. int main()
3. {
4. for (int k = 0; k < 10; k++);
5. return 0;
6. }
a) Yes
b) No
c) Depends on the C standard implemented by compilers
d) Error
View Answer
Answer: c
Explanation: Compilers implementing C90 do not allow this, but compilers implementing
C99 allow it.
Output:
$ cc pgm4.c
pgm4.c: In function ‘main’:
pgm4.c:4: error: ‘for’ loop initial declarations are only allowed in C99 mode
pgm4.c:4: note: use option -std=c99 or -std=gnu99 to compile your code

5. Will the following C code compile without any error?

1. #include <stdio.h>
2. int main()
3. {
4. int k;
5. {
6. int k;
7. for (k = 0; k < 10; k++);
8. }
9. }
a) Yes
b) No
c) Depends on the compiler
d) Depends on the C standard implemented by compilers
View Answer
Answer: a
Explanation: There can be blocks inside the block. But within a block, variables have only
block scope.
Output:
$ cc pgm5.c

6. Which of the following declaration is not supported by C?


a) String str;
b) char *str;
c) float str = 3e2;
d) Both “String str;” and “float str = 3e2;”
View Answer
Answer: a
Explanation: It is legal in Java, but not in C.

7. Which of the following format identifier can never be used for the variable var?

1. #include <stdio.h>
2. int main()
3. {
4. char *var = "Advanced Training in C by Sanfoundry.com";
5. }
a) %f
b) %d
c) %c
d) %s
View Answer
Answer: a

C Programming Questions and Answers –


Declarations – 2
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Declarations – 2”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. Which of the following declaration is illegal?
a) char *str = “Best C programming classes by Sanfoundry”;
b) char str[] = “Best C programming classes by Sanfoundry”;
c) char str[20] = “Best C programming classes by Sanfoundry”;
d) char[] str = “Best C programming classes by Sanfoundry”;
View Answer
Answer: d
Explanation: char[] str is a declaration in Java, but not in C.

2. Which keyword is used to prevent any changes in the variable within a C program?
a) immutable
b) mutable
c) const
d) volatile
View Answer
Answer: c
Explanation: const is a keyword constant in C program.

advertisement

3. Which of the following is not a pointer declaration?


a) char a[10];
b) char a[] = {‘1’, ‘2’, ‘3’, ‘4’};
c) char *str;
d) char a;
View Answer
Answer: d
Explanation: Array declarations are pointer declarations.

4. What will be the output of the following C code?

Note: Join free Sanfoundry classes at Telegram or Youtube


1. #include <stdio.h>
2. void main()
3. {
4. int k = 4;
5. float k = 4;
6. printf("%d", k)
7. }
a) Compile time error
b) 4
c) 4.0000000
d) 4.4
View Answer
Answer: a
Explanation: Since the variable k is defined both as integer and as float, it results in an
error.
Output:
$ cc pgm8.c
pgm8.c: In function ‘main’:
pgm8.c:5: error: conflicting types for ‘k’
pgm8.c:4: note: previous definition of ‘k’ was here
pgm8.c:6: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
pgm8.c:7: error: expected ‘;’ before ‘}’ token

5. Which of the following statement is false?


a) A variable defined once can be defined again with different scope
b) A single variable cannot be defined with two different types in the same scope
c) A variable must be declared and defined at the same time
d) A variable refers to a location in memory
View Answer
Answer: c
Explanation: It is not an error if the variable is declared and not defined. For example –
extern declarations.

6. A variable declared in a function can be used in main().


a) True
b) False
c) True if it is declared static
d) None of the mentioned
View Answer
Answer: b
Explanation: Since the scope of the variable declared within a function is restricted only
within that function, so the above statement is false.

7. The name of the variable used in one function cannot be used in another function.
a) True
b) False
View Answer
Answer: b

C Programming Questions and Answers –


Arithmetic Operators – 1
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Arithmetic
Operators – 1”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = -3;
5. int k = i % 2;
6. printf("%d\n", k);
7. }
a) Compile time error
b) -1
c) 1
d) Implementation defined
View Answer
Answer: b
Explanation: None.

advertisement

2. What will be the output of the following C code?

Note: Join free Sanfoundry classes at Telegram or Youtube


1. #include <stdio.h>
2. int main()
3. {
4. int i = 3;
5. int l = i / -2;
6. int k = i % -2;
7. printf("%d %d\n", l, k);
8. return 0;
9. }
a) Compile time error
b) -1 1
c) 1 -1
d) Implementation defined
View Answer
Answer: b
Explanation: None.

3. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 5;
5. i = i / 3;
6. printf("%d\n", i);
7. return 0;
8. }
a) Implementation defined
b) 1
c) 3
d) Compile time error
View Answer
Answer: b
Explanation: None.

4. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = -5;
5. i = i / 3;
6. printf("%d\n", i);
7. return 0;
8. }
a) Implementation defined
b) -1
c) -3
d) Compile time error
View Answer
Answer: b
Explanation: None.

5. What will be the final value of x in the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int x = 5 * 9 / 3 + 9;
5. }
a) 3.75
b) Depends on compiler
c) 24
d) 3
View Answer
Answer: c
Explanation: None.

6. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int x = 5.3 % 2;
5. printf("Value of x is %d", x);
6. }
a) Value of x is 2.3
b) Value of x is 1
c) Value of x is 0.3
d) Compile time error
View Answer
Answer: d
Explanation: None.

7. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int y = 3;
5. int x = 5 % 2 * 3 / 2;
6. printf("Value of x is %d", x);
7. }
a) Value of x is 1
b) Value of x is 2
c) Value of x is 3
d) Compile time error
View Answer
Answer: a

C Programming Questions and Answers –


Arithmetic Operators – 2
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Arithmetic
Operators – 2”.
Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.
1. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int a = 3;
5. int b = ++a + a++ + --a;
6. printf("Value of b is %d", b);
7. }
a) Value of x is 12
b) Value of x is 13
c) Value of x is 10
d) Undefined behaviour
View Answer
Answer: d
Explanation: None.

advertisement

ADVERTISEMENT

ADVERTISEMENT

2. What is the precedence of arithmetic operators (from highest to lowest)?


a) %, *, /, +, –
b) %, +, /, *, –
c) +, -, %, *, /
d) %, +, -, *, /
View Answer
Answer: a
Explanation: None.

Note: Join free Sanfoundry classes at Telegram or Youtube

3. Which of the following is not an arithmetic operation?


a) a * = 10;
b) a / = 10;
c) a ! = 10;
d) a % = 10;
View Answer
Answer: c
Explanation: None.

4. Which of the following data type will throw an error on modulus operation(%)?
a) char
b) short
c) int
d) float
View Answer
Answer: d
Explanation: None.

5. Which among the following are the fundamental arithmetic operators, i.e, performing the
desired operation can be done using that operator only?
a) +, –
b) +, -, %
c) +, -, *, /
d) +, -, *, /, %
View Answer
Answer: a
Explanation: None.

6. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int a = 10;
5. double b = 5.6;
6. int c;
7. c = a + b;
8. printf("%d", c);
9. }
a) 15
b) 16
c) 15.6
d) 10
View Answer
Answer: a
Explanation: None.

7. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int a = 10, b = 5, c = 5;
5. int d;
6. d = a == (b + c);
7. printf("%d", d);
8. }
a) Syntax error
b) 1
c) 10
d) 5
View Answer
Answer: b
Explanation: None.

C Programming Questions and Answers –


Relational & Logical Operators – 1
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Relational &
Logical Operators – 1”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int x = 1, y = 0, z = 5;
5. int a = x && y || z++;
6. printf("%d", z);
7. }
a) 6
b) 5
c) 0
d) Varies
View Answer
Answer: a
Explanation: None.

advertisement

2. What will be the output of the following C code?

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate


Now!
1. #include <stdio.h>
2. void main()
3. {
4. int x = 1, y = 0, z = 5;
5. int a = x && y && z++;
6. printf("%d", z);
7. }
a) 6
b) 5
c) 0
d) Varies
View Answer
Answer: b
Explanation: None.
3. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int x = 1, y = 0, z = 3;
5. x > y ? printf("%d", z) : return z;
6. }
a) 3
b) 1
c) Compile time error
d) Run time error
View Answer
Answer: c
Explanation: None.

4. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int x = 1, z = 3;
5. int y = x << 3;
6. printf(" %d\n", y);
7. }
a) -2147483648
b) -1
c) Run time error
d) 8
View Answer
Answer: d
Explanation: None.

5. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int x = 0, y = 2, z = 3;
5. int a = x & y | z;
6. printf("%d", a);
7. }
a) 3
b) 0
c) 2
d) Run time error
View Answer
Answer: a
Explanation: None.
6. What will be the final value of j in the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 0, j = 0;
5. if (i && (j = i + 10))
6. //do something
7. ;
8. }
a) 0
b) 10
c) Depends on the compiler
d) Depends on language standard
View Answer
Answer: a
Explanation: None.

7. What will be the final value of j in the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 10, j = 0;
5. if (i || (j = i + 10))
6. //do something
7. ;
8. }
a) 0
b) 20
c) Compile time error
d) Depends on language standard
View Answer
Answer: a
Explanation: None.

8. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 1;
5. if (i++ && (i == 1))
6. printf("Yes\n");
7. else
8. printf("No\n");
9. }
a) Yes
b) No
c) Depends on the compiler
d) Depends on the standard
View Answer
Answer: b

C Programming Questions and Answers –


Relational & Logical Operators – 2
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Relational &
Logical Operators – 2”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. Are logical operator sequence points?
a) True
b) False
c) Depends on the compiler
d) Depends on the standard
View Answer
Answer: a
Explanation: None.

2. Do logical operators in the C language are evaluated with the short circuit?
a) True
b) False
c) Depends on the compiler
d) Depends on the standard
View Answer
Answer: a
Explanation: None.

advertisement

3. What is the result of logical or relational expression in C?


a) True or False
b) 0 or 1
c) 0 if an expression is false and any positive number if an expression is true
d) None of the mentioned
View Answer
Answer: b
Explanation: None.

4. What will be the final value of d in the following C code?

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate


Now!
1. #include <stdio.h>
2. int main()
3. {
4. int a = 10, b = 5, c = 5;
5. int d;
6. d = b + c == a;
7. printf("%d", d);
8. }
a) Syntax error
b) 1
c) 5
d) 10
View Answer
Answer: b
Explanation: None.

5. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int a = 10, b = 5, c = 3;
5. b != !a;
6. c = !!a;
7. printf("%d\t%d", b, c);
8. }
1 a) 5
3 b) 0
3 c) 5
1 d) 1
View Answer
Answer: a
Explanation: None.

6. Which among the following is NOT a logical or relational operator?


a) !=
b) ==
c) ||
d) =
View Answer
Answer: d
Explanation: None.

7. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int a = 10;
5. if (a == a--)
6. printf("TRUE 1\t");
7. a = 10;
8. if (a == --a)
9. printf("TRUE 2\t");
10. }
a) TRUE 1
b) TRUE 2
TRUE 2 c) TRUE 1
d) Compiler Dependent
View Answer
Answer: d
Explanation: This is a sequence point problem and hence the result will be implementation
dependent.

8. Relational operators cannot be used on ____________


a) structure
b) long
c) strings
d) float
View Answer
Answer: a

C Programming Questions and Answers –


Type Conversions – 1
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Type Conversions
– 1”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. float x = 0.1;
5. if (x == 0.1)
6. printf("Sanfoundry");
7. else
8. printf("Advanced C Classes");
9. }
a) Advanced C Classes
b) Sanfoundry
c) Run time error
d) Compile time error
View Answer
Answer: a
Explanation: None.

advertisement

2. What will be the output of the following C code?


Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate
Now!
1. #include <stdio.h>
2. void main()
3. {
4. float x = 0.1;
5. printf("%d, ", x);
6. printf("%f", x);
7. }
a) 0.100000, junk value
b) Junk value, 0.100000
c) 0, 0.100000
d) 0, 0.999999
View Answer
Answer: b
Explanation: None.

3. What will be the output of the following C code? (Initial values: x= 7, y = 8)

1. #include <stdio.h>
2. void main()
3. {
4. float x;
5. int y;
6. printf("enter two numbers \n");
7. scanf("%f %f", &x, &y);
8. printf("%f, %d", x, y);
9. }
a) 7.000000, 7
b) Run time error
c) 7.000000, junk
d) Varies
View Answer
Answer: c
Explanation: None.

4. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. double x = 123828749.66;
5. int y = x;
6. printf("%d\n", y);
7. printf("%lf\n", y);
8. }
a) 0, 0.0
b) 123828749, 123828749.66
c) 12382874, 12382874.0
d) 123828749, 0.000000
View Answer
Answer: d
Explanation: None.

5. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int x = 97;
5. char y = x;
6. printf("%c\n", y);
7. }
a) a
b) b
c) 97
d) Run time error
View Answer
Answer: a
Explanation: None.

6. When double is converted to float, then the value is?


a) Truncated
b) Rounded
c) Depends on the compiler
d) Depends on the standard
View Answer
Answer: c
Explanation: None.

7. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. unsigned int i = 23;
5. signed char c = -23;
6. if (i > c)
7. printf("Yes\n");
8. else if (i < c)
9. printf("No\n");
10. }
a) Yes
b) No
c) Depends on the compiler
d) Depends on the operating system
View Answer
Answer: b
Explanation: None.

8. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 23;
5. char c = -23;
6. if (i < c)
7. printf("Yes\n");
8. else
9. printf("No\n");
10. }
a) Yes
b) No
c) Depends on the compiler
d) Depends on the standard
View Answer
Answer: b

C Programming Questions and Answers –


Type Conversions – 2
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Type Conversions
– 2”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. function tolower(c) defined in library <ctype.h> works for ___________
a) Ascii character set
b) Unicode character set
c) Ascii and utf-8 but not EBCDIC character set
d) Any character set
View Answer
Answer: d
Explanation: None.

2. What will be the output of the following C code considering the size of a short int is 2,
char is 1 and int is 4 bytes?

advertisement

1. #include <stdio.h>
2. int main()
3. {
4. short int i = 20;
5. char c = 97;
6. printf("%d, %d, %d\n", sizeof(i), sizeof(c), sizeof(c + i));
7. return 0;
8. }
a) 2, 1, 2
b) 2, 1, 1
c) 2, 1, 4
d) 2, 2, 8
View Answer
Answer: c
Explanation: None.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate


Now!

3. Which type of conversion is NOT accepted?


a) From char to int
b) From float to char pointer
c) From negative int to char
d) From double to char
View Answer
Answer: b
Explanation: Conversion of a float to pointer type is not allowed.

4. What will be the data type of the result of the following operation?

(float)a * (int)b / (long)c * (double)d


a) int
b) long
c) float
d) double
View Answer
Answer: d
Explanation: None.

5. Which of the following type-casting have chances for wrap around?


a) From int to float
b) From int to char
c) From char to short
d) From char to int
View Answer
Answer: b
Explanation: None.

6. Which of the following typecasting is accepted by C?


a) Widening conversions
b) Narrowing conversions
c) Widening & Narrowing conversions
d) None of the mentioned
View Answer
Answer: c
Explanation: None.

7. When do you need to use type-conversions?


a) The value to be stored is beyond the max limit
b) The value to be stored is in a form not supported by that data type
c) To reduce the memory in use, relevant to the value
d) All of the mentioned
View Answer
Answer: d

C Programming Questions and Answers –


Increment and Decrement Operators – 1
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Increment and
Decrement Operators – 1”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. What is the difference between the following 2 codes?

1. #include <stdio.h> //Program 1


2. int main()
3. {
4. int d, a = 1, b = 2;
5. d = a++ + ++b;
6. printf("%d %d %d", d, a, b);
7. }
1. #include <stdio.h> //Program 2
2. int main()
3. {
4. int d, a = 1, b = 2;
5. d = a++ +++b;
6. printf("%d %d %d", d, a, b);
7. }
a) No difference as space doesn’t make any difference, values of a, b, d are same in both
the case
b) Space does make a difference, values of a, b, d are different
c) Program 1 has syntax error, program 2 is not
d) Program 2 has syntax error, program 1 is not
View Answer
Answer: d
Explanation: None.

advertisement

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate


Now!

2. What will be the output of the following C code?


1. #include <stdio.h>
2. int main()
3. {
4. int a = 1, b = 1, c;
5. c = a++ + b;
6. printf("%d, %d", a, b);
7. }
a) a = 1, b = 1
b) a = 2, b = 1
c) a = 1, b = 2
d) a = 2, b = 2
View Answer
Answer: b
Explanation: None.

3. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int a = 1, b = 1, d = 1;
5. printf("%d, %d, %d", ++a + ++a+a++, a++ + ++b, ++d + d++ + a+
+);
6. }
a) 15, 4, 5
b) 9, 6, 9
c) 9, 3, 5
d) Undefined (Compiler Dependent)
View Answer
Answer: d
Explanation: None.

4. For which of the following, “PI++;” code will fail?


a) #define PI 3.14
b) char *PI = “A”;
c) float PI = 3.14;
d) none of the Mentioned
View Answer
Answer: a
Explanation: None.

5. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int a = 10, b = 10;
5. if (a = 5)
6. b--;
7. printf("%d, %d", a, b--);
8. }
a) a = 10, b = 9
b) a = 10, b = 8
c) a = 5, b = 9
d) a = 5, b = 8
View Answer
Answer: c
Explanation: None.

6. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 0;
5. int j = i++ + i;
6. printf("%d\n", j);
7. }
a) 0
b) 1
c) 2
d) Compile time error
View Answer
Answer: b
Explanation: None.

7. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 2;
5. int j = ++i + i;
6. printf("%d\n", j);
7. }
a) 6
b) 5
c) 4
d) Compile time error
View Answer
Answer: a
Explanation: None.

8. Comment on the behaviour of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 2;
5. i = i++ + i;
6. printf("%d\n", i);
7. }
a) = operator is not a sequence point
b) ++ operator may return value with or without side effects
c) it can be evaluated as (i++)+i or i+(++i)
d) = operator is a sequence point
View Answer
Answer: a

C Programming Questions and Answers –


Increment and Decrement Operators – 2
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Increment and
Decrement Operators – 2”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 0;
5. int x = i++, y = ++i;
6. printf("%d % d\n", x, y);
7. return 0;
8. }
a) 0, 2
b) 0, 1
c) 1, 2
d) Undefined
View Answer
Answer: a
Explanation: None.

advertisement

2. What will be the output of the following C code?

Subscribe Now: C Newsletter | Important Subjects Newsletters


1. #include <stdio.h>
2. int main()
3. {
4. int i = 10;
5. int *p = &i;
6. printf("%d\n", *p++);
7. }
a) 10
b) 11
c) Garbage value
d) Address of i
View Answer
Answer: a
Explanation: None.

3. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int x = 97;
5. int y = sizeof(x++);
6. printf("X is %d", x);
7. }
a) X is 97
b) X is 98
c) X is 99
d) Run time error
View Answer
Answer: a
Explanation: None.

4. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int x = 4, y, z;
5. y = --x;
6. z = x--;
7. printf("%d%d%d", x, y, z);
8. }
a) 3 2 3
b) 2 3 3
c) 3 2 2
d) 2 3 4
View Answer
Answer: b
Explanation: None.

5. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int x = 4;
5. int *p = &x;
6. int *k = p++;
7. int r = p - k;
8. printf("%d", r);
9. }
a) 4
b) 8
c) 1
d) Run time error
View Answer
Answer: c
Explanation: None.

6. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int a = 5, b = -7, c = 0, d;
5. d = ++a && ++b || ++c;
6. printf("\n%d%d%d%d", a, b, c, d);
7. }
a) 6 -6 0 0
b) 6 -5 0 1
c) -6 -6 0 1
d) 6 -6 0 1
View Answer
Answer: d
Explanation: None.

7. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int a = -5;
5. int k = (a++, ++a);
6. printf("%d\n", k);
7. }
a) -4
b) -5
c) 4
d) -3
View Answer
Answer: d

C Programming MCQ – Precedence and Order


of Evaluation
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Precedence and
Order of Evaluation”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. Which of the following operators has an associativity from Right to Left?
a) <=
b) <<
c) ==
d) +=
View Answer
Answer: d
Explanation: None.

2. Which operators of the following have same precedence?

advertisement

P. "!=", Q. "+=", R. "<<="


a) P and Q
b) Q and R
c) P and R
d) P, Q and R
View Answer
Answer: b
Explanation: None.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate


Now!

3. Comment on the following statement.

n = 1;
printf("%d, %dn", 3*n, n++);
a) Output will be 3, 2
b) Output will be 3, 1
c) Output will be 6, 1
d) Output is compiler dependent
View Answer
4. Which of the following option is the correct representation of the following C statement?

e = a * b + c / d * f;
a) e = (a * (b +(c /(d * f))));
b) e = ((a * b) + (c / (d * f)));
c) e = ((a * b) + ((c / d)* f));
d) e = (a * (b +((c / d) * f)));
View Answer
5. While swapping 2 numbers what precautions to be taken care?

b = (b / a);
a = a * b;
b = a / b;
a) Data type should be either of short, int and long
b) Data type should be either of float and double
c) All data types are accepted except for (char *)
d) This code doesn’t swap 2 numbers
View Answer
6. What will be the output of the following C code?

1. #include<stdio.h>
2. int main()
3. {
4. int a = 1, b = 2, c = 3, d = 4, e;
5. e = c + d = b * a;
6. printf("%d, %d\n", e, d);
7. }
a) 7, 4
b) 7, 2
c) 5, 2
d) Syntax error
View Answer
7. Which of the following is the correct order of evaluation for the given expression?

a = w % x / y * z;
a) % / * =
b) / * % =
c) = % * /
d) * % / =
View Answer
8. Which function in the following expression will be called first?

a = func3(6) - func2(4, 5) / func1(1, 2, 3);


a) func1();
b) func2();
c) func3();
d) Cannot be predicted
View Answer
9. Which of the following operator has the highest precedence in the following?
a) ()
b) sizeof
c) *
d) +
View Answer
Answer: a
Explanation: None.

10. Which of the following is a ternary operator?


a) &&
b) >>=
c) ?:
d) ->
View Answer
Answer: c

C Programming Questions and Answers –


Precedence and Order of Evaluation – 6
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Precedence and
Order of Evaluation – 6”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. Which of the following are unary operators?
a) sizeof
b) –
c) ++
d) all of the mentioned
View Answer
Answer: d
Explanation: None.

2. Where in C the order of precedence of operators do not exist?


a) Within conditional statements, if, else
b) Within while, do-while
c) Within a macro definition
d) None of the mentioned
View Answer
Answer: d
Explanation: None.

advertisement

3. Associativity of an operator is ___________


a) Right to Left
b) Left to Right
c) Random fashion
d) Both Right to Left and Left to Right
View Answer
Answer: d
Explanation: None.

4. Which of the following method is accepted for assignment?


a) 5 = a = b = c = d;
b) a = b = c = d = 5;
c) a = b = 5 = c = d;
d) None of the mentioned
View Answer
Answer: b
Explanation: None.

Subscribe Now: C Newsletter | Important Subjects Newsletters

5. Which of the following is NOT possible with any 2 operators in C?


a) Different precedence, same associativity
b) Different precedence, different associativity
c) Same precedence, different associativity
d) All of the mentioned
View Answer
Answer: c
Explanation: None.

6. Which of the following is possible with any 2 operators in C?


a) Same associativity, different precedence
b) Same associativity, same precedence
c) Different associativity, different precedence
d) All of the mentioned
View Answer
Answer: d
Explanation: None.

7. Which of the following operators has the lowest precedence?


a) !=
b) &&
c) ?:
d) ,
View Answer
Answer: d
Explanation: None.

8. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int x = 3, i = 0;
5. do {
6. x = x++;
7. i++;
8. } while (i != 3);
9. printf("%d\n", x);
10. }
a) Undefined behaviour
b) Output will be 3
c) Output will be 6
d) Output will be 5
View Answer
Answer: b
Explanation: None.

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

1. #include <stdio.h>
2. int main()
3. {
4. int a = -1, b = 4, c = 1, d;
5. d = ++a && ++b || ++c;
6. printf("%d, %d, %d, %d\n", a, b, c, d);
7. return 0;
8. }
a) 0, 4, 2, 1
b) 0, 5, 2, 1
c) -1, 4, 1, 1
d) 0, 5, 1, 0
View Answer
Answer: a
Explanation: None.

10. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int p = 10, q = 20, r;
5. if (r = p = 5 || q > 20)
6. printf("%d", r);
7. else
8. printf("No Output\n");
9. }
a) 1
b) 10
c) 20
d) No Output
View Answer
Answer: a

C Programming Questions and Answers – If-


then-else Statements – 1
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “If-then-else
Statements – 1”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. What will be the output of the following C code?
1. #include <stdio.h>
2. void main()
3. {
4. int x = 5;
5. if (x < 1)
6. printf("hello");
7. if (x == 5)
8. printf("hi");
9. else
10. printf("no");
11. }
a) hi
b) hello
c) no
d) error
View Answer
Answer: a
Explanation: None.

advertisement

2. What will be the output of the following C code?

Subscribe Now: C Newsletter | Important Subjects Newsletters


1. #include <stdio.h>
2. int x;
3. void main()
4. {
5. if (x)
6. printf("hi");
7. else
8. printf("how are u");
9. }
a) hi
b) how are you
c) compile time error
d) error
View Answer
Answer: b
Explanation: None.

3. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int x = 5;
5. if (true);
6. printf("hello");
7. }
a) It will display hello
b) It will throw an error
c) Nothing will be displayed
d) Compiler dependent
View Answer
Answer: b
Explanation: None.

4. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int x = 0;
5. if (x == 0)
6. printf("hi");
7. else
8. printf("how are u");
9. printf("hello");
10. }
a) hi
b) how are you
c) hello
d) hihello
View Answer
Answer: d
Explanation: None.

5. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int x = 5;
5. if (x < 1);
6. printf("Hello");
7.
8. }
a) Nothing
b) Run time error
c) Hello
d) Varies
View Answer
Answer: c
Explanation: None.

6. What will be the output of the following C code? (Assuming that we have entered the
value 1 in the standard input)

1. #include <stdio.h>
2. void main()
3. {
4. double ch;
5. printf("enter a value between 1 to 2:");
6. scanf("%lf", &ch);
7. switch (ch)
8. {
9. case 1:
10. printf("1");
11. break;
12. case 2:
13. printf("2");
14. break;
15. }
16. }
a) Compile time error
b) 1
c) 2
d) Varies
View Answer
Answer: a
Explanation: None.

7. What will be the output of the following C code? (Assuming that we have entered the
value 1 in the standard input)

1. #include <stdio.h>
2. void main()
3. {
4. char *ch;
5. printf("enter a value between 1 to 3:");
6. scanf("%s", ch);
7. switch (ch)
8. {
9. case "1":
10. printf("1");
11. break;
12. case "2":
13. printf("2");
14. break;
15. }
16. }
a) 1
b) 2
c) Compile time error
d) No Compile time error
View Answer
Answer: c
Explanation: None.

8. What will be the output of the following C code? (Assuming that we have entered the
value 1 in the standard input)
1. #include <stdio.h>
2. void main()
3. {
4. int ch;
5. printf("enter a value between 1 to 2:");
6. scanf("%d", &ch);
7. switch (ch)
8. {
9. case 1:
10. printf("1\n");
11. default:
12. printf("2\n");
13. }
14. }
a) 1
b) 2
c) 1 2
d) Run time error
View Answer
Answer: c
Explanation: None.

9. What will be the output of the following C code? (Assuming that we have entered the
value 2 in the standard input)

1. #include <stdio.h>
2. void main()
3. {
4. int ch;
5. printf("enter a value between 1 to 2:");
6. scanf("%d", &ch);
7. switch (ch)
8. {
9. case 1:
10. printf("1\n");
11. break;
12. printf("Hi");
13. default:
14. printf("2\n");
15. }
16. }
a) 1
b) Hi 2
c) Run time error
d) 2
View Answer
Answer: d
Explanation: None.

10. What will be the output of the following C code? (Assuming that we have entered the
value 1 in the standard input)
1. #include <stdio.h>
2. void main()
3. {
4. int ch;
5. printf("enter a value between 1 to 2:");
6. scanf("%d", &ch);
7. switch (ch, ch + 1)
8. {
9. case 1:
10. printf("1\n");
11. break;
12. case 2:
13. printf("2");
14. break;
15. }
16. }
a) 1
b) 2
c) 3
d) Run time error
View Answer
Answer: b

C Programming Questions and Answers – If-


then-else Statements – 2
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “If-then-else
Statements – 2”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int x = 1;
5. if (x > 0)
6. printf("inside if\n");
7. else if (x > 0)
8. printf("inside elseif\n");
9. }
a) inside if
b) inside elseif
c)

inside if

inside elseif

advertisement
d) compile time error
View Answer
Answer: a
Explanation: None.

Note: Join free Sanfoundry classes at Telegram or Youtube

2. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int x = 0;
5. if (x++)
6. printf("true\n");
7. else if (x == 1)
8. printf("false\n");
9. }
a) true
b) false
c) compile time error
d) undefined behaviour
View Answer
Answer: b
Explanation: None.

3. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int x = 0;
5. if (x == 1)
6. if (x == 0)
7. printf("inside if\n");
8. else
9. printf("inside else if\n");
10. else
11. printf("inside else\n");
12. }
a) inside if
b) inside else if
c) inside else
d) compile time error
View Answer
Answer: c
Explanation: None.

4. What will be the output of the following C code?


1. #include <stdio.h>
2. int main()
3. {
4. int x = 0;
5. if (x == 0)
6. printf("true, ");
7. else if (x = 10)
8. printf("false, ");
9. printf("%d\n", x);
10. }
a) false, 0
b) true, 0
c) true, 10
d) compile time error
View Answer
Answer: b
Explanation: None.

5. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int x = 0;
5. if (x == 1)
6. if (x >= 0)
7. printf("true\n");
8. else
9. printf("false\n");
10. }
a) true
b) false
c) Depends on the compiler
d) No print statement
View Answer
Answer: d
Explanation: None.

6. The C statement “”if (a == 1 || b == 2) {}”” can be re-written as ___________


a)

if (a == 1)
if (b == 2){}
b)

if (a == 1){}
if (b == 2){}
c)

if (a == 1){}
else if (b == 2){}
d) none of the mentioned
View Answer
Answer: d
Explanation: None.

7. Which of the following is an invalid if-else statement?


a) if (if (a == 1)){}
b) if (func1 (a)){}
c) if (a){}
d) if ((char) a){}
View Answer
Answer: a
Explanation: None.

8. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int a = 1;
5. if (a--)
6. printf("True");
7. if (a++)
8. printf("False");
9. }
a) True
b) False
c) True False
d) No Output
View Answer
Answer: a
Explanation: None.

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

1. #include <stdio.h>
2. int main()
3. {
4. int a = 1;
5. if (a)
6. printf("All is Well ");
7. printf("I am Well\n");
8. else
9. printf("I am not a River\n");
10. }
a) Output will be All is Well I am Well
b) Output will be I am Well I am not a River
c) Output will be I am Well
d) Compile time errors during compilation
View Answer
Answer: d
Explanation: None.

10. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. if (printf("%d", printf(")))
5. printf("We are Happy");
6. else if (printf("1"))
7. printf("We are Sad");
8. }
a) 0We are Happy
b) 1We are Happy
c) 1We are Sad
d) compile time error
View Answer
Answer: d

C Programming MCQ – Switch Statement


This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Switch Statement”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. What will be the output of the following C code? (Assuming that we have entered the
value 1 in the standard input)

1. #include <stdio.h>
2. void main()
3. {
4. double ch;
5. printf("enter a value between 1 to 2:");
6. scanf("%lf", &ch);
7. switch (ch)
8. {
9. case 1:
10. printf("1");
11. break;
12. case 2:
13. printf("2");
14. break;
15. }
16. }
a) Compile time error
b) 1
c) 2
d) Varies
View Answer
Answer: a
Explanation: None.

advertisement

2. What will be the output of the following C code? (Assuming that we have entered the
value 1 in the standard input)

Note: Join free Sanfoundry classes at Telegram or Youtube


1. #include <stdio.h>
2. void main()
3. {
4. char *ch;
5. printf("enter a value between 1 to 3:");
6. scanf("%s", ch);
7. switch (ch)
8. {
9. case "1":
10. printf("1");
11. break;
12. case "2":
13. printf("2");
14. break;
15. }
16. }
a) 1
b) Compile time error
c) 2
d) Run time error
View Answer
Answer: b
Explanation: None.

3. What will be the output of the following C code? (Assuming that we have entered the
value 1 in the standard input)

1. #include <stdio.h>
2. void main()
3. {
4. int ch;
5. printf("enter a value between 1 to 2:");
6. scanf("%d", &ch);
7. switch (ch)
8. {
9. case 1:
10. printf("1\n");
11. default:
12. printf("2\n");
13. }
14. }
a) 1
b) 2
c) 1 2
d) Run time error
View Answer
Answer: c
Explanation: None.

4. What will be the output of the following C code? (Assuming that we have entered the
value 2 in the standard input)

1. #include <stdio.h>
2. void main()
3. {
4. int ch;
5. printf("enter a value between 1 to 2:");
6. scanf("%d", &ch);
7. switch (ch)
8. {
9. case 1:
10. printf("1\n");
11. break;
12. printf("hi");
13. default:
14. printf("2\n");
15. }
16. }
a) 1
b) hi 2
c) Run time error
d) 2
View Answer
Answer: d
Explanation: None.

5. What will be the output of the following C code? (Assuming that we have entered the
value 1 in the standard input)

1. #include <stdio.h>
2. void main()
3. {
4. int ch;
5. printf("enter a value between 1 to 2:");
6. scanf("%d", &ch);
7. switch (ch, ch + 1)
8. {
9. case 1:
10. printf("1\n");
11. break;
12. case 2:
13. printf("2");
14. break;
15. }
16. }
a) 1
b) 2
c) 3
d) Run time error
View Answer
Answer: b
Explanation: None.

6. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int a = 1, b = 1;
5. switch (a)
6. {
7. case a*b:
8. printf("yes ");
9. case a-b:
10. printf("no\n");
11. break;
12. }
13. }
a) yes
b) no
c) Compile time error
d) yes no
View Answer
Answer: c
Explanation: None.

7. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int x = 97;
5. switch (x)
6. {
7. case 'a':
8. printf("yes ");
9. break;
10. case 97:
11. printf("no\n");
12. break;
13. }
14. }
a) yes
b) yes no
c) Duplicate case value error
d) Character case value error
View Answer
Answer: c
Explanation: None.

8. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. float f = 1;
5. switch (f)
6. {
7. case 1.0:
8. printf("yes\n");
9. break;
10. default:
11. printf("default\n");
12. }
13. }
a) yes
b) yes default
c) Undefined behaviour
d) Compile time error
View Answer
Answer: d

C Programming Questions and Answers –


Switch Statements – 2
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Switch Statements
– 2”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. What will be the output of the following C code?

1. #include <stdio.h>
2. const int a = 1, b = 2;
3. int main()
4. {
5. int x = 1;
6. switch (x)
7. {
8. case a:
9. printf("yes ");
10. case b:
11. printf("no\n");
12. break;
13. }
14. }
a) yes no
b) yes
c) no
d) Compile time error
View Answer
Answer: d
Explanation: We are violating a C programming rule which states that in switch-case
statements, the labels must be integer constants. When you compile the code, the c
compiler will give an error message indicating that the case label is not an integer constant
because the labels given in the code are integer variables ‘a’ and ‘b’. You will get the
following error message:

advertisement

error: case label does not reduce to an integer constant


case a:
error: case label does not reduce to an integer constant
case b:
2. What will be the output of the following C code?

Note: Join free Sanfoundry classes at Telegram or Youtube


1. #include <stdio.h>
2. #define max(a) a
3. int main()
4. {
5. int x = 1;
6. switch (x)
7. {
8. case max(2):
9. printf("yes\n");
10. case max(1):
11. printf("no\n");
12. break;
13. }
14. }
a) yes no
b) yes
c) no
d) Compile time error
View Answer
Answer: c
Explanation: None.

3. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. switch (printf("Do"))
5. {
6. case 1:
7. printf("First\n");
8. break;
9. case 2:
10. printf("Second\n");
11. break;
12. default:
13. printf("Default\n");
14. break;
15. }
16. }
a) Do
b) DoFirst
c) DoSecond
d) DoDefault
View Answer
Answer: c
Explanation: None.

4. Comment on the output of the following C code.

1. #include <stdio.h>
2. int main()
3. {
4. int a = 1;
5. switch (a)
6. case 1:
7. printf("%d", a);
8. case 2:
9. printf("%d", a);
10. case 3:
11. printf("%d", a);
12. default:
13. printf("%d", a);
14. }
a) No error, output is 1111
b) No error, output is 1
c) Compile time error, no break statements
d) Compile time error, case label outside switch statement
View Answer
Answer: d
Explanation: None.

5. Which datatype can accept the switch statement?


a) int
b) char
c) long
d) all of the mentioned
View Answer
Answer: d
Explanation: None.

6. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int a = 1;
5. switch (a)
6. {
7. case a:
8. printf("Case A ");
9. default:
10. printf("Default");
11. }
12. }
a) Output: Case A
b) Output: Default
c) Output: Case A Default
d) Compile time error
View Answer
Answer: d
Explanation: None.

7. What will be the output of the following C code?

1. #include <stdio.h>
2. switch (ch)
3. {
4. case 'a':
5. case 'A':
6. printf("true");
7. }
a) if (ch == ‘a’ && ch == ‘A’) printf(“true”);
b)

if (ch == 'a')

if (ch == 'a') printf("true");

c) if (ch == ‘a’ || ch == ‘A’) printf(“true”);


d) none of the mentioned
View Answer
Answer: c

C Programming Questions and Answers – For


Loops – 1
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “For Loops – 1”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. The C code ‘for(;;)’ represents an infinite loop. It can be terminated by ___________
a) break
b) exit(0)
c) abort()
d) terminate
View Answer
Answer: a
Explanation: None.

2. What will be the correct syntax for running two variable for loop simultaneously?
a)

advertisement

for (i = 0; i < n; i++)


for (j = 0; j < n; j += 5)
b)

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate


Now!
for (i = 0, j = 0; i < n, j < n; i++, j += 5)
c)

for (i = 0; i < n;i++){}


for (j = 0; j < n;j += 5){}
d) none of the mentioned
View Answer
Answer: b
Explanation: None.

3. Which for loop has range of similar indexes of ‘i’ used in for (i = 0;i < n; i++)?
a) for (i = n; i>0; i–)
b) for (i = n; i >= 0; i–)
c) for (i = n-1; i>0; i–)
d) for (i = n-1; i>-1; i–)
View Answer
Answer: d
Explanation: None.

4. Which of the following cannot be used as LHS of the expression in for (exp1;exp2;
exp3)?
a) variable
b) function
c) typedef
d) macros
View Answer
Answer: d
Explanation: None.

5. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. short i;
5. for (i = 1; i >= 0; i++)
6. printf("%d\n", i);
7.
8. }
a) The control won’t fall into the for loop
b) Numbers will be displayed until the signed limit of short and throw a runtime error
c) Numbers will be displayed until the signed limit of short and program will successfully
terminate
d) This program will get into an infinite loop and keep printing numbers with no errors
View Answer
Answer: c
Explanation: None.

6. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int k = 0;
5. for (k)
6. printf("Hello");
7. }
a) Compile time error
b) hello
c) Nothing
d) Varies
View Answer
Answer: a
Explanation: None.

7. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int k = 0;
5. for (k < 3; k++)
6. printf("Hello");
7. }
a) Compile time error
b) Hello is printed thrice
c) Nothing
d) Varies
View Answer
Answer: a
Explanation: None.

8. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. double k = 0;
5. for (k = 0.0; k < 3.0; k++)
6. printf("Hello");
7. }
a) Run time error
b) Hello is printed thrice
c) Hello is printed twice
d) Hello is printed infinitely
View Answer
Answer: b

C Programming Questions and Answers – For


Loops – 2
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “For Loops – 2”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. double k = 0;
5. for (k = 0.0; k < 3.0; k++);
6. printf("%lf", k);
7. }
a) 2.000000
b) 4.000000
c) 3.000000
d) Run time error
View Answer
Answer: c
Explanation: None.

2. What will be the output of the following C code?


advertisement

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate


Now!
1. #include <stdio.h>
2. void main()
3. {
4. int k;
5. for (k = -3; k < -5; k++)
6. printf("Hello");
7. }
a) Hello
b) Infinite hello
c) Run time error
d) Nothing
View Answer
Answer: d
Explanation: None.

3. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 0;
5. for (; ; ;)
6. printf("In for loop\n");
7. printf("After loop\n");
8. }
a) Compile time error
b) Infinite loop
c) After loop
d) Undefined behaviour
View Answer
Answer: a
Explanation: None.

4. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 0;
5. for (i++; i == 1; i = 2)
6. printf("In for loop ");
7. printf("After loop\n");
8. }
a) In for loop after loop
b) After loop
c) Compile time error
d) Undefined behaviour
View Answer
Answer: a
Explanation: None.

5. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 0;
5. for (foo(); i == 1; i = 2)
6. printf("In for loop\n");
7. printf("After loop\n");
8. }
9. int foo()
10. {
11. return 1;
12. }
a) After loop
b) In for loop after loop
c) Compile time error
d) Infinite loop
View Answer
Answer: a
Explanation: None.

6. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int *p = NULL;
5. for (foo(); p; p = 0)
6. printf("In for loop\n");
7. printf("After loop\n");
8. }
a) In for loop after loop
b) Compile time error
c) Infinite loop
d) Depends on the value of NULL
View Answer
Answer: b
Explanation: None.

7. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. for (int i = 0;i < 1; i++)
5. printf("In for loop\n");
6. }
a) Compile time error
b) In for loop
c) Depends on the standard compiler implements
d) Run time error
View Answer
Answer: c

C Programming Questions and Answers –


While Loops – 1
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “While Loops – 1”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. while ()
5. printf("In while loop ");
6. printf("After loop\n");
7. }
a) In while loop after loop
b) After loop
c) Compile time error
d) Infinite loop
View Answer
Answer: c
Explanation: None.

advertisement

2. What will be the output of the following C code?

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate


Now!
1. #include <stdio.h>
2. int main()
3. {
4. do
5. printf("In while loop ");
6. while (0);
7. printf("After loop\n");
8. }
a) In while loop
b) In while loop After loop
c) After loop
d) Infinite loop
View Answer
Answer: b
Explanation: None.

3. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 0;
5. do {
6. i++;
7. printf("In while loop\n");
8. } while (i < 3);
9. }
a)

In while loop

In while loop

In while loop

b)

In while loop

In while loop

c) Depends on the compiler


d) Compile time error
View Answer
Answer: a
Explanation: None.

4. How many times i value is checked in the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 0;
5. do {
6. i++;
7. printf("in while loop\n");
8. } while (i < 3);
9. }
a) 2
b) 3
c) 4
d) 1
View Answer
Answer: b
Explanation: None.

5. How many times i value is checked in the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 0;
5. while (i < 3)
6. i++;
7. printf("In while loop\n");
8. }
a) 2
b) 3
c) 4
d) 1
View Answer
Answer: c
Explanation: None.

6. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int i = 2;
5. do
6. {
7. printf("Hi");
8. } while (i < 2)
9. }
a) Compile time error
b) Hi Hi
c) Hi
d) Varies
View Answer
Answer: a
Explanation: None.

7. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int i = 0;
5. while (++i)
6. {
7. printf("H");
8. }
9. }
a) H
b) H is printed infinite times
c) Compile time error
d) Varies
View Answer
Answer: b
Explanation: None.

8. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int i = 0;
5. do
6. {
7. printf("Hello");
8. } while (i != 0);
9. }
a) Nothing
b) H is printed infinite times
c) Hello
d) Run time error
View Answer
Answer: c

C Programming Questions and Answers –


While Loops – 2
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “While Loops – 2”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. char *str = "";
5. do
6. {
7. printf("hello");
8. } while (str);
9. }
a) Nothing
b) Run time error
c) Varies
d) Hello is printed infinite times
View Answer
Answer: d
Explanation: None.

advertisement

2. What will be the output of the following C code?

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate


Now!
1.#include <stdio.h>
2.void main()
3.{
4. int i = 0;
5. while (i < 10)
6. {
7. i++;
8. printf("hi\n");
9. while (i < 8)
10. {
11. i++;
12. printf("hello\n");
13. }
14. }
15. }
a) Hi is printed 8 times, hello 7 times and then hi 2 times
b) Hi is printed 10 times, hello 7 times
c) Hi is printed once, hello 7 times
d) Hi is printed once, hello 7 times and then hi 2 times
View Answer
Answer: d
Explanation: None.

3. What is an example of iteration in C?


a) for
b) while
c) do-while
d) all of the mentioned
View Answer
Answer: d
Explanation: None.

4. How many times while loop condition is tested in the following C code snippets, if i is
initialized to 0 in both the cases?
1.while (i < n)
2. i++;
3. ————-
4. do
5. i++;
6. while (i <= n);
a) n, n
b) n, n+1
c) n+1, n
d) n+1, n+1
View Answer
Answer: d
Explanation: None.

5. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 0;
5. while (i = 0)
6. printf("True\n");
7. printf("False\n");
8. }
a) True (infinite time)
b) True (1 time) False
c) False
d) Compiler dependent
View Answer
Answer: c
Explanation: None.

6. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 0, j = 0;
5. while (i < 5, j < 10)
6. {
7. i++;
8. j++;
9. }
10. printf("%d, %d\n", i, j);
11. }
a) 5, 5
b) 5, 10
c) 10, 10
d) Syntax error
View Answer
Answer: c
Explanation: None.

7. Which loop is most suitable to first perform the operation and then test the condition?
a) for loop
b) while loop
c) do-while loop
d) none of the mentioned
View Answer
Answer: c

C Programming Questions and Answers –


Break and Continue – 1
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Break and
Continue – 1”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. Which keyword can be used for coming out of recursion?
a) break
b) return
c) exit
d) both break and return
View Answer
Answer: b
Explanation: None.

2. What will be the output of the following C code?

advertisement

1. #include <stdio.h>
2. int main()
3. {
4. int a = 0, i = 0, b;
5. for (i = 0;i < 5; i++)
6. {
7. a++;
8. continue;
9. }
10. }
a) 2
b) 3
c) 4
d) 5
View Answer
Answer: d
Explanation: None.
Subscribe Now: C Newsletter | Important Subjects Newsletters

3. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int a = 0, i = 0, b;
5. for (i = 0;i < 5; i++)
6. {
7. a++;
8. if (i == 3)
9. break;
10. }
11. }
a) 1
b) 2
c) 3
d) 4
View Answer
Answer: d
Explanation: None.

4. The keyword ‘break’ cannot be simply used within _________


a) do-while
b) if-else
c) for
d) while
View Answer
Answer: b
Explanation: None.

5. Which keyword is used to come out of a loop only for that iteration?
a) break
b) continue
c) return
d) none of the mentioned
View Answer
Answer: b
Explanation: None.

6. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int i = 0, j = 0;
5. for (i = 0;i < 5; i++)
6. {
7. for (j = 0;j < 4; j++)
8. {
9. if (i > 1)
10. break;
11. }
12. printf("Hi \n");
13. }
14. }
a) Hi is printed 5 times
b) Hi is printed 9 times
c) Hi is printed 7 times
d) Hi is printed 4 times
View Answer
Answer: a
Explanation: None.

7. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int i = 0;
5. int j = 0;
6. for (i = 0;i < 5; i++)
7. {
8. for (j = 0;j < 4; j++)
9. {
10. if (i > 1)
11. continue;
12. printf("Hi \n");
13. }
14. }
15. }
a) Hi is printed 9 times
b) Hi is printed 8 times
c) Hi is printed 7 times
d) Hi is printed 6 times
View Answer
Answer: b
Explanation: None.

8. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int i = 0;
5. for (i = 0;i < 5; i++)
6. if (i < 4)
7. {
8. printf("Hello");
9. break;
10. }
11. }
a) Hello is printed 5 times
b) Hello is printed 4 times
c) Hello
d) Hello is printed 3 times
View Answer
Answer: c

C Programming Questions and Answers –


Break and Continue – 2
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Break and
Continue – 2”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int i = 0;
5. if (i == 0)
6. {
7. printf("Hello");
8. continue;
9. }
10. }
a) Hello is printed infinite times
b) Hello
c) Varies
d) Compile time error
View Answer
Answer: d
Explanation: None.

advertisement

2. What will be the output of the following C code?

Subscribe Now: C Newsletter | Important Subjects Newsletters


1. #include <stdio.h>
2. void main()
3. {
4. int i = 0;
5. if (i == 0)
6. {
7. printf("Hello");
8. break;
9. }
10. }
a) Hello is printed infinite times
b) Hello
c) Varies
d) Compile time error
View Answer
Answer: d
Explanation: None.

3. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 0;
5. do
6. {
7. i++;
8. if (i == 2)
9. continue;
10. printf("In while loop ");
11. } while (i < 2);
12. printf("%d\n", i);
13. }
a) In while loop 2
b) In while loop in while loop 3
c) In while loop 3
d) Infinite loop
View Answer
Answer: a
Explanation: None.

4. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 0, j = 0;
5. for (i; i < 2; i++){
6. for (j = 0; j < 3; j++)
7. {
8. printf("1\n");
9. break;
10. }
11. printf("2\n");
12. }
13. printf("after loop\n");
14. }
a)

1
2
after loop
b)

1
after loop
c)

1
2
1
2
after loop
d)

1
1
2
after loop
View Answer

Answer: c
Explanation: None.

5. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 0;
5. while (i < 2)
6. {
7. if (i == 1)
8. break;
9. i++;
10. if (i == 1)
11. continue;
12. printf("In while loop\n");
13. }
14. printf("After loop\n");
15. }
a)

In while loop

After loop
b) After loop
c)

In while loop

In while loop

After loop

d) In while loop
View Answer
Answer: b
Explanation: None.

6. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 0;
5. char c = 'a';
6. while (i < 2)
7. {
8. i++;
9. switch (c)
10. {
11. case 'a':
12. printf("%c ", c);
13. break;
14. break;
15. }
16. }
17. printf("after loop\n");
18. }
a) a after loop
b) a a after loop
c) after loop
d) error
View Answer
Answer: b
Explanation: None.

7. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. printf("before continue ");
5. continue;
6. printf("after continue\n");
7. }
a) Before continue after continue
b) Before continue
c) After continue
d) Compile time error
View Answer
Answer: d

C Programming Questions and Answers –


Goto & Labels – 1
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Goto & Labels – 1”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. printf("%d ", 1);
5. goto l1;
6. printf("%d ", 2);
7. l1:goto l2;
8. printf("%d ", 3);
9. l2:printf("%d ", 4);
10. }
a) 1 4
b) Compilation error
c) 1 2 4
d) 1 3 4
View Answer
Answer: a
Explanation: None.

advertisement

2. What will be the output of the following C code?

Subscribe Now: C Newsletter | Important Subjects Newsletters


1. #include <stdio.h>
2. int main()
3. {
4. printf("%d ", 1);
5. l1:l2:
6. printf("%d ", 2);
7. printf("%d\n", 3);
8. }
a) Compilation error
b) 1 2 3
c) 1 2
d) 1 3
View Answer
Answer: b
Explanation: None.

3. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. printf("%d ", 1);
5. goto l1;
6. printf("%d ", 2);
7. }
8. void foo()
9. {
10. l1 : printf("3 ", 3);
11. }
a) 1 2 3
b) 1 3
c) 1 3 2
d) Compilation error
View Answer
Answer: d
Explanation: None.

4. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 0, j = 0;
5. while (i < 2)
6. {
7. l1 : i++;
8. while (j < 3)
9. {
10. printf("Loop\n");
11. goto l1;
12. }
13. }
14. }
a) Loop Loop
b) Compilation error
c) Loop Loop Loop Loop
d) Infinite Loop
View Answer
Answer: d
Explanation: None.
5. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 0, j = 0;
5. while (l1: i < 2)
6. {
7. i++;
8. while (j < 3)
9. {
10. printf("loop\n");
11. goto l1;
12. }
13. }
14. }
a) loop loop
b) Compilation error
c) loop loop loop loop
d) Infinite loop
View Answer
Answer: b
Explanation: None.

6. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 0, j = 0;
5. l1: while (i < 2)
6. {
7. i++;
8. while (j < 3)
9. {
10. printf("loop\n");
11. goto l1;
12. }
13. }
14. }
a) loop loop
b) compilation error
c) oop loop loop loop
d) infinite loop
View Answer
Answer: a
Explanation: None.

7. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int i = 0;
5. if (i == 0)
6. {
7. goto label;
8. }
9. label: printf("Hello");
10. }
a) Nothing
b) Error
c) Infinite Hello
d) Hello
View Answer
Answer: d
Explanation: None.

8. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int i = 0, k;
5. if (i == 0)
6. goto label;
7. for (k = 0;k < 3; k++)
8. {
9. printf("hi ");
10. label: k = printf("%03d", i);
11. }
12. }
a) 0
b) hi hi hi 000
c) 0 hi hi hi 000
d) 000
View Answer
Answer: d
Explanation: None.

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

1. #include <stdio.h>
2. void main()
3. {
4. int i = 0, k;
5. label: printf("%d", i);
6. if (i == 0)
7. goto label;
8. }
a) 0
b) Infinite 0
c) Nothing
d) Error
View Answer
Answer: b

C Programming Questions and Answers –


Goto & Labels – 2
This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Goto & Labels – 2”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int i = 5, k;
5. if (i == 0)
6. goto label;
7. label: printf("%d", i);
8. printf("Hey");
9. }
a) 5
b) Hey
c) 5 Hey
d) Nothing
View Answer
Answer: c
Explanation: None.

advertisement

2. goto can be used to jump from main() to within a function.


a) true
b) false
c) depends
d) varies
View Answer
Answer: b
Explanation: None.

Note: Join free Sanfoundry classes at Telegram or Youtube

3. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. printf("%d ", 1);
5. goto l1;
6. printf("%d ", 2);
7. l1:goto l2;
8. printf("%d ", 3);
9. l2:printf("%d ", 4);
10. }
a) 1 4
b) Compile time error
c) 1 2 4
d) 1 3 4
View Answer
Answer: a
Explanation: None.

4. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. printf("%d ", 1);
5. l1:l2:
6. printf("%d ", 2);
7. printf("%d\n", 3);
8. }
a) Compile time error
b) 1 2 3
c) 1 2
d) 1 3
View Answer
Answer: b
Explanation: None.

5. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. printf("%d ", 1);
5. goto l1;
6. printf("%d ", 2);
7. }
8. void foo()
9. {
10. l1: printf("3 ", 3);
11. }
a) 1 2 3
b) 1 3
c) 1 3 2
d) Compile time error
View Answer
Answer: d
Explanation: None.
6. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 0, j = 0;
5. while (i < 2)
6. {
7. l1: i++;
8. while (j < 3)
9. {
10. printf("loop\n");
11. goto l1;
12. }
13. }
14. }
a) loop loop
b) Compile time error
c) loop loop loop loop
d) Infinite loop
View Answer
Answer: d
Explanation: None.

7. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 0, j = 0;
5. while (l1: i < 2)
6. {
7. i++;
8. while (j < 3)
9. {
10. printf("loop\n");
11. goto l1;
12. }
13. }
14. }
a) loop loop
b) Compile time error
c) loop loop loop loop
d) Infinite loop
View Answer
Answer: b
Explanation: None.

8. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 0, j = 0;
5. l1: while (i < 2)
6. {
7. i++;
8. while (j < 3)
9. {
10. printf("loop\n");
11. goto l1;
12. }
13. }
14. }
a) loop loop
b) Compile time error
c) loop loop loop loop
d) Infinite loop
View Answer
Answer: a

C Questions and Answers – Inline


This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Inline”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. Name the function whose definition can be substituted at a place where its function call is
made _________
a) friends function
b) inline function
c) volatile function
d) external function
View Answer
Answer: b
Explanation: The inline function, whose definitions being small can be substituted at a place
where its function call is made. They are inlined with their function calls.

2. What will be the output of the following C code?

advertisement

#include <stdio.h>
void inline func1(int a, int b)
{
printf ("a=%d and b=%d\n", a, b);
}
int inline func2(int x)
{
return x*x;
}
int main()
{
int tmp;
func1(1,4);
tmp = func2(6);
printf("square val=%d\n", tmp);
return 0;
}
a)

a=1 and b=4

square val = 36

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate


Now!

b) a=4 and b=1


c) error
d) square val = 36
View Answer
Answer: a
Explanation: The code shown above is an example of inline function. Both functions 1 and 2
are inline functions. Hence the output will be as shown in option

a=1 and b=4

square val = 36

3. What will be the error (if any) in the following C code?

#include <stdio.h>
void inline func1(float b)
{
printf ("%lf\n",b*2);
}
int main()
{
inline func1(2.2);
return 0;
}
a) No error
b) Error in statement: void inline func1(float b)
c) Error in statement: printf(“%lf\n”,b*2);
d) Error in statement: inline func1(2.2);
View Answer
Answer: d
Explanation: The keyword inline is used only with the function definition. In the code shown
above it has been used with the function call. Hence there is an error in the statement: inline
func1(2.2);

4. What will be the output of the following C code?

#include <stdio.h>
void inline f1(char b)
{
printf ("%d\n",b);
}
int main()
{
f1('a');
return 0;
}
a) a
b) 65
c) error
d) 97
View Answer
Answer: d
Explanation: The definition of the function f1 is replaced with the function call. Since the
format specifier used is %d, the ASCII value of the said character is printed. Hence the
output of the code shown above is 97 (ASCII value of the alphabet ‘a’).

5. What will be the output of the following C code?

#include <stdio.h>
void inline func1(char b[10])
{
printf ("%c\n",b[2]);
}
int main()
{
func1("sanfoundry");
return 0;
}
a) s
b) n
c) a
d) error
View Answer
Answer: b
Explanation: In the code shown above, the definition of function func1 is replaced with its
function call. The function prints the alphabet at the second index of the given string. Hence
the output of this code is ‘n’.
6. The following C code results in an error. State whether this statement is true or false.

#include <stdio.h>
void f(double b)
{
printf ("%ld\n",b);
}
int main()
{
inline f(100.56);
return 0;
}
a) True
b) False
View Answer
Answer: a
Explanation: The above code will result in an error because we have used the keyword
inline in the function call. Had the keyword inline been used in the function definition, the
code would not have thrown an error.

7. What will be the output of the following C code?

#include<stdio.h>
static inline int max(int a, int b)
{
return a > b ? a : b;
}
main()
{
int m;
m=max(-6,-5);
printf("%d",m);
}
a) -6
b) -5
c) Junk value
d) Error
View Answer
Answer: b
Explanation: The code shown a replaces the definition of a function max with it’s function
call. This function is used to print the bigger of the two arguments. Hence -5 is printed.

8. What will be the output of the following C code?

#include<stdio.h>
#define inline
inline f(char a)
{
#ifdef inline
printf("%c",a);
#endif
}
main()
{
f('a');
}
a) Error
b) a
c) No error but nothing will be printed as output
d) 97
View Answer
Answer: b
Explanation: In the code shown above, we have defined identifier names inline using the
macro #define. The output will be printed only if inline is defined. Since it is defined, ‘a’ is
printed as output.

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

#include<stdio.h>
extern inline int min(int a, int b)
{
return a < b ? a : b;
}
main()
{
int m;
m=min(3,-5);
printf("%d",m);
}
a) Error
b) 3
c) -5
d) Junk value
View Answer
Answer: a
Explanation: The above code will result in an error due to the statement: extern inline int
min(int a, int b). The keyword ‘extern’ causes an error: undefined reference to min.

10. To have GCC inline the given function regardless of the level of optimization, we must
declare the function with the attribute _________________
a) optimize_inline
b) packed_inline
c) always_inline
d) level_inline
View Answer
Answer: b
Explanation: To have GCC inline the given function regardless of the level of optimization,
we declare the function with the attribute always_inline.

C Questions and Answers – Endianness


This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Endianness”.

Pre-requisite for this C Endianness MCQ set: Video Tutorial on C Endianness.


1. A machine in which the least significant byte is stored in the smallest address is
__________
a) Big endian machine
b) Bi-endian machine
c) Binary bit machine
d) Little endian machine
View Answer
Answer: d
Explanation: In little endian machines, last byte of binary representation (least significant
byte) of a multi byte data type is stored first, whereas in big endian method, the first byte of
binary representation of a multi byte data type is stored first.

2. If the output of the following C code is “Big endian”, then what will be the value of *a is?

advertisement

#include <stdio.h>
int main()
{
unsigned int i = 1;
char *a = (char*)&i;
if (*a)
printf("Little endian");
else
printf("Big endian");
getchar();
return 0;
}
a) -1
b) 0
c) 1
d) 2
View Answer
Answer: b
Explanation: In the code shown above, a character pointer ‘a’ points to an integer ‘i’. Since
the size of the character is 1 byte, when the character pointer is de-referenced, it contains 1
integer byte. If the machine is a little endian machine then the value of *a will be 1, since the
last byte is stored first. If the machine is big endian, the value of *a will be 0.

Note: Join free Sanfoundry classes at Telegram or Youtube

3. It is possible for a processor to support both little and big endian methods.
a) True
b) False
View Answer
Answer: a
Explanation: It is possible for a processor to support both little and big endian methods.
Such machines are known as bi-endian machines.

4. The standard byte order for networks is ____________


a) Bit-Binary
b) Little endian
c) Big endian
d) Bi-endian
View Answer
Answer: c
Explanation: The standard byte order (network byte order) for networks is big endian.
Before transferring data on a network, data is converted to big endian.

5. Which of the following is not an example of big endian machines?


a) Power PC
b) Motorola 68K
c) SPARC processors
d) ARM processors
View Answer
Answer: d
Explanation: ARM processor is an example of little endian machine. Current generation
ARM processor is an example of bi-endian machine. Power PC, Motorola 68K and SPARC
are examples of big endian mahines.

6. Suppose we transfer a file written on a little endian machine to a big endian machine and
there is no transformation from little endian to big endian, then what happens?
a) The big endian machine throws an error when it receives the file
b) The big endian machine reads the file in the reverse order
c) The big endian machine does not read the file
d) The big endian machine reads the file in the normal order
View Answer
Answer: b
Explanation: If there is no transformation from little endian to big endian on transfer of a file
from a little endian machine to a big endian machine, the big endian machine reads the file
in reverse order.

7. File formats which have _________ as a basic unit are independent of endianness.
a) 1 byte
b) 2 bytes
c) 3 bytes
d) 4 bytes
View Answer
Answer: a
Explanation: File formats which have 1 byte as the basic unit are not endianness
dependent. Eg: ASCII files. Other file formats have fixed endianness formats.
8. If the code shown below is executed on a little endian machine, then what will be the
output of the following C code?

#include<stdio.h>
main()
{
int y=1;
printf("%d", (*(char*)&y));
}
a) 1
b) 1000
c) 9999
d) 0
View Answer
Answer: a
Explanation: The output of the above code will be one when it is run on a little endian
machine. This is because a little endian machine stores the least significant byte in the
smallest address.

9. If the data “ABCD” is to be stored in a little endian machine, it will be stored as


_________
a) ABCD
b) DCBA
c) CDAB
d) BCDA
View Answer
Answer: b
Explanation: In a little endian machine, the least significant byte (right most) is stored in the
smallest address. Hence the data “ABCD” will be stored in the form of “DCBA”.

10. The sequential order used to interpret a range of bytes in the memory of a computer is
known as _________
a) Ordering
b) Sequencing
c) Endianness
d) Byte prediction
View Answer
Answer: c

C Questions and Answers – Recursion


This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Recursion”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.


1. What will be the output of the following C code?

#include<stdio.h>
main()
{
int n;
n=f1(4);
printf("%d",n);
}
f1(int x)
{
int b;
if(x==1)
return 1;
else
b=x*f1(x-1);
return b;
}
a) 24
b) 4
c) 12
d) 10
View Answer
Answer: a
Explanation: The above code returns the factorial of a given number using the method of
recursion. The given number is 4 in the above code, hence the factorial of 4, that is, 24 will
be returned.

advertisement

2. The data structure used to implement recursive function calls _____________


a) Array
b) Linked list
c) Binary tree
d) Stack
View Answer
Answer: d
Explanation: The compiler uses the data type stack for implementing normal as well as
recursive function calls.

Subscribe Now: C Newsletter | Important Subjects Newsletters

3. The principle of stack is __________


a) First in first out
b) First in last out
c) Last in first out
d) Last in last out
View Answer
Answer: c
Explanation: A stack is a last in first out(LIFO) data type. This means that the last item to
get stored in the stack is the first item to get out of it.

4. In the absence of a exit condition in a recursive function, the following error is given
__________
a) Compile time error
b) Run time error
c) Logical error
d) No error
View Answer
Answer: b
Explanation: When a recursive function is called in the absence of an exit condition, it
results in an infinite loop due to which the stack keeps getting filled(stack overflow). This
results in a run time error.

5. What will be the output of the following C code?

#include<stdio.h>
main()
{
int n,i;
n=f(6);
printf("%d",n);
}
f(int x)
{
if(x==2)
return 2;
else
{
printf("+");
f(x-1);
}
}
a) ++++2
b) +++++2
c) +++++
d) 2
View Answer
Answer: a
Explanation:
When x=6: ‘+’ is printed.
When x=5: ‘+’ is printed.
When x=4: ‘+’ is printed.
When x=3: ‘+’ is printed.
When x=2: 2 is printed.
Hence the output is: ++++2.

6. How many times is ‘a’ printed when the following C code is executed?

#include<stdio.h>
main()
{
int a;
a=f1(10);
printf("%d",a);
}
f1(int b)
{
if(b==0)
return 0;
else
{
printf("a");
f1(b--);
}
}
a) 9 times
b) 10 times
c) 0 times
d) Infinite number of times
View Answer
Answer: d
Explanation: Although we have specified the exit condition, the code above results in an
infinite loop because we have used b- -(decrement operator) to call the recursive function.
Due to this, the loop goes on infinitely. However, if we had used f1(b-1) instead, the answer
would have been 10 times.

7. What will be the output of the following C code?

#include<stdio.h>
int main()
{
int n=10;
int f(int n);
printf("%d",f(n));
}
int f(int n)
{
if(n>0)
return(n+f(n-2));
}
a) 10
b) 80
c) 30
d) Error
View Answer
Answer: c
Explanation: The recursive function returns n+f(n-2) till 10>0.
Therefore, the above code will be evaluated as: 10+8+6+4+2, which is equal to 30.

8. What will be the output of the following C code?

#include<stdio.h>
int main()
{
printf("Hello");
main();
return 0;
}
a) Hello is printed once
b) Hello infinite number of times
c) Hello is not printed at all
d) 0 is returned
View Answer
Answer: b
Explanation: in the above code, we are calling main() from main(), which is recursion.
However, we have not defined any condition for the program to exit. Hence, “hello” will be
printed infinite number of times. To prevent this, we need to define a proper exit condition in
the recursive function.

9. What will be the output of the following C code if the input given to the code shown below
is “sanfoundry”?

#include<stdio.h>
#include<stdio.h>
#define NL '\n'
void main()
{
void f(void);
printf("enter the word\n");
f();
}
void f(void)
{
char c;
if((c=getchar())!=NL)
{
f();
printf("%c",c);
}
return;
}
a) sanfoundry
b) infinite loop
c) yrdnuofnas
d) fnasyrdnuo
View Answer
Answer: c
Explanation: The above code prints the reverse of the word entered. The recursive function
terminates when getchar() is equal to newline character.

10. Iteration requires more system memory than recursion.


a) True
b) False
View Answer
Answer: b
Explanation: Recursion requires more system memory than iteration due to the
maintenance of stack.

You might also like