0% found this document useful (0 votes)
195 views196 pages

C Programming MCQ Vrushabh RT

The document contains a comprehensive collection of C programming interview questions and answers, focusing on data types, variable names, operators, and expressions. It includes multiple-choice questions with detailed explanations to aid understanding for beginners and experienced professionals alike. The content is designed to help individuals prepare for interviews and improve their C programming skills.
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)
195 views196 pages

C Programming MCQ Vrushabh RT

The document contains a comprehensive collection of C programming interview questions and answers, focusing on data types, variable names, operators, and expressions. It includes multiple-choice questions with detailed explanations to aid understanding for beginners and experienced professionals alike. The content is designed to help individuals prepare for interviews and improve their C programming skills.
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/ 196

Terminator21 C programming vrushabh

1. Data Types, Operators and Expressions in C


The section contains multiple choice questions and answers on variable names,
datatpes, constants, declarations, arithmetic operators, relational and logical operators,
type conversions, bitwise operators, assignment operators, increment and decrement
operators, conditional expressions, evaluation order and precedence.

C Programming Questions and Answers


– Variable Names – 1
This section on C interview questions and answers focuses on “Variable Names”. One
shall practice these interview questions to improve their C programming skills needed for
various interviews (campus interviews, walk-in interviews, company interviews),
placements, entrance exams and other competitive exams. These questions can be
attempted by anyone focusing on learning C Programming language. They can be a
beginner, fresher, engineering graduate or an experienced IT professional. Our C
Interview questions come with the detailed explanation of the answers which helps in
better understanding of C concepts.
Here is a listing of C interview questions on “Variable Names” along with answers,
explanations and/or solutions:

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.
advertisement

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.
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.
Terminator21 C programming vrushabh

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.
advertisement

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.
advertisement

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)
Terminator21 C programming vrushabh

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
Sanfoundry’s 1000+ Interview Questions & Answers on C help anyone
preparing for Oracle and other companies C interviews. One should practice
these 1000+ interview questions and answers continuously for 2-3 months to
clear Oracle interviews on C Programming language.
Here is a listing of C interview questions on “Variable Names” along with
answers, explanations and/or solutions:

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.)
Terminator21 C programming vrushabh

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

advertisement
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
Terminator21 C programming vrushabh

$ a.out
3
advertisement

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
advertisement

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.
Terminator21 C programming vrushabh

C Programming Questions and Answers


– Data Types and Sizes – 1
<="" div="" style="border: 0px; margin: 0px; padding: 0px;">

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

1. #include <stdio.h>
2. int main()
3. {
4. int a[5] = {1, 2, 3, 4, 5};
5. int i;
6. for (i = 0; i < 5; i++)
7. if ((char)a[i] == '5')
8. printf("%d\n", a[i]);
9. else
10. printf("FAIL\n");
11. }
a) The compiler will flag an error
b) The program will compile and print the output 5
c) The program will compile and print the ASCII value of 5
d) The program will compile and print FAIL for 5 times
View Answer
Answer: d
Explanation: The ASCII value of 5 is 53, the char type-casted integral value 5
is 5 only.
Output:
$ cc pgm1.c
$ a.out
FAIL
FAIL
FAIL
FAIL
FAIL
advertisement

2. The format identifier ‘%i’ is also used for _____ data type.
a) char
b) int
c) float
d) double
View Answer
Answer: b
Explanation: Both %d and %i can be used as a format identifier for int data
type.
3. Which data type is most suitable for storing a number 65000 in a 32-bit
system?
a) signed short
b) unsigned short
c) long
Terminator21 C programming vrushabh

d) int
View Answer
Answer: b
Explanation: 65000 comes in the range of short (16-bit) which occupies the
least memory. Signed short ranges from -32768 to 32767 and hence we
should use unsigned short.
4. Which of the following is a User-defined data type?
a) typedef int Boolean;
b) typedef enum {Mon, Tue, Wed, Thu, Fri} Workdays;
c) struct {char name[10], int age};
d) all of the mentioned
View Answer
Answer: d
Explanation: typedef and struct are used to define user-defined data types.
Join Sanfoundry@YouTube

5. What is the size of an int data type?


a) 4 Bytes
b) 8 Bytes
c) Depends on the system/compiler
d) Cannot be determined
View Answer
Answer: c
Explanation: The size of the data types depend on the system.
6. 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: signed char will be a negative number.
Output:
$ cc pgm2.c
$ a.out
-128
advertisement

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


Terminator21 C programming vrushabh

1. #include <stdio.h>
2. int main()
3. {
4. char c;
5. int i = 0;
6. FILE *file;
7. file = fopen("test.txt", "w+");
8. fprintf(file, "%c", 'a');
9. fprintf(file, "%c", -1);
10. fprintf(file, "%c", 'b');
11. fclose(file);
12. file = fopen("test.txt", "r");
13. while ((c = fgetc(file)) != -1)
14. printf("%c", c);
15. return 0;
16. }
a) a
b) Infinite loop
c) Depends on what fgetc returns
d) Depends on the compiler
View Answer
Answer: a
Explanation: None.
Output:
$ cc pgm3.c
$ a.out
a
8. 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.

C Programming Questions and Answers


– Data Types and Sizes – 2
Sanfoundry’s 1000+ MCQs on C helps anyone preparing for placement in
Wipro and other companies. Anyone looking for Wipro placement papers
should practice these 1000+ questions continuously for 2-3 months, thereby
ensuring a top position in placements.
Here is a listing of C language programming interview questions on “Data
Types and Sizes” along with answers, explanations and/or solutions:

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

advertisement
1. #include <stdio.h>
Terminator21 C programming vrushabh

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
2. 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.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
advertisement

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;
Terminator21 C programming vrushabh

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
advertisement

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
Terminator21 C programming vrushabh

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 section on online C test focuses on “Constants”. One shall practice these
test questions to improve their C programming skills needed for various
interviews (campus interviews, walk-in interviews, company interviews),
placements, entrance exams and other competitive exams. These questions
can be attempted by anyone focusing on learning C Programming language.
They can be a beginner, fresher, engineering graduate or an experienced IT
professional. Our online C test questions come with the detailed explanation
of the answers which helps in better understanding of C concepts.
Here is a listing of online C test questions on “Constants” along with answers,
explanations and/or solutions:

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

advertisement
Terminator21 C programming vrushabh

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
2. What will be the output of the following C code?

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

advertisement

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
Terminator21 C programming vrushabh

$ 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
advertisement

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
Terminator21 C programming vrushabh

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:
Terminator21 C programming vrushabh

$ cc pgm6.c
$ a.out
5
advertisement

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
Explanation: str is null terminated, but ary is not null terminated.
Output:
$ cc pgm7.c
$ a.out
15

C Programming Questions and Answers


– Constants – 2
Sanfoundry’s 1000+ MCQs on C helps anyone preparing for placement in TCS
and other companies. Anyone looking for TCS placement papers should
practice these 1000+ questions continuously for 2-3 months, thereby
ensuring a top position in placements.
Here is a listing of C problems on “Constants” along with answers,
explanations and/or solutions:

1. enum types are processed by _________


a) Compiler
b) Preprocessor
c) Linker
d) Assembler
View Answer
Answer: a
Explanation: None.
advertisement
Terminator21 C programming vrushabh

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

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
advertisement

Do Lizards Develop a ‘Love Language?’

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
Terminator21 C programming vrushabh

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?

advertisement
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
Terminator21 C programming vrushabh

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’
advertisement

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. {
Terminator21 C programming vrushabh

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

C Programming Questions and Answers


– Declarations – 1
This section on C interview questions and answers focuses on “Declarations”.
One shall practice these interview questions to improve their C programming
skills needed for various interviews (campus interviews, walk-in interviews,
company interviews), placements, entrance exams and other competitive
exams. These questions can be attempted by anyone focusing on learning C
Programming language. They can be a beginner, fresher, engineering
graduate or an experienced IT professional. Our C Interview questions come
with the detailed explanation of the answers which helps in better
understanding of C concepts.
Here is a listing of C interview questions on “Declarations” along with
answers, explanations and/or solutions:

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

advertisement
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. }
Terminator21 C programming vrushabh

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’
2. What will be the output of the following C code?

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
Join Sanfoundry@YouTube

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
Terminator21 C programming vrushabh

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?

advertisement
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,
Terminator21 C programming vrushabh

variables have only block scope.


Output:
$ cc pgm5.c
advertisement

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; & 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
Explanation: %c can be used to print the indexed position.
%d can still be used to display its ASCII value.
%s is recommended.
%f cannot be used for the variable var.

C Programming Questions and Answers


– Declarations – 2
Sanfoundry’s 1000+ Interview Questions & Answers on C help anyone
preparing for TCS and other companies C interviews. One should practice
these 1000+ interview questions and answers continuously for 2-3 months to
clear TCS interviews on C Programming language.
Here is a listing of C questions and puzzles on “Declarations” along with
answers, explanations and/or solutions:

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
Terminator21 C programming vrushabh

Answer: d
Explanation: char[] str is a declaration in Java, but not in C.
advertisement

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.
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?

advertisement
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
Terminator21 C programming vrushabh

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.
advertisement

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
Explanation: Since the scope of the variable declared within a function is
restricted only within that function, the same name can be used to declare
another variable in another function.

C Programming Questions and Answers


– Arithmetic Operators – 1
This section on C test focuses on “Arithmetic Operators”. One shall practice
these test questions to improve their C programming skills needed for various
interviews (campus interviews, walkin interviews, company interviews),
placements, entrance exams and other competitive exams. These questions
can be attempted by anyone focusing on learning C Programming language.
They can be a beginner, fresher, engineering graduate or an experienced IT
professional. Our C test questions come with detailed explanation of the
answers which helps in better understanding of C concepts.
Here is a listing of C test questions on “Arithmetic Operators” along with
answers, explanations and/or solutions:

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


Terminator21 C programming vrushabh

advertisement
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.
2. What will be the output of the following C code?

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.
advertisement

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.
Terminator21 C programming vrushabh

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

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.
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.
advertisement

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?
Terminator21 C programming vrushabh

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
Explanation: None.

C Programming Questions and Answers


– Arithmetic Operators – 2
Sanfoundry’s 1000+ MCQs on C helps anyone preparing for placement in
Infosys and other companies. Anyone looking for Infosys placement papers
should practice these 1000+ questions continuously for 2-3 months, thereby
ensuring a top position in placements.
Here is a listing of C test questions on “Arithmetic Operators” along with
answers, explanations and/or solutions:

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

advertisement
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.
2. What is the precedence of arithmetic operators (from highest to lowest)?
a) %, *, /, +, –
b) %, +, /, *, –
c) +, -, %, *, /
d) %, +, -, *, /
View Answer
Answer: a
Explanation: None.
Terminator21 C programming vrushabh

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.
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.
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.
advertisement

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


Terminator21 C programming vrushabh

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 section on C interview questions and answers focuses on “Relational &
Logical Operators”. One shall practice these interview questions to improve
their C programming skills needed for various interviews (campus interviews,
walkin interviews, company interviews), placements, entrance exams and
other competitive exams. These questions can be attempted by anyone
focusing on learning C Programming language. They can be a beginner,
fresher, engineering graduate or an experienced IT professional. Our C
Interview questions come with detailed explanation of the answers which
helps in better understanding of C concepts.
Here is a listing of C interview questions on “Relational & Logical Operators”
along with answers, explanations and/or solutions:

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

advertisement
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.
2. What will be the output of the following C code?
Terminator21 C programming vrushabh

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.
advertisement

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?

advertisement
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>
Terminator21 C programming vrushabh

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.
advertisement

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>
Terminator21 C programming vrushabh

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
Explanation: None.

C Programming Questions and Answers


– Relational & Logical Operators – 2
Sanfoundry’s 1000+ MCQs on C helps anyone preparing for placement in IBM
and other companies. Anyone looking for IBM placement papers should
practice these 1000+ questions continuously for 2-3 months, thereby
ensuring a top position in placements.
Here is a listing of C interview questions on “Relational & Logical Operators”
along with answers, explanations and/or solutions:

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.
advertisement

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.
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
Terminator21 C programming vrushabh

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?

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.
advertisement

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. }
a) 5 1
b) 0 3
c) 5 3
d) 1 1
View Answer
Answer: a
Explanation: None.
advertisement

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?
Terminator21 C programming vrushabh

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
c) TRUE 1 TRUE 2
d) Compiler Dependent
View Answer
Answer: d
Explanation: This is a sequence point problem and hence the result will be
implementation dependent.
advertisement

8. Relational operators cannot be used on ____________


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

C Programming Questions and Answers


– Type Conversions – 1
This section on C language interview questions and answers focuses on
“Type Conversions”. One shall practice these interview questions to improve
their C programming skills needed for various interviews (campus interviews,
walkin interviews, company interviews), placements, entrance exams and
other competitive exams. These questions can be attempted by anyone
focusing on learning C Programming language. They can be a beginner,
fresher, engineering graduate or an experienced IT professional. Our C
language interview questions come with detailed explanation of the answers
which helps in better understanding of C concepts.
Here is a listing of C language interview questions on “Type Conversions”
along with answers, explanations and/or solutions:

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

advertisement
1. #include <stdio.h>
2. void main()
3. {
Terminator21 C programming vrushabh

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.
2. What will be the output of the following C code?

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)

advertisement
1. #include <stdio.h>
2. void main()
3. {
4. float x;
5. int y;
6. printf("enter two numbers \n", x);
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?

advertisement
Terminator21 C programming vrushabh

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.
advertisement

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. }
Terminator21 C programming vrushabh

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
Explanation: None.

C Programming Questions and Answers


– Type Conversions – 2
Sanfoundry’s 1000+ Interview Questions & Answers on C helps anyone
preparing for Sasken and other companies C interviews. One should practice
these 1000+ interview questions and answers continuously for 2-3 months to
clear Sasken interviews on C Programming language.
Here is a listing of C programming interview questions on “Type Conversions”
along with answers, explanations and/or solutions:

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.
advertisement

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?
Terminator21 C programming vrushabh

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.
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.
Join Sanfoundry@YouTube

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.
advertisement

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


a) Widening conversions
b) Narrowing conversions
c) Widening & Narrowing conversions
Terminator21 C programming vrushabh

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
Explanation: None.

C Programming Questions and Answers


– Increment and Decrement Operators
–1
This section on online C test focuses on “Increment and Decrement
Operators”. One shall practice these test questions to improve their C
programming skills needed for various interviews (campus interviews, walkin
interviews, company interviews), placements, entrance exams and other
competitive exams. These questions can be attempted by anyone focusing
on learning C Programming language. They can be a beginner, fresher,
engineering graduate or an experienced IT professional. Our C online test
questions come with detailed explanation of the answers which helps in
better understanding of C concepts.
Here is a listing of online C test questions on “Increment and Decrement
Operators” along with answers, explanations and/or solutions:

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

advertisement
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
Terminator21 C programming vrushabh

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.
2. What will be the output of the following C code?

advertisement
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.
advertisement

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?
Terminator21 C programming vrushabh

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?

advertisement
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. What will be the output of the following C code?

1. #include <stdio.h>
Terminator21 C programming vrushabh

2. int main()
3. {
4. int i = 2;
5. int 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
Explanation: None.

C Programming Questions and Answers


– Increment and Decrement Operators
–2
Sanfoundry’s 1000+ MCQs on C helps anyone preparing for placement in
Capgemini and other companies. Anyone looking for Capgemini placement
papers should practice these 1000+ questions continuously for 2-3 months,
thereby ensuring a top position in placements.
Here is a listing of C questions and puzzles on “Increment and Decrement
Operators” along with answers, explanations and/or solutions:

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

advertisement
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.
2. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 10;
Terminator21 C programming vrushabh

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.
advertisement

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?

advertisement
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. {
Terminator21 C programming vrushabh

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.
advertisement

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
Explanation: None.
Terminator21 C programming vrushabh

C Programming Questions and Answers


– Bitwise Operators – 1
This section on C interview questions and answers focuses on “Bitwise
Operators”. One shall practice these interview questions to improve their C
programming skills needed for various interviews (campus interviews, walkin
interviews, company interviews), placements, entrance exams and other
competitive exams. These questions can be attempted by anyone focusing
on learning C Programming language. They can be a beginner, fresher,
engineering graduate or an experienced IT professional. Our C Interview
questions come with detailed explanation of the answers which helps in
better understanding of C concepts.
Here is a listing of C interview questions on “Bitwise Operators” along with
answers, explanations and/or solutions:

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

advertisement
1. #include <stdio.h>
2. int main()
3. {
4. int c = 2 ^ 3;
5. printf("%d\n", c);
6. }
a) 1
b) 8
c) 9
d) 0
View Answer
Answer: a
Explanation: None.
2. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. unsigned int a = 10;
5. a = ~a;
6. printf("%d\n", a);
7. }
a) -9
b) -10
c) -11
d) 10
View Answer
Answer: c
Explanation: None.
advertisement
Terminator21 C programming vrushabh

Do Lizards Develop a ‘Love Language?’

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

1. #include <stdio.h>
2. int main()
3. {
4. if (7 & 8)
5. printf("Honesty");
6. if ((~7 & 0x000f) == 8)
7. printf("is the best policy\n");
8. }
a) Honesty is the best policy
b) Honesty
c) is the best policy
d) No output
View Answer
Answer: c
Explanation: None.
4. What will be the output of the following C code?

advertisement
1. #include <stdio.h>
2. int main()
3. {
4. int a = 2;
5. if (a >> 1)
6. printf("%d\n", a);
7. }
a) 0
b) 1
c) 2
d) No Output
View Answer
Answer: c
Explanation: None.
5. Comment on the output of the following C code.

1. #include <stdio.h>
2. int main()
3. {
4. int i, n, a = 4;
5. scanf("%d", &n);
Terminator21 C programming vrushabh

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


7. a = a * 2;
8. }
a) Logical Shift left
b) No output
c) Arithmetic Shift right
d) Bitwise exclusive OR
View Answer
Answer: b
Explanation: None.
advertisement

6. 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.
7. 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 2 3
c) 3 2 2
d) 2 3 3
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 x = 4;
5. int *p = &x;
Terminator21 C programming vrushabh

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.

C Programming Questions and Answers


– Bitwise Operators – 2
Sanfoundry’s 1000+ MCQs on C helps anyone preparing for placement in HCL
and other companies. Anyone looking for HCL placement papers should
practice these 1000+ questions continuously for 2-3 months, thereby
ensuring a top position in placements.
Here is a listing of C questions on “Bitwise Operators” along with answers,
explanations and/or solutions:

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

advertisement
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.
2. 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) -3
b) -5
Terminator21 C programming vrushabh

c) 4
d) Undefined
View Answer
Answer: a
Explanation: None.
Join Sanfoundry@YouTube

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

1. #include <stdio.h>
2. int main()
3. {
4. int x = 2;
5. x = x << 1;
6. printf("%d\n", x);
7. }
a) 4
b) 1
c) Depends on the compiler
d) Depends on the endianness of the machine
View Answer
Answer: a
Explanation: None.
4. What will be the output of the following C code?

advertisement
1. #include <stdio.h>
2. int main()
3. {
4. int x = -2;
5. x = x >> 1;
6. printf("%d\n", x);
7. }
a) 1
b) -1
c) 2 31 – 1 considering int to be 4 bytes
d) Either -1 or 1
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. if (~0 == 1)
5. printf("yes\n");
6. else
7. printf("no\n");
8. }
Terminator21 C programming vrushabh

a) yes
b) no
c) compile time error
d) undefined
View Answer
Answer: b
Explanation: None.
advertisement

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

1. #include <stdio.h>
2. int main()
3. {
4. int x = -2;
5. if (!0 == 1)
6. printf("yes\n");
7. else
8. printf("no\n");
9. }
a) yes
b) no
c) run time error
d) undefined
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 y = 0;
5. if (1 |(y = 1))
6. printf("y is %d\n", y);
7. else
8. printf("%d\n", y);
9.
10. }
a) y is 1
b) 1
c) run time error
d) undefined
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 y = 1;
Terminator21 C programming vrushabh

5. if (y & (y = 2))
6. printf("true %d\n", y);
7. else
8. printf("false %d\n", y);
9.
10. }
a) true 2
b) false 2
c) either true 2 or false 2
d) true 1
View Answer
Answer: a
Explanation: None.

C Programming Questions and Answers


– Assignment Operators & Expressions
–1
This section on C aptitude questions and answers focuses on “Assignment
Operators & Expressions”. One shall practice these aptitude questions to
improve their C programming skills needed for various interviews (campus
interviews, walkin interviews, company interviews), placements, entrance
exams, aptitude tests and other competitive exams. These questions can be
attempted by anyone focusing on learning C Programming language. They
can be a beginner, fresher, engineering graduate or an experienced IT
professional. Our C questions come with detailed explanation of the answers
which helps in better understanding of C concepts.
Here is a listing of C aptitude questions on “Assignment Operators &
Expressions” along with answers, explanations and/or solutions:

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

advertisement
1. #include <stdio.h>
2. void main()
3. {
4. int x = 0;
5. if (x = 0)
6. printf("Its zero\n");
7. else
8. printf("Its not zero\n");
9. }
a) Its not zero
b) Its zero
c) Run time error
d) None
View Answer
Answer: a
Explanation: None.
Terminator21 C programming vrushabh

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

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

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

1. #include <stdio.h>
2. void main()
3. {
4. char a = 'a';
5. int x = (a % 10)++;
6. printf("%d\n", x);
7. }
a) 6
b) Junk value
c) Compile time error
d) 7
View Answer
Answer: c
Explanation: None.
4. What will be the output of the following C code snippet?

advertisement
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.
5. What will be the output of the following C code snippet?
Terminator21 C programming vrushabh

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

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

1. #include <stdio.h>
2. int main()
3. {
4. int x = 2, y = 1;
5. x *= x + y;
6. printf("%d\n", x);
7. return 0;
8. }
a) 5
b) 6
c) Undefined behaviour
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 x = 2, y = 2;
5. x /= x / y;
6. printf("%d\n", x);
7. return 0;
8. }
a) 2
b) 1
c) 0.5
d) Undefined behaviour
View Answer
Answer: a
Explanation: None.
8. What will be the output of the following C code?

1. #include <stdio.h>
Terminator21 C programming vrushabh

2. int main()
3. {
4. int x = 1, y = 0;
5. x &&= y;
6. printf("%d\n", x);
7. }
a) Compile time error
b) 1
c) 0
d) Undefined behaviour
View Answer
Answer: a
Explanation: None.

C Programming Questions and Answers


– Assignment Operators & Expressions
–2
Sanfoundry’s 1000+ Interview Questions & Answers on C help anyone
preparing for Microsoft and other companies C interviews. One should
practice these 1000+ interview questions and answers continuously for 2-3
months to clear Microsoft interviews on C Programming language.
Here is a listing of C test questions on “Assignment Operators & Expressions”
along with answers, explanations and/or solutions:

1. What is the type of the following assignment expression if x is of type float


and y is of type int?

y = x + y;

advertisement

a) int
b) float
c) there is no type for an assignment expression
d) double
View Answer
Answer: a
Explanation: None.
2. 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
Terminator21 C programming vrushabh

Answer: a
Explanation: None.
3. Operation “a = a * b + a” can also be written as ___________
a) a *= b + 1;
b) (c = a * b)!=(a = c + a);
c) a = (b + 1)* a;
d) All of the mentioned
View Answer
Answer: d
Explanation: None.
advertisement

4. What will be the final value of c in the following C statement? (Initial value:
c = 2)

1.c <<= 1;
a) c = 1;
b) c = 2;
c) c = 3;
d) c = 4;
View Answer
Answer: d
Explanation: None.
5. What will be the output of the following C code?

advertisement
1. #include <stdio.h>
2. int main()
3. {
4. int a = 1, b = 2;
5. a += b -= a;
6. printf("%d %d", a, b);
7. }
a) 1 1
b) 1 2
c) 2 1
d) 2 2
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 a = 4, n, i, result = 0;
5. scanf("%d", n);
6. for (i = 0;i < n; i++)
7. result += a;
8. }
Terminator21 C programming vrushabh

a) Addition of a and n
b) Subtraction of a and n
c) Multiplication of a and n
d) Division of a and n
View Answer
Answer: c
Explanation: None.
advertisement

7. Which of the following is an invalid assignment operator?


a) a %= 10;
b) a /= 10;
c) a |= 10;
d) None of the mentioned
View Answer
Answer: d
Explanation: None.

C Programming Questions and Answers


– Conditional Expressions – 1
This section on C interview questions and answers focuses on “Conditional
Expressions”. One shall practice these interview questions to improve their C
programming skills needed for various interviews (campus interviews, walkin
interviews, company interviews), placements, entrance exams and other
competitive exams. These questions can be attempted by anyone focusing
on learning C Programming language. They can be a beginner, fresher,
engineering graduate or an experienced IT professional. Our C Interview
questions come with detailed explanation of the answers which helps in
better understanding of C concepts.
Here is a listing of C interview questions on “Conditional Expressions” along
with answers, explanations and/or solutions:

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

advertisement
1. #include <stdio.h>
2. int main()
3. {
4. int x = 2, y = 0;
5. int z = (y++) ? y == 1 && x : 0;
6. printf("%d\n", z);
7. return 0;
8. }
a) 0
b) 1
c) Undefined behaviour
d) Compile time error
View Answer
Terminator21 C programming vrushabh

Answer: a
Explanation: None.
2. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int x = 1;
5. int y = x == 1 ? getchar(): 2;
6. printf("%d\n", y);
7. }
a) Compile time error
b) Whatever character getchar function returns
c) Ascii value of character getchar function returns
d) 2
View Answer
Answer: c
Explanation: None.
advertisement

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

1. #include <stdio.h>
2. int main()
3. {
4. int x = 1;
5. short int i = 2;
6. float f = 3;
7. if (sizeof((x == 2) ? f : i) == sizeof(float))
8. printf("float\n");
9. else if (sizeof((x == 2) ? f : i) == sizeof(short int))
10. printf("short int\n");
11. }
a) float
b) short int
c) Undefined behaviour
d) Compile time error
View Answer
Answer: a
Explanation: None.
4. What will be the output of the following C code?

advertisement
1. #include <stdio.h>
2. int main()
3. {
4. int a = 2;
5. int b = 0;
6. int y = (b == 0) ? a :(a > b) ? (b = 1): a;
7. printf("%d\n", y);
8. }
Terminator21 C programming vrushabh

a) Compile time error


b) 1
c) 2
d) Undefined behaviour
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 y = 1, x = 0;
5. int l = (y++, x++) ? y : x;
6. printf("%d\n", l);
7. }
a) 1
b) 2
c) Compile time error
d) Undefined behaviour
View Answer
Answer: a
Explanation: None.
advertisement

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

1. #include <stdio.h>
2. void main()
3. {
4. int k = 8;
5. int m = 7;
6. int z = k < m ? k++ : m++;
7. printf("%d", z);
8. }
a) 7
b) 8
c) Run time error
d) 15
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 = 8;
5. int m = 7;
6. int z = k < m ? k = m : m++;
7. printf("%d", z);
8. }
Terminator21 C programming vrushabh

a) Run time error


b) 7
c) 8
d) Depends on compiler
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. 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.

C Programming Questions and Answers


– Conditional Expressions – 2
Sanfoundry’s 1000+ Interview Questions & Answers on C helps anyone
preparing for IBM and other companies C interviews. One should practice
these 1000+ interview questions and answers continuously for 2-3 months to
clear IBM interviews on C Programming language.
Here is a listing of C interview questions on “Conditional Expressions” along
with answers, explanations and/or solutions:

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

advertisement
1. #include <stdio.h>
2. void main()
3. {
4. int k = 8;
5. int m = 7;
6. k < m ? k++ : m = k;
7. printf("%d", k);
8. }
a) 7
b) 8
c) Compile time error
d) Run time error
View Answer
Terminator21 C programming vrushabh

Answer: c
Explanation: None.
2. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int k = 8;
5. int m = 7;
6. k < m ? k = k + 1 : m = m + 1;
7. printf("%d", k);
8. }
a) Compile time error
b) 9
c) 8
d) Run time error
View Answer
Answer: a
Explanation: None.
3. What will be the final values of a and c in the following C statement? (Initial
values: a = 2, c = 1)

advertisement
c = (c) ? a = 0 : 2;
a) a = 0, c = 0;
b) a = 2, c = 2;
c) a = 2, c = 2;
d) a = 1, c = 2;
View Answer
Answer: a
Explanation: None.
4. What will be the data type of the following expression? (Initial data type: a
= int, var1 = double, var2 = float)

expression (a < 50)? var1 : var2;


a) int
b) float
c) double
d) Cannot be determined
View Answer
Answer: c
Explanation: None.
5. Which expression has to be present in the following?

exp1 ? exp2 : exp3;

a) exp1
b) exp2
Terminator21 C programming vrushabh

c) exp3
d) all of the mentioned
View Answer
Answer: d
Explanation: None.
advertisement

6. What will be the final value of c in the following C code snippet? (Initial
values: a = 1, b = 2, c = 1)

c += (-c) ? a : b;
a) Syntax Error
b) c = 1
c) c = 2
d) c = 3
View Answer
Answer: c
Explanation: None.
7. The following C code can be rewritten as _______

c = (n) ? a : b;
a)

if (!n)c = b;
else c = a;
b)

advertisement
if (n <;= 0)c = b;
else c = a;
c)

if (n > 0)c = a;
else c = b;
d) All of the mentioned
View Answer
Answer: a
Explanation: None.

C Programming Questions and Answers


– Precedence and Order of Evaluation –
1
This section on C language interview questions and answers focuses on
“Precedence and Order of Evaluation”. One shall practice these interview
questions to improve their C programming skills needed for various
interviews (campus interviews, walkin interviews, company interviews),
Terminator21 C programming vrushabh

placements, entrance exams and other competitive exams. These questions


can be attempted by anyone focusing on learning C Programming language.
They can be a beginner, fresher, engineering graduate or an experienced IT
professional. Our C language interview questions come with detailed
explanation of the answers which helps in better understanding of C
concepts.
Here is a listing of C language interview questions “Precedence and Order of
Evaluation” along with answers, explanations and/or solutions:

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

advertisement
1. #include <stdio.h>
2. int main()
3. {
4. reverse(1);
5. }
6. void reverse(int i)
7. {
8. if (i > 5)
9. exit(0);
10. printf("%d\n", i);
11. return reverse(i++);
12. }
a) 1 2 3 4 5
b) 1 2 3 4
c) Compile time error
d) Stack overflow
View Answer
Answer: d
Explanation: None.
2. 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
Terminator21 C programming vrushabh

Answer: a
Explanation: None.
3. In expression i = g() + f(), first function called depends on __________
a) Compiler
b) Associativiy of () operator
c) Precedence of () and + operator
d) Left to write of the expression
View Answer
Answer: a
Explanation: None.
Join Sanfoundry@YouTube

4. 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 main()
4. {
5. int i = (f() + g()) || g();
6. int j = g() || (f() + g());
7. }
8. int f()
9. {
10. if (x == 0)
11. return x + 1;
12. else
13. return x - 1;
14. }
15. int g()
16. {
17. return x++;
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: d
Explanation: None.
5. 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 main()
4. {
5. int i = (f() + g()) | g(); //bitwise or
6. int j = g() | (f() + g()); //bitwise or
7. }
8. int f()
9. {
10. if (x == 0)
11. return x + 1;
12. else
13. return x - 1;
Terminator21 C programming vrushabh

14. }
15. int g()
16. {
17. return x++;
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
6. What will be the output of the following C code?

advertisement
1. #include <stdio.h>
2. int main()
3. {
4. int x = 2, y = 0;
5. int z = y && (y |= 10);
6. printf("%d\n", z);
7. return 0;
8. }
a) 1
b) 0
c) Undefined behaviour due to order of evaluation
d) 2
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 x = 2, y = 0;
5. int z = (y++) ? 2 : y == 1 && x;
6. printf("%d\n", z);
7. return 0;
8. }
a) 0
b) 1
c) 2
d) Undefined behaviour
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 x = 2, y = 0;
5. int z;
Terminator21 C programming vrushabh

6. z = (y++, y);
7. printf("%d\n", z);
8. return 0;
9. }
a) 0
b) 1
c) Undefined behaviour
d) Compilation error
View Answer
Answer: b
Explanation: None.
advertisement

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

1. #include <stdio.h>
2. int main()
3. {
4. int x = 2, y = 0, l;
5. int z;
6. z = y = 1, l = x && y;
7. printf("%d\n", l);
8. return 0;
9. }
a) 0
b) 1
c) Undefined behaviour due to order of evaluation can be different
d) Compilation error
View Answer
Answer: b
Explanation: None.
10. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int y = 2;
5. int z = y +(y = 10);
6. printf("%d\n", z);
7. }
a) 12
b) 20
c) 4
d) Either 12 or 20
View Answer
Answer: b
Explanation: None.
Terminator21 C programming vrushabh

C Programming Questions and Answers


– Precedence and Order of Evaluation –
2
Sanfoundry’s 1000+ Interview Questions & Answers on C helps anyone
preparing for VMware and other companies C interviews. One should practice
these 1000+ interview questions and answers continuously for 2-3 months to
clear VMware interviews on C Programming language.
Here is a listing of online C test questions on “Precedence and Order of
Evaluation” along with answers, explanations and/or solutions:

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

advertisement
1. #include <stdio.h>
2. int main()
3. {
4. int x = 2, y = 2;
5. float f = y + x /= x / y;
6. printf("%d %f\n", x, f);
7. return 0;
8. }
a) 2 4.000000
b) Compile time error
c) 2 3.500000
d) Undefined behaviour
View Answer
Answer: b
Explanation: None.
2. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int x = 1, y = 2;
5. if (x && y == 1)
6. printf("true\n");
7. else
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?
Terminator21 C programming vrushabh

advertisement
1. #include <stdio.h>
2. int main()
3. {
4. int x = 1, y = 2;
5. int z = x & y == 2;
6. printf("%d\n", z);
7. }
a) 0
b) 1
c) Compile time error
d) Undefined behaviour
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 x = 3, y = 2;
5. int z = x /= y %= 2;
6. printf("%d\n", z);
7. }
a) 1
b) Compile time error
c) Floating point exception
d) Segmentation fault
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 x = 3, y = 2;
5. int z = x << 1 > 5;
6. printf("%d\n", z);
7. }
a) 1
b) 0
c) 3
d) Compile time error
View Answer
Answer: a
Explanation: None.
advertisement

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


Terminator21 C programming vrushabh

1. #include <stdio.h>
2. int main()
3. {
4. int x = 3; //, y = 2;
5. const int *p = &x;
6. *p++;
7. printf("%d\n", *p);
8. }
a) Increment of read-only location compile error
b) 4
c) Some garbage value
d) Undefined behaviour
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 = 2, y = 2;
5. int z = x ^ y & 1;
6. printf("%d\n", z);
7. }
a) 1
b) 2
c) 0
d) 1 or 2
View Answer
Answer: b
Explanation: None.
8. What will be the output of the following C code?

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

1. #include <stdio.h>
Terminator21 C programming vrushabh

2. int main()
3. {
4. int x = 0, y = 2;
5. if (!x && y)
6. printf("true\n");
7. else
8. printf("false\n");
9. }
a) True
b) False
c) Compile time error
d) Undefined behaviour
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 x = 0, y = 2;
5. int z = ~x & y;
6. printf("%d\n", z);
7. }
a) -1
b) 2
c) 0
d) Compile time error
View Answer
Answer: b
Explanation: None.

C Programming Questions and Answers


– Precedence and Order of Evaluation –
3
This section on online C test focuses on “Precedence and Order of
Evaluation”. One shall practice these test questions to improve their C
programming skills needed for various interviews (campus interviews, walkin
interviews, company interviews), placements, entrance exams and other
competitive exams. These questions can be attempted by anyone focusing
on learning C Programming language. They can be a beginner, fresher,
engineering graduate or an experienced IT professional. Our online C test
questions come with detailed explanation of the answers which helps in
better understanding of C concepts.
Here is a listing of online C test questions on “Precedence and Order of
Evaluation” along with answers, explanations and/or solutions:

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


Terminator21 C programming vrushabh

advertisement
1. #include <stdio.h>
2. void main()
3. {
4. int a = 5 * 3 + 2 - 4;
5. printf("%d", a);
6. }
a) 13
b) 14
c) 12
d) 1 6
View Answer
Answer: a
Explanation: None.
2. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int a = 2 + 4 + 3 * 5 / 3 - 5;
5. printf("%d", a);
6. }
a) 7
b) 6
c) 10
d) 9
View Answer
Answer: b
Explanation: None.
3. What will be the output of the following C code?

advertisement
1. #include <stdio.h>
2. void main()
3. {
4. int a = 5 * 3 % 6 - 8 + 3;
5. printf("%d", a);
6. }
a) 10
b) 2
c) -2
d) -3
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. {
Terminator21 C programming vrushabh

4. int b = 6;
5. int c = 7;
6. int a = ++b + c--;
7. printf("%d", a);
8. }
a) Run time error
b) 15
c) 13
d) 14
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. double b = 8;
5. b++;
6. printf("%lf", b);
7. }
a) 9.000000
b) 9
c) 9.0
d) Run time error
View Answer
Answer: a
Explanation: None.
6. What will be the output of the following C code?

advertisement
1. #include <stdio.h>
2. void main()
3. {
4. double b = 3 % 0 * 1 - 4 / 2;
5. printf("%lf", b);
6. }
a) -2
b) Floating point Exception
c) 1
d) 0
View Answer
Answer: b
Explanation: None.
7. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. double b = 5 % 3 & 4 + 5 * 6;
5. printf("%lf", b);
Terminator21 C programming vrushabh

6. }
a) 2
b) 30
c) 2.000000
d) Run time error
View Answer
Answer: c
Explanation: None.
8. What will be the output of the following C code?

advertisement
1. #include <stdio.h>
2. void main()
3. {
4. double b = 3 && 5 & 4 % 3;
5. printf("%lf", b);
6. }
a) 3.000000
b) 4.000000
c) 5.000000
d) 1.000000
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. double b = 5 & 3 && 4 || 5 | 6;
5. printf("%lf", b);
6. }
a) 1.000000
b) 0.000000
c) 7.000000
d) 2.000000
View Answer
Answer: a
Explanation: None.
10. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int k = 0;
5. double b = k++ + ++k + k--;
6. printf("%d", k);
7. }
a) 6
b) 1
Terminator21 C programming vrushabh

c) 5
d) undefined
View Answer
Answer: d
Explanation: None.

C Programming Questions and Answers


– Precedence and Order of Evaluation –
4
Sanfoundry’s 1000+ Interview Questions & Answers on C helps anyone
preparing for Wipro and other companies C interviews. One should practice
these 1000+ interview questions and answers continuously for 2-3 months to
clear Wipro interviews on C Programming language.
Here is a listing of C programming interview questions on “Precedence and
Order of Evaluation” along with answers, explanations and/or solutions:

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

advertisement
1. #include <stdio.h>
2. void main()
3. {
4. int b = 5 - 4 + 2 * 5;
5. printf("%d", b);
6. }
a) 25
b) -5
c) 11
d) 16
View Answer
2. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int b = 5 & 4 & 6;
5. printf("%d", b);
6. }
a) 5
b) 6
c) 3
d) 4
View Answer
Answer: d
Explanation: None.
3. What will be the output of the following C code?
Terminator21 C programming vrushabh

advertisement
1. #include <stdio.h>
2. void main()
3. {
4. int b = 5 & 4 | 6;
5. printf("%d", b);
6. }
a) 6
b) 4
c) 1
d) 0
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 b = 5 + 7 * 4 - 9 * (3, 2);
5. printf("%d", b);
6. }
a) 6
b) 15
c) 13
d) 21
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 h = 8;
5. int b = (h++, h++);
6. printf("%d%d\n", b, h);
7. }
a) 10 10
b) 10 9
c) 9 10
d) 8 10
View Answer
Answer: c
Explanation: None.
advertisement

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

1. #include <stdio.h>
2. void main()
Terminator21 C programming vrushabh

3. {
4. int h = 8;
5. int b = h++ + h++ + h++;
6. printf("%d\n", h);
7. }
a) 9
b) 10
c) 12
d) 11
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 h = 8;
5. int b = 4 * 6 + 3 * 4 < 3 ? 4 : 3;
6. printf("%d\n", b);
7. }
a) 3
b) 33
c) 34
d) Run time error
View Answer
Answer: a
Explanation: None.
8. What will be the output of the following C code?

advertisement
1. #include <stdio.h>
2. void main()
3. {
4. int a = 2 + 3 - 4 + 8 - 5 % 4;
5. printf("%d\n", a);
6. }
a) 0
b) 8
c) 11
d) 9
View Answer
Answer: b
Explanation: None.
9. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. char a = '0';
5. char b = 'm';
Terminator21 C programming vrushabh

6. int c = a && b || '1';


7. printf("%d\n", c);
8. }
a) 0
b) a
c) 1
d) m
View Answer
Answer: c
Explanation: None.
10. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. char a = 'A';
5. char b = 'B';
6. int c = a + b % 3 - 3 * 2;
7. printf("%d\n", c);
8. }
a) 65
b) 58
c) 64
d) 59
View Answer
Answer: d
Explanation: None.

C Programming Questions and Answers


– Precedence and Order of Evaluation –
5
This section on C interview questions and answers focuses on “Precedence
and Order of Evaluation”. One shall practice these interview questions to
improve their C programming skills needed for various interviews (campus
interviews, walkin interviews, company interviews), placements, entrance
exams and other competitive exams. These questions can be attempted by
anyone focusing on learning C Programming language. They can be a
beginner, fresher, engineering graduate or an experienced IT professional.
Our C Interview questions come with detailed explanation of the answers
which helps in better understanding of C concepts.
Here is a listing of C interview questions on “Precedence and Order of
Evaluation” along with answers, explanations and/or solutions:

1. Which of the following operators has an associativity from Right to Left?


a) <=
b) <<
c) ==
Terminator21 C programming vrushabh

d) +=
View Answer
Answer: d
Explanation: None.
advertisement

2. Which operators of the following have same precedence?

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.
3. Comment on the following statement.

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
Answer: d
Explanation: None.
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) Both e = ((a * b) + (c / (d * f))); and e = ((a * b) + ((c / d)* f));
View Answer
Answer: d
Explanation: Verified by e = 1 * 2 + 3 / 4 * 5; and then using respective
braces according to the option.
advertisement

5. While swapping 2 numbers what precautions to be taken care?

b = (b / a);
a = a * b;
b = a / b;
Terminator21 C programming vrushabh

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
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 = 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
Answer: d
Explanation: None.
advertisement

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
Answer: a
Explanation: None.
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
Answer: d
Explanation: None.
9. Which of the following operator has the highest precedence in the
following?
a) ()
Terminator21 C programming vrushabh

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
Explanation: None.

C Programming Questions and Answers


– Precedence and Order of Evaluation –
6
Sanfoundry’s 1000+ MCQs on C helps anyone preparing for placement in
Accenture and other companies. Anyone looking for Accenture placement
papers should practice these 1000+ questions continuously for 2-3 months,
thereby ensuring a top position in placements.
Here is a listing of C questions on “Precedence and Order of Evaluation”
along with answers, explanations and/or solutions:

1. Which of the following are unary operators?


a) sizeof
b) –
c) ++
d) all of the mentioned
View Answer
Answer: d
Explanation: None.
advertisement

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.
3. Associativity of an operator is ___________
a) Right to Left
Terminator21 C programming vrushabh

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.
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.
advertisement

Do Lizards Develop a ‘Love Language?’

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
Terminator21 C programming vrushabh

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: c
Explanation: None.
advertisement

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.
advertisement

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. }
Terminator21 C programming vrushabh

a) 1
b) 10
c) 20
d) No Output
View Answer
Answer: a
Explanation: None.
Terminator21 C programming vrushabh

2. Control Flow Statements in C


The section contains questions and answers on switch statements, if-then-
else statements, for and while loops, break and continue, goto and labels.

C Programming Questions and Answers


– If-then-else Statements – 1
This section on C programming questions and answers focuses on “If-then-
else Statements”. One shall practice these questions to improve their C
programming skills needed for various interviews (campus interviews, walkin
interviews, company interviews), placements, entrance exams and other
competitive exams. These questions can be attempted by anyone focusing
on learning C Programming language. They can be a beginner, fresher,
engineering graduate or an experienced IT professional. Our C programming
questions come with detailed explanation of the answers which helps in
better understanding of C concepts.
Here is a listing of C programming questions on “If-then-else Statements”
along with answers, explanations and/or solutions:

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

advertisement
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.
2. What will be the output of the following C code?

1. #include <stdio.h>
2. int x;
3. void main()
Terminator21 C programming vrushabh

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?

Join Sanfoundry@YouTube
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?
Terminator21 C programming vrushabh

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.
advertisement

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":
Terminator21 C programming vrushabh

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)

advertisement
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");
Terminator21 C programming vrushabh

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
Explanation: None

C Programming Questions and Answers


– If-then-else Statements – 2
Sanfoundry’s 1000+ Interview Questions & Answers on C helps anyone
preparing for Infosys and other companies C interviews. One should practice
these 1000+ interview questions and answers continuously for 2-3 months to
clear Infosys interviews on C Programming language.
Here is a listing of C test questions on “If-then-else Statements” along with
answers, explanations and/or solutions:

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

advertisement
Terminator21 C programming vrushabh

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

d) compile time error


View Answer
Answer: a
Explanation: None.
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.
advertisement

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
Terminator21 C programming vrushabh

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?

advertisement
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)
Terminator21 C programming vrushabh

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.
advertisement

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
Terminator21 C programming vrushabh

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
Explanation: None.

C Programming Questions and Answers


– Switch Statements – 1
This section on C interview questions and answers focuses on “Switch
Statements”. One shall practice these interview questions to improve their C
programming skills needed for various interviews (campus interviews, walkin
interviews, company interviews), placements, entrance exams and other
competitive exams. These questions can be attempted by anyone focusing
on learning C Programming language. They can be a beginner, fresher,
engineering graduate or an experienced IT professional. Our C Interview
questions come with detailed explanation of the answers which helps in
better understanding of C concepts.
Here is a listing of C interview questions on “Switch Statements” along with
answers, explanations and/or solutions:

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

advertisement
1. #include <stdio.h>
2. void main()
3. {
Terminator21 C programming vrushabh

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.
2. 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) Compile time error
c) 2
d) Run time error
View Answer
Answer: b
Explanation: None.
advertisement

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;
Terminator21 C programming vrushabh

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)

advertisement
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)
Terminator21 C programming vrushabh

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.
advertisement

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. }
Terminator21 C programming vrushabh

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
Explanation: None.

C Programming Questions and Answers


– Switch Statements – 2
Sanfoundry’s 1000+ Interview Questions & Answers on C helps anyone
preparing for HCL and other companies C interviews. One should practice
these 1000+ interview questions and answers continuously for 2-3 months to
clear HCL interviews on C Programming language.
Here is a listing of C interview questions on “Switch Statements” along with
answers, explanations and/or solutions:

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

advertisement
1. #include <stdio.h>
2. const int a = 1, b = 2;
3. int main()
4. {
5. int x = 1;
6. switch (x)
7. {
Terminator21 C programming vrushabh

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: None.
2. What will be the output of the following C code?

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.
advertisement

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;
Terminator21 C programming vrushabh

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.

advertisement
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:
Terminator21 C programming vrushabh

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.
advertisement

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
Explanation: None.

C Programming Questions and Answers


– For Loops – 1
This section on C language interview questions and answers focuses on “For
Loops”. One shall practice these interview questions to improve their C
programming skills needed for various interviews (campus interviews, walkin
interviews, company interviews), placements, entrance exams and other
competitive exams. These questions can be attempted by anyone focusing
on learning C Programming language. They can be a beginner, fresher,
engineering graduate or an experienced IT professional. Our C language
interview questions come with detailed explanation of the answers which
helps in better understanding of C concepts.
Here is a listing of C language interview questions on “For Loops” along with
answers, explanations and/or solutions:
Terminator21 C programming vrushabh

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.
advertisement

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

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


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

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


c)

advertisement
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.
advertisement
Terminator21 C programming vrushabh

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.
advertisement

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
Terminator21 C programming vrushabh

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
Explanation: None.

C Programming Questions and Answers


– For Loops – 2
Sanfoundry’s 1000+ Interview Questions & Answers on C helps anyone
preparing for Accenture and other companies C interviews. One should
practice these 1000+ interview questions and answers continuously for 2-3
months to clear Accenture interviews on C Programming language.
Here is a listing of online C test questions on “For Loops” along with answers,
explanations and/or solutions:

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

advertisement
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?

1. #include <stdio.h>
Terminator21 C programming vrushabh

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.
advertisement

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?

advertisement
1. #include <stdio.h>
2. int main()
3. {
4. int i = 0;
5. for (i++; i == 1; i = 2)
6. prints("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?
Terminator21 C programming vrushabh

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.
advertisement

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) Depends on the compiler
View Answer
Answer: c
Explanation: None.
Terminator21 C programming vrushabh

C Programming Questions and Answers


– While Loops – 1
This section on online C test focuses on “While Loops”. One shall practice
these test questions to improve their C programming skills needed for various
interviews (campus interviews, walkin interviews, company interviews),
placements, entrance exams and other competitive exams. These questions
can be attempted by anyone focusing on learning C Programming language.
They can be a beginner, fresher, engineering graduate or an experienced IT
professional. Our online C test questions come with detailed explanation of
the answers which helps in better understanding of C concepts.
Here is a listing of online C test questions on “While Loops” along with
answers, explanations and/or solutions:

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

advertisement
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.
2. What will be the output of the following C code?

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
Terminator21 C programming vrushabh

c) After loop
d) Infinite loop
View Answer
Answer: b
Explanation: None.
Join Sanfoundry@YouTube

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
Terminator21 C programming vrushabh

d) 1
View Answer
Answer: b
Explanation: None.
advertisement

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?

advertisement
1. #include <stdio.h>
2. void main()
3. {
4. int i = 0;
5. while (++i)
6. {
7. printf("H");
8. }
Terminator21 C programming vrushabh

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
Explanation: None.

C Programming Questions and Answers


– While Loops – 2
Sanfoundry’s 1000+ Interview Questions & Answers on C helps anyone
preparing for Capgemini and other companies C interviews. One should
practice these 1000+ interview questions and answers continuously for 2-3
months to clear Capgemini interviews on C Programming language.
Here is a listing of basic C interview questions on “While Loops” along with
answers, explanations and/or solutions:

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

advertisement
1. #include <stdio.h>
2. void main()
3. {
4. char *str = "";
5. do
6. {
7. printf("hello");
8. } while (str);
9. }
Terminator21 C programming vrushabh

a) Nothing
b) Run time error
c) Varies
d) Hello is printed infinite times
View Answer
Answer: d
Explanation: None.
2. 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 < 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.
advertisement

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
Terminator21 C programming vrushabh

c) n+1, n
d) n+1, n+1
View Answer
Answer: d
Explanation: None.
advertisement

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.
advertisement

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
Terminator21 C programming vrushabh

d) none of the mentioned


View Answer
Answer: c
Explanation: None.

C Programming Questions and Answers


– Break and Continue – 1
This section on C interview questions and answers focuses on “Break and
Continue statements”. One shall practice these interview questions to
improve their C programming skills needed for various interviews (campus
interviews, walkin interviews, company interviews), placements, entrance
exams and other competitive exams. These questions can be attempted by
anyone focusing on learning C Programming language. They can be a
beginner, fresher, engineering graduate or an experienced IT professional.
Our C Interview questions come with detailed explanation of the answers
which helps in better understanding of C concepts.
Here is a listing of C interview questions on “Break and Continue statements”
along with answers, explanations and/or solutions:

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.
advertisement

2. 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. continue;
9. }
10. }
a) 2
b) 3
c) 4
d) 5
View Answer
Answer: d
Explanation: None.
Terminator21 C programming vrushabh

3. 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. 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?

advertisement
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. }
Terminator21 C programming vrushabh

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.
advertisement

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
Terminator21 C programming vrushabh

Answer: c
Explanation: None.

C Programming Questions and Answers


– Break and Continue – 2
Sanfoundry’s 1000+ MCQs on C helps anyone preparing for placement in
Microsoft and other companies. Anyone looking for Microsoft placement
papers should practice these 1000+ questions continuously for 2-3 months,
thereby ensuring a top position in placements.
Here is a listing of basic C questions on “Break and Continue” along with
answers, explanations and/or solutions:

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

advertisement
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.
2. 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. break;
9. }
10. }
a) Hello is printed infinite times
b) Hello
c) Varies
d) Compile time error
View Answer
Terminator21 C programming vrushabh

Answer: d
Explanation: None.
3. What will be the output of the following C code?

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.
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)

advertisement
1
after loop
c)
Terminator21 C programming vrushabh

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

advertisement

b) After loop
c)

In while loop

In while loop

After loop
Terminator21 C programming vrushabh

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
Explanation: None.
Terminator21 C programming vrushabh

C Programming Questions and Answers


– Goto & Labels – 1
This section on tough C programming questions focuses on “Goto & Labels”.
One shall practice these questions to improve their C programming skills
needed for various interviews (campus interviews, walkin interviews,
company interviews), placements, entrance exams and other competitive
exams. These questions can be attempted by anyone focusing on learning C
Programming language. They can be a beginner, fresher, engineering
graduate or an experienced IT professional. Our C programming questions
come with detailed explanation of the answers which helps in better
understanding of C concepts.
Here is a listing of tough C programming questions on “Goto & Labels” along
with answers, explanations and/or solutions:

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

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) Compilation error
c) 1 2 4
d) 1 3 4
View Answer
Answer: a
Explanation: None.
2. 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) Compilation error
b) 1 2 3
c) 1 2
d) 1 3
View Answer
Terminator21 C programming vrushabh

Answer: b
Explanation: None.
3. What will be the output of the following C code?

Join Sanfoundry@YouTube
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;
Terminator21 C programming vrushabh

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.
advertisement

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. }
Terminator21 C programming vrushabh

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?

advertisement
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\n");
10. label: k = printf("%03d", i);
11. }
12. }
a) 0
b) hi hi hi 0 0 0
c) 0 hi hi hi 0 0 0
d) 0 0 0
View Answer
Answer: a
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
Explanation: None.
Terminator21 C programming vrushabh

C Programming Questions and Answers


– Goto & Labels – 2
Sanfoundry’s 1000+ MCQs on C helps anyone preparing for placement in
Cognizant and other companies. Anyone looking for Cognizant placement
papers should practice these 1000+ questions continuously for 2-3 months,
thereby ensuring a top position in placements.
Here is a listing of C programming questions on “Goto & Labels” along with
answers, explanations and/or solutions:

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

advertisement
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.
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.
3. What will be the output of the following C code?

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);
Terminator21 C programming vrushabh

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.
advertisement

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. {
Terminator21 C programming vrushabh

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.
advertisement

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");
Terminator21 C programming vrushabh

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
Explanation: None.
Terminator21 C programming vrushabh

3. C Functions and Structure of a Program


The section contains MCQs on functions basics, external variables, variable
scope, static and register variables, automatic variables, c-preprocessor, file
inclusion, macro substitution and conditional inclusion.

C Programming Questions and Answers


– Basics of Functions – 1
This section on C interview questions and answers focuses on “Basics of
Functions”. One shall practice these interview questions to improve their C
programming skills needed for various interviews (campus interviews, walkin
interviews, company interviews), placements, entrance exams and other
competitive exams. These questions can be attempted by anyone focusing
on learning C Programming language. They can be a beginner, fresher,
engineering graduate or an experienced IT professional. Our C Interview
questions come with detailed explanation of the answers which helps in
better understanding of C concepts.
Here is a listing of C interview questions on “Basics of Functions” along with
answers, explanations and/or solutions:

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

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

1. #include <stdio.h>
2. int main()
3. {
4. void foo(), f();
Terminator21 C programming vrushabh

5. f();
6. }
7. void foo()
8. {
9. printf("2 ");
10. }
11. void f()
12. {
13. printf("1 ");
14. foo();
15. }
a) Compile time error as foo is local to main
b) 1 2
c) 2 1
d) Compile time error due to declaration of functions inside main
View Answer
Answer: b
Explanation: None.
advertisement

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

1. #include <stdio.h>
2. int main()
3. {
4. void foo();
5. void f()
6. {
7. foo();
8. }
9. f();
10. }
11. void foo()
12. {
13. printf("2 ");
14. }
a) 2 2
b) 2
c) Compile time error
d) Depends on the compiler
View Answer
Answer: d
Explanation: Even though the answer is 2, this code will compile fine only
with gcc. GNU C supports nesting of functions in C as a language extension
whereas standard C compiler doesn’t.
4. What will be the output of the following C code?

advertisement
1. #include <stdio.h>
2. void foo();
3. int main()
4. {
5. void foo();
6. foo();
Terminator21 C programming vrushabh

7. return 0;
8. }
9. void foo()
10. {
11. printf("2 ");
12. }
a) Compile time error
b) 2
c) Depends on the compiler
d) Depends on the standard
View Answer
Answer: b
Explanation: None.
5. What will be the output of the following C code?

1. #include <stdio.h>
2. void foo();
3. int main()
4. {
5. void foo(int);
6. foo(1);
7. return 0;
8. }
9. void foo(int i)
10. {
11. printf("2 ");
12. }
a) 2
b) Compile time error
c) Depends on the compiler
d) Depends on the standard
View Answer
Answer: a
Explanation: None.
advertisement

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

1. #include <stdio.h>
2. void foo();
3. int main()
4. {
5. void foo(int);
6. foo();
7. return 0;
8. }
9. void foo()
10. {
11. printf("2 ");
12. }
a) 2
b) Compile time error
c) Depends on the compiler
Terminator21 C programming vrushabh

d) Depends on the standard


View Answer
Answer: b
Explanation: None.
7. What will be the output of the following C code?

1. #include <stdio.h>
2. void m()
3. {
4. printf("hi");
5. }
6. void main()
7. {
8. m();
9. }
a) hi
b) Run time error
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 m();
3. void n()
4. {
5. m();
6. }
7. void main()
8. {
9. void m()
10. {
11. printf("hi");
12. }
13. }
a) hi
b) Compile time error
c) Nothing
d) Varies
View Answer
Answer: b
Explanation: None.

C Programming Questions and Answers


– Basics of Functions – 2
This section on C interview questions and answers focuses on “Basics of
Functions”. One shall practice these interview questions to improve their C
programming skills needed for various interviews (campus interviews, walkin
interviews, company interviews), placements, entrance exams and other
Terminator21 C programming vrushabh

competitive exams. These questions can be attempted by anyone focusing


on learning C Programming language. They can be a beginner, fresher,
engineering graduate or an experienced IT professional. Our C Interview
questions come with detailed explanation of the answers which helps in
better understanding of C concepts.
Here is a listing of C interview questions on “Basics of Functions” along with
answers, explanations and/or solutions:

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

advertisement
1. #include <stdio.h>
2. void main()
3. {
4. m();
5. void m()
6. {
7. printf("hi");
8. }
9. }
a) hi
b) Compile time error
c) Nothing
d) Varies
View Answer
Answer: b
Explanation: None.
2. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. m();
5. }
6. void m()
7. {
8. printf("hi");
9. m();
10. }
a) Compile time error
b) hi
c) Infinite hi
d) Nothing
View Answer
Answer: c
Explanation: None.
advertisement

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


Terminator21 C programming vrushabh

1. #include <stdio.h>
2. void main()
3. {
4. static int x = 3;
5. x++;
6. if (x <= 5)
7. {
8. printf("hi");
9. main();
10. }
11. }
a) Run time error
b) hi
c) Infinite hi
d) hi hi
View Answer
Answer: d
Explanation: None.
4. Which of the following is a correct format for declaration of function?
a) return-type function-name(argument type);
b) return-type function-name(argument type){}
c) return-type (argument type)function-name;
d) all of the mentioned
View Answer
Answer: a
Explanation: None.
advertisement

5. Which of the following function declaration is illegal?


a) int 1bhk(int);
b) int 1bhk(int a);
c) int 2bhk(int*, int []);
d) all of the mentioned
View Answer
Answer: d
Explanation: None.
6. Which function definition will run correctly?
a)

int sum(int a, int b)


return (a + b);
b)

int sum(int a, int b)


{return (a + b);}
c)

advertisement
int sum(a, b)
return (a + b);
Terminator21 C programming vrushabh

d) none of the mentioned


View Answer
Answer: b
Explanation: None.
7. Can we use a function as a parameter of another function? [Eg: void
wow(int func())].
a) Yes, and we can use the function value conveniently
b) Yes, but we call the function again to get the value, not as convenient as in
using variable
c) No, C does not support it
d) This case is compiler dependent
View Answer
Answer: c
Explanation: None.
8. The value obtained in the function is given back to main by using ________
keyword.
a) return
b) static
c) new
d) volatile
View Answer
Answer: a
Explanation: None.

C Programming Questions and Answers


– Functions Returning Non-integers – 1
Sanfoundry’s 1000+ Interview Questions & Answers on C helps anyone
preparing for Cisco and other companies C interviews. One should practice
these 1000+ interview questions and answers continuously for 2-3 months to
clear Cisco interviews on C Programming language.
Here is a listing of C language interview questions on “Functions Returning
Non-integers” along with answers, explanations and/or solutions:

1. What is the return-type of the function sqrt()?


a) int
b) float
c) double
d) depends on the data type of the parameter
View Answer
Answer: c
Explanation: None.
advertisement

2. Which of the following function declaration is illegal?


a)
Terminator21 C programming vrushabh

double func();
int main(){}
double func(){}
b)

double func(){};
int main(){}
c)

int main()
{
double func();
}
double func(){//statements}
d) None of the mentioned
View Answer
Answer: d
Explanation: None.
advertisement

3. What will be the output of the following C code having void return-type
function?

1. #include <stdio.h>
2. void foo()
3. {
4. return 1;
5. }
6. void main()
7. {
8. int x = 0;
9. x = foo();
10. printf("%d", x);
11. }
a) 1
b) 0
c) Runtime error
d) Compile time error
View Answer
Answer: d
Explanation: None.
4. 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
Terminator21 C programming vrushabh

Answer: b
Explanation: None.
advertisement

5. 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.
6. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int k = m();
5. printf("%d", k);
6. }
7. void m()
8. {
9. printf("hello");
10. }
a) hello 5
b) Error
c) Nothing
d) Junk value
View Answer
Answer: a
Explanation: None.
7. What will be the output of the following C code?

1. #include <stdio.h>
2. int *m()
3. {
4. int *p = 5;
5. return p;
6. }
7. void main()
8. {
9. int *k = m();
10. printf("%d", k);
11. }
a) 5
b) Junk value
c) 0
Terminator21 C programming vrushabh

d) Error
View Answer
Answer: a
Explanation: None.
advertisement

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

1. #include <stdio.h>
2. int *m();
3. void main()
4. {
5. int *k = m();
6. printf("hello ");
7. printf("%d", k[0]);
8. }
9. int *m()
10. {
11. int a[2] = {5, 8};
12. return a;
13. }
a) hello 5 8
b) hello 5
c) hello followed by garbage value
d) Compilation error
View Answer
Answer: c
Explanation: None.

C Programming Questions and Answers


– Functions Returning Non-integers – 2
Sanfoundry’s 1000+ MCQs on C helps anyone preparing for placement in
Hexaware and other companies. Anyone looking for Hexaware placement
papers should practice these 1000+ questions continuously for 2-3 months,
thereby ensuring a top position in placements.
Here is a listing of online C test questions on “Functions Returning Non-
integers” along with answers, explanations and/or solutions:

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

advertisement
1. #include <stdio.h>
2. int *m();
3. void main()
4. {
5. int k = m();
6. printf("%d", k);
7. }
8. int *m()
9. {
Terminator21 C programming vrushabh

10. int a[2] = {5, 8};


11. return a;
12. }
a) 5
b) 8
c) Nothing
d) Varies
View Answer
Answer: d
Explanation: None.
2. What will be the output of the following C code?

1. #include <stdio.h>
2. void m(int k)
3. {
4. printf("hi");
5. }
6. void m(double k)
7. {
8. printf("hello");
9. }
10. void main()
11. {
12. m(3);
13. }
a) hi
b) hello
c) Compile time error
d) Nothing
View Answer
Answer: c
Explanation: None.
advertisement

3. What is the default return type if it is not specified in function definition?


a) void
b) int
c) double
d) short int
View Answer
Answer: b
Explanation: None.
4. What will be the output of the following C code?

1. #include <stdio.h>
2. int foo();
3. int main()
4. {
5. int i = foo();
6. }
7. foo()
8. {
9. printf("2 ");
Terminator21 C programming vrushabh

10. return 2;
11. }
a) 2
b) Compile time error
c) Depends on the compiler
d) Depends on the standard
View Answer
Answer: a
Explanation: None.
advertisement

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

1. #include <stdio.h>
2. double foo();
3. int main()
4. {
5. foo();
6. return 0;
7. }
8. foo()
9. {
10. printf("2 ");
11. return 2;
12. }
a) 2
b) Compile time error
c) Depends on the compiler
d) Depends on the standard
View Answer
Answer: b
Explanation: None.
6. Functions can return structure in C?
a) True
b) False
c) Depends on the compiler
d) Depends on the standard
View Answer
Answer: a
Explanation: None.
7. 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.
advertisement

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


Terminator21 C programming vrushabh

1. #include <stdio.h>
2. enum m{JAN, FEB, MAR};
3. enum m foo();
4. int main()
5. {
6. enum m i = foo();
7. printf("%d\n", i);
8. }
9. int foo()
10. {
11. return JAN;
12. }
a) Compile time error
b) 0
c) Depends on the compiler
d) Depends on the standard
View
Answer: a
Explanation: None.

C Programming Questions and Answers


– External Variables – 1
This section on C questions and puzzles focuses on “External Variables”. One
shall practice these questions and puzzles to improve their C programming
skills needed for various interviews (campus interviews, walkin interviews,
company interviews), placements, entrance exams and other competitive
exams. These programming puzzles can be attempted by anyone focusing on
learning C Programming language. They can be a beginner, fresher,
engineering graduate or an experienced IT professional. Our C questions
come with detailed explanation of the answers which helps in better
understanding of C concepts.
Here is a listing of C questions and puzzles on “External Variables” along with
answers, explanations and/or solutions:

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

advertisement
1. #include <stdio.h>
2. void main()
3. {
4. m();
5. printf("%d", x);
6. }
7. int x;
8. void m()
9. {
10. x = 4;
11. }
a) 4
b) Compile time error
c) 0
Terminator21 C programming vrushabh

d) Undefined
View Answer
Answer: b
Explanation: None.
2. What will be the output of the following C code?

1. #include <stdio.h>
2. int x;
3. void main()
4. {
5. printf("%d", x);
6. }
a) Junk value
b) Run time error
c) 0
d) Undefined
View Answer
Answer: c
Explanation: None.
Join Sanfoundry@YouTube

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

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

advertisement
1. #include <stdio.h>
2. int x = 5;
3. void main()
4. {
5. int x = 3;
6. printf("%d", x);
7. {
8. int x = 4;
Terminator21 C programming vrushabh

9. }
10. printf("%d", x);
11. }
a) 3 3
b) 3 4
c) 3 5
d) Run time error
View Answer
Answer: a
Explanation: None.
5. Functions in C 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.
6. Global variables are ____________
a) Internal
b) External
c) Both Internal and External
d) None of the mentioned
View Answer
Answer: b
Explanation: None.
7. Which of the following is an external variable in the following C code?

advertisement
1. #include <stdio.h>
2. int func (int a)
3. {
4. int b;
5. return b;
6. }
7. int main()
8. {
9. int c;
10. func (c);
11. }
12. int d;
a) a
b) b
c) c
d) d
View Answer
Answer: d
Explanation: None.
8. What will be the output of the following C code?
Terminator21 C programming vrushabh

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

1. #include <stdio.h>
2. double var = 8;
3. int main()
4. {
5. int var = 5;
6. printf("%d", var);
7. }
a) 5
b) 8
c) Compile time error due to wrong format identifier for double
d) Compile time error due to redeclaration of variable with same name
View Answer
Answer: a
Explanation: None.

C Programming Questions and Answers


– External Variables – 2
Sanfoundry’s 1000+ Interview Questions & Answers on C helps anyone
preparing for Cognizant and other companies C interviews. One should
practice these 1000+ interview questions and answers continuously for 2-3
months to clear Cognizant interviews on C Programming language.
Here is a listing of basic C interview questions on “External Variable” along
with answers, explanations and/or solutions:

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

advertisement
1. #include <stdio.h>
2. double i;
3. int main()
4. {
5. printf("%g\n",i);
6. return 0;
7. }
Terminator21 C programming vrushabh

a) 0
b) 0.000000
c) Garbage value
d) Depends on the compiler
View Answer
Answer: a
Explanation: None.
2. Which part of the program address space is p stored in the following C
code?

1. #include <stdio.h>
2. int *p = NULL;
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: b
Explanation: None.
advertisement

3. 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.
4. Can variable i be accessed by functions in another source file?

advertisement
1. #include <stdio.h>
2. int i;
3. int main()
Terminator21 C programming vrushabh

4. {
5. printf("%d\n", i);
6. }
a) Yes
b) No
c) Only if static keyword is used
d) Depends on the type of the variable
View Answer
Answer: a
Explanation: None.
5. Property of the external variable to be accessed by any source file is called
by the C90 standard as __________
a) external linkage
b) external scope
c) global scope
d) global linkage
View Answer
Answer: a
Explanation: None.
6. What will be the output of the following C code?

1. #include <stdio.h>
2. int *i;
3. int main()
4. {
5. if (i == NULL)
6. printf("true\n");
7. return 0;
8. }
a) true
b) true only if NULL value is 0
c) Compile time error
d) Nothing
View Answer
Answer: a
Explanation: None.
advertisement

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

1. #include <stdio.h>
2. int *i;
3. int main()
4. {
5. if (i == 0)
6. printf("true\n");
7. return 0;
8. }
a) true
b) true only if NULL value is 0
c) Compile time error
Terminator21 C programming vrushabh

d) Nothing
View Answer
Answer: b
Explanation: None.
8. What will be the output of the following C code?

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

C Programming Questions and Answers


– Scope of a Variable – 1
This section on C programming quiz focuses on “Scope of a Variable”. One
shall practice these quizzes to improve their C programming skills needed for
various interviews (campus interviews, walkin interviews, company
interviews), placements, entrance exams and other competitive exams.
These questions can be attempted by anyone focusing on learning C
Programming language. They can be a beginner, fresher, engineering
graduate or an experienced IT professional. Our C quiz comes with detailed
explanation of the answers which helps in better understanding of C
concepts.
Here is a listing of C programming quiz on “Scope of a Variable” along with
answers, explanations and/or solutions:

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

advertisement
1. #include <stdio.h>
2. int i;
3. int main()
4. {
5. extern int i;
6. if (i == 0)
7. printf("scope rules\n");
8. }
Terminator21 C programming vrushabh

a) scope rules
b) Compile time error due to multiple declaration
c) Compile time error due to not defining type in statement extern i
d) Nothing will be printed as value of i is not zero because i is an automatic
variable
View Answer
Answer: a
Explanation: None.
2. What will be the output of the following C code (without linking the source
file in which ary1 is defined)?

1. #include <stdio.h>
2. int main()
3. {
4. extern ary1[];
5. printf("scope rules\n");
6. }
a) scope rules
b) Linking error due to undefined reference
c) Compile time error because size of array is not provided
d) Compile time error because datatype of array is not provided
View Answer
Answer: a
Explanation: None.
advertisement

3. What will be the output of the following C code (after linking to source file
having definition of ary1)?

1. #include <stdio.h>
2. int main()
3. {
4. extern ary1[];
5. printf("%d\n", ary1[0]);
6. }
a) Value of ary1[0];
b) Compile time error due to multiple definition
c) Compile time error because size of array is not provided
d) Compile time error because datatype of array is not provided
View Answer
Answer: d
Explanation: None.
4. What is the scope of an external variable?
a) Whole source file in which it is defined
b) From the point of declaration to the end of the file in which it is defined
c) Any source file in a program
d) From the point of declaration to the end of the file being compiled
View Answer
Answer: d
Explanation: None.
Terminator21 C programming vrushabh

advertisement

5. What is the scope of a function?


a) Whole source file in which it is defined
b) From the point of declaration to the end of the file in which it is defined
c) Any source file in a program
d) From the point of declaration to the end of the file being compiled
View Answer
Answer: d
Explanation: None.
6. Comment on the output of the following C code.

1. #include <stdio.h>
2. int main()
3. {
4. int i;
5. for (i = 0;i < 5; i++)
6. int a = i;
7. printf("%d", a);
8. }
a) a is out of scope when printf is called
b) Redeclaration of a in same scope throws error
c) Syntax error in declaration of a
d) No errors, program will show the output 5
View Answer
Answer: c
Explanation: None.
7. Which variable has the longest scope in the following C code?

advertisement
1. #include <stdio.h>
2. int b;
3. int main()
4. {
5. int c;
6. return 0;
7. }
8. int a;
a) a
b) b
c) c
d) Both a and b
View Answer
Answer: b
Explanation: None.
8. Comment on the following 2 C programs.

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


2. int main()
3. {
4. int a;
Terminator21 C programming vrushabh

5. int b;
6. int c;
7. }
8.
9. #include <stdio.h> //Program 2
10. int main()
11. {
12. int a;
13. {
14. int b;
15. }
16. {
17. int c;
18. }
19. }
a) Both are same
b) Scope of c is till the end of the main function in Program 2
c) In Program 1, variables a, b and c can be used anywhere in the main
function whereas in Program 2, variables b and c can be used only inside
their respective blocks
d) None of the mentioned
View Answer
Answer: c
Explanation: None.

C Programming Questions and Answers


– Scope of a Variable – 2
This section on basic C questions focuses on “Scope of a Variable”. One shall
practice these basic interview questions to improve their C programming
skills needed for various interviews (campus interviews, walkin interviews,
company interviews), placements, entrance exams and other competitive
exams. These questions can be attempted by anyone focusing on learning C
Programming language. They can be a beginner, fresher, engineering
graduate or an experienced IT professional. Our C interview questions come
with detailed explanation of the answers which helps in better understanding
of C concepts.
Here is a listing of basic C questions on “Scope of a Variable” along with
answers, explanations and/or solutions:

1. What will be the sequence of allocation and deletion of variables in the


following C code?

advertisement
1. #include <stdio.h>
2. int main()
3. {
4. int a;
5. {
6. int b;
7. }
8. }
Terminator21 C programming vrushabh

a) a->b, a->b
b) a->b, b->a
c) b->a, a->b
d) b->a, b->a
View Answer
Answer: b
Explanation: None.
2. Array sizes are optional during array declaration by using ______ keyword.
a) auto
b) static
c) extern
d) register
View Answer
Answer: c
Explanation: None.
3. What will be the output of the following C code?

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

1. #include <stdio.h>
2. int x = 5;
3. void main()
4. {
5. int x = 3;
6. m();
7. printf("%d", x);
8. }
9. void m()
10. {
11. x = 8;
12. n();
13. }
14. void n()
15. {
16. printf("%d", x);
Terminator21 C programming vrushabh

17. }
a) 8 3
b) 3 8
c) 8 5
d) 5 3
View Answer
Answer: a
Explanation: None.
advertisement

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

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

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

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


Terminator21 C programming vrushabh

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

C Programming Questions and Answers


– Static Variables – 1
Sanfoundry’s 1000+ MCQs on C helps anyone preparing for placement in
Oracle and other companies. Anyone looking for Oracle placement papers
should practice these 1000+ questions continuously for 2-3 months, thereby
ensuring a top position in placements.
Here is a listing of C questions and puzzles on “Static Variables” along with
answers, explanations and/or solutions:

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

advertisement
1. #include <stdio.h>
2. void main()
3. {
4. m();
5. m();
6. }
7. void m()
8. {
9. static int x = 5;
10. x++;
11. printf("%d", x);
12. }
a) 6 7
b) 6 6
c) 5 5
d) 5 6
View Answer
Answer: a
Explanation: None.
2. What will be the output of the following C code?

1. #include <stdio.h>
Terminator21 C programming vrushabh

2. void main()
3. {
4. static int x;
5. printf("x is %d", x);
6. }
a) 0
b) 1
c) Junk value
d) Run time error
View Answer
Answer: a
Explanation: None.
Join Sanfoundry@YouTube

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

1. #include <stdio.h>
2. static int x;
3. void main()
4. {
5. int x;
6. printf("x is %d", x);
7. }
a) 0
b) Junkvalue
c) Run time error
d) Nothing
View Answer
Answer: b
Explanation: None.
4. What will be the output of the following C code?

advertisement
1. #include <stdio.h>
2. void main()
3. {
4. static double x;
5. int x;
6. printf("x is %d", x);
7. }
a) Nothing
b) 0
c) Compile time error
d) Junkvalue
View Answer
Answer: c
Explanation: None.
5. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
Terminator21 C programming vrushabh

3. {
4. static int x;
5. if (x++ < 2)
6. main();
7. }
a) Infinite calls to main
b) Run time error
c) Varies
d) main is called twice
View Answer
Answer: d
Explanation: None.
advertisement

6. 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.
7. Which of the following cannot be static in C?
a) Variables
b) Functions
c) Structures
d) None of the mentioned
View Answer
Answer: d
Explanation: None.

C Programming Questions and Answers


– Static Variables – 2
Sanfoundry’s 1000+ MCQs on C helps anyone preparing for placement in
Cisco and other companies. Anyone looking for Cisco placement papers
should practice these 1000+ questions continuously for 2-3 months, thereby
ensuring a top position in placements.
Here is a listing of C programming questions on “Static Variables” along with
answers, explanations and/or solutions:

1. What will be the output of the following C code if these two files namely
test.c and test1.c are linked and run?

advertisement
1. -------file test.c-------
2. #include <stdio.h>
3. #include ""test.h""
4. int main()
Terminator21 C programming vrushabh

5. {
6. i = 10;
7. printf(""%d "", i);
8. foo();
9. }
10.
11. -----file test1.c------
12. #include <stdio.h>
13. #include ""test.h""
14. int foo()
15. {
16. printf(""%d\n"", i);
17. }
18.
19. -----file test.h-----
20. #include <stdio.h>
21. #include <stdlib.h>
22. static int i;
a) 10 0
b) 0 0
c) 10 10
d) Compilation Error
View Answer
Answer: a
Explanation: None.
2. Functions have static qualifier for its declaration by default.
a) True
b) False
c) Depends on the compiler
d) Depends on the standard
View Answer
Answer: b
Explanation: None.
3. Is initialisation mandatory for local static variables?
a) Yes
b) No
c) Depends on the compiler
d) Depends on the standard
View Answer
Answer: b
Explanation: None.
advertisement

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

1. #include <stdio.h>
2. int main()
3. {
4. foo();
5. foo();
6. }
7. void foo()
8. {
Terminator21 C programming vrushabh

9. int i = 11;
10. printf("%d ", i);
11. static int j = 12;
12. j = j + 1;
13. printf("%d\n", j);
14. }
a) 11 12 11 12
b) 11 13 11 14
c) 11 12 11 13
d) Compile time error
View Answer
Answer: b
Explanation: None.
5. Assignment statements assigning value to local static variables are
executed only once.
a) True
b) False
c) Depends on the code
d) None of the mentioned
View Answer
Answer: b
Explanation: None.
advertisement

6. What is the format identifier for “static a = 20.5;”?


a) %s
b) %d
c) %f
d) Illegal declaration due to absence of data type
View Answer
Answer: b
Explanation: None.
7. Which of the following is true for the static variable?
a) It can be called from another function
b) It exists even after the function ends
c) It can be modified in another function by sending it as a parameter
d) All of the mentioned
View Answer
Answer: b
Explanation: None.
8. What will be the output of the following C code?

1. #include <stdio.h>
2. void func();
3. int main()
4. {
5. static int b = 20;
6. func();
7. }
8. void func()
9. {
Terminator21 C programming vrushabh

10. static int b;


11. printf("%d", b);
12. }
a) Output will be 0
b) Output will be 20
c) Output will be a garbage value
d) Compile time error due to redeclaration of static variable
View Answer
Answer: a
Explanation: None.

C Programming Questions and Answers


– Register Variables – 1
This section on C interview questions and answers focuses on “Register
Variables”. One shall practice these interview questions to improve their C
programming skills needed for various interviews (campus interviews, walkin
interviews, company interviews), placements, entrance exams and other
competitive exams. These questions can be attempted by anyone focusing
on learning C Programming language. They can be a beginner, fresher,
engineering graduate or an experienced IT professional. Our C Interview
questions come with detailed explanation of the answers which helps in
better understanding of C concepts.
Here is a listing of C interview questions on “Register Variables” along with
answers, explanations and/or solutions:

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

advertisement
1. #include <stdio.h>
2. int main()
3. {
4. register int i = 10;
5. int *p = &i;
6. *p = 11;
7. printf("%d %d\n", i, *p);
8. }
a) Depends on whether i is actually stored in machine register
b) 10 10
c) 11 11
d) Compile time error
View Answer
2. register keyword mandates compiler to place it in machine register.
a) True
b) False
c) Depends on the standard
d) None of the mentioned
View Answer
Answer: b
Explanation: None.
Terminator21 C programming vrushabh

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

advertisement
1. #include <stdio.h>
2. int main()
3. {
4. register static int i = 10;
5. i = 11;
6. printf("%d\n", i);
7. }
a) 10
b) Compile time error
c) Undefined behaviour
d) 11
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. register auto int i = 10;
5. i = 11;
6. printf("%d\n", i);
7. }
a) 10
b) Compile time error
c) Undefined behaviour
d) 11
View Answer
Answer: b
Explanation: None.
advertisement

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

1. #include <stdio.h>
2. int main()
3. {
4. register const int i = 10;
5. i = 11;
6. printf("%d\n", i);
7. }
a) 10
b) Compile time error
c) Undefined behaviour
d) 11
View Answer
Answer: b
Explanation: None.
Terminator21 C programming vrushabh

6. Register storage class can be specified to global variables.


a) True
b) False
c) Depends on the compiler
d) Depends on the standard
View Answer
Answer: b
Explanation: None.
7. Which among the following is wrong for “register int a;”?
a) Compiler generally ignores the request
b) You cannot take the address of this variable
c) Access time to a is critical
d) None of the mentioned
View Answer
Answer: d
Explanation: None.
advertisement

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

1. #include <stdio.h>
2. void main()
3. {
4. register int x = 5;
5. m();
6. printf("x is %d", x);
7. }
8. void m()
9. {
10. x++;
11. }
a) 6
b) 5
c) Junk value
d) Compile time error
View Answer
Answer: d
Explanation: None.

C Programming Questions and Answers


– Register Variables – 2
Sanfoundry’s 1000+ MCQs on C helps anyone preparing for placement in
Samsung and other companies. Anyone looking for Samsung placement
papers should practice these 1000+ questions continuously for 2-3 months,
thereby ensuring a top position in placements.
Here is a listing of C interview questions on “Register Variables” along with
answers, explanations and/or solutions:
Terminator21 C programming vrushabh

1. When compiler accepts the request to use the variable as a register?


a) It is stored in CPU
b) It is stored in cache memory
c) It is stored in main memory
d) It is stored in secondary memory
View Answer
Answer: a
Explanation: None.
advertisement

2. Which data type can be stored in register?


a) int
b) long
c) float
d) all of the mentioned
View Answer
Answer: d
Explanation: None.
3. Which of the following operation is not possible in a register variable?
a) Reading the value into a register variable
b) Copy the value from a memory variable
c) Global declaration of register variable
d) All of the mentioned
View Answer
Answer: d
Explanation: None.
4. Which among the following is the correct syntax to declare a static variable
register?
a) static register a;
b) register static a;
c) Both static register a; and register static a;
d) We cannot use static and register together
View Answer
Answer: d
Explanation: None.
5. Register variables reside in ________
a) stack
b) registers
c) heap
d) main memory
View Answer
Answer: b
Explanation: None.
advertisement

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

1. #include <stdio.h>
Terminator21 C programming vrushabh

2. void main()
3. {
4. register int x = 0;
5. if (x < 2)
6. {
7. x++;
8. main();
9. }
10. }
a) Segmentation fault
b) main is called twice
c) main is called once
d) main is called thrice
View Answer
Answer: a
Explanation: None.
7. What will be the output of the following C code?

advertisement
1. #include <stdio.h>
2. void main()
3. {
4. register int x;
5. printf("%d", x);
6. }
a) 0
b) Junk value
c) Compile time error
d) Nothing
View Answer
Answer: b
Explanation: None.
8. What will be the output of the following C code?

1. #include <stdio.h>
2. register int x;
3. void main()
4. {
5. printf("%d", x);
6. }
a) Varies
b) 0
c) Junk value
d) Compile time error
View Answer
Answer: d
Explanation: None.
Terminator21 C programming vrushabh

C Programming Questions and Answers


– C-Preprocessor – 1
This section on C questions and puzzles focuses on “C Preprocessor”. One
shall practice these questions and puzzles to improve their C programming
skills needed for various interviews (campus interviews, walkin interviews,
company interviews), placements, entrance exams and other competitive
exams. These programming puzzles can be attempted by anyone focusing on
learning C Programming language. They can be a beginner, fresher,
engineering graduate or an experienced IT professional. Our C questions
come with detailed explanation of the answers which helps in better
understanding of C concepts.
Here is a listing of C questions and puzzles on “C Preprocessor” along with
answers, explanations and/or solutions:

1. 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.
advertisement

2. 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.
3. 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.
4. What will be the output of the following C code?

Join Sanfoundry@YouTube
Terminator21 C programming vrushabh

1.#include <stdio.h>
2.#define foo(m, n) m * n = 10
3.int main()
4.{
5. printf("in main\n");
6.}
a) In main
b) Compilation error as lvalue is required for the expression m*n=10
c) Preprocessor error as lvalue is required for the expression m*n=10
d) None of the mentioned
View Answer
Answer: a
Explanation: Preprocessor just replaces whatever is given compiler then
checks for error at the replaced part of the code. Here it is not replaced
anywhere.
Output:
$ cc pgm1.c
$ a.out
in main
5. C preprocessor is conceptually the first step during compilation.
a) True
b) False
c) Depends on the compiler
d) Depends on the standard
View Answer
Answer: a
Explanation: None.
advertisement

6. Preprocessor feature that supply line numbers and filenames to compiler is


called?
a) Selective inclusion
b) macro substitution
c) Concatenation
d) Line control
View Answer
Answer: d
Explanation: None.
7. #include <somefile.h> are _______ files and #include “somefile.h” ________
files.
a) Library, Library
b) Library, user-created header
c) User-created header, library
d) They can include all types of file
View Answer
Answer: d
Explanation: Both of these statement can be used to select any file.
8. What is a preprocessor?
a) That processes its input data to produce output that is used as input to
Terminator21 C programming vrushabh

another program
b) That is nothing but a loader
c) That links various source files
d) All of the mentioned
View Answer
Answer: a
Explanation: A preprocessor is a program that processes its input data to
produce output that is used as input to another program.

C Programming Questions and Answers


– C-Preprocessor – 2
Sanfoundry’s 1000+ MCQs on C helps anyone preparing for placement in Dell
and other companies. Anyone looking for Dell placement papers should
practice these 1000+ questions continuously for 2-3 months, thereby
ensuring a top position in placements.
Here is a listing of C interview questions on “C Preprocessor” along with
answers, explanations and/or solutions:

1. Which of the following are C preprocessors?


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

2. #include statement must be written __________


a) Before main()
b) Before any scanf/printf
c) After main()
d) It can be written anywhere
View Answer
Answer: b
Explanation: Using these directives before main() improves readability.
3. #pragma exit is primarily used for?
a) Checking memory leaks after exiting the program
b) Informing Operating System that program has terminated
c) Running a function at exiting the program
d) No such preprocessor exist
View Answer
Answer: c
Explanation: It is primarily used for running a function upon exiting the
program.
4. What will be the output of the following C code?
Terminator21 C programming vrushabh

advertisement
1. #include <stdio.h>
2. int main()
3. {
4. int one = 1, two = 2;
5. #ifdef next
6. one = 2;
7. two = 1;
8. #endif
9. printf("%d, %d", one, two);
10. }
a) 1, 1
b) 1, 2
c) 2, 1
d) 2, 2
View Answer
Answer: b
Explanation: None.
5. The C-preprocessors are specified with _________symbol.
a) #
b) $
c) ” ”
d) &
View Answer
Answer: a
Explanation: The C-preprocessors are specified with # symbol.
advertisement

6. What is #include directive?


a) Tells the preprocessor to grab the text of a file and place it directly into the
current file
b) Statements are not typically placed at the top of a program
c) All of the mentioned
d) None of the mentioned
View Answer
Answer: a
Explanation: The #include directive tells the preprocessor to grab the text of
a file and place it directly into the current file and are statements are
typically placed at the top of a program.
7. The preprocessor provides the ability for _______________
a) The inclusion of header files
b) The inclusion of macro expansions
c) Conditional compilation and line control
d) All of the mentioned
View Answer
Answer: d
Explanation: The preprocessor provides the ability for the inclusion of header
files, macro expansions, conditional compilation, and line control.
Terminator21 C programming vrushabh

8. If #include is used with file name in angular brackets.


a) The file is searched for in the standard compiler include paths
b) The search path is expanded to include the current source directory
c) The search path will expand
d) None of the mentioned
View Answer
Answer: a
Explanation: With the #include, if the filename is enclosed within angle
brackets, the file is searched for in the standard compiler include paths.

C Programming Questions and Answers


– Conditional Inclusion – 1
Sanfoundry’s 1000+ MCQs on C helps anyone preparing for placement in
MIndtree and other companies. Anyone looking for Mindtree placement
papers should practice these 1000+ questions continuously for 2-3 months,
thereby ensuring a top position in placements.
Here is a listing of C interview questions on “Conditional Inclusion” along with
answers, explanations and/or solutions:

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

advertisement
1. #include <stdio.h>
2. #define SYSTEM 20
3. int main()
4. {
5. int a = 20;
6. #if SYSTEM == a
7. printf("HELLO ");
8. #endif
9. #if SYSTEM == 20
10. printf("WORLD\n");
11. #endif
12. }
a) HELLO
b) WORLD
c) HELLO WORLD
d) No Output
View Answer
Answer: b
Explanation: None.
2. What will be the output of the following C code?

1. #include <stdio.h>
2. #define Cprog
3. int main()
4. {
5. int a = 2;
6. #ifdef Cprog
Terminator21 C programming vrushabh

7. a = 1;
8. printf("%d", Cprog);
9. }
a) No output on execution
b) Output as 1
c) Output as 2
d) Compile time error
View Answer
Answer: d
Explanation: None.
Join Sanfoundry@YouTube

3. The “else if” in conditional inclusion is written by?


a) #else if
b) #elseif
c) #elsif
d) #elif
View Answer
Answer: d
Explanation: None.
4. What will be the output of the following C code?

1. #include <stdio.h>
2. #define COLD
3. int main()
4. {
5. #ifdef COLD
6. printf("COLD\t");
7. #undef COLD
8. #endif
9. #ifdef COLD
10. printf("HOT\t");
11. #endif
12. }
a) HOT
b) COLD
c) COLD HOT
d) No Output
View Answer
Answer: b
Explanation: None.
advertisement

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


a)

#if
#else
#endif
b)

#if
Terminator21 C programming vrushabh

#elif
#endif
c)

advertisement
#if
#if
#endif
d)

#if
#undef
#endif
View Answer

Answer: c
Explanation: None.

6. In a conditional inclusion, if the condition that comes after the if is true,


then what will happen during compilation?
a) Then the code up to the following #else or #elif or #endif is compiled
b) Then the code up to the following #endif is compiled even if #else or #elif
is present
c) Then the code up to the following #eliif is compiled
d) None of the mentioned
View Answer
Answer: a
Explanation: None.
7. Conditional inclusion can be used for ___________
a) Preventing multiple declarations of a variable
b) Check for existence of a variable and doing something if it exists
c) Preventing multiple declarations of same function
d) All of the mentioned
View Answer
Answer: d
Explanation: None.
8. The #elif directive cannot appear after the preprocessor #else directive.
a) True
b) False
View Answer
Answer: a
Explanation: None.
Terminator21 C programming vrushabh

C Programming Questions and Answers


– Conditional Inclusion – 2
This section on C interview questions and answers focuses on “Conditional
Inclusion”. One shall practice these interview questions to improve their C
programming skills needed for various interviews (campus interviews, walkin
interviews, company interviews), placements, entrance exams and other
competitive exams. These questions can be attempted by anyone focusing
on learning C Programming language. They can be a beginner, fresher,
engineering graduate or an experienced IT professional. Our C Interview
questions come with detailed explanation of the answers which helps in
better understanding of C concepts.
Here is a listing of C interview questions on “Conditional Inclusion” along with
answers, explanations and/or solutions:

1. For each #if, #ifdef, and #ifndef directive.


a) There are zero or more #elif directives
b) Zero or one #else directive
c) One matching #endif directive
d) All of the mentioned
View Answer
Answer: d
Explanation: None.
advertisement

2. The #else directive is used for _________


a) Conditionally include source text if the previous #if, #ifdef, #ifndef, or
#elif test fails
b) Conditionally include source text if a macro name is not defined
c) Conditionally include source text if a macro name is defined
d) Ending conditional text
View Answer
Answer: a
Explanation: None.
3. What will be the output of the following C code?

1. #include <stdio.h>
2. #define MIN 0
3. #if MIN
4. #define MAX 10
5. #endif
6. int main()
7. {
8. printf("%d %d\n", MAX, MIN);
9. return 0;
10. }
a) 10 0
b) Compile time error
c) Undefined behaviour
Terminator21 C programming vrushabh

d) None of the mentioned


View Answer
Answer: b
Explanation: None.
advertisement

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

1. #include <stdio.h>
2. #define MIN 0
3. #ifdef MIN
4. #define MAX 10
5. #endif
6. int main()
7. {
8. printf("%d %d\n", MAX, MIN);
9. return 0;
10. }
a) 10 0
b) Compile time error
c) Undefined behaviour
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. #define MIN 0
3. #if defined(MIN) + defined(MAX)
4. #define MAX 10
5. #endif
6. int main()
7. {
8. printf("%d %d\n", MAX, MIN);
9. return 0;
10. }
a) 10 0
b) Compile time error
c) Undefined behaviour
d) Somegarbagevalue 0
View Answer
Answer: a
Explanation: None.
advertisement

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

1. #include <stdio.h>
2. #define MIN 0
3. #if defined(MIN) - (!defined(MAX))
4. #define MAX 10
5. #endif
Terminator21 C programming vrushabh

6. int main()
7. {
8. printf("%d %d\n", MAX, MIN);
9. return 0;
10. }
a) 10 0
b) Compile time error
c) Undefined behaviour
d) Somegarbagevalue 0
View Answer
Answer: b
Explanation: None.
7. What will be the output of the following C code?

advertisement
1. #include <stdio.h>
2. #define MIN 0
3. #ifdef(MIN)
4. #define MAX 10
5. #endif
6. int main()
7. {
8. printf("%d %d\n", MAX, MIN);
9. return 0;
10. }
a) 10 0
b) Compile time error
c) Run time error
d) Preprocessor error
View Answer
Answer: d
Explanation: None.
8. What will be the output of the following C code?

1. #include <stdio.h>
2. #define MIN 0);
3. #ifdef MIN
4. #define MAX 10
5. #endif
6. int main()
7. {
8. printf("%d %d\n", MAX, MIN
9. return 0;
10. }
a) 10 0
b) Compile time error due to illegal syntax for printf
c) Undefined behaviour
d) Compile time error due to illegal MIN value
View Answer
Answer: a
Explanation: None.
Terminator21 C programming vrushabh

4. Structures, Unions and Bit-Fields in C


The section contains questions and answers on structures basics, functions,
arrays of structures, pointer to structires, self referential structures, table
lookup, typedefs, unions and bit fields.

C Programming Questions and Answers


– Basics of Structures – 1
This section on online C quiz focuses on “Basics of Structures”. One shall
practice these online quizzes to improve their C programming skills needed
for various interviews (campus interviews, walkin interviews, company
interviews), placements, entrance exams and other competitive exams.
These questions can be attempted by anyone focusing on learning C
Programming language. They can be a beginner, fresher, engineering
graduate or an experienced IT professional. Our C quiz comes with detailed
explanation of the answers which helps in better understanding of C
concepts.
Here is a listing of online C quiz on “Basics of Structures” along with answers,
explanations and/or solutions:

1. Which of the following are themselves a collection of different data types?


a) string
b) structures
c) char
d) all of the mentioned
View Answer
Answer: b
Explanation: None.
advertisement

2. User-defined data type can be derived by___________


a) struct
b) enum
Terminator21 C programming vrushabh

c) typedef
d) all of the mentioned
View Answer
Answer: d
Explanation: None.
3. Which operator connects the structure name to its member name?
a) –
b) <-
c) .
d) Both <- and .
View Answer
Answer: c
Explanation: None.
4. Which of the following cannot be a structure member?
a) Another structure
b) Function
c) Array
d) None of the mentioned
View Answer
Answer: b
Explanation: None.
5. Which of the following structure declaration will throw an error?
a)

struct temp{}s;
main(){}
b)

advertisement
struct temp{};
struct temp s;
main(){}
c)

struct temp s;
struct temp{};
main(){}
d) None 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. struct student
3. {
4. int no;
5. char name[20];
Terminator21 C programming vrushabh

6. }
7. void main()
8. {
9. struct student s;
10. s.no = 8;
11. printf("hello");
12. }
a) Compile time error
b) Nothing
c) hello
d) Varies
View Answer
Answer: a
Explanation: None.
advertisement

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

1. #include <stdio.h>
2. struct student
3. {
4. int no = 5;
5. char name[20];
6. };
7. void main()
8. {
9. struct student s;
10. s.no = 8;
11. printf("hello");
12. }
a) Nothing
b) Compile time error
c) hello
d) Varies
View Answer
Answer: b
Explanation: None.
8. What will be the output of the following C code?

1. #include <stdio.h>
2. struct student
3. {
4. int no;
5. char name[20];
6. };
7. void main()
8. {
9. student s;
10. s.no = 8;
11. printf("hello");
12. }
a) Nothing
b) hello
c) Compile time error
Terminator21 C programming vrushabh

d) Varies
View Answer
Answer: c
Explanation: None.
9. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. struct student
5. {
6. int no;
7. char name[20];
8. };
9. struct student s;
10. s.no = 8;
11. printf("%d", s.no);
12. }
a) Nothing
b) Compile time error
c) Junk
d) 8
View Answer
Answer: d
Explanation: None.
advertisement

10. Can the following C code be compiled successfully?

1. #include <stdio.h>
2. struct p
3. {
4. int k;
5. char c;
6. float f;
7. };
8. int main()
9. {
10. struct p x = {.c = 97, .f = 3, .k = 1};
11. printf("%f\n", x.f);
12. }
a) Yes
b) No
c) Depends on the standard
d) Depends on the platform
View Answer
Answer: c
Explanation: None.
Terminator21 C programming vrushabh

C Programming Questions and Answers


– Basics of Structures – 2
Sanfoundry’s 1000+ Interview Questions & Answers on C helps anyone
preparing for Juniper Networks and other companies C interviews. One should
practice these 1000+ interview questions and answers continuously for 2-3
months to clear Juniper interviews on C Programming language.
Here is a listing of C multiple choice questions on “Basics of Structures” along
with answers, explanations and/or solutions:

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

advertisement
1. #include <stdio.h>
2. void main()
3. {
4. struct student
5. {
6. int no;
7. char name[20];
8. };
9. struct student s;
10. no = 8;
11. printf("%d", no);
12. }
a) Nothing
b) Compile time error
c) Junk
d) 8
View Answer
Answer: b
Explanation: None.
2. How many bytes in memory taken by the following C structure?

1. #include <stdio.h>
2. struct test
3. {
4. int k;
5. char c;
6. };
a) Multiple of integer size
b) integer size+character size
c) Depends on the platform
d) Multiple of word size
View Answer
Answer: a
Explanation: None.
3. What will be the output of the following C code?
Terminator21 C programming vrushabh

Join Sanfoundry@YouTube
1. #include <stdio.h>
2. struct
3. {
4. int k;
5. char c;
6. };
7. int main()
8. {
9. struct p;
10. p.k = 10;
11. printf("%d\n", p.k);
12. }
a) Compile time error
b) 10
c) Undefined behaviour
d) Segmentation fault
View Answer
Answer: a
Explanation: None.
4. What will be the output of the following C code?

1. #include <stdio.h>
2. struct
3. {
4. int k;
5. char c;
6. } p;
7. int p = 10;
8. int main()
9. {
10. p.k = 10;
11. printf("%d %d\n", p.k, p);
12. }
a) Compile time error
b) 10 10
c) Depends on the standard
d) Depends on the compiler
View Answer
Answer: a
Explanation: None.
5. What will be the output of the following C code?

1. #include <stdio.h>
2. struct p
3. {
4. int k;
5. char c;
6. };
7. int p = 10;
8. int main()
9. {
10. struct p x;
11. x.k = 10;
Terminator21 C programming vrushabh

12. printf("%d %d\n", x.k, p);


13. }
a) Compile time error
b) 10 10
c) Depends on the standard
d) Depends on the compiler
View Answer
Answer: b
Explanation: None.
advertisement

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

1. #include <stdio.h>
2. struct p
3. {
4. int k;
5. char c;
6. float f;
7. };
8. int p = 10;
9. int main()
10. {
11. struct p x = {1, 97};
12. printf("%f %d\n", x.f, p);
13. }
a) Compile time error
b) 0.000000 10
c) Somegarbage value 10
d) 0 10
View Answer
Answer: b
Explanation: None.
7. What will be the output of the following C code according to C99 standard?

1. #include <stdio.h>
2. struct p
3. {
4. int k;
5. char c;
6. float f;
7. };
8. int main()
9. {
10. struct p x = {.c = 97, .f = 3, .k = 1};
11. printf("%f\n", x.f);
12. }
a) 3.000000
b) Compile time error
c) Undefined behaviour
d) 1.000000
View Answer
Answer: a
Explanation: None.
Terminator21 C programming vrushabh

8. What will be the output of the following C code according to C99 standard?

advertisement
1. #include <stdio.h>
2. struct p
3. {
4. int k;
5. char c;
6. float f;
7. };
8. int main()
9. {
10. struct p x = {.c = 97, .k = 1, 3};
11. printf("%f \n", x.f);
12. }
a) 3.000000
b) 0.000000
c) Compile time error
d) Undefined behaviour
View Answer
Answer: b
Explanation: None.
9. What will be the output of the following C code according to C99 standard?

1. #include <stdio.h>
2. struct p
3. {
4. int k;
5. char c;
6. float f;
7. };
8. int main()
9. {
10. struct p x = {.c = 97};
11. printf("%f\n", x.f);
12. }
a) 0.000000
b) Somegarbagevalue
c) Compile time error
d) None of the mentioned
View Answer
Answer: a
Explanation: None.
Terminator21 C programming vrushabh

5. Input and Output in C


The section contains MCQs on standard input and output, formatted input
and output, variable length argument, file access, error handling, line input
and output, string operations, character class testing, ungetc, storage
management, mathematical functions, random number generation, file
operations, printf and scanf.

C Programming Questions and Answers


– Standard Input & Output – 1
This section on C MCQs (multiple choice questions) focuses on “Standard
Input and Output”. One shall practice these MCQs to improve their C
programming skills needed for various interviews (campus interviews, walkin
Terminator21 C programming vrushabh

interviews, company interviews), placements, entrance exams and other


competitive exams. These questions can be attempted by anyone focusing
on learning C Programming language. They can be a beginner, fresher,
engineering graduate or an experienced IT professional. Our multiple choice
questions come with detailed explanation of the answers which helps in
better understanding of C concepts.
Here is a listing of C multiple choice questions on “Standard Input and
Output” along with answers, explanations and/or solutions:

1. Which among the following is the odd one out?


a) printf
b) fprintf
c) putchar
d) scanf
View Answer
Answer: d
Explanation: None.
advertisement

2. For a typical program, the input is taken using _________


a) scanf
b) Files
c) Command-line
d) All of the mentioned
View Answer
Answer: d
Explanation: None.
3. What does the following command line signify?

prog1|prog2
a) It runs prog1 first, prog2 second
b) It runs prog2 first, prog1 second
c) It runs both the programs, pipes output of prog1 to input of prog2
d) It runs both the programs, pipes output of prog2 to input of prog1
View Answer
Answer: c
Explanation: None.
advertisement

4. What is the default return-type of getchar()?


a) char
b) int
c) char *
d) reading character doesn’t require a return-type
View Answer
Answer: b
Explanation: None.
Terminator21 C programming vrushabh

5. What is the value of EOF?


a) -1
b) 0
c) 1
d) 10
View Answer
Answer: a
Explanation: None.
advertisement

6. What is the use of getchar()?


a) The next input character each time it is called
b) EOF when it encounters end of file
c) The next input character each time it is called EOF when it encounters end
of file
d) None of the mentioned
View Answer
Answer: c
Explanation: None.
7. Which of the following statement is true?
a) The symbolic constant EOF is defined in <stdio.h>
b) The value is -1
c) The symbolic constant EOF is defined in <stdio.h> & value is -1
d) Only value is -1
View Answer
Answer: c
Explanation: None.
8. What is the return value of putchar()?
a) The character written
b) EOF if an error occurs
c) Nothing
d) Both character written & EOF if an error occurs
View Answer
Answer: d
Explanation: None.

C Programming Questions and Answers


– Standard Input & Output – 2
Sanfoundry’s 1000+ Interview Questions & Answers on C helps anyone
preparing for Motorola and other companies C interviews. One should
practice these 1000+ interview questions and answers continuously for 2-3
months to clear Motorola interviews on C Programming language.
Here is a listing of C interview questions on “Standard Input & Output” along
with answers, explanations and/or solutions:
Terminator21 C programming vrushabh

1. Which is true about function tolower?


a) The function tolower is defined in <ctype.h>
b) Converts an uppercase letter to lowercase
c) Returns other characters untouched
d) None of the mentioned
View Answer
Answer: d
Explanation: None.
advertisement

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

1. #include <stdio.h>
2. int main()
3. {
4. char c = '�';
5. putchar(c);
6. }
a) Compile time error
b) Nothing
c) 0
d) Undefined behaviour
View Answer
Answer: b
Explanation: None.
3. putchar(c) function/macro always outputs character c to the __________
a) screen
b) standard output
c) depends on the compiler
d) depends on the standard
View Answer
Answer: b
Explanation: None.
advertisement

4. What will be the output of the following C code if following commands are
used to run (considering myfile exists)?

1. gcc -otest test.c


2. ./test < myfile
3.
4. #include <stdio.h>
5. int main()
6. {
7. char c = 'd';
8. putchar(c);
9. }
a) Compile time error (after first command)
b) d in the myfile file
c) d on the screen
Terminator21 C programming vrushabh

d) Undefined behaviour
View Answer
Answer: c
Explanation: None.
5. What will be the output of the following C code if following commands are
used to run (considering myfile exists)?

advertisement
1. gcc -otest test.c
2. ./test > myfile
3.
4. #include <stdio.h>
5. int main(int argc, char **argv)
6. {
7. char c = 'd';
8. putchar(c);
9. printf(" %d\n", argc);
10. }
a) d 2 in myfile
b) d 1 in myfile
c) d in myfile and 1 in screen
d) d in myfile and 2 in screen
View Answer
Answer: b
Explanation: None.
6. What will be the output of the following C code if following commands are
used to run and if myfile does not exist?

1. gcc -o test test.c


2. ./test > myfile
3.
4. #include <stdio.h>
5. int main(int argc, char **argv)
6. {
7. char c = 'd';
8. putchar(c);
9. printf(" %d\n", argc);
10. }
a) d 2 in myfile
b) d 1 in myfile
c) Depends on the system
d) Depends on the standard
View Answer
Answer: b
Explanation: None.
advertisement

7. The statement prog < infile causes _________


a) prog to read characters from infile
b) prog to write characters to infile
c) infile to read characters from prog instead
Terminator21 C programming vrushabh

d) nothing
View Answer
Answer: a
Explanation: None.

C Programming Questions and Answers


– Formatted Output – 1
This section on advanced C interview questions focuses on “Formatted
Output”. One shall practice these advanced C questions to improve their C
programming skills needed for various interviews (campus interviews, walkin
interviews, company interviews), placements, entrance exams and other
competitive exams. These questions can be attempted by anyone focusing
on learning C Programming language. They can be a beginner, fresher,
engineering graduate or an experienced IT professional. Our advanced C
questions come with detailed explanation of the answers which helps in
better understanding of C concepts.
Here is a listing of advanced C interview questions on “Formatted Output”
along with answers, explanations and/or solutions::”

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

advertisement
1. #include <stdio.h>
2. int main()
3. {
4. int i = 10, j = 2;
5. printf("%d\n", printf("%d %d ", i, j));
6. }
a) Compile time error
b) 10 2 4
c) 10 2 2
d) 10 2 5
View Answer
Answer: d
Explanation: None.
2. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 10, j = 3;
5. printf("%d %d %d", i, j);
6. }
a) Compile time error
b) 10 3
c) 10 3 some garbage value
d) Undefined behaviour
View Answer
Terminator21 C programming vrushabh

Answer: c
Explanation: None.
advertisement

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

1. #include <stdio.h>
2. int main()
3. {
4. int i = 10, j = 3, k = 3;
5. printf("%d %d ", i, j, k);
6. }
a) Compile time error
b) 10 3 3
c) 10 3
d) 10 3 somegarbage value
View Answer
Answer: c
Explanation: None.
4. What will be the output of the following C code?

advertisement
1. #include <stdio.h>
2. int main()
3. {
4. char *s = "myworld";
5. int i = 9;
6. printf("%*s", i, s);
7. }
a) myworld
b) myworld(note: spaces to the left of myworld)
c) myworld (note:followed by two spaces after myworld)
d) Undefined
View Answer
Answer: b
Explanation: None.
5. What will be the output of the following C code?

1. #include <stdio.h>
2. int main(int argc, char** argv)
3. {
4. char *s = "myworld";
5. int i = 3;
6. printf("%10.*s", i, s);
7. }
a) myw(note:7 spaces before myw)
b) myworld(note:2 spaces before myworld)
c) myworld (note:2 spaces after myworld)
d) myw(note:6 spaces after myw)
View Answer
Terminator21 C programming vrushabh

Answer: a
Explanation: In the format represented by “%10.*s”, the width of the string
will be 10 spaces which is aligned to the right, by default. Since we have
asterisk (*) after the precision dot (.), the value of precision will be the value
stored in the variable i. The value of i is 3, so this signifies max length of the
string as 3 characters. So, the final formatted output will be a 10 character
output with 3 characters “myw” printed with right alignment and the 1st 7
characters will be simply space characters.
advertisement

6. What is the difference between %e and %g?


a) %e output formatting depends on the argument and %g always formats in
the format [-]m.dddddd or [-]m.dddddE[+|-]xx where no.of ds are optional
b) %e always formats in the format [-]m.dddddd or [-]m.dddddE[+|-]xx where
no.of ds are optional and output formatting depends on the argument
c) No differences
d) Depends on the standard
View Answer
Answer: b
Explanation: None.
7. Escape sequences are prefixed with ________
a) %
b) /
c) ”
d) None of the mentioned
View Answer
Answer: d
Explanation: None.
8. What is the purpose of sprintf?
a) It prints the data into stdout
b) It writes the formatted data into a string
c) It writes the formatted data into a file
d) None of the mentioned
View Answer
Answer: b
Explanation: None.
9. The syntax to print a % using printf statement can be done by ________
a) %
b) \%
c) ‘%’
d) %%
View Answer
Answer: d
Explanation: None.
Terminator21 C programming vrushabh

C Programming Questions and Answers


– Formatted Output – 2
Sanfoundry’s 1000+ Interview Questions & Answers on C helps anyone
preparing for Qlogic and other companies C interviews. One should practice
these 1000+ interview questions and answers continuously for 2-3 months to
clear Qlogic interviews on C Programming language.
Here is a listing of advanced C questions on “Formatted Output” along with
answers, explanations and/or solutions:

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

printf(“%10s”, state);

advertisement

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.
2. What are the Properties of the first argument of a printf() functions?
a) It is defined by a user
b) It keeps the record of the types of arguments that will follow
c) There may no be first argument
d) None of the mentioned
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 = 10, j = 2;
5. printf("%d\n", printf("%d %d ", i, j));
6. }
a) Compile time error
b) 10 2 4
c) 10 2 2
d) 10 2 5
View Answer
Answer: d
Explanation: None.
advertisement
Terminator21 C programming vrushabh

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

1. #include <stdio.h>
2. int main()
3. {
4. int i = 10, j = 3;
5. printf("%d %d %d", i, j);
6. }
a) Compile time error
b) 10 3
c) 10 3 some garbage value
d) Undefined behaviour
View Answer
Answer: c
Explanation: None.
5. What will be the output of the following C code?

advertisement
1. #include <stdio.h>
2. int main()
3. {
4. int i = 10, j = 3, k = 3;
5. printf("%d %d ", i, j, k);
6. }
a) Compile time error
b) 10 3 3
c) 10 3
d) 10 3 somegarbage value
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. char *s = "myworld";
5. int i = 9;
6. printf("%*s", i, s);
7. }
a) myworld
b) myworld(note: spaces to the left of myworld)
c) myworld (note:followed by two spaces after myworld)
d) Undefined
View Answer
Answer: b
Explanation: None.
advertisement

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


Terminator21 C programming vrushabh

1. #include <stdio.h>
2. int main(int argc, char **argv)
3. {
4. char *s = "myworld";
5. int i = 3;
6. printf("%10.*s", i, s);
7. }
a) myw(note:7 spaces before myw)
b) myworld(note:2 spaces before myworld)
c) myworld (note:2 spaces after myworld)
d) myw(note:6 spaces after myworld)
View Answer
Answer: a
Explanation: In the format represented by “%10.*s”, the width of the string
will be 10 spaces which is aligned to the right, by default. Since we have
asterisk (*) after the precision dot (.), the value of precision will be the value
stored in the variable i. The value of i is 3, so this signifies max length of the
string as 3 characters. So, the final formatted output will be a 10 character
output with 3 characters “myw” printed with right alignment and the 1st 7
characters will be simply space characters.
8. What is the difference between %e and %g?
a) %e output formatting depends on the argument and %g always formats in
the format [-]m.dddddd or [-]m.dddddE[+|-]xx where no.of ds are optional
b) %e always formats in the format [-]m.dddddd or [-]m.dddddE[+|-]xx where
no.of ds are optional and output formatting depends on the argument
c) No differences
d) Depends on the standard
View Answer
Answer: b
Explanation: None.

6. Floating Point & Sizeof Operator in C


The section contains multiple choice questions and answers on float datatype
and sizeof keyword.

C Programming Questions and Answers


– Float Datatype – 1
This section on C interview questions and answers focuses on “Float
Datatype”. One shall practice these interview questions to improve their C
programming skills needed for various interviews (campus interviews, walkin
interviews, company interviews), placements, entrance exams and other
competitive exams. These questions can be attempted by anyone focusing
on learning C Programming language. They can be a beginner, fresher,
engineering graduate or an experienced IT professional. Our C Interview
Terminator21 C programming vrushabh

questions come with detailed explanation of the answers which helps in


better understanding of C concepts.
Here is a listing of C interview questions on “Float Datatype” along with
answers, explanations and/or solutions:

1. How many digits are present after the decimal in float value?
a) 1
b) 3
c) 6
d) 16
View Answer
Answer: c
Explanation: None.
advertisement

2. Which among the following is never possible as an output for a float?


a) 3.666666
b) 3.666
c) 3
d) None of the mentioned
View Answer
Answer: d
Explanation: None.
3. In a 32-bit compiler, which 2 types have the same size?
a) char and short
b) short and int
c) int and float
d) float and double
View Answer
Answer: c
Explanation: None.
4. What is the size of float in a 32-bit compiler?
a) 1
b) 2
c) 4
d) 8
View Answer
Answer: c
Explanation: None.
advertisement

5. Loss in precision occurs for typecasting from____________


a) char to short
b) float to double
c) long to float
d) float to int
View Answer
Terminator21 C programming vrushabh

Answer: d
Explanation: None.
6. In the following C code, the union size is decided by?

1. union temp
2. {
3. char a;
4. int b;
5. float c;
6. };
a) char
b) int
c) float
d) both int and float
View Answer
Answer: d
Explanation: None.
advertisement

7. %f access specifier is used for ________


a) Strings
b) Integral types
c) Floating type
d) All of the mentioned
View Answer
Answer: c
Explanation: None.
8. Select the odd one out with respect to type?
a) char
b) int
c) long
d) float
View Answer
Answer: d
Explanation: None.

C Programming Questions and Answers


– Float Datatype – 2
This section on tricky C interview questions focuses on “Float Datatype”. One
shall practice these tricky questions to improve their C programming skills
needed for various interviews (campus interviews, walkin interviews,
company interviews), placements, entrance exams and other competitive
exams. These questions can be attempted by anyone focusing on learning C
Programming language. They can be a beginner, fresher, engineering
graduate or an experienced IT professional. Our C questions come with
detailed explanation of the answers which helps in better understanding of C
concepts.
Terminator21 C programming vrushabh

Here is a listing of C interview questions on “Float Datatype” along with


answers, explanations and/or solutions:

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

advertisement
#include <stdio.h>
printf("%.0f", 2.89);
a) 2.890000
b) 2.89
c) 2
d) 3
View Answer
Answer: d
Explanation: None.
2. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. float a = 2.455555555555;
5. printf("%f", a);
6. }
a) 2.455555
b) 2.455556
c) 2.456
d) 2.46
View Answer
Answer: a
Explanation: None.
advertisement

3. Which of the following % operation is invalid?


a) 2 % 4;
b) 2 % 4l;
c) 2 % 4f;
d) Both 2 % 4l; and 2 % 4f;
View Answer
Answer: c
Explanation: None.
4. Which data type is suitable for storing a number like?

10.0000000001

a) int
b) float
c) double
Terminator21 C programming vrushabh

d) both float and double


View Answer
Answer: c
Explanation: None.
advertisement

5. Modulus for float could be achieved by?


a) a % b
b) modulus(a, b);
c) fmod(a, b);
d) mod(a, b);
View Answer
Answer: c
Explanation: None.
6. Predict the data type of the following mathematical operation?

2*9+3/2.0

a) int
b) long
c) float
d) double
View Answer
Answer: d
Explanation: None.
7. %lf is used to display?
a) float
b) long float
c) double
d) all of the mentioned
View Answer
Answer: c
Explanation: None.

C Programming Questions and Answers


– Sizeof Keyword – 1
This section on C programming questions and answers focuses on “Sizeof”.
One shall practice these questions to improve their C programming skills
needed for various interviews (campus interviews, walkin interviews,
company interviews), placements, entrance exams and other competitive
exams. These questions can be attempted by anyone focusing on learning C
Programming language. They can be a beginner, fresher, engineering
graduate or an experienced IT professional. Our C programming questions
come with detailed explanation of the answers which helps in better
understanding of C concepts.
Terminator21 C programming vrushabh

Here is a listing of C programming questions on “Sizeof” along with answers,


explanations and/or solutions:

1. 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.
advertisement

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

#include <stdio.h>
printf("%d", sizeof('a'));
a) 1
b) 2
c) 4
d) None of the mentioned
View Answer
Answer: c
Explanation: None.
3. Size of an array can be evaluated by __________

(Assuming array declaration int a[10];)

advertisement

a) sizeof(a);
b) sizeof(*a);
c) sizeof(a[10]);
d) 10 * sizeof(a);
View Answer
Answer: a
Explanation: None.
4. What will be the output of the following C code?

1. #include <stdio.h>
2. union temp
3. {
4. char a;
5. char b;
6. int c;
7. }t;
8. int main()
9. {
10. printf("%d", sizeof(t));
11. return 0;
Terminator21 C programming vrushabh

12. }
a) 1
b) 2
c) 4
d) 6
View Answer
Answer: c
Explanation: None.
advertisement

5. 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.
6. Which among the following has the highest precedence?
a) &
b) <<
c) sizeof()
d) &&
View Answer
Answer: c
Explanation: None.
7. What is the sizeof(void) in a 32-bit C?
a) 0
b) 1
c) 2
d) 4
View Answer
Answer: b
Explanation: None.
8. What type of value does sizeof return?
a) char
b) short
c) unsigned int
d) long
View Answer
Answer: c
Explanation: None.

C Programming Questions and Answers


– Sizeof Keyword – 2
This section on C interview questions and answers focuses on “Sizeof
operator”. One shall practice these interview questions to improve their C
Terminator21 C programming vrushabh

programming skills needed for various interviews (campus interviews, walkin


interviews, company interviews), placements, entrance exams and other
competitive exams. These questions can be attempted by anyone focusing
on learning C Programming language. They can be a beginner, fresher,
engineering graduate or an experienced IT professional. Our C Interview
questions come with detailed explanation of the answers which helps in
better understanding of C concepts.
Here is a listing of C interview questions on “Sizeof operator” along with
answers, explanations and/or solutions:

1. Which among the following is never possible in C when members are


different in a structure and union?

//Let P be a structure

//Let Q be a union

advertisement

a) sizeof(P) is greater than sizeof(Q)


b) sizeof(P) is less than sizeof(Q)
c) sizeof(P) is equal to sizeof(Q)
d) none of the mentioned
View Answer
Answer: d
Explanation: None.
2. Which among the following is never possible in C when members in a
structure are the same as that in a union?

//Let P be a structure

//Let Q be a union

a) sizeof(P) is greater than sizeof(Q)


b) sizeof(P) is equal to sizeof(Q)
c) sizeof(P) is less than to sizeof(Q)
d) none of the mentioned
View Answer
Answer: c
Explanation: None.
3. What will be the size of the following C structure?

advertisement
1. #include <stdio.h>
2. struct temp
3. {
4. int a[10];
Terminator21 C programming vrushabh

5. char p;
6. };
a) 5
b) 11
c) 41
d) 44
View Answer
Answer: d
Explanation: None.
4. What will be the output of the following C code?

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

5. Which among the following statement is right?


a) sizeof(struct stemp*) > sizeof(union utemp*) > sizeof(char *)
b) sizeof(struct stemp*) < sizeof(union utemp*) < sizeof(char *)
c) sizeof(struct stemp*) = sizeof(union utemp*) = sizeof(char *)
d) the order Depends on the compiler
View Answer
Answer: c
Explanation: None.
6. What will be the output of the following C code?

1. #include <stdio.h>
2. printf("%d", sizeof(strlen("HELLOWORLD")));
a) Output, 4
b) Output, 10
c) Output, 16
d) Error, sizeof cannot evaluate size of a function
View Answer
Answer: a
Explanation: None.
7. Which of the following cannot be used inside sizeof?
a) pointers
b) functions
c) macro definition
Terminator21 C programming vrushabh

d) none of the mentioned


View Answer
Answer: d
Explanation: None.
advertisement

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

1. #include <stdio.h>
2. (sizeof double = 8, float = 4, void = 1)
3. #define PI 3.14
4. int main()
5. {
6. printf("%d", sizeof(PI));
7. }
a) Output is 8
b) Output is 4
c) Output is 1
d) Error, we can’t use sizeof on macro-definitions
View Answer
Answer: a
Explanation: None.

You might also like