C Programming MCQ
C Programming MCQ
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.
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.
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.
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.
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.
fp = fopen("Random.txt", "a");
a) Attach
b) Append
c) Apprehend
d) Add
View Answer
Answer: b
Explanation: None.
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. }
1. #include <stdio.h>
2. int main()
3. {
4. int main = 3;
5. printf("%d", main);
6. return 0;
7. }
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
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
1. #include <stdio.h>
2. int const print()
3. {
4. printf("Sanfoundry.com");
5. return 0;
6. }
7. void main()
8. {
9. print();
10. }
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
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.
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.
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.
a) 2
b) True
c) 1
d) 0
View Answer
Answer: a
Explanation: None.
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. }
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.
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.
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.
int func(int);
double func(int);
int func(float);
string p = "HELLO";
printf(“%10s”, state);
56. What are the elements present in the array of the following C code?
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?
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.
#if
#else
#endif
b)
#if
#elif
#endif
c)
#if
#if
#endif
d)
#if
#undef
#endif
View Answer
Answer: c
Explanation: None.
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)
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
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)
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
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
advertisement
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.
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.)
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
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
float 3Bedroom-Hall-Kitchen?;
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
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
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
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
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
C programming Class by
WOW Sanfoundry
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
___________
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
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
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
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
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
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
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
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
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’
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’
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’
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
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
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.)
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
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
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
7. The name of the variable used in one function cannot be used in another function.
a) True
b) False
View Answer
Answer: b
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
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.
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.
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.
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.
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
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
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.
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.
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.
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
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.
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.
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.
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.
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
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
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.
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.
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
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.
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.
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.
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.
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
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.
4. What will be the data type of the result of the following operation?
advertisement
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.
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.
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.
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.
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
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
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.
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.
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.
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.
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
advertisement
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?
advertisement
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.
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.
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
advertisement
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.
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.
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
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.
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.
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.
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.
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.
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.
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.
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
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)
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.
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.
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.
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
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
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.
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.
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.
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')
2. What will be the correct syntax for running two variable for loop simultaneously?
a)
advertisement
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.
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.
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.
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.
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
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.
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.
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.
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.
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.
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
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
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
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.
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.
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.
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.
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
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
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.
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.
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
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
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.
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.
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.
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.
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
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
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.
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.
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.
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.
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
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
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.
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.
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.
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.
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.
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
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
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.
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.
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.
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.
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
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)
square val = 36
square val = 36
#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);
#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’).
#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.
#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.
#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.
#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.
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.
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.
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.
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
#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
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.
#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.
#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.
#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.