0% found this document useful (0 votes)
121 views210 pages

C (MCQ+Program)

Uploaded by

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

C (MCQ+Program)

Uploaded by

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

Programming in C a) 31

b) 63
c) 12
d) 14
TOPIC 1.1 DATA TYPES, Answer: b
OPERATORS AND Explanation: ISO C99 compiler may
EXPRESSIONS IN C consider only first 63 characters for internal
names.
This section on C interview questions and
2. C99 standard guarantees uniqueness of
answers focuses on “Variable Names”. One
characters for external names.
shall practice these interview questions to
a) 31
improve their C programming skills needed
b) 6
for various interviews (campus interviews,
c) 12
walk-in interviews, company interviews),
d) 14
placements, entrance exams and other
competitive exams. These questions can be Answer: a
attempted by anyone focusing on learning C
Explanation: ISO C99 compiler may
Programming language. They can be a
consider only first 31 characters for external
beginner, fresher, engineering graduate or an
names.
experienced IT professional. Our C Interview
questions come with the detailed explanation 3. Which of the following is not a valid
of the answers which helps in better variable name declaration?
understanding of C concepts. a) int a3;
b) int 3a;
Here is a listing of C interview questions on
c) int A3;
“Variable Names” along with answers,
d) None of the mentioned
explanations and/or solutions:
Answer: d
1. C99 standard guarantees uniqueness of
Explanation: None.
characters for internal names.
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

Answer: c
Explanation: Variable name cannot start with
a digit.

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 c) Variable names cannot start with a digit
use such names d) Variable can be of any length
d) To avoid conflicts with environment
variables of an operating system Answer: c
Explanation: According to the syntax for C
Answer: c variable name, it cannot start with a digit.
Explanation: None.
1. Which is valid C expression?
6. All keywords in C are in a) int my_num = 100,000;
a) LowerCase letters b) int my_num = 100000;
b) UpperCase letters c) int my num = 1000;
c) CamelCase letters d) int $my_num = 10000;
d) None of the mentioned
Answer: b
Answer: a Explanation: Space, comma and $ cannot be
Explanation: None. used in a variable name.

7. Variable name resolution (number of 2. What will be the output of the following C
significant characters for the uniqueness of code?
variable) depends on
a) Compiler and linker implementations 1. #include <stdio.h>
b) Assemblers and loaders implementations
c) C language 2. int main()
d) None of the mentioned 3. {

Answer: a 4. printf("Hello World! %d \n"


Explanation: It depends on the standard to , x);
which compiler and linkers are adhering.
5. return 0;
8. Which of the following is not a valid C 6. }
variable name?
a) int number; a) Hello World! x;
b) float rate; b) Hello World! followed by a junk value
c) int variable_count; c) Compile time error
d) int $main; d) Hello World!
Answer: d Answer: c
Explanation: Since only underscore and no Explanation: It results in an error since x is
other special character is allowed in a used without declaring the variable x.
variable name, it results in an error. Output:
$ cc pgm1.c
9. Which of the following is true for variable
pgm1.c: In function ‘main’:
names in C?
pgm1.c:4: error: ‘x’ undeclared (first use in
a) They can contain alphanumeric characters
this function)
as well as special characters
pgm1.c:4: error: (Each undeclared identifier
b) It is not an error to declare a variable to be
is reported only once
one of the keywords(like goto, static)
pgm1.c:4: error: for each function it appears
in.)
3. What will be the output of the following C 2. int main()
code?
3. {
1. #include <stdio.h>
4. int main = 3;
2. int main()
5. printf("%d", main);
3. {
6. return 0;
4. int y = 10000;
7. }
5. int y = 34;
a) It will cause a compile-time error
6. printf("Hello World! %d\n",y); b) It will cause a run-time error
c) It will run without any error and prints 3
7. return 0; d) It will experience infinite looping
8. }
Answer: c
a) Compile time error Explanation: A C program can have same
b) Hello World! 34 function name and same variable name.
c) Hello World! 1000 $ cc pgm3.c
d) Hello World! followed by a junk value $ a.out
3
Answer: a
Explanation: Since y is already defined, 6. What is the problem in the following
redefining it results in an error. variable declaration?
Output: float 3Bedroom-Hall-Kitchen?;
$ cc pgm2.c
pgm2.c: In function ‘main’: a) The variable name begins with an integer
pgm2.c:5: error: redefinition of ‘y’ b) The special character ‘-‘
pgm2.c:4: note: previous definition of ‘y’ was c) The special character ‘?’
here d) All of the mentioned
4. Which of the following is not a valid Answer: d
variable name declaration? Explanation: A variable name cannot start
a) float PI = 3.14; with an integer, along with that the C
b) double PI = 3.14; compiler interprets the ‘-‘ and ‘?’ as a minus
c) int PI = 3.14; operator and a question mark operator
d) #define PI 3.14 respectively.
Answer: d 7. What will be the output of the following C
Explanation: #define PI 3.14 is a macro code?
preprocessor, it is a textual substitution.
1. #include <stdio.h>
5. What will happen if the following C code
is executed? 2. int main()

1. #include <stdio.h> 3. {
4. int ThisIsVariableName = 12 5. int i;
;
6. for (i = 0; i < 5; i++)
5. int ThisIsVariablename = 14
; 7. if ((char)a[i] == '5')

6. printf("%d", ThisIsVariable name); 8. printf("%d\n", a[i]


);
7. return 0;
9. else
8. }
10. printf("FAIL\n");
a) The program will print 12
b) The program will print 14 11. }
c) The program will have a runtime error
d) The program will cause a compile-time a) The compiler will flag an error
error due to redeclaration b) The program will compile and print the
output 5
Answer: b c) The program will compile and print the
Explanation: Variable names ASCII value of 5
ThisIsVariablename and ThisIsVariableName d) The program will compile and print FAIL
are both distinct as C is case sensitive. for 5 times
Output:
$ cc pgm4.c Answer: d
$ a.out Explanation: The ASCII value of 5 is 53, the
14 char type-casted integral value 5 is 5 only.
Output:
8. Which of the following cannot be a $ cc pgm1.c
variable name in C? $ a.out
a) volatile FAIL
b) true FAIL
c) friend FAIL
d) export FAIL
FAIL
Answer: a
2. The format identifier ‘%i’ is also used for
Explanation: volatile is C keyword. data type.
Best C Books a) char
b) int
c) float
1. What will be the output of the following C d) double
code?
Answer: b
1. #include <stdio.h> Explanation: Both %d and %i can be used as
a format identifier for int data type.
2. int main()

3. {
3. Which data type is most suitable for storing
a number 65000 in a 32-bit system?
4. int a[5] = {1, 2, 3, 4, 5}; a) signed short
b) unsigned short
c) long a) 128
d) int b) -128
c) Depends on the compiler
Answer: b d) None of the mentioned
Explanation: 65000 comes in the range of
short (16-bit) which occupies the least Answer: b
memory. Signed short ranges from -32768 to Explanation: signed char will be a negative
32767 and hence we should use unsigned number.
short. Output:
$ cc pgm2.c
4. Which of the following is a User-defined $ a.out
data type? -128
a) typedef int Boolean;
b) typedef enum {Mon, Tue, Wed, Thu, Fri} 7. What will be the output of the following C
Workdays; code?
c) struct {char name[10], int age};
d) all of the mentioned 1. #include <stdio.h>

Answer: d 2. int main()


Explanation: typedef and struct are used to 3. {
define user-defined data types.
4. char c;
5. What is the size of an int data type?
a) 4 Bytes 5. int i = 0;
b) 8 Bytes
c) Depends on the system/compiler 6. FILE *file;
d) Cannot be determined 7. file = fopen("test.txt", "w
+");
Answer: c
Explanation: The size of the data types 8. fprintf(file, "%c", 'a');
depend on the system.
9. fprintf(file, "%c", -1);
6. What will be the output of the following C
10. fprintf(file, "%c", 'b');
code?
11. fclose(file);
1. #include <stdio.h>
12. file = fopen("test.txt",
2. int main()
"r");
3. { 13. while ((c = fgetc(file)) !=
-1)
4. signed char chr;
14. printf("%c", c);
5. chr = 128;
15. return 0;
6. printf("%d\n", chr);
16. }
7. return 0;

8. }
a) a
b) Infinite loop
c) Depends on what fgetc returns Output:
d) Depends on the compiler $ cc pgm4.c
$ a.out
Answer: a not equal
Explanation: None.
Output: 2. What will be the output of the following C
$ cc pgm3.c code?
$ a.out
a 1. #include <stdio.h>

8. What is short int in C programming? 2. int main()


a) The basic data type of C 3. {
b) Qualifier
c) Short is the qualifier and int is the basic 4. float f1 = 0.1;
data type
d) All of the mentioned 5. if (f1 == 0.1f)

Answer: c 6. printf("equal\n");
Explanation: None. 7. else

1. What will be the output of the following C 8. printf("not equal\n");


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

9. } 3. What will be the output of the following C


code on a 32-bit machine?
a) equal
b) not equal 1. #include <stdio.h>
c) output depends on the compiler
d) error 2. int main()

3. {
Answer: b
Explanation: 0.1 by default is of type double 4. int x = 10000;
which has different representation than float
resulting in inequality even after conversion. 5. double y = 56;
6. int *p = &x; 8. {

7. double *q = &y; 9. union Sti s;

8. printf("p and q are %d and 10. printf("%d", sizeof(s));


%d", sizeof(p), sizeof(q));
11. return 0;
9. return 0;
12. }
10. }
a) 8
a) p and q are 4 and 4 b) 5
b) p and q are 4 and 8 c) 9
c) compiler error d) 4
d) p and q are 2 and 8
Answer: d
Answer: a Explanation: Since the size of a union is the
Explanation: Size of any type of pointer is 4 size of its maximum data type, here int is the
on a 32-bit machine. largest data type. Hence the size of the union
Output: is 4.
$ cc pgm6.c Output:
$ a.out $ cc pgm7.c
p and q are 4 and 4 $ a.out
4
4. Which is correct with respect to the size of
the data types? 6. What will be the output of the following C
a) char > int > float code?
b) int > char > float
c) char < int < double 1. #include <stdio.h>
d) double > char > int
2. int main()
Answer: c
Explanation: char has less bytes than int and 3. {

int has less bytes than double in any system 4. float x = 'a';

5. What will be the output of the following C 5. printf("%f", x);


code on a 64 bit machine?
6. return 0;
1. #include <stdio.h>
7. }
2. union Sti
a) a
3. { b) run time error
c) a.0000000
4. int nu;
d) 97.000000
5. char m;
Answer: d
6. }; Explanation: Since the ASCII value of a is
97, the same is assigned to the float variable
7. int main() and printed.
Output: 6. }
$ cc pgm8.c
$ a.out a) PEACH = 3
97.000000 b) PEACH = 4
c) PEACH = 5
7. Which of the data types has the size that is d) PEACH = 6
variable?
a) int Answer: c
b) struct Explanation: In enum, the value of constant
c) float is defined to the recent assignment from left.
d) double Output:
$ cc pgm1.c
Answer: b $ a.out
Explanation: Since the size of the structure PEACH = 5
depends on its fields, it has a variable size.
2. What will be the output of the following C
code?
This section on online C test focuses on
“Constants”. One shall practice these test 1. #include <stdio.h>
questions to improve their C programming
skills needed for various interviews (campus 2. int main()
interviews, walk-in interviews, company
interviews), placements, entrance exams and 3. {
other competitive exams. These questions can
be attempted by anyone focusing on learning 4. printf("C programming %s",
"Class by\n%s Sanfoundry", "WOW");
C Programming language. They can be a
beginner, fresher, engineering graduate or an 5. }
experienced IT professional. Our online C
test questions come with the detailed a)
explanation of the answers which helps in
better understanding of C concepts. C programming Class by

WOW Sanfoundry
Here is a listing of online C test questions on
“Constants” along with answers, explanations b) C programming Class by\n%s Sanfoundry
and/or solutions: c)
1. What will be the output of the following C C programming Class by
code?
%s Sanfoundry
1. #include <stdio.h>
d) Compilation error
2. int main()
Answer: c
3. {
Explanation: This program has only one %s
4. enum {ORANGE = 5, MANGO, BA within first double quotes, so it does not read
NANA = 4, PEACH}; the string “WOW”.
The %s along with the Sanfoundry is not read
5. printf("PEACH = %d\n", PEAC as a format modifier while new line character
H); prints the new line.
Output: results in Compilation error.
$ cc pgm2.c Output:
$ a.out $ cc pgm3.c
C programming Class by pgm3.c: In function ‘main’:
%s Sanfoundry pgm3.c:5: error: expected identifier or ‘(’
before numeric constant
3. In the following code snippet, character
pointer str holds a reference to the string 5. What will be the output of the following C
code?
char *str = "annauniv.edu\0" "training c 1. #include <stdio.h>
lasses";
2. int main()
a) annauniv.edu
b) annauniv.edu\0training classes 3. {
c) annauniv.edutraining classes
d) Invalid declaration 4. int var = 010;

5. printf("%d", var);
Answer: b
Explanation: ‘\0’ is accepted as a char in the 6. }
string. Even though strlen will give length of
string “annauniv.edu”, in memory str is a) 2
pointing to entire string including training b) 8
classes. c) 9
d) 10
4. What will be the output of the following C
code? Answer: b
Explanation: 010 is octal representation of 8.
1. #include <stdio.h> Output:
$ cc pgm4.c
2. #define a 10
$ a.out
3. int main() 8

4. { 6. What will be the output of the following C


function?
5. const int a = 5;
1. #include <stdio.h>
6. printf("a = %d\n", a);
2. enum birds {SPARROW, PEACOCK, P
7. } ARROT};

a) a = 5 3. enum animals {TIGER = 8, LION,


b) a = 10 RABBIT, ZEBRA};
c) Compilation error
d) Runtime error 4. int main()

5. {
Answer: c
Explanation: The #define substitutes a with 6. enum birds m = TIGER;
10 without leaving any identifier, which
7. int k; PARROT will have value 3 + 2.
Output:
8. k = m; $ cc pgm6.c
9. printf("%d\n", k);
$ a.out
5
10. return 0;
8. What will be the output of the following C
11. } code?

a) 0 1. #include <stdio.h>
b) Compile time error
c) 1 2. #include <string.h>
d) 8
3. int main()
Answer: d 4. {
Explanation: m is an integer constant, hence
it is compatible. 5. char *str = "x";
Output:
$ cc pgm5.c 6. char c = 'x';
$ a.out 7. char ary[1];
8
8. ary[0] = c;
7. What will be the output of the following C
code? 9. printf("%d %d", strlen(str)
, strlen(ary));
1. #include <stdio.h>
10. return 0;
2. #define MAX 2
11. }
3. enum bird {SPARROW = MAX + 1, P
ARROT = SPARROW + MAX}; a) 1 1
b) 2 1
4. int main() c) 2 2
d) 1 (undefined value)
5. {

6. enum bird b = PARROT; Answer: d


Explanation: str is null terminated, but ary is
7. printf("%d\n", b); not null terminated.
Output:
8. return 0; $ cc pgm7.c
9. }
$ a.out
15
a) Compilation error
b) 5 Sanfoundry’s 1000+ MCQs on C helps
c) Undefined value anyone preparing for placement in TCS and
d) 2 other companies. Anyone looking for TCS
placement papers should practice these 1000+
Answer: b
Explanation: MAX value is 2 and hence
questions continuously for 2-3 months, 3. What will be the output of the following C
thereby ensuring a top position in placements. code?

Here is a listing of C problems on 1. #include <stdio.h>


“Constants” along with answers, explanations
and/or solutions: 2. int main()

3. {
1. enum types are processed by
a) Compiler 4. printf("sanfoundry\r\nclass
b) Preprocessor \n");
c) Linker
d) Assembler 5. return 0;

Answer: a 6. }
Explanation: None.
a) sanfoundryclass
2. What will be the output of the following C b)
code? sanfoundry

1. #include <stdio.h> class

2. int main() c) classundry


3. {
d) sanfoundry

4. printf("sanfoundry\rclass\n Answer: b
"); Explanation: rn combination makes the
cursor move to the next line.
5. return 0; Output:
$ cc pgm9.c
6. }
$ a.out
a) sanfoundryclass sanfoundry
b) class

sanfoundry 4. What will be the output of the following C


code?
class
1. #include <stdio.h>
c) classundry
d) sanfoundry 2. int main()

Answer: c 3. {
Explanation: r is carriage return and moves 4. const int p;
the cursor back. sanfo is replaced by class.
Output: 5. p = 4;
$ cc pgm8.c
$ a.out 6. printf("p is %d", p);
classundry
7. return 0;
8. } pgm11.c:7: error: assignment of read-only
variable ‘p’
a) p is 4 pgm11.c:8: warning: format ‘%d’ expects
b) Compile time error type ‘int’, but argument 2 has type ‘int *
c) Run time error const’
d) p is followed by a garbage value
6. Which of the following statement is false?
Answer: b a) Constant variables need not be defined as
Explanation: Since the constant variable has they are declared and can be defined later
to be declared and defined at the same time, b) Global constant variables are initialized to
not doing it results in an error. zero
Output: c) const keyword is used to define constant
$ cc pgm10.c values
pgm10.c: In function ‘main’: d) You cannot reassign a value to a constant
pgm10.c:5: error: assignment of read-only variable
variable ‘p’
Answer: a
5. What will be the output of the following C Explanation: Since the constant variable has
code? to be declared and defined at the same time,
not doing it results in an error.
1. #include <stdio.h>
7. What will be the output of the following C
2. void main()
code?
3. {
1. #include <stdio.h>
4. int k = 4;
2. void main()
5. int *const p = &k;
3. {
6. int r = 3;
4. int const k = 5;
7. p = &r;
5. k++;
8. printf("%d", p);
6. printf("k is %d", k);
9. }
7. }
a) Address of k
b) Address of r a) k is 6
c) Compile time error b) Error due to const succeeding int
d) Address of k + address of r c) Error, because a constant variable can be
changed only twice
Answer: c d) Error, because a constant variable cannot
Explanation: Since the pointer p is declared be changed
to be constant, trying to assign it with a new
value results in an error. Answer: d
Output: Explanation: Constant variable has to be
$ cc pgm11.c declared and defined at the same time. Trying
pgm11.c: In function ‘main’: to change it results in an error.
Output:
$ cc pgm12.c language. They can be a beginner, fresher,
pgm12.c: In function ‘main’: engineering graduate or an experienced IT
pgm12.c:5: error: increment of read-only professional. Our C Interview questions come
variable ‘k’ with the detailed explanation of the answers
which helps in better understanding of C
8. What will be the output of the following C concepts.
code?
Here is a listing of C interview questions on
1. #include <stdio.h> “Declarations” along with answers,
explanations and/or solutions:
2. int const print()

3. {
1. What will be the output of the following C
code?
4. printf("annauniv.edu");
1. #include <stdio.h>
5. return 0;
2. void foo(const int *);
6. }
3. int main()
7. void main()
4. {
8. {
5. const int i = 10;
9. print();
6. printf("%d ", i);
10. }
7. foo(&i);
a) Error because function name cannot be
8. printf("%d", i);
preceded by const
b) annauniv.edu 9.
c) annauniv.edu is printed infinite times
d) Blank screen, no output 10. }

Answer: b 11. void foo(const int *i)


Explanation: None.
12. {
Output:
$ cc pgm13.c 13. *i = 20;
$ a.out
annauniv.edu 14. }

a) Compile time error


This section on C interview questions and
b) 10 20
answers focuses on “Declarations”. One shall
c) Undefined value
practice these interview questions to improve
d) 10
their C programming skills needed for various
interviews (campus interviews, walk-in Answer: a
interviews, company interviews), placements, Explanation: Cannot change a const type
entrance exams and other competitive exams. value.
These questions can be attempted by anyone Output:
focusing on learning C Programming $ cc pgm1.c
pgm1.c: In function ‘foo’: 4. j = 10;
pgm1.c:13: error: assignment of read-only
location ‘*i’ 5. printf("%d\n", j++);

6. return 0;
2. What will be the output of the following C
code? 7. }

1. #include <stdio.h> a) 10
b) 11
2. int main()
c) Compile time error
3. { d) 0

4. const int i = 10; Answer: c


Explanation: Variable j is not defined.
5. int *ptr = &i; Output:
6. *ptr = 20;
$ cc pgm3.c
pgm3.c: In function ‘main’:
7. printf("%d\n", i); pgm3.c:4: error: ‘j’ undeclared (first use in
this function)
8. return 0; pgm3.c:4: error: (Each undeclared identifier
is reported only once
9. }
pgm3.c:4: error: for each function it appears
a) Compile time error in.)
b) Compile time warning and printf displays
4. Will the following C code compile without
20
any error?
c) Undefined behaviour
d) 10 1. #include <stdio.h>

Answer: b 2. int main()


Explanation: Changing const variable
through non-constant pointers invokes 3. {
compiler warning.
Output: 4. for (int k = 0; k < 10; k++
);
$ cc pgm2.c
pgm2.c: In function ‘main’: 5. return 0;
pgm2.c:5: warning: initialization discards
qualifiers from pointer target type 6. }
$ a.out
20 a) Yes
b) No
3. What will be the output of the following C c) Depends on the C standard implemented
code? by compilers
d) Error
1. #include <stdio.h>
Answer: c
2. int main() Explanation: Compilers implementing C90
do not allow this, but compilers implementing
3. {
C99 allow it. Answer: a
Output: Explanation: It is legal in Java, but not in C.
$ cc pgm4.c
pgm4.c: In function ‘main’: 7. Which of the following format identifier
pgm4.c:4: error: ‘for’ loop initial declarations can never be used for the variable var?
are only allowed in C99 mode
pgm4.c:4: note: use option -std=c99 or - 1. #include <stdio.h>
std=gnu99 to compile your code
2. int main()
5. Will the following C code compile without 3. {
any error?
4. char *var = "Advanced Train
1. #include <stdio.h> ing in C by annauniv.edu";

2. int main() 5. }

3. { a) %f
b) %d
4. int k;
c) %c
5. { d) %s

6. int k; Answer: a
Explanation: %c can be used to print the
7. for (k = 0; k < 10; k++ indexed position.
); %d can still be used to display its ASCII
8. }
value.
%s is recommended.
9. } %f cannot be used for the variable var.

a) Yes Sanfoundry’s 1000+ Interview Questions &


b) No Answers on C help anyone preparing for TCS
c) Depends on the compiler and other companies C interviews. One
d) Depends on the C standard implemented should practice these 1000+ interview
by compilers questions and answers continuously for 2-3
months to clear TCS interviews on C
Answer: a
Programming language.
Explanation: There can be blocks inside the
block. But within a block, variables have only Here is a listing of C questions and puzzles
block scope. on “Declarations” along with answers,
Output: explanations and/or solutions:
$ cc pgm5.c
1. Which of the following declaration is
6. Which of the following declaration is not illegal?
supported by C? a) char *str = “Best C programming classes
a) String str; by Sanfoundry”;
b) char *str; b) char str[] = “Best C programming classes
c) float str = 3e2; by Sanfoundry”;
d) Both String str; & float str = 3e2; c) char str[20] = “Best C programming
classes by Sanfoundry”; c) 4.0000000
d) char[] str = “Best C programming classes d) 4.4
by Sanfoundry”;
Answer: a
Answer: d Explanation: Since the variable k is defined
Explanation: char[] str is a declaration in both as integer and as float, it results in an
Java, but not in C. error.
Output:
2. Which keyword is used to prevent any $ cc pgm8.c
changes in the variable within a C program? pgm8.c: In function ‘main’:
a) immutable pgm8.c:5: error: conflicting types for ‘k’
b) mutable pgm8.c:4: note: previous definition of ‘k’ was
c) const here
d) volatile pgm8.c:6: warning: format ‘%d’ expects type
‘int’, but argument 2 has type ‘double’
Answer: c pgm8.c:7: error: expected ‘;’ before ‘}’ token
Explanation: const is a keyword constant in
C program. 5. Which of the following statement is false?
a) A variable defined once can be defined
3. Which of the following is not a pointer again with different scope
declaration? b) A single variable cannot be defined with
a) char a[10]; two different types in the same scope
b) char a[] = {‘1’, ‘2’, ‘3’, ‘4’}; c) A variable must be declared and defined at
c) char *str; the same time
d) char a; d) A variable refers to a location in memory
Answer: d Answer: c
Explanation: Array declarations are pointer Explanation: It is not an error if the variable
declarations. is declared and not defined. For example –
extern declarations.
4. What will be the output of the following C
code? 6. A variable declared in a function can be
used in main().
1. #include <stdio.h> a) True
2. void main()
b) False
c) True if it is declared static
3. { d) None of the mentioned

4. int k = 4; Answer: b
Explanation: Since the scope of the variable
5. float k = 4;
declared within a function is restricted only
6. printf("%d", k) within that function, so the above statement is
false.
7. }
7. The name of the variable used in one
a) Compile time error function cannot be used in another function.
b) 4 a) True
b) False
Answer: b Answer: b
Explanation: Since the scope of the variable Explanation: None.
declared within a function is restricted only
within that function, the same name can be 2. What will be the output of the following C
used to declare another variable in another code?
function.
1. #include <stdio.h>

This section on C test focuses on “Arithmetic 2. int main()


Operators”. One shall practice these test
questions to improve their C programming 3. {
skills needed for various interviews (campus
interviews, walkin interviews, company 4. int i = 3;

interviews), placements, entrance exams and 5. int l = i / -2;


other competitive exams. These questions can
be attempted by anyone focusing on learning 6. int k = i % -2;
C Programming language. They can be a
beginner, fresher, engineering graduate or an 7. printf("%d %d\n", l, k);
experienced IT professional. Our C test 8. return 0;
questions come with detailed explanation of
the answers which helps in better 9. }
understanding of C concepts.
a) Compile time error
Here is a listing of C test questions on b) -1 1
“Arithmetic Operators” along with answers, c) 1 -1
explanations and/or solutions: d) Implementation defined

1. What will be the output of the following C Answer: b


code? Explanation: None.
1. #include <stdio.h> 3. What will be the output of the following C
code?
2. int main()

3. {
1. #include <stdio.h>

4. int i = -3;
2. int main()

5. int k = i % 2;
3. {

6. printf("%d\n", k);
4. int i = 5;

7. }
5. i = i / 3;

6. printf("%d\n", i);
a) Compile time error
b) -1 7. return 0;
c) 1
d) Implementation defined 8. }
a) Implementation defined c) 24
b) 1 d) 3
c) 3
d) Compile time error Answer: c
Explanation: None.
Answer: b
Explanation: None. 6. What will be the output of the following C
code?
4. What will be the output of the following C
code? 1. #include <stdio.h>

1. #include <stdio.h> 2. void main()

2. int main() 3. {

3. { 4. int x = 5.3 % 2;

4. int i = -5; 5. printf("Value of x is %d",


x);
5. i = i / 3;
6. }
6. printf("%d\n", i);
a) Value of x is 2.3
7. return 0; b) Value of x is 1
c) Value of x is 0.3
8. } d) Compile time error
a) Implementation defined Answer: d
b) -1 Explanation: None.
c) -3
d) Compile time error 7. What will be the output of the following C
code?
Answer: b
Explanation: None. 1. #include <stdio.h>

5. What will be the final value of x in the 2. void main()


following C code?
3. {
1. #include <stdio.h>
4. int y = 3;
2. void main()
5. int x = 5 % 2 * 3 / 2;
3. {
6. printf("Value of x is %d",
4. int x = 5 * 9 / 3 + 9; x);

5. } 7. }

a) 3.75 a) Value of x is 1
b) Depends on compiler b) Value of x is 2
c) Value of x is 3
d) Compile time error
Answer: a 3. Which of the following is not an arithmetic
Explanation: None. operation?
a) a * = 10;
Sanfoundry’s 1000+ MCQs on C helps b) a / = 10;
anyone preparing for placement in Infosys c) a ! = 10;
and other companies. Anyone looking for d) a % = 10;
Infosys placement papers should practice
Answer: c
these 1000+ questions continuously for 2-3
Explanation: None.
months, thereby ensuring a top position in
placements. 4. Which of the following data type will
Here is a listing of C test questions on throw an error on modulus operation(%)?
a) char
“Arithmetic Operators” along with answers,
b) short
explanations and/or solutions:
c) int
1. What will be the output of the following C d) float
code?
Answer: d
1. #include <stdio.h> Explanation: None.

2. void main() 5. Which among the following are the


fundamental arithmetic operators, i.e,
3. { performing the desired operation can be done
using that operator only?
4. int a = 3;
a) +, –
5. int b = ++a + a++ + --a; b) +, -, %
c) +, -, *, /
6. printf("Value of b is %d", d) +, -, *, /, %
b);
Answer: a
7. } Explanation: None.
a) Value of x is 12 6. What will be the output of the following C
b) Value of x is 13 code?
c) Value of x is 10
d) Undefined behaviour 1. #include <stdio.h>

Answer: d 2. int main()


Explanation: None.
3. {
2. What is the precedence of arithmetic
4. int a = 10;
operators (from highest to lowest)?
a) %, *, /, +, – 5. double b = 5.6;
b) %, +, /, *, –
c) +, -, %, *, / 6. int c;
d) %, +, -, *, /
7. c = a + b;
Answer: a
8. printf("%d", c);
Explanation: None.
9. } the answers which helps in better
understanding of C concepts.
a) 15
b) 16 Here is a listing of C interview questions on
c) 15.6 “Relational & Logical Operators” along with
d) 10 answers, explanations and/or solutions:

Answer: a 1. What will be the output of the following C


Explanation: None. code?

7. What will be the output of the following C 1. #include <stdio.h>


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

2. void main()
This section on C interview questions and
answers focuses on “Relational & Logical 3. {
Operators”. One shall practice these interview
questions to improve their C programming 4. int x = 1, y = 0, z = 5;
skills needed for various interviews (campus
interviews, walkin interviews, company 5. int a = x && y && z++;
interviews), placements, entrance exams and 6. printf("%d", z);
other competitive exams. These questions can
be attempted by anyone focusing on learning 7. }
C Programming language. They can be a
beginner, fresher, engineering graduate or an a) 6
experienced IT professional. Our C Interview b) 5
questions come with detailed explanation of
c) 0 Answer: d
d) Varies Explanation: None.

Answer: b 5. What will be the output of the following C


Explanation: None. code?

3. What will be the output of the following C 1. #include <stdio.h>


code?
2. void main()
1. #include <stdio.h>
3. {
2. int main()
4. int x = 0, y = 2, z = 3;
3. {
5. int a = x & y | z;
4. int x = 1, y = 0, z = 3;
6. printf("%d", a);
5. x > y ? printf("%d", z) : r
eturn z;
7. }

6. } a) 3
b) 0
a) 3 c) 2
b) 1 d) Run time error
c) Compile time error
d) Run time error Answer: a
Explanation: None.
Answer: c
Explanation: None. 6. What will be the final value of j in the
following C code?
4. What will be the output of the following C
code? 1. #include <stdio.h>

1. #include <stdio.h> 2. int main()

2. void main() 3. {

3. { 4. int i = 0, j = 0;

4. int x = 1, z = 3; 5. if (i && (j = i + 10))

5. int y = x << 3; 6. //do something

6. printf(" %d\n", y); 7. ;

7. } 8. }

a) -2147483648 a) 0
b) -1 b) 10
c) Run time error c) Depends on the compiler
d) 8 d) Depends on language standard
Answer: a a) Yes
Explanation: None. b) No
c) Depends on the compiler
7. What will be the final value of j in the d) Depends on the standard
following C code?
Answer: b
1. #include <stdio.h> Explanation: None.
2. int main()
Sanfoundry’s 1000+ MCQs on C helps
3. { anyone preparing for placement in IBM and
other companies. Anyone looking for IBM
4. int i = 10, j = 0; placement papers should practice these 1000+
5. if (i || (j = i + 10))
questions continuously for 2-3 months,
thereby ensuring a top position in placements.
6. //do something
Here is a listing of C interview questions on
7. ; “Relational & Logical Operators” along with
answers, explanations and/or solutions:
8. }
1. Are logical operator sequence points?
a) 0 a) True
b) 20 b) False
c) Compile time error c) Depends on the compiler
d) Depends on language standard d) Depends on the standard
Answer: a Answer: a
Explanation: None. Explanation: None.
8. What will be the output of the following C 2. Do logical operators in the C language are
code? evaluated with the short circuit?
a) True
1. #include <stdio.h>
b) False
2. int main() c) Depends on the compiler
d) Depends on the standard
3. {
Answer: a
4. int i = 1; Explanation: None.
5. if (i++ && (i == 1)) 3. What is the result of logical or relational
6. printf("Yes\n");
expression in C?
a) True or False
7. else b) 0 or 1
c) 0 if an expression is false and any positive
8. printf("No\n"); number if an expression is true
d) None of the mentioned
9. }
Answer: b
Explanation: None.
4. What will be the final value of d in the Answer: a
following C code? Explanation: None.

1. #include <stdio.h> 6. Which among the following is NOT a


logical or relational operator?
2. int main() a) !=
3. {
b) ==
c) ||
4. int a = 10, b = 5, c = 5; d) =

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

6. printf("TRUE 1\t");
5. What will be the output of the following C
code? 7. a = 10;

1. #include <stdio.h> 8. if (a == --a)

2. int main() 9. printf("TRUE 2\t");

3. { 10. }

4. int a = 10, b = 5, c = 3; a) TRUE 1


5. b != !a;
b) TRUE 2
c) TRUE 1 TRUE 2
6. c = !!a; d) Compiler Dependent

7. printf("%d\t%d", b, c); Answer: d


Explanation: This is a sequence point
8. } problem and hence the result will be
implementation dependent.
a) 5 1
b) 0 3 8. Relational operators cannot be used on
c) 5 3
d) 1 1 a) structure
b) long
c) strings a) Advanced C Classes
d) float b) Sanfoundry
c) Run time error
Answer: a d) Compile time error
Explanation: None.
Answer: a
This section on C language interview Explanation: None.
questions and answers focuses on “Type
2. What will be the output of the following C
Conversions”. One shall practice these
interview questions to improve their C code?
programming skills needed for various 1. #include <stdio.h>
interviews (campus interviews, walkin
interviews, company interviews), placements, 2. void main()
entrance exams and other competitive exams.
These questions can be attempted by anyone 3. {
focusing on learning C Programming
language. They can be a beginner, fresher, 4. float x = 0.1;

engineering graduate or an experienced IT 5. printf("%d, ", x);


professional. Our C language interview
questions come with detailed explanation of 6. printf("%f", x);
the answers which helps in better
understanding of C concepts. 7. }

Here is a listing of C language interview a) 0.100000, junk value


questions on “Type Conversions” along with b) Junk value, 0.100000
answers, explanations and/or solutions: c) 0, 0.100000
d) 0, 0.999999
1. What will be the output of the following C
code? Answer: b
Explanation: None.
1. #include <stdio.h>
3. What will be the output of the following C
2. void main() code? (Initial values: x= 7, y = 8)
3. { 1. #include <stdio.h>

4. float x = 0.1; 2. void main()

5. if (x == 0.1) 3. {

6. printf("Sanfoundry"); 4. float x;

7. else 5. int y;

8. printf("Advanced C Clas 6. printf("enter two numbers


ses"); \n", x);

9. } 7. scanf("%f %f", &x, &y);

8. printf("%f, %d", x, y);


9. } 7. }

a) 7.000000, 7 a) a
b) Run time error b) b
c) 7.000000, junk c) 97
d) Varies d) Run time error

Answer: c Answer: a
Explanation: None. Explanation: None.

4. What will be the output of the following C 6. When double is converted to float, then the
code? value is?
a) Truncated
1. #include <stdio.h> b) Rounded
c) Depends on the compiler
2. void main()
d) Depends on the standard
3. {
Answer: c
4. double x = 123828749.66; Explanation: None.

5. int y = x; 7. What will be the output of the following C


code?
6. printf("%d\n", y);
1. #include <stdio.h>
7. printf("%lf\n", y);
2. int main()
8. }
3. {
a) 0, 0.0
b) 123828749, 123828749.66 4. unsigned int i = 23;
c) 12382874, 12382874.0
d) 123828749, 0.000000 5. signed char c = -23;

Answer: d 6. if (i > c)

Explanation: None. 7. printf("Yes\n");

5. What will be the output of the following C 8. else if (i < c)


code?
9. printf("No\n");
1. #include <stdio.h>
10. }
2. void main()
a) Yes
3. { b) No
c) Depends on the compiler
4. int x = 97;
d) Depends on the operating system
5. char y = x;
Answer: b
6. printf("%c\n", y); Explanation: None.
8. What will be the output of the following C Answer: d
code? Explanation: None.

1. #include <stdio.h> 2. What will be the output of the following C


code considering the size of a short int is 2,
2. int main() char is 1 and int is 4 bytes?
3. {
1. #include <stdio.h>
4. int i = 23;
2. int main()
5. char c = -23;
3. {
6. if (i < c)
4. short int i = 20;
7. printf("Yes\n");
5. char c = 97;
8. else
6. printf("%d, %d, %d\n", size
of(i), sizeof(c), sizeof(c + i));
9. printf("No\n");

10. }
7. return 0;

8. }
a) Yes
b) No a) 2, 1, 2
c) Depends on the compiler b) 2, 1, 1
d) Depends on the standard c) 2, 1, 4
d) 2, 2, 8
Answer: b
Explanation: None. Answer: c
Explanation: None.
Sanfoundry’s 1000+ Interview Questions &
Answers on C helps anyone preparing for 3. Which type of conversion is NOT
Sasken and other companies C interviews. accepted?
One should practice these 1000+ interview a) From char to int
questions and answers continuously for 2-3 b) From float to char pointer
months to clear Sasken interviews on C c) From negative int to char
Programming language. d) From double to char

Here is a listing of C programming interview Answer: b


questions on “Type Conversions” along with Explanation: Conversion of a float to pointer
answers, explanations and/or solutions: type is not allowed.

1. function tolower(c) defined in library 4. What will be the data type of the result of
<ctype.h> works for the following operation?
a) Ascii character set
(float)a * (int)b / (long)c * (double)d
b) Unicode character set
c) Ascii and utf-8 but not EBCDIC character a) int
set
b) long
d) Any character set
c) float language. They can be a beginner, fresher,
d) double engineering graduate or an experienced IT
professional. Our C online test questions
Answer: d come with detailed explanation of the
Explanation: None. answers which helps in better understanding
of C concepts.
5. Which of the following type-casting have
chances for wrap around? Here is a listing of online C test questions on
a) From int to float “Increment and Decrement Operators” along
b) From int to char with answers, explanations and/or solutions:
c) From char to short
d) From char to int 1. What is the difference between the
following 2 codes?
Answer: b
Explanation: None. 1. #include <stdio.h> //Program 1

6. Which of the following typecasting is 2. int main()


accepted by C?
3. {
a) Widening conversions
b) Narrowing conversions 4. int d, a = 1, b = 2;
c) Widening & Narrowing conversions
d) None of the mentioned 5. d = a++ + ++b;

Answer: c 6. printf("%d %d %d", d, a, b)


Explanation: None. ;

7. }
7. When do you need to use type-
conversions? 1. #include <stdio.h> //Program 2
a) The value to be stored is beyond the max
limit 2. int main()
b) The value to be stored is in a form not
supported by that data type 3. {
c) To reduce the memory in use, relevant to
4. int d, a = 1, b = 2;
the value
d) All of the mentioned 5. d = a++ +++b;

Answer: d 6. printf("%d %d %d", d, a, b)


Explanation: None. ;

7. }
This section on online C test focuses on
“Increment and Decrement Operators”. One a) No difference as space doesn’t make any
shall practice these test questions to improve difference, values of a, b, d are same in both
their C programming skills needed for various the case
interviews (campus interviews, walkin b) Space does make a difference, values of a,
interviews, company interviews), placements, b, d are different
entrance exams and other competitive exams. c) Program 1 has syntax error, program 2 is
These questions can be attempted by anyone not
focusing on learning C Programming
d) Program 2 has syntax error, program 1 is Answer: d
not Explanation: None.

Answer: d 4. For which of the following, “PI++;” code


Explanation: None. will fail?
a) #define PI 3.14
2. What will be the output of the following C b) char *PI = “A”;
code? c) float PI = 3.14;
d) none of the Mentioned
1. #include <stdio.h>
Answer: a
2. int main()
Explanation: None.
3. {
5. What will be the output of the following C
4. int a = 1, b = 1, c; code?

5. c = a++ + b; 1. #include <stdio.h>

6. printf("%d, %d", a, b); 2. int main()

7. } 3. {

a) a = 1, b = 1 4. int a = 10, b = 10;


b) a = 2, b = 1
c) a = 1, b = 2 5. if (a = 5)

d) a = 2, b = 2 6. b--;

Answer: b 7. printf("%d, %d", a, b--);


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

6. } 2. int main()

a) 15, 4, 5 3. {
b) 9, 6, 9
c) 9, 3, 5 4. int i = 0;
d) Undefined (Compiler Dependent)
5. int j = i++ + i; 6. printf("%d\n", i);

6. printf("%d\n", j); 7. }

7. } a) = operator is not a sequence point


b) ++ operator may return value with or
a) 0 without side effects
b) 1 c) it can be evaluated as (i++)+i or i+(++i)
c) 2 d) = operator is a sequence point
d) Compile time error
Answer: a
Answer: b Explanation: None.
Explanation: None.

7. What will be the output of the following C Sanfoundry’s 1000+ MCQs on C helps
code? anyone preparing for placement in Capgemini
and other companies. Anyone looking for
1. #include <stdio.h> Capgemini placement papers should practice
these 1000+ questions continuously for 2-3
2. int main() months, thereby ensuring a top position in
placements.
3. {

4. int i = 2;
Here is a listing of C questions and puzzles
on “Increment and Decrement Operators”
5. int j = ++i + i; along with answers, explanations and/or
solutions:
6. printf("%d\n", j);
1. What will be the output of the following C
7. }
code?
a) 6 1. #include <stdio.h>
b) 5
c) 4 2. int main()
d) Compile time error
3. {
Answer: a
Explanation: None. 4. int i = 0;

5. int x = i++, y = ++i;


8. What will be the output of the following C
code? 6. printf("%d % d\n", x, y);

1. #include <stdio.h> 7. return 0;

2. int main() 8. }

3. { a) 0, 2
b) 0, 1
4. int i = 2;
c) 1, 2
5. int i = i++ + i; d) Undefined
Answer: a 4. What will be the output of the following C
Explanation: None. code?

2. What will be the output of the following C 1. #include <stdio.h>


code?
2. void main()
1. #include <stdio.h>
3. {
2. int main()
4. int x = 4, y, z;
3. {
5. y = --x;
4. int i = 10;
6. z = x--;
5. int *p = &i;
7. printf("%d%d%d", x, y, z);
6. printf("%d\n", *p++);
8. }
7. }
a) 3 2 3
a) 10 b) 2 3 3
b) 11 c) 3 2 2
c) Garbage value d) 2 3 4
d) Address of i
Answer: b
Answer: a Explanation: None.
Explanation: None.
5. What will be the output of the following C
3. What will be the output of the following C code?
code?
1. #include <stdio.h>
1. #include <stdio.h>
2. void main()
2. void main()
3. {
3. {
4. int x = 4;
4. int x = 97;
5. int *p = &x;
5. int y = sizeof(x++);
6. int *k = p++;
6. printf("X is %d", x);
7. int r = p - k;
7. }
8. printf("%d", r);
a) X is 97 9. }
b) X is 98
c) X is 99 a) 4
d) Run time error b) 8
c) 1
Answer: a d) Run time error
Explanation: None.
Answer: c Answer: d
Explanation: None. Explanation: None.

6. What will be the output of the following C This section on C interview questions and
code? answers focuses on “Bitwise Operators”. One
1. #include <stdio.h>
shall practice these interview questions to
improve their C programming skills needed
2. void main() for various interviews (campus interviews,
walkin interviews, company interviews),
3. { placements, entrance exams and other
competitive exams. These questions can be
4. int a = 5, b = -7, c = 0, d
attempted by anyone focusing on learning C
;
Programming language. They can be a
5. d = ++a && ++b || ++c; beginner, fresher, engineering graduate or an
experienced IT professional. Our C Interview
6. printf("\n%d%d%d%d", a, b, questions come with detailed explanation of
c, d); the answers which helps in better
7. }
understanding of C concepts.

a) 6 -6 0 0 Here is a listing of C interview questions on


b) 6 -5 0 1 “Bitwise Operators” along with answers,
c) -6 -6 0 1 explanations and/or solutions:
d) 6 -6 0 1
1. What will be the output of the following C
Answer: d code?
Explanation: None.
1. #include <stdio.h>
7. What will be the output of the following C 2. int main()
code?
3. {
1. #include <stdio.h>
4. int c = 2 ^ 3;
2. void main()
5. printf("%d\n", c);
3. {
6. }
4. int a = -5;
a) 1
5. int k = (a++, ++a); b) 8
6. printf("%d\n", k);
c) 9
d) 0
7. }
Answer: a
a) -4 Explanation: None.
b) -5
c) 4 2. What will be the output of the following C
d) -3 code?
1. #include <stdio.h> 1. #include <stdio.h>

2. int main() 2. int main()

3. { 3. {

4. unsigned int a = 10; 4. int a = 2;

5. a = ~a; 5. if (a >> 1)

6. printf("%d\n", a); 6. printf("%d\n", a);

7. } 7. }

a) -9 a) 0
b) -10 b) 1
c) -11 c) 2
d) 10 d) No Output

Answer: c Answer: c
Explanation: None. Explanation: None.

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

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

2. int main() 2. int main()

3. { 3. {

4. if (7 & 8) 4. int i, n, a = 4;

5. printf("Honesty"); 5. scanf("%d", &n);

6. if ((~7 & 0x000f) == 8) 6. for (i = 0; i < n; i++)

7. printf("is the best 7. a = a * 2;


policy\n");
8. }
8. }
a) Logical Shift left
a) Honesty is the best policy b) No output
b) Honesty c) Arithmetic Shift right
c) is the best policy d) Bitwise exclusive OR
d) No output
Answer: b
Answer: c Explanation: None.
Explanation: None.
6. What will be the output of the following C
4. What will be the output of the following C code?
code?
1. #include <stdio.h> 1. #include <stdio.h>

2. void main() 2. void main()

3. { 3. {

4. int x = 97; 4. int x = 4;

5. int y = sizeof(x++); 5. int *p = &x;

6. printf("x is %d", x); 6. int *k = p++;

7. } 7. int r = p - k;

a) x is 97 8. printf("%d", r);
b) x is 98
9. }
c) x is 99
d) Run time error a) 4
b) 8
Answer: a
c) 1
Explanation: None.
d) Run time error
7. What will be the output of the following C
Answer: c
code?
Explanation: None.
1. #include <stdio.h>
Sanfoundry’s 1000+ MCQs on C helps
2. void main() anyone preparing for placement in HCL and
3. {
other companies. Anyone looking for HCL
placement papers should practice these 1000+
4. int x = 4, y, z; questions continuously for 2-3 months,
thereby ensuring a top position in placements.
5. y = --x;
Here is a listing of C questions on “Bitwise
6. z = x--; Operators” along with answers, explanations
and/or solutions:
7. printf("%d%d%d", x, y, z);

8. } 1. What will be the output of the following C


code?
a) 3 2 3
b) 2 2 3 1. #include <stdio.h>
c) 3 2 2
2. void main()
d) 2 3 3
3. {
Answer: d
Explanation: None. 4. int a = 5, b = -7, c = 0, d
;
8. What will be the output of the following C
code? 5. d = ++a && ++b || ++c;
6. printf("\n%d%d%d%d", a, b, 7. }
c, d);
a) 4
7. } b) 1
c) Depends on the compiler
a) 6 -6 0 0
d) Depends on the endianness of the machine
b) 6 -5 0 1
c) -6 -6 0 1 Answer: a
d) 6 -6 0 1 Explanation: None.
Answer: d 4. What will be the output of the following C
Explanation: None. code?
2. What will be the output of the following C 1. #include <stdio.h>
code?
2. int main()
1. #include <stdio.h>
3. {
2. void main()
4. int x = -2;
3. {
5. x = x >> 1;
4. int a = -5;
6. printf("%d\n", x);
5. int k = (a++, ++a);
7. }
6. printf("%d\n", k);
a) 1
7. } b) -1
a) -3 c) 2 31 – 1 considering int to be 4 bytes
b) -5 d) Either -1 or 1
c) 4
Answer: b
d) Undefined
Explanation: None.
Answer: a
5. What will be the output of the following C
Explanation: None.
code?
3. What will be the output of the following C
1. #include <stdio.h>
code?
2. int main()
1. #include <stdio.h>
3. {
2. int main()
4. if (~0 == 1)
3. {
5. printf("yes\n");
4. int x = 2;
6. else
5. x = x << 1;
7. printf("no\n");
6. printf("%d\n", x);
8. } 6. printf("y is %d\n", y);

a) yes 7. else
b) no
8. printf("%d\n", y);
c) compile time error
d) undefined 9.
Answer: b 10. }
Explanation: None.
a) y is 1
6. What will be the output of the following C b) 1
code? c) run time error
d) undefined
1. #include <stdio.h>
Answer: a
2. int main() Explanation: None.
3. {
8. What will be the output of the following C
4. int x = -2; code?

5. if (!0 == 1) 1. #include <stdio.h>

6. printf("yes\n"); 2. int main()

7. else 3. {

8. printf("no\n"); 4. int y = 1;

9. } 5. if (y & (y = 2))

a) yes 6. printf("true %d\n", y);


b) no
7. else
c) run time error
d) undefined 8. printf("false %d\n", y)
;
Answer: a
Explanation: None. 9.

7. What will be the output of the following C 10. }


code?
a) true 2
1. #include <stdio.h> b) false 2
c) either true 2 or false 2
2. int main() d) true 1
3. { Answer: a
Explanation: None.
4. int y = 0;

5. if (1 |(y = 1))
This section on C aptitude questions and 2. What will be the output of the following C
answers focuses on “Assignment Operators & code?
Expressions”. One shall practice these
aptitude questions to improve their C 1. #include <stdio.h>
programming skills needed for various
interviews (campus interviews, walkin 2. void main()

interviews, company interviews), placements, 3. {


entrance exams, aptitude tests and other
competitive exams. These questions can be 4. int k = 8;
attempted by anyone focusing on learning C
Programming language. They can be a 5. int x = 0 == 1 && k++;
beginner, fresher, engineering graduate or an
6. printf("%d%d\n", x, k);
experienced IT professional. Our C questions
come with detailed explanation of the 7. }
answers which helps in better understanding
of C concepts. a) 0 9
b) 0 8
Here is a listing of C aptitude questions on c) 1 8
“Assignment Operators & Expressions” along d) 1 9
with answers, explanations and/or solutions:
Answer: b
1. What will be the output of the following C Explanation: None.
code?
3. What will be the output of the following C
1. #include <stdio.h> code?
2. void main()
1. #include <stdio.h>
3. {
2. void main()
4. int x = 0;
3. {
5. if (x = 0)
4. char a = 'a';
6. printf("Its zero\n");
5. int x = (a % 10)++;
7. else
6. printf("%d\n", x);
8. printf("Its not zero\n"
7. }
);

9. } a) 6
b) Junk value
a) Its not zero c) Compile time error
b) Its zero d) 7
c) Run time error
d) None Answer: c
Explanation: None.
Answer: a
Explanation: None. 4. What will be the output of the following C
code snippet?
1. #include <stdio.h> 5. x *= x + y;

2. void main() 6. printf("%d\n", x);

3. { 7. return 0;

4. 1 < 2 ? return 1: return 2; 8. }

5. } a) 5
b) 6
a) returns 1 c) Undefined behaviour
b) returns 2 d) Compile time error
c) Varies
d) Compile time error Answer: b
Explanation: None.
Answer: d
Explanation: None. 7. What will be the output of the following C
code?
5. What will be the output of the following C
code snippet? 1. #include <stdio.h>

1. #include <stdio.h> 2. int main()

2. void main() 3. {

3. { 4. int x = 2, y = 2;

4. unsigned int x = -5; 5. x /= x / y;

5. printf("%d", x); 6. printf("%d\n", x);

6. } 7. return 0;

a) Run time error 8. }


b) Aries
c) -5 a) 2
d) 5 b) 1
c) 0.5
Answer: c d) Undefined behaviour
Explanation: None.
Answer: a
6. What will be the output of the following C Explanation: None.
code?
8. What will be the output of the following C
1. #include <stdio.h> code?

2. int main() 1. #include <stdio.h>

3. { 2. int main()

4. int x = 2, y = 1; 3. {
4. int x = 1, y = 0; a) 2
b) True
5. x &&= y; c) 1
d) 0
6. printf("%d\n", x);

7. } Answer: a
Explanation: None.
a) Compile time error
b) 1 3. Operation “a = a * b + a” can also be
c) 0 written as
d) Undefined behaviour a) a *= b + 1;
b) (c = a * b)!=(a = c + a);
Answer: a c) a = (b + 1)* a;
Explanation: None. d) All of the mentioned

Answer: d
Sanfoundry’s 1000+ Interview Questions & Explanation: None.
Answers on C help anyone preparing for
Microsoft and other companies C interviews. 4. What will be the final value of c in the
One should practice these 1000+ interview following C statement? (Initial value: c = 2)
questions and answers continuously for 2-3
months to clear Microsoft interviews on C 1. c <<= 1;
Programming language.
a) c = 1;
Here is a listing of C test questions on b) c = 2;
“Assignment Operators & Expressions” along c) c = 3;
with answers, explanations and/or solutions: d) c = 4;
1. What is the type of the following Answer: d
assignment expression if x is of type float and Explanation: None.
y is of type int?
5. What will be the output of the following C
y = x + y; code?
a) int 1. #include <stdio.h>
b) float
c) there is no type for an assignment 2. int main()
expression
d) double 3. {

Answer: a 4. int a = 1, b = 2;
Explanation: None. 5. a += b -= a;

2. What will be the value of the following 6. printf("%d %d", a, b);


assignment expression?
7. }
(x = foo())!= 1 considering foo() return
s 2 a) 1 1
b) 1 2
c) 2 1 interviews, company interviews), placements,
d) 2 2 entrance exams and other competitive exams.
These questions can be attempted by anyone
Answer: c focusing on learning C Programming
Explanation: None. language. They can be a beginner, fresher,
engineering graduate or an experienced IT
6. What will be the output of the following C professional. Our C Interview questions come
code? with detailed explanation of the answers
which helps in better understanding of C
1. #include <stdio.h> concepts.
2. int main()
Here is a listing of C interview questions on
3. { “Conditional Expressions” along with
answers, explanations and/or solutions:
4. int a = 4, n, i, result = 0
; 1. What will be the output of the following C
code?
5. scanf("%d", n);

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


1. #include <stdio.h>

7. result += a;
2. int main()

8. }
3. {

4. int x = 2, y = 0;
a) Addition of a and n
b) Subtraction of a and n 5. int z = (y++) ? y == 1 && x
c) Multiplication of a and n : 0;
d) Division of a and n
6. printf("%d\n", z);
Answer: c
7. return 0;
Explanation: None.
8. }
7. Which of the following is an invalid
assignment operator? a) 0
a) a %= 10; b) 1
b) a /= 10; c) Undefined behaviour
c) a |= 10; d) Compile time error
d) None of the mentioned
Answer: a
Answer: d Explanation: None.
Explanation: None.
2. What will be the output of the following C
This section on C interview questions and code?
answers focuses on “Conditional
Expressions”. One shall practice these 1. #include <stdio.h>
interview questions to improve their C 2. int main()
programming skills needed for various
interviews (campus interviews, walkin 3. {
4. int x = 1; Answer: a
Explanation: None.
5. int y = x == 1 ? getchar()
: 2; 4. What will be the output of the following C
6. printf("%d\n", y); code?

7. } 1. #include <stdio.h>

a) Compile time error 2. int main()


b) Whatever character getchar function 3. {
returns
c) Ascii value of character getchar function 4. int a = 2;
returns
d) 2 5. int b = 0;

Answer: c 6. int y = (b == 0) ? a :(a >


b) ? (b = 1): a;
Explanation: None.
7. printf("%d\n", y);
3. What will be the output of the following C
code? 8. }

1. #include <stdio.h> a) Compile time error


b) 1
2. int main() c) 2
3. {
d) Undefined behaviour

4. int x = 1; Answer: c
Explanation: None.
5. short int i = 2;
5. What will be the output of the following C
6. float f = 3; code?
7. if (sizeof((x == 2) ? f : i
1. #include <stdio.h>
) == sizeof(float))

8. printf("float\n");
2. int main()

9. else if (sizeof((x == 2) ?
3. {
f : i) == sizeof(short int))
4. int y = 1, x = 0;
10. printf("short int\n");
5. int l = (y++, x++) ? y : x;
11. }
6. printf("%d\n", l);
a) float 7. }
b) short int
c) Undefined behaviour a) 1
d) Compile time error b) 2
c) Compile time error
d) Undefined behaviour
Answer: a c) 8
Explanation: None. d) Depends on compiler

6. What will be the output of the following C Answer: b


code? Explanation: None.

1. #include <stdio.h> 8. What will be the output of the following C


code?
2. void main()
1. #include <stdio.h>
3. {
2. void main()
4. int k = 8;
3. {
5. int m = 7;
4. 1 < 2 ? return 1 : return 2
6. int z = k < m ? k++ : m++; ;
7. printf("%d", z); 5. }

8. }
a) returns 1
a) 7 b) returns 2
b) 8 c) Varies
c) Run time error d) Compile time error
d) 15 Answer: d
Answer: a Explanation: None.
Explanation: None.
Sanfoundry’s 1000+ Interview Questions &
7. What will be the output of the following C Answers on C helps anyone preparing for
code? IBM and other companies C interviews. One
should practice these 1000+ interview
1. #include <stdio.h> questions and answers continuously for 2-3
months to clear IBM interviews on C
2. void main()
Programming language.
3. {
Here is a listing of C interview questions on
4. int k = 8; “Conditional Expressions” along with
answers, explanations and/or solutions:
5. int m = 7;
1. What will be the output of the following C
6. int z = k < m ? k = m : m+ code?
+;

7. printf("%d", z); 1. #include <stdio.h>

8. } 2. void main()

3. {
a) Run time error
b) 7 4. int k = 8;
5. int m = 7; c) a = 2, c = 2;
d) a = 1, c = 2;
6. k < m ? k++ : m = k;
Answer: a
7. printf("%d", k);
Explanation: None.
8. }
4. What will be the data type of the following
a) 7 expression? (Initial data type: a = int, var1 =
b) 8 double, var2 = float)
c) Compile time error
expression (a < 50)? var1 : var2;
d) Run time error
a) int
Answer: c
b) float
Explanation: None.
c) double
d) Cannot be determined
2. What will be the output of the following C
code? Answer: c
1. #include <stdio.h>
Explanation: None.

2. void main() 5. Which expression has to be present in the


following?
3. {
exp1 ? exp2 : exp3;
4. int k = 8;
a) exp1
5. int m = 7; b) exp2
c) exp3
6. k < m ? k = k + 1 : m = m +
1;
d) all of the mentioned

7. printf("%d", k); Answer: d


Explanation: None.
8. }
6. What will be the final value of c in the
a) Compile time error following C code snippet? (Initial values: a =
b) 9 1, b = 2, c = 1)
c) 8
d) Run time error c += (-c) ? a : b;

Answer: a a) Syntax Error


Explanation: None. b) c = 1
c) c = 2
3. What will be the final values of a and c in d) c = 3
the following C statement? (Initial values: a =
2, c = 1) Answer: c
Explanation: None.
c = (c) ? a = 0 : 2;
7. The following C code can be rewritten as
a) a = 0, c = 0;
b) a = 2, c = 2;
c = (n) ? a : b; 3. {

a) 4. reverse(1);

if (!n)c = b; 5. }
else c = a;
6. void reverse(int i)
b)
7. {
if (n <;= 0)c = b;
else c = a; 8. if (i > 5)

c) 9. exit(0);

if (n > 0)c = a; 10. printf("%d\n", i);


else c = b;
11. return reverse(i++);
d) All of the mentioned 12. }

Answer: a a) 1 2 3 4 5
Explanation: None. b) 1 2 3 4
c) Compile time error
This section on C language interview d) Stack overflow
questions and answers focuses on
“Precedence and Order of Evaluation”. One Answer: d
shall practice these interview questions to Explanation: None.
improve their C programming skills needed
for various interviews (campus interviews, 2. What will be the output of the following C
walkin interviews, company interviews), function?
placements, entrance exams and other
competitive exams. These questions can be 1. #include <stdio.h>
attempted by anyone focusing on learning C 2. void reverse(int i);
Programming language. They can be a
beginner, fresher, engineering graduate or an 3. int main()
experienced IT professional. Our C language
interview questions come with detailed 4. {
explanation of the answers which helps in
5. reverse(1);
better understanding of C concepts.
6. }
Here is a listing of C language interview
questions “Precedence and Order of 7. void reverse(int i)
Evaluation” along with answers, explanations
and/or solutions: 8. {

1. What will be the output of the following C 9. if (i > 5)


function? 10. return ;

1. #include <stdio.h> 11. printf("%d ", i);

2. int main()
12. return reverse((i++, i)); 15. int g()

13. } 16. {

a) 1 2 3 4 5 17. return x++;


b) Segmentation fault
c) Compilation error 18. }

d) Undefined behaviour a) i value is 1 and j value is 1


Answer: a b) i value is 0 and j value is 0
Explanation: None. c) i value is 1 and j value is undefined
d) i and j value are undefined
3. In expression i = g() + f(), first function
Answer: d
called depends on
a) Compiler Explanation: None.
b) Associativiy of () operator
5. What will be the final values of i and j in
c) Precedence of () and + operator
the following C code?
d) Left to write of the expression
1. #include <stdio.h>
Answer: a
Explanation: None. 2. int x = 0;

4. What will be the final values of i and j in 3. int main()


the following C code?
4. {
1. #include <stdio.h>
5. int i = (f() + g()) | g();
2. int x = 0; //bitwise or

3. int main() 6. int j = g() | (f() + g());


//bitwise or
4. {
7. }
5. int i = (f() + g()) || g();
8. int f()
6. int j = g() || (f() + g());
9. {
7. }
10. if (x == 0)
8. int f()
11. return x + 1;
9. {
12. else
10. if (x == 0)
13. return x - 1;
11. return x + 1;
14. }
12. else
15. int g()
13. return x - 1;
16. {
14. }
17. return x++; 5. int z = (y++) ? 2 : y == 1
&& x;
18. }
6. printf("%d\n", z);
a) i value is 1 and j value is 1
b) i value is 0 and j value is 0 7. return 0;
c) i value is 1 and j value is undefined 8. }
d) i and j value are undefined
a) 0
Answer: c b) 1
Explanation: None. c) 2
d) Undefined behaviour
6. What will be the output of the following C
code? Answer: b
Explanation: None.
1. #include <stdio.h>

2. int main() 8. What will be the output of the following C


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

Answer: b a) 0
Explanation: None. b) 1
c) Undefined behaviour
7. What will be the output of the following C d) Compilation error
code?
Answer: b
1. #include <stdio.h> Explanation: None.
2. int main()
9. What will be the output of the following C
3. { code?

4. int x = 2, y = 0; 1. #include <stdio.h>

2. int main()
3. {
Sanfoundry’s 1000+ Interview Questions &
4. int x = 2, y = 0, l;
Answers on C helps anyone preparing for
5. int z; VMware and other companies C interviews.
One should practice these 1000+ interview
6. z = y = 1, l = x && y; questions and answers continuously for 2-3
months to clear VMware interviews on C
7. printf("%d\n", l); Programming language.
8. return 0;
Here is a listing of online C test questions on
9. } “Precedence and Order of Evaluation” along
with answers, explanations and/or solutions:
a) 0
b) 1 1. What will be the output of the following C
c) Undefined behaviour due to order of code?
evaluation can be different
d) Compilation error 1. #include <stdio.h>

2. int main()
Answer: b
Explanation: None. 3. {

10. What will be the output of the following 4. int x = 2, y = 2;


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

3. {

TOPIC 1.2 CONTROL FLOW 4. int x = 1, y = 2;


STATEMENTS IN C
5. if (x && y == 1) 4. int x = 3, y = 2;

6. printf("true\n"); 5. int z = x /= y %= 2;

7. else 6. printf("%d\n", z);

8. printf("false\n"); 7. }

9. } a) 1
b) Compile time error
a) true c) Floating point exception
b) false d) Segmentation fault
c) compile time error
d) undefined behaviour Answer: c
Explanation: None.
Answer: b
Explanation: None. 5. What will be the output of the following C
code?
3. What will be the output of the following C
code? 1. #include <stdio.h>

1. #include <stdio.h> 2. int main()

2. int main() 3. {

3. { 4. int x = 3, y = 2;

4. int x = 1, y = 2; 5. int z = x << 1 > 5;

5. int z = x & y == 2; 6. printf("%d\n", z);

6. printf("%d\n", z); 7. }

7. } a) 1
b) 0
a) 0 c) 3
b) 1 d) Compile time error
c) Compile time error
d) Undefined behaviour Answer: a
Explanation: None.
Answer: b
Explanation: None. 6. What will be the output of the following C
code?
4. What will be the output of the following C
code? 1. #include <stdio.h>

1. #include <stdio.h> 2. int main()

2. int main() 3. {

3. { 4. int x = 3; //, y = 2;
5. const int *p = &x; 5. int z = x && y = 1;

6. *p++; 6. printf("%d\n", z);

7. printf("%d\n", *p); 7. }

8. } a) 0
b) 1
a) Increment of read-only location compile c) Compile time error
error d) 2
b) 4
c) Some garbage value Answer: c
d) Undefined behaviour Explanation: None.
Answer: c 9. What will be the output of the following C
Explanation: None. code?
7. What will be the output of the following C 1. #include <stdio.h>
code?
2. int main()
1. #include <stdio.h>
3. {
2. int main()
4. int x = 0, y = 2;
3. {
5. if (!x && y)
4. int x = 2, y = 2;
6. printf("true\n");
5. int z = x ^ y & 1;
7. else
6. printf("%d\n", z);
8. printf("false\n");
7. }
9. }
a) 1
b) 2 a) True
c) 0 b) False
d) 1 or 2 c) Compile time error
d) Undefined behaviour
Answer: b
Explanation: None. Answer: a
Explanation: None.
8. What will be the output of the following C
code? 10. What will be the output of the following
C code?
1. #include <stdio.h>
1. #include <stdio.h>
2. int main()
2. int main()
3. {
3. {
4. int x = 2, y = 0;
4. int x = 0, y = 2; a) 13
b) 14
5. int z = ~x & y; c) 12
d) 1 6
6. printf("%d\n", z);

7. } Answer: a
Explanation: None.
a) -1
b) 2 2. What will be the output of the following C
c) 0 code?
d) Compile time error
1. #include <stdio.h>
Answer: b
2. void main()
Explanation: None.
3. {
This section on online C test focuses on
4. int a = 2 + 4 + 3 * 5 / 3 -
“Precedence and Order of Evaluation”. One 5;
shall practice these test questions to improve
their C programming skills needed for various 5. printf("%d", a);
interviews (campus interviews, walkin
interviews, company interviews), placements, 6. }
entrance exams and other competitive exams.
These questions can be attempted by anyone a) 7
focusing on learning C Programming b) 6
language. They can be a beginner, fresher, c) 10
engineering graduate or an experienced IT d) 9
professional. Our online C test questions
come with detailed explanation of the Answer: b
answers which helps in better understanding Explanation: None.
of C concepts.
3. What will be the output of the following C
Here is a listing of online C test questions on code?
“Precedence and Order of Evaluation” along
1. #include <stdio.h>
with answers, explanations and/or solutions:
2. void main()
1. What will be the output of the following C
code? 3. {

1. #include <stdio.h> 4. int a = 5 * 3 % 6 - 8 + 3;

2. void main() 5. printf("%d", a);

3. { 6. }

4. int a = 5 * 3 + 2 - 4; a) 10
b) 2
5. printf("%d", a); c) -2
6. }
d) -3
Answer: c Answer: a
Explanation: None. Explanation: None.

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

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

2. void main() 2. void main()

3. { 3. {

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

4. double b = 8; 5. printf("%lf", b);

5. b++; 6. }

6. printf("%lf", b); a) 2
b) 30
7. }
c) 2.000000
d) Run time error
a) 9.000000
b) 9 Answer: c
c) 9.0 Explanation: None.
d) Run time error
8. What will be the output of the following C 2. void main()
code?
3. {
1. #include <stdio.h>
4. int k = 0;
2. void main()
5. double b = k++ + ++k + k--;
3. {
6. printf("%d", k);
4. double b = 3 && 5 & 4 % 3;
7. }
5. printf("%lf", b);
a) 6
6. } b) 1
c) 5
a) 3.000000 d) undefined
b) 4.000000
c) 5.000000 Answer: d
d) 1.000000 Explanation: None.

Answer: d Sanfoundry’s 1000+ Interview Questions &


Explanation: None. Answers on C helps anyone preparing for
Wipro and other companies C interviews.
9. What will be the output of the following C
One should practice these 1000+ interview
code?
questions and answers continuously for 2-3
1. #include <stdio.h>
months to clear Wipro interviews on C
Programming language.
2. void main()
Here is a listing of C programming interview
3. { questions on “Precedence and Order of
Evaluation” along with answers, explanations
4. double b = 5 & 3 && 4 || 5
and/or solutions:
| 6;

5. printf("%lf", b); 1. What will be the output of the following C


code?
6. }
1. #include <stdio.h>
a) 1.000000
b) 0.000000 2. void main()
c) 7.000000 3. {
d) 2.000000
4. int b = 5 - 4 + 2 * 5;
Answer: a
Explanation: None. 5. printf("%d", b);

10. What will be the output of the following 6. }


C code?
a) 25
1. #include <stdio.h> b) -5
c) 11 4. What will be the output of the following C
d) 16 code?

Answer: c 1. #include <stdio.h>


Explanation: None.
2. void main()
2. What will be the output of the following C
3. {
code?
4. int b = 5 + 7 * 4 - 9 * (3,
1. #include <stdio.h> 2);

2. void main() 5. printf("%d", b);

3. { 6. }

4. int b = 5 & 4 & 6;


a) 6
5. printf("%d", b); b) 15
c) 13
6. } d) 21

a) 5 Answer: b
b) 6 Explanation: None.
c) 3
d) 4 5. What will be the output of the following C
code?
Answer: d
Explanation: None. 1. #include <stdio.h>

3. What will be the output of the following C 2. void main()


code? 3. {

1. #include <stdio.h> 4. int h = 8;

2. void main() 5. int b = (h++, h++);

3. { 6. printf("%d%d\n", b, h);

4. int b = 5 & 4 | 6; 7. }

5. printf("%d", b);
a) 10 10
6. } b) 10 9
c) 9 10
a) 6 d) 8 10
b) 4
c) 1 Answer: c
d) 0 Explanation: None.

Answer: a 6. What will be the output of the following C


Explanation: None. code?
1. #include <stdio.h> 2. void main()

2. void main() 3. {

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

6. printf("%d\n", b); 8. }

7. } a) 0
b) a
a) 3 c) 1
b) 33 d) m
c) 34
d) Run time error Answer: c
Explanation: None.
Answer: a
Explanation: None. 10. What will be the output of the following
C code?
8. What will be the output of the following C
code? 1. #include <stdio.h>

1. #include <stdio.h> 2. void main()


3. { Answer: d
Explanation: None.
4. char a = 'A';
2. Which operators of the following have
5. char b = 'B';
same precedence?
6. int c = a + b % 3 - 3 * 2;
P. "!=", Q. "+=", R. "<<="
7. printf("%d\n", c);
a) P and Q
8. } b) Q and R
c) P and R
a) 65 d) P, Q and R
b) 58
c) 64 Answer: b
d) 59 Explanation: None.

Answer: d 3. Comment on the following statement.


Explanation: None.
n = 1;

This section on C interview questions and printf("%d, %dn", 3*n, n++);


answers focuses on “Precedence and Order of
Evaluation”. One shall practice these a) Output will be 3, 2
interview questions to improve their C b) Output will be 3, 1
programming skills needed for various c) Output will be 6, 1
interviews (campus interviews, walkin d) Output is compiler dependent
interviews, company interviews), placements,
entrance exams and other competitive exams. Answer: d
These questions can be attempted by anyone Explanation: None.
focusing on learning C Programming
language. They can be a beginner, fresher, 4. Which of the following option is the
engineering graduate or an experienced IT correct representation of the following C
professional. Our C Interview questions come statement?
with detailed explanation of the answers e = a * b + c / d * f;
which helps in better understanding of C
concepts. a) e = (a * (b +(c /(d * f))));
b) e = ((a * b) + (c / (d * f)));
Here is a listing of C interview questions on c) e = ((a * b) + ((c / d)* f));
“Precedence and Order of Evaluation” along d) Both e = ((a * b) + (c / (d * f))); and e = ((a
with answers, explanations and/or solutions: * b) + ((c / d)* f));
1. Which of the following operators has an Answer: d
associativity from Right to Left? Explanation: Verified by e = 1 * 2 + 3 / 4 *
a) <= 5; and then using respective braces according
b) << to the option.
c) ==
d) += 5. While swapping 2 numbers what
precautions to be taken care?
b = (b / a); c) = % * /
d) * % / =
a = a * b;

b = a / b; Answer: a
Explanation: None.
a) Data type should be either of short, int and
long 8. Which function in the following expression
b) Data type should be either of float and will be called first?
double
a = func3(6) - func2(4, 5) / func1(1, 2,
c) All data types are accepted except for (char 3);
*)
d) This code doesn’t swap 2 numbers a) func1();
b) func2();
Answer: b c) func3();
Explanation: None. d) Cannot be predicted
6. What will be the output of the following C Answer: d
code? Explanation: None.
1. #include<stdio.h> 9. Which of the following operator has the
2. int main()
highest precedence in the following?
a) ()
3. { b) sizeof
c) *
4. int a = 1, b = 2, c = 3, d d) +
= 4, e;
Answer: a
5. e = c + d = b * a;
Explanation: None.
6. printf("%d, %d\n", e, d);
10. Which of the following is a ternary
7. } operator?
a) &&
a) 7, 4 b) >>=
b) 7, 2 c) ?:
c) 5, 2 d) ->
d) Syntax error
Answer: c
Answer: d Explanation: None.
Explanation: None.
Sanfoundry’s 1000+ MCQs on C helps
7. Which of the following is the correct order
anyone preparing for placement in Accenture
of evaluation for the given expression?
and other companies. Anyone looking for
a = w % x / y * z; Accenture placement papers should practice
these 1000+ questions continuously for 2-3
a) % / * = months, thereby ensuring a top position in
b) / * % = placements.
Here is a listing of C questions on associativity
“Precedence and Order of Evaluation” along c) Same precedence, different associativity
with answers, explanations and/or solutions: d) All of the mentioned

1. Which of the following are unary Answer: c


operators? Explanation: None.
a) sizeof
b) – 6. Which of the following is possible with
c) ++ any 2 operators in C?
d) all of the mentioned a) Same associativity, different precedence
b) Same associativity, same precedence
Answer: d c) Different associativity, different
Explanation: None. precedence
d) All of the mentioned
2. Where in C the order of precedence of
operators do not exist? Answer: d
a) Within conditional statements, if, else Explanation: None.
b) Within while, do-while
c) Within a macro definition 7. Which of the following operators has the
d) None of the mentioned lowest precedence?
a) !=
Answer: d b) &&
Explanation: None. c) ?:
d) ,
3. Associativity of an operator is
Answer: d
a) Right to Left Explanation: None.
b) Left to Right
c) Random fashion 8. What will be the output of the following C
d) Both Right to Left and Left to Right code?

Answer: d 1. #include <stdio.h>


Explanation: None.
2. int main()
4. Which of the following method is accepted
3. {
for assignment?
a) 5 = a = b = c = d; 4. int x = 3, i = 0;
b) a = b = c = d = 5;
c) a = b = 5 = c = d; 5. do {
d) None of the mentioned
6. x = x++;
Answer: b
7. i++;
Explanation: None.
8. } while (i != 3);
5. Which of the following is NOT possible
with any 2 operators in C? 9. printf("%d\n", x);
a) Different precedence, same associativity
b) Different precedence, different 10. }
a) Undefined behaviour 7. else
b) Output will be 3
c) Output will be 6 8. printf("No Output\n");
d) Output will be 5 9. }

Answer: c a) 1
Explanation: None. b) 10
c) 20
9. What will be the output of the following C
d) No Output
code?
Answer: a
1. #include <stdio.h>
Explanation: None.
2. int main()
This section on C programming questions and
3. {
answers focuses on “If-then-else Statements”.
4. int a = -1, b = 4, c = 1, d One shall practice these questions to improve
; their C programming skills needed for various
interviews (campus interviews, walkin
5. d = ++a && ++b || ++c; interviews, company interviews), placements,
entrance exams and other competitive exams.
6. printf("%d, %d, %d, %d\n",
a, b, c, d);
These questions can be attempted by anyone
focusing on learning C Programming
7. return 0; language. They can be a beginner, fresher,
engineering graduate or an experienced IT
8. } professional. Our C programming questions
come with detailed explanation of the
a) 0, 4, 2, 1 answers which helps in better understanding
b) 0, 5, 2, 1 of C concepts.
c) -1, 4, 1, 1
d) 0, 5, 1, 0 Here is a listing of C programming questions
on “If-then-else Statements” along with
Answer: a answers, explanations and/or solutions:
Explanation: None.
1. What will be the output of the following C
10. What will be the output of the following code?
C code?
1. #include <stdio.h>
1. #include <stdio.h>
2. void main()
2. int main()
3. {
3. {
4. int x = 5;
4. int p = 10, q = 20, r;
5. if (x < 1)
5. if (r = p = 5 || q > 20)
6. printf("hello");
6. printf("%d", r);
7. if (x == 5)
8. printf("hi"); 3. {

9. else 4. int x = 5;

10. printf("no"); 5. if (true);

11. } 6. printf("hello");

a) hi 7. }
b) hello
c) no a) It will display hello
d) error b) It will throw an error
c) Nothing will be displayed
Answer: a d) Compiler dependent
Explanation: None.
Answer: b
2. What will be the output of the following C Explanation: None.
code?
4. What will be the output of the following C
1. #include <stdio.h> code?

2. int x; 1. #include <stdio.h>

3. void main() 2. void main()

4. { 3. {

5. if (x) 4. int x = 0;

6. printf("hi"); 5. if (x == 0)

7. else 6. printf("hi");

8. printf("how are u"); 7. else

9. } 8. printf("how are u");

a) hi 9. printf("hello");
b) how are you
c) compile time error 10. }

d) error a) hi
Answer: b b) how are you
c) hello
Explanation: None.
d) hihello
3. What will be the output of the following C
Answer: d
code?
Explanation: None.
1. #include <stdio.h>
5. What will be the output of the following C
2. void main() code?
1. #include <stdio.h> 13. printf("2");

2. void main() 14. break;

3. { 15. }

4. int x = 5; 16. }

5. if (x < 1); a) Compile time error


b) 1
6. printf("Hello");
c) 2
7. d) Varies

8. } Answer: a
Explanation: None.
a) Nothing
b) Run time error 7. What will be the output of the following C
c) Hello code? (Assuming that we have entered the
d) Varies value 1 in the standard input)

Answer: c 1. #include <stdio.h>


Explanation: None.
2. void main()
6. What will be the output of the following C 3. {
code? (Assuming that we have entered the
value 1 in the standard input) 4. char *ch;

1. #include <stdio.h> 5. printf("enter a value betwe


en 1 to 3:");
2. void main()
6. scanf("%s", ch);
3. {
7. switch (ch)
4. double ch;
8. {
5. printf("enter a value betwe
en 1 to 2:"); 9. case "1":

6. scanf("%lf", &ch); 10. printf("1");

7. switch (ch) 11. break;

8. { 12. case "2":

9. case 1: 13. printf("2");

10. printf("1"); 14. break;

11. break; 15. }

12. case 2: 16. }


a) 1 1. #include <stdio.h>
b) 2
c) Compile time error 2. void main()
d) No Compile time error 3. {

Answer: c 4. int ch;


Explanation: None.
5. printf("enter a value betwe
8. What will be the output of the following C en 1 to 2:");
code? (Assuming that we have entered the
value 1 in the standard input) 6. scanf("%d", &ch);

7. switch (ch)
1. #include <stdio.h>
8. {
2. void main()
9. case 1:
3. {
10. printf("1\n");
4. int ch;
11. break;
5. printf("enter a value betwe
en 1 to 2:"); 12. printf("Hi");
6. scanf("%d", &ch); 13. default:

7. switch (ch) 14. printf("2\n");


8. { 15. }

9. case 1: 16. }

10. printf("1\n");
a) 1
11. default: b) Hi 2
c) Run time error
12. printf("2\n"); d) 2
13. } Answer: d
Explanation: None.
14. }

10. What will be the output of the following


a) 1
C code? (Assuming that we have entered the
b) 2
value 1 in the standard input)
c) 1 2
d) Run time error 1. #include <stdio.h>

Answer: c 2. void main()


Explanation: None.
3. {
9. What will be the output of the following C
code? (Assuming that we have entered the 4. int ch;
value 2 in the standard input)
5. printf("enter a value betwe 3. {
en 1 to 2:");
4. int x = 1;
6. scanf("%d", &ch);
5. if (x > 0)
7. switch (ch, ch + 1)
6. printf("inside if\n");
8. {
7. else if (x > 0)
9. case 1:
8. printf("inside elseif\n
10. printf("1\n"); ");

11. break; 9. }

12. case 2: a) inside if


b) inside elseif
13. printf("2");
c)
14. break;
inside if
15. }
inside elseif
16. }
d) compile time error
a) 1
b) 2 Answer: a
c) 3 Explanation: None.
d) Run time error
2. What will be the output of the following C
Answer: b code?
Explanation: None.
1. #include <stdio.h>

Sanfoundry’s 1000+ Interview Questions & 2. int main()


Answers on C helps anyone preparing for
Infosys and other companies C interviews. 3. {
One should practice these 1000+ interview 4. int x = 0;
questions and answers continuously for 2-3
months to clear Infosys interviews on C 5. if (x++)
Programming language.
6. printf("true\n");
Here is a listing of C test questions on “If-
then-else Statements” along with answers, 7. else if (x == 1)

explanations and/or solutions: 8. printf("false\n");

1. What will be the output of the following C 9. }


code?
a) true
1. #include <stdio.h> b) false
c) compile time error
2. int main()
d) undefined behaviour
Answer: b 6. printf("true, ");
Explanation: None.
7. else if (x = 10)
3. What will be the output of the following C
8. printf("false, ");
code?
9. printf("%d\n", x);
1. #include <stdio.h>
10. }
2. int main()
a) false, 0
3. {
b) true, 0
4. int x = 0; c) true, 10
d) compile time error
5. if (x == 1)
Answer: b
6. if (x == 0) Explanation: None.
7. printf("inside if\n
5. What will be the output of the following C
");
code?
8. else
1. #include <stdio.h>
9. printf("inside else
if\n"); 2. int main()

10. else 3. {

11. printf("inside else\n") 4. int x = 0;


;
5. if (x == 1)
12. }
6. if (x >= 0)
a) inside if
b) inside else if 7. printf("true\n");
c) inside else 8. else
d) compile time error
9. printf("false\n");
Answer: c
Explanation: None. 10. }

4. What will be the output of the following C a) true


code? b) false
c) Depends on the compiler
1. #include <stdio.h> d) No print statement
2. int main() Answer: d
Explanation: None.
3. {

4. int x = 0; 6. The C statement “”if (a == 1 || b == 2) {}””


can be re-written as
5. if (x == 0) a)
if (a == 1) c) True False
if (b == 2){} d) No Output
b)
Answer: a
Explanation: None.
if (a == 1){}
if (b == 2){}
9. What will be the output of the following C
c) code?

if (a == 1){} 1. #include <stdio.h>


else if (b == 2){}
2. int main()
d) none of the mentioned
3. {
Answer: d
4. int a = 1;
Explanation: None.
5. if (a)
7. Which of the following is an invalid if-else
statement? 6. printf("All is Well ");
a) if (if (a == 1)){}
b) if (func1 (a)){} 7. printf("I am Well\n");
c) if (a){} 8. else
d) if ((char) a){}
9. printf("I am not a Rive
Answer: a r\n");
Explanation: None.
10. }
8. What will be the output of the following C
code? a) Output will be All is Well I am Well
b) Output will be I am Well I am not a River
1. #include <stdio.h> c) Output will be I am Well
d) Compile time errors during compilation
2. int main()
Answer: d
3. { Explanation: None.
4. int a = 1;
10. What will be the output of the following
5. if (a--) C code?

6. printf("True"); 1. #include <stdio.h>

7. if (a++) 2. int main()

8. printf("False"); 3. {

9. } 4. if (printf("%d", printf(
")))
a) True
b) False 5. printf("We are Happy");
6. else if (printf("1")) 7. switch (ch)

7. printf("We are Sad"); 8. {

8. } 9. case 1:

a) 0We are Happy 10. printf("1");


b) 1We are Happy
11. break;
c) 1We are Sad
d) compile time error 12. case 2:

Answer: d 13. printf("2");


Explanation: None.
14. break;

This section on C interview questions and 15. }


answers focuses on “Switch Statements”. One
shall practice these interview questions to 16. }
improve their C programming skills needed
for various interviews (campus interviews, a) Compile time error
walkin interviews, company interviews), b) 1
placements, entrance exams and other c) 2
competitive exams. These questions can be d) Varies
attempted by anyone focusing on learning C
Programming language. They can be a Answer: a
beginner, fresher, engineering graduate or an Explanation: None.
experienced IT professional. Our C Interview
2. What will be the output of the following C
questions come with detailed explanation of
code? (Assuming that we have entered the
the answers which helps in better
value 1 in the standard input)
understanding of C concepts.
1. #include <stdio.h>
Here is a listing of C interview questions on
“Switch Statements” along with answers, 2. void main()
explanations and/or solutions:
3. {
1. What will be the output of the following C
code? (Assuming that we have entered the 4. char *ch;
value 1 in the standard input)
5. printf("enter a value betwe
en 1 to 3:");
1. #include <stdio.h>
6. scanf("%s", ch);
2. void main()
7. switch (ch)
3. {
8. {
4. double ch;
9. case "1":
5. printf("enter a value betwe
en 1 to 2:"); 10. printf("1");
6. scanf("%lf", &ch);
11. break; a) 1
b) 2
12. case "2": c) 1 2
13. printf("2");
d) Run time error

14. break; Answer: c


Explanation: None.
15. }
4. What will be the output of the following C
16. } code? (Assuming that we have entered the
value 2 in the standard input)
a) 1
b) Compile time error 1. #include <stdio.h>
c) 2
d) Run time error 2. void main()

Answer: b 3. {
Explanation: None.
4. int ch;

3. What will be the output of the following C 5. printf("enter a value betwe


code? (Assuming that we have entered the en 1 to 2:");
value 1 in the standard input)
6. scanf("%d", &ch);
1. #include <stdio.h>
7. switch (ch)
2. void main()
8. {
3. {
9. case 1:
4. int ch;
10. printf("1\n");
5. printf("enter a value betwe
en 1 to 2:"); 11. break;

6. scanf("%d", &ch); 12. printf("hi");

7. switch (ch) 13. default:

8. { 14. printf("2\n");

9. case 1: 15. }

10. printf("1\n"); 16. }

11. default: a) 1
b) hi 2
12. printf("2\n"); c) Run time error
13. }
d) 2

14. } Answer: d
Explanation: None.
5. What will be the output of the following C 4. int a = 1, b = 1;
code? (Assuming that we have entered the
value 1 in the standard input) 5. switch (a)

6. {
1. #include <stdio.h>

2. void main()
7. case a*b:

3. {
8. printf("yes ");

9. case a-b:
4. int ch;
10. printf("no\n");
5. printf("enter a value betwe
en 1 to 2:");
11. break;
6. scanf("%d", &ch);
12. }
7. switch (ch, ch + 1)
13. }
8. {
a) yes
9. case 1: b) no
c) Compile time error
10. printf("1\n"); d) yes no
11. break;
Answer: c
12. case 2: Explanation: None.

13. printf("2"); 7. What will be the output of the following C


code?
14. break;
1. #include <stdio.h>
15. }
2. int main()
16. }
3. {
a) 1
b) 2 4. int x = 97;
c) 3
d) Run time error 5. switch (x)

6. {
Answer: b
Explanation: None. 7. case 'a':

6. What will be the output of the following C 8. printf("yes ");


code?
9. break;
1. #include <stdio.h>
10. case 97:
2. int main()
11. printf("no\n");
3. {
12. break; Sanfoundry’s 1000+ Interview Questions &
Answers on C helps anyone preparing for
13. } HCL and other companies C interviews. One
14. }
should practice these 1000+ interview
questions and answers continuously for 2-3
a) yes months to clear HCL interviews on C
b) yes no Programming language.
c) Duplicate case value error
d) Character case value error Here is a listing of C interview questions on
“Switch Statements” along with answers,
Answer: c explanations and/or solutions:
Explanation: None.
1. What will be the output of the following C
8. What will be the output of the following C code?
code?
1. #include <stdio.h>
1. #include <stdio.h>
2. const int a = 1, b = 2;
2. int main()
3. int main()
3. {
4. {
4. float f = 1;
5. int x = 1;
5. switch (f)
6. switch (x)
6. {
7. {
7. case 1.0:
8. case a:
8. printf("yes\n");
9. printf("yes ");
9. break;
10. case b:
10. default:
11. printf("no\n");
11. printf("default\n");
12. break;
12. }
13. }
13. }
14. }

a) yes a) yes no
b) yes default b) yes
c) Undefined behaviour c) no
d) Compile time error d) Compile time error
Answer: d Answer: d
Explanation: None. Explanation: None.
2. What will be the output of the following C 7. printf("First\n");
code?
8. break;
1. #include <stdio.h>
9. case 2:
2. #define max(a) a
10. printf("Second\n");
3. int main()
11. break;
4. {
12. default:
5. int x = 1;
13. printf("Default\n");
6. switch (x)
14. break;
7. {
15. }
8. case max(2):
16. }
9. printf("yes\n");
a) Do
10. case max(1): b) DoFirst
c) DoSecond
11. printf("no\n"); d) DoDefault
12. break;
Answer: c
13. } Explanation: None.

14. } 4. Comment on the output of the following C


code.
a) yes no
b) yes 1. #include <stdio.h>
c) no
d) Compile time error 2. int main()

3. {
Answer: c
Explanation: None. 4. int a = 1;

3. What will be the output of the following C 5. switch (a)


code?
6. case 1:
1. #include <stdio.h>
7. printf("%d", a);
2. int main()
8. case 2:
3. {
9. printf("%d", a);
4. switch (printf("Do"))
10. case 3:
5. {
11. printf("%d", a);
6. case 1:
12. default: a) Output: Case A
b) Output: Default
13. printf("%d", a); c) Output: Case A Default
14. }
d) Compile time error

a) No error, output is 1111 Answer: d


b) No error, output is 1 Explanation: None.
c) Compile time error, no break statements
7. What will be the output of the following C
d) Compile time error, case label outside
code?
switch statement
1. #include <stdio.h>
Answer: d
Explanation: None. 2. switch (ch)

5. Which datatype can accept the switch 3. {


statement?
a) int 4. case 'a':
b) char 5. case 'A':
c) long
d) all of the mentioned 6. printf("true");

Answer: d 7. }
Explanation: None.
a) if (ch == ‘a’ && ch == ‘A’) printf(“true”);
6. What will be the output of the following C b)
code?
if (ch == 'a')
1. #include <stdio.h>
if (ch == 'a') printf("true");
2. int main()
c) if (ch == ‘a’ || ch == ‘A’) printf(“true”);
3. { d) none of the mentioned

4. int a = 1; Answer: c
Explanation: None.
5. switch (a)

6. { This section on C language interview


questions and answers focuses on “For
7. case a: Loops”. One shall practice these interview
8. printf("Case A ");
questions to improve their C programming
skills needed for various interviews (campus
9. default: interviews, walkin interviews, company
interviews), placements, entrance exams and
10. printf("Default"); other competitive exams. These questions can
be attempted by anyone focusing on learning
11. }
C Programming language. They can be a
12. } beginner, fresher, engineering graduate or an
experienced IT professional. Our C language
interview questions come with detailed 4. Which of the following cannot be used as
explanation of the answers which helps in LHS of the expression in for (exp1;exp2;
better understanding of C concepts. exp3)?
a) variable
Here is a listing of C language interview b) function
questions on “For Loops” along with c) typedef
answers, explanations and/or solutions: d) macros

1. The C code ‘for(;;)’ represents an infinite Answer: d


loop. It can be terminated by Explanation: None.
a) break
b) exit(0) 5. What will be the output of the following C
c) abort() code?
d) terminate
1. #include <stdio.h>
Answer: a
Explanation: None. 2. int main()

3. {
2. What will be the correct syntax for running
two variable for loop simultaneously? 4. short i;
a)
5. for (i = 1; i >= 0; i++)
for (i = 0; i < n; i++)
for (j = 0; j < n; j += 5) 6. printf("%d\n", i);

b) 7.

for (i = 0, j = 0; i < n, j < n; i++, j + 8. }


= 5)
a) The control won’t fall into the for loop
c) b) Numbers will be displayed until the signed
limit of short and throw a runtime error
for (i = 0; i < n;i++){}
c) Numbers will be displayed until the signed
for (j = 0; j < n;j += 5){}
limit of short and program will successfully
d) none of the mentioned terminate
d) This program will get into an infinite loop
Answer: b and keep printing numbers with no errors
Explanation: None.
Answer: c
3. Which for loop has range of similar Explanation: None.
indexes of ‘i’ used in for (i = 0;i < n; i++)?
a) for (i = n; i>0; i–) 6. What will be the output of the following C
b) for (i = n; i >= 0; i–) code?
c) for (i = n-1; i>0; i–)
d) for (i = n-1; i>-1; i–) 1. #include <stdio.h>

2. void main()
Answer: d
Explanation: None. 3. {
4. int k = 0; 5. for (k = 0.0; k < 3.0; k++)

5. for (k) 6. printf("Hello");

6. printf("Hello"); 7. }

7. } a) Run time error


b) Hello is printed thrice
a) Compile time error c) Hello is printed twice
b) hello d) Hello is printed infinitely
c) Nothing
d) Varies Answer: b
Explanation: None.
Answer: a
Explanation: None.
Sanfoundry’s 1000+ Interview Questions &
7. What will be the output of the following C Answers on C helps anyone preparing for
code? Accenture and other companies C interviews.
One should practice these 1000+ interview
1. #include <stdio.h> questions and answers continuously for 2-3
months to clear Accenture interviews on C
2. void main() Programming language.
3. {
Here is a listing of online C test questions on
4. int k = 0;
“For Loops” along with answers,
explanations and/or solutions:
5. for (k < 3; k++)
1. What will be the output of the following C
6. printf("Hello"); code?
7. } 1. #include <stdio.h>

a) Compile time error 2. void main()


b) Hello is printed thrice
c) Nothing 3. {
d) Varies
4. double k = 0;
Answer: a 5. for (k = 0.0; k < 3.0; k++)
Explanation: None. ;

8. What will be the output of the following C 6. printf("%lf", k);


code?
7. }
1. #include <stdio.h>
a) 2.000000
2. void main() b) 4.000000
c) 3.000000
3. { d) Run time error
4. double k = 0;
Answer: c Answer: a
Explanation: None. Explanation: None.

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

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

2. void main() 2. int main()

3. { 3. {

4. int k; 4. int i = 0;

5. for (k = -3; k < -5; k++) 5. for (i++; i == 1; i = 2)

6. printf("Hello"); 6. printf("In for loop ");

7. } 7. printf("After loop\n");

a) Hello 8. }
b) Infinite hello
c) Run time error a) In for loop after loop
d) Nothing b) After loop
c) Compile time error
Answer: d d) Undefined behaviour
Explanation: None.
Answer: a
3. What will be the output of the following C Explanation: None.
code?
5. What will be the output of the following C
1. #include <stdio.h> code?

2. int main() 1. #include <stdio.h>

3. { 2. int main()

4. int i = 0; 3. {

5. for (; ; ;) 4. int i = 0;

6. printf("In for loop\n") 5. for (foo(); i == 1; i = 2)


;
6. printf("In for loop\n")
7. printf("After loop\n"); ;

8. } 7. printf("After loop\n");

a) Compile time error 8. }


b) Infinite loop
9. int foo()
c) After loop
d) Undefined behaviour 10. {
11. return 1; 5. printf("In for loop\n")
;
12. }
6. }
a) After loop
b) In for loop after loop a) Compile time error
c) Compile time error b) In for loop
d) Infinite loop c) Depends on the standard compiler
implements
Answer: a d) Depends on the compiler
Explanation: None.
Answer: c
6. What will be the output of the following C Explanation: None.
code?
This section on online C test focuses on
1. #include <stdio.h>
“While Loops”. One shall practice these test
2. int main() questions to improve their C programming
skills needed for various interviews (campus
3. { interviews, walkin interviews, company
interviews), placements, entrance exams and
4. int *p = NULL; other competitive exams. These questions can
be attempted by anyone focusing on learning
5. for (foo(); p; p = 0)
C Programming language. They can be a
6. printf("In for loop\n") beginner, fresher, engineering graduate or an
; experienced IT professional. Our online C
test questions come with detailed explanation
7. printf("After loop\n"); of the answers which helps in better
8. }
understanding of C concepts.

a) In for loop after loop Here is a listing of online C test questions on


“While Loops” along with answers,
b) Compile time error
explanations and/or solutions:
c) Infinite loop
d) Depends on the value of NULL 1. What will be the output of the following C
code?
Answer: b
Explanation: None. 1. #include <stdio.h>

7. What will be the output of the following C 2. int main()


code?
3. {
1. #include <stdio.h>
4. while ()
2. int main()
5. printf("In while loop "
3. { );

4. for (int i = 0;i < 1; i++) 6. printf("After loop\n");

7. }
a) In while loop after loop 5. do {
b) After loop
c) Compile time error 6. i++;
d) Infinite loop 7. printf("In while loop\n
");
Answer: c
Explanation: None. 8. } while (i < 3);

2. What will be the output of the following C 9. }


code?
a)
1. #include <stdio.h>
In while loop
2. int main()
In while loop
3. {
In while loop
4. do
b)
5. printf("In while loop "
); In while loop

6. while (0); In while loop

7. printf("After loop\n"); c) Depends on the compiler


d) Compile time error
8. }
Answer: a
a) In while loop Explanation: None.
b)
4. How many times i value is checked in the
In while loop
following C code?
after loop
1. #include <stdio.h>
c) After loop
2. int main()
d) Infinite loop
3. {
Answer: b
Explanation: None. 4. int i = 0;

3. What will be the output of the following C 5. do {


code?
6. i++;
1. #include <stdio.h>
7. printf("in while loop\n
");
2. int main()
8. } while (i < 3);
3. {
9. }
4. int i = 0;
a) 2 8. } while (i < 2)
b) 3
c) 4 9. }
d) 1
a) Compile time error
Answer: b b) Hi Hi
Explanation: None. c) Hi
d) Varies
5. How many times i value is checked in the
following C code? Answer: a
Explanation: None.
1. #include <stdio.h>
7. What will be the output of the following C
2. int main() code?
3. { 1. #include <stdio.h>

4. int i = 0; 2. void main()

5. while (i < 3) 3. {

6. i++; 4. int i = 0;

7. printf("In while loop\n"); 5. while (++i)

8. } 6. {

a) 2 7. printf("H");
b) 3
c) 4 8. }
d) 1 9. }

Answer: c a) H
Explanation: None. b) H is printed infinite times
c) Compile time error
6. What will be the output of the following C
d) Varies
code?
Answer: b
1. #include <stdio.h>
Explanation: None.
2. void main()
8. What will be the output of the following C
3. { code?
4. int i = 2; 1. #include <stdio.h>

5. do 2. void main()

6. { 3. {

7. printf("Hi"); 4. int i = 0;
5. do a) Nothing
b) Run time error
6. { c) Varies
7. printf("Hello");
d) Hello is printed infinite times

8. } while (i != 0); Answer: d


Explanation: None.
9. }
2. What will be the output of the following C
a) Nothing code?
b) H is printed infinite times
c) Hello 1. #include <stdio.h>
d) Run time error
2. void main()
Answer: c 3. {
Explanation: None.
4. int i = 0;
Sanfoundry’s 1000+ Interview Questions &
5. while (i < 10)
Answers on C helps anyone preparing for
Capgemini and other companies C interviews. 6. {
One should practice these 1000+ interview
questions and answers continuously for 2-3 7. i++;
months to clear Capgemini interviews on C
Programming language. 8. printf("hi\n");

9. while (i < 8)
Here is a listing of basic C interview
questions on “While Loops” along with 10. {
answers, explanations and/or solutions:
11. i++;
1. What will be the output of the following C
code? 12. printf("hello\n");

1. #include <stdio.h>
13. }

2. void main()
14. }

3. {
15. }

4. char *str = ""; a) Hi is printed 8 times, hello 7 times and


then hi 2 times
5. do b) Hi is printed 10 times, hello 7 times
c) Hi is printed once, hello 7 times
6. {
d) Hi is printed once, hello 7 times and then
7. printf("hello"); hi 2 times

8. } while (str); Answer: d


Explanation: None.
9. }
3. What is an example of iteration in C? a) True (infinite time)
a) for b) True (1 time) False
b) while c) False
c) do-while d) Compiler dependent
d) all of the mentioned
Answer: c
Answer: d Explanation: None.
Explanation: None.
6. What will be the output of the following C
4. How many times while loop condition is code?
tested in the following C code snippets, if i is
initialized to 0 in both the cases? 1. #include <stdio.h>

1. while (i < n) 2. int main()

2. i++; 3. {

3. ————- 4. int i = 0, j = 0;

4. do 5. while (i < 5, j < 10)

5. i++; 6. {

6. while (i <= n); 7. i++;

a) n, n 8. j++;

b) n, n+1 9. }
c) n+1, n
d) n+1, n+1 10. printf("%d, %d\n", i, j);

Answer: d 11. }
Explanation: None.
a) 5, 5
5. What will be the output of the following C b) 5, 10
code? c) 10, 10
d) Syntax error
1. #include <stdio.h>
Answer: c
2. int main() Explanation: None.
3. { 7. Which loop is most suitable to first
4. int i = 0;
perform the operation and then test the
condition?
5. while (i = 0) a) for loop
b) while loop
6. printf("True\n"); c) do-while loop
d) none of the mentioned
7. printf("False\n");

8. }
Answer: c
Explanation: None.
9. }
This section on C interview questions and
answers focuses on “Break and Continue 10. }
statements”. One shall practice these
interview questions to improve their C a) 2
programming skills needed for various b) 3
interviews (campus interviews, walkin c) 4
interviews, company interviews), placements, d) 5
entrance exams and other competitive exams.
These questions can be attempted by anyone Answer: d
focusing on learning C Programming Explanation: None.
language. They can be a beginner, fresher,
engineering graduate or an experienced IT 3. What will be the output of the following C
professional. Our C Interview questions come code?
with detailed explanation of the answers
which helps in better understanding of C 1. #include <stdio.h>
concepts. 2. int main()

Here is a listing of C interview questions on 3. {


“Break and Continue statements” along with
answers, explanations and/or solutions: 4. int a = 0, i = 0, b;

1. Which keyword can be used for coming 5. for (i = 0;i < 5; i++)
out of recursion?
6. {
a) break
b) return 7. a++;
c) exit
d) both break and return 8. if (i == 3)

Answer: b 9. break;
Explanation: None.
10. }
2. What will be the output of the following C 11. }
code?
a) 1
1. #include <stdio.h>
b) 2
2. int main() c) 3
d) 4
3. {
Answer: d
4. int a = 0, i = 0, b; Explanation: None.
5. for (i = 0;i < 5; i++) 4. The keyword ‘break’ cannot be simply
6. {
used within
a) do-while
7. a++; b) if-else
c) for
8. continue; d) while
Answer: b 7. What will be the output of the following C
Explanation: None. code?

5. Which keyword is used to come out of a 1. #include <stdio.h>


loop only for that iteration?
a) break 2. void main()
b) continue 3. {
c) return
d) none of the mentioned 4. int i = 0;

Answer: b 5. int j = 0;
Explanation: None.
6. for (i = 0;i < 5; i++)
6. What will be the output of the following C
7. {
code?
8. for (j = 0;j < 4; j++)
1. #include <stdio.h>
9. {
2. void main()
10. if (i > 1)
3. {
11. continue;
4. int i = 0, j = 0;
12. printf("Hi \n")
5. for (i = 0;i < 5; i++) ;

6. { 13. }

7. for (j = 0;j < 4; j++) 14. }

8. { 15. }

9. if (i > 1)
a) Hi is printed 9 times
10. break; b) Hi is printed 8 times
c) Hi is printed 7 times
11. } d) Hi is printed 6 times
12. printf("Hi \n"); Answer: b
Explanation: None.
13. }

14. }
8. What will be the output of the following C
code?
a) Hi is printed 5 times
b) Hi is printed 9 times 1. #include <stdio.h>

c) Hi is printed 7 times 2. void main()


d) Hi is printed 4 times
3. {
Answer: a
Explanation: None. 4. int i = 0;
5. for (i = 0;i < 5; i++) 8. continue;

6. if (i < 4) 9. }

7. { 10. }

8. printf("Hello"); a) Hello is printed infinite times


b) Hello
9. break;
c) Varies
10. } d) Compile time error

11. } Answer: d
Explanation: None.
a) Hello is printed 5 times
b) Hello is printed 4 times 2. What will be the output of the following C
c) Hello code?
d) Hello is printed 3 times
1. #include <stdio.h>
Answer: c
Explanation: None. 2. void main()

3. {
Sanfoundry’s 1000+ MCQs on C helps
anyone preparing for placement in Microsoft 4. int i = 0;
and other companies. Anyone looking for 5. if (i == 0)
Microsoft placement papers should practice
these 1000+ questions continuously for 2-3 6. {
months, thereby ensuring a top position in
placements. 7. printf("Hello");

Here is a listing of basic C questions on 8. break;


“Break and Continue” along with answers, 9. }
explanations and/or solutions:
10. }
1. What will be the output of the following C
code? a) Hello is printed infinite times
b) Hello
1. #include <stdio.h> c) Varies
d) Compile time error
2. void main()

3. { Answer: d
Explanation: None.
4. int i = 0;
3. What will be the output of the following C
5. if (i == 0) code?
6. { 1. #include <stdio.h>
7. printf("Hello"); 2. int main()
3. { 11. printf("2\n");

4. int i = 0; 12. }

5. do 13. printf("after loop\n");

6. { 14. }

7. i++; a)
8. if (i == 2) 1
2
9. continue; after loop

10. printf("In while lo b)


op ");
1
11. } while (i < 2);
after loop
12. printf("%d\n", i);
c)
13. }
1
2
a) In while loop 2 1
b) In while loop in while loop 3 2
c) In while loop 3 after loop
d) Infinite loop
d)
Answer: a
Explanation: None. 1
1
2
4. What will be the output of the following C after loop
code?
Answer: c
1. #include <stdio.h> Explanation: None.
2. int main()

3. {
5. What will be the output of the following C
4. int i = 0, j = 0; code?

5. for (i; i < 2; i++){ 1. #include <stdio.h>

6. for (j = 0; j < 3; j++) 2. int main()

7. { 3. {

8. printf("1\n"); 4. int i = 0;

9. break; 5. while (i < 2)

10. } 6. {
7. if (i == 1) 7. {

8. break; 8. i++;

9. i++; 9. switch (c)

10. if (i == 1) 10. {

11. continue; 11. case 'a':

12. printf("In whil 12. printf("%c ", c)


e loop\n"); ;

13. } 13. break;

14. printf("After loop\n"); 14. break;

15. } 15. }

a) 16. }

In while loop 17. printf("after loop\n");

After loop 18. }

b) After loop a) a after loop


c) b) a a after loop
c) after loop
In while loop d) error
In while loop Answer: b
After loop
Explanation: None.

d) In while loop 7. What will be the output of the following C


code?
Answer: b
Explanation: None. 1. #include <stdio.h>

2. int main()
6. What will be the output of the following C
code? 3. {

1. #include <stdio.h> 4. printf("before continue ");

2. int main() 5. continue;

3. { 6. printf("after continue\n");

4. int i = 0; 7. }

5. char c = 'a'; a) Before continue after continue


6. while (i < 2)
b) Before continue
c) After continue a) 1 4
d) Compile time error b) Compilation error
c) 1 2 4
Answer: d d) 1 3 4
Explanation: None.
Answer: a
This section on tough C programming Explanation: None.
questions focuses on “Goto & Labels”. One
2. What will be the output of the following C
shall practice these questions to improve their
C programming skills needed for various code?
interviews (campus interviews, walkin 1. #include <stdio.h>
interviews, company interviews), placements,
entrance exams and other competitive exams. 2. int main()
These questions can be attempted by anyone
focusing on learning C Programming 3. {
language. They can be a beginner, fresher,
engineering graduate or an experienced IT 4. printf("%d ", 1);

professional. Our C programming questions 5. l1:l2:


come with detailed explanation of the
answers which helps in better understanding 6. printf("%d ", 2);
of C concepts.
7. printf("%d\n", 3);
Here is a listing of tough C programming
questions on “Goto & Labels” along with 8. }

answers, explanations and/or solutions: a) Compilation error


1. What will be the output of the following C b) 1 2 3
code? c) 1 2
d) 1 3
1. #include <stdio.h>
Answer: b
2. int main() Explanation: None.

3. { 3. What will be the output of the following C


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

6. {
4. What will be the output of the following C
code? 7. i++;

1. #include <stdio.h> 8. while (j < 3)

2. int main() 9. {

3. { 10. printf("loop\n");

4. int i = 0, j = 0; 11. goto l1;

5. while (i < 2) 12. }

6. { 13. }

7. l1 : i++; 14. }

8. while (j < 3) a) loop loop


b) Compilation error
9. {
c) loop loop loop loop
10. printf("Loop\n"); d) Infinite loop

11. goto l1; Answer: b


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

10. printf("loop\n"); 1. #include <stdio.h>

11. goto l1; 2. void main()

12. } 3. {

13. } 4. int i = 0, k;

14. } 5. if (i == 0)

a) loop loop 6. goto label;


b) compilation error
c) oop loop loop loop 7. for (k = 0;k < 3; k++)
d) infinite loop 8. {

Answer: a 9. printf("hi\n");
Explanation: None.
10. label: k = printf(
7. What will be the output of the following C "%03d", i);
code?
11. }
1. #include <stdio.h> 12. }
2. void main()
a) 0
3. { b) hi hi hi 0 0 0
c) 0 hi hi hi 0 0 0
4. int i = 0; d) 0 0 0
5. if (i == 0) Answer: a
Explanation: None.
6. {

7. goto label; 9. What will be the output of the following C


code?
8. }
1. #include <stdio.h>
9. label: printf("Hello");
2. void main()
10. }
3. {
a) Nothing
b) Error 4. int i = 0, k;
5. label: printf("%d", i); c) 5 Hey
d) Nothing
6. if (i == 0)
Answer: c
7. goto label;
Explanation: None.
8. }
2. goto can be used to jump from main() to
a) 0 within a function.
b) Infinite 0 a) true
c) Nothing b) false
d) Error c) depends
d) varies
Answer: b
Explanation: None. Answer: b
Explanation: None.
Sanfoundry’s 1000+ MCQs on C helps 3. What will be the output of the following C
anyone preparing for placement in Cognizant code?
and other companies. Anyone looking for
Cognizant placement papers should practice 1. #include <stdio.h>
these 1000+ questions continuously for 2-3
months, thereby ensuring a top position in 2. int main()
placements.
3. {
Here is a listing of C programming questions 4. printf("%d ", 1);
on “Goto & Labels” along with answers,
explanations and/or solutions: 5. goto l1;

1. What will be the output of the following C 6. printf("%d ", 2);


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

9. } 4. What will be the output of the following C


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

9. {
5. What will be the output of the following C
code? 10. printf("loop\n");

1. #include <stdio.h> 11. goto l1;

2. int main() 12. }

3. { 13. }

4. printf("%d ", 1); 14. }

5. goto l1; a) loop loop


b) Compile time error
6. printf("%d ", 2);
c) loop loop loop loop
7. } d) Infinite loop

8. void foo() Answer: d


Explanation: None.
9. {
7. What will be the output of the following C
10. l1: printf("3 ", 3);
code?
11. }
1. #include <stdio.h>
a) 1 2 3
2. int main()
b) 1 3
c) 1 3 2 3. {
d) Compile time error
4. int i = 0, j = 0;
5. while (l1: i < 2) 12. }

6. { 13. }

7. i++; 14. }

8. while (j < 3) a) loop loop


b) Compile time error
9. {
c) loop loop loop loop
10. printf("loop\n"); d) Infinite loop

11. goto l1; Answer: a


Explanation: None.
12. }

13. }

14. }
UNIT II ARRAYS AND
a) loop loop
b) Compile time error STRINGS & UNIT III
c) loop loop loop loop FUNCTIONS AND
d) Infinite loop
POINTERS
Answer: b
Explanation: None.
TOPIC 2.1 STRING
8. What will be the output of the following C OPERATIONS IN C
code?
1. There are two groups of string functions
1. #include <stdio.h> defined in the header <string.h>. What are
they?
2. int main()
a) first group names beginning with str;
3. { second group names beginning with mem
b) first group names beginning with str;
4. int i = 0, j = 0; second group names beginning with is
c) first group names beginning with string;
5. l1: while (i < 2)
second group names beginning with mem
6. {
d) first group names beginning with str;
second group names beginning with type
7. i++;
Answer: a
8. while (j < 3) Explanation: There are two groups of string
functions declared under the header
9. { <string.h>. The first have names beginning
10. printf("loop\n"
with str and second have names beginning
); with mem.

11. goto l1;


2. What is the use of function char *strchr(ch, c) return length of prefix of c consisting of
c)? characters not in s
a) return pointer to first occurrence of ch in c d) return length of prefix of c consisting of
or NULL if not present characters present in s
b) return pointer to first occurrence of c in ch
or NULL if not present Answer: c
c) return pointer to first occurrence of ch in c Explanation: The function size_t strcspn(c, s)
or ignores if not present is used to return length of prefix of c
d) return pointer to first occurrence of cin ch consisting of characters not in s.
or ignores if not present
6. The mem functions are meant for
Answer: b a) returning a pointer to the token
Explanation: The given code char *strchr(ch, b) manipulating objects as character arrays
c) return pointer to first occurrence of c in ch c) returning a pointer for implemented-
or NULL if not present. defined string
d) returning a pointer to first occurrence of
3. Which code from the given option return string in another string
pointer to last occurrence of c in ch or NULL
if not present? Answer: b
a) char *strchr(ch, c) Explanation: The mem functions is used for
b) char *strrchr(ch, c) manipulating objects as character arrays.
c) char *strncat(ch, c)
d) char *strcat(ch, c) 7. What is the function of void *memset(s, c,
n)?
Answer: b a) places character s into first n characters of
Explanation: The function char *strrchr(ch, c, return c
c) returns pointer to last occurrence of c in ch b) places character c into first n characters of
or NULL if not present. s, return s
c) places character s into first n characters of
4. Which among the given options compares c, return s
atmost n characters of string ch to string s? d) places character c into first n character of s,
a) int strncmp(ch, s, n) return c
b) int strcmp(ch, s)
c) int strncmp(s, ch, n) Answer: b
d) int strcmp(s, ch) Explanation: The void *memset(s, c, n)
places character c into first n characters of s,
Answer: a return s.
Explanation: int strncmp(ch, s, n) is used to
compare at most n characters of string ch to 8. Functions whose names begin with “strn”
string s; return <0 if ch0 of ch >s. a) manipulates sequences of arbitrary
characters
5. Which among the given options is the right b) manipulates null-terminated sequences of
explanation for the statement size_t strcspn(c, characters
s)? c) manipulates sequence of non – null
a) return length of prefix of s consisting of characters.
characters not in c d) returns a pointer to the token
b) return length of prefix of s consisting of
characters present in c
Answer: c to copy n characters from the object.
Explanation: Functions whose names begin The code is void *memcpy(void *s1,const
with “strn” manipulates the sequence of non- void *s2, size_t n).
null characters.
2. Which function will you choose to join two
9. Which of the following is the right syntax words?
to copy n characters from the object pointed a) strcpy()
to by s2 into the object pointed to by s1? b) strcat()
a) void *memcpy(void *s1,const void c) strncon()
*s2,size_t n); d) memcon()
b) void *memcpy(void *s2, const void *s1,
size_t n); Answer: b
c) void memcpy(void *s1,const void *s2, Explanation: The strcat() function is used for
size_t n); concatenating two strings, appends a copy of
d) void memcpy(void *s2,const void the string.
*s1,size_t n); char *strcat(char *s1,const char *s2);

Answer: a 3. The function appends not more


Explanation: The memcpy() function copies than n characters.
n characters from the object pointed to by s2 a) strcat()
into the object pointed to by s1. If copying b) strcon()
takes place between objects that overlap, the c) strncat()
behavior is undefined. d) memcat()

10. What does the following function returns Answer: c


void *memmove(void *s1,const void s2, Explanation: The strncat() function appends
size_t n);? not more than n characters from the array(s2)
a) returns the value of s1 to the end of the string(s1).char *strncat(char
b) returns the value of s2 *s1, const char *s2,size_t n);
c) doesn’t return any value
d) returns the value of s1 and s2 4. What will strcmp() function do?
a) compares the first n characters of the object
Answer: a b) compares the string
Explanation: The memmove() function c) undefined function
copies n characters from the object pointed to d) copies the string
by s2 into the object pointed to by s1.The
memmove() function returns the value of s1. Answer: b
Explanation: The strcmp() function
compares the string s1 to the string s2.
1. Which among the following is Copying int strcmp(const char *s1,const char *s2);
function?
a) memcpy() 5. What is the prototype of strcoll() function?
b) strcopy() a) int strcoll(const char *s1,const char *s2)
c) memcopy() b) int strcoll(const char *s1)
d) strxcpy() c) int strcoll(const *s1,const *s2)
d) int strcoll(const *s1)
Answer: a
Explanation: The memcpy() function is used
Answer: a printf("After memmove place = %s, src = %
Explanation: The prototype of strcoll() s\n", pla, src);
function is int strcoll(const char *s1,const
a) Before memmove place= string1, src =
char *s2).
string2 After memmove place = string2, src =
6. What is the function of strcoll()? string2
a) compares the string, result is dependent on b) Before memmove place = string2, src =
the LC_COLLATE string2 After memmove place= string1, src =
b) copies the string, result is dependent on the string2
LC_COLLATE c) Before memmove place = string2, src =
c) compares the string, result is not dependent string1 After memmove place= string2, src
on the LC_COLLATE =string2
d) copies the string, result is not dependent on d) Before memmove place= string1, src =
the LC_COLLATE string2 After memmove place=string1, src =
string1
Answer: a
Explanation: The strcoll() function compares Answer: a
the string s1 to the string s2, both interpreted Explanation: In the C library function void
as appropriate to the LC_COLLATE category *memmove(void *str1, const void *str2,
of the current locale. size_t n) copies n characters from str2 to str1.

7. Which of the following is the variable type 10. What will be the output of the following
defined in header string. h? C code?
a) sizet
const char str1[]="ABCDEF1234567";
b) size const char str2[] = "269";
c) size_t len = strcspn(str1, str2);
d) size-t printf("First matching character is at %d
\n", len + 1);
Answer: c
Explanation: This is the unsigned integral a) First matching character is at 8
type and is the result of the sizeof keyword. b) First matching character is at 7
c) First matching character is at 9
8. NULL is the macro defined in the header d) First matching character is at 12
string. h.
a) true Answer: a
b) false Explanation: size_t strcspn(const char *str1,
const char *str2) is used to calculate the
Answer: a length of the initial segment of str1, which
Explanation: NULL macro is the value of a consists entirely of characters not in str2.
null pointer constant.

9. What will be the output of the following C 1. What is the return value of strxfrm()?
code? a) length of the transformed string, not
including the terminating null-character
const char pla[] = "string1"; b) length of the transformed string, including
const char src[] = "string2"; the terminating null-character
printf("Before memmove place= %s, src = % c) display the transformed string, not
s\n", pla, src);
memmove(pla, src, 7);
including the terminating null character
d) display the transformed string, not 5. which of the following function returns a
including the terminating null-character pointer to the located string or a null pointer
if string is not found.
Answer: a a) strtok()
Explanation: This function returns the length b) strstr()
of the transformed string, not including the c) strspn()
terminating null character. d) strrchr()
2. Is there any function declared as strstr()? Answer: b
a) true Explanation: The strstr() function is used to
b) false return a pointer to the located string, or if
string is not found a null pointer is returned.
Answer: a
Explanation: This function returns a pointer 6. Which of the given function is used to
to the first occurrence in s1 of any of the return a pointer to the located character?
entire sequence of characters specified in s2, a) strrchr()
or a null pointer if the sequence is not present b) strxfrm()
in s1. c) memchar()
char *strstr(const char *s1, const char *s2) d) strchar()

3. The C library function breaks Answer: d


string s1 into a series of tokens using the Explanation: The strchr() function is used to
delimiter s2. return a pointer to the located character if
a) char *strtok(char *s1,const char *s2); character does not occur then a null pointer is
b) char *strtok(char *s2,const char *s1); returned.
c) char *strstr(char *s1,const char *s2);
d) char *strstr(char *s2,const char *s1); 7. The strpbrk() function is used to return a
pointer to the character, or a null pointer if no
Answer: a character from s2 occurs in s1.
Explanation: The C library function char a) true
*strtok(char *s1, const char *s2) breaks string b) false
s1 into a series of tokens using the delimiter
s2. Answer: a
Explanation: char *strpbrk(const char
4. The function returns a pointer to the *s1,const char *s2);
first character of a token. The first occurrence in the string s1 of any
a) strstr() character from the string s2 is done by
b) strcpy() strpbrk().
c) strspn()
d) strtok() 8. What will be the output of the following C
code?
Answer: d
Explanation: The strtok() function returns a const char str1[] = "abcdef";
pointer to the first character of a token, if const char str2[] = "fgha";
char *mat;
there is no token then a null pointer is mat= strpbrk(str1, str2);
returned. if(mat)
printf("First matching character: %c\n",
*mat);
else 1. What will be returned in the following C
printf("Character not found"); code?
a) g size- t strlen(const char *s)
b) a const char *sc;
c) h for(sc = s; *sc!= ' \ O ' ; ++sc)
d) f return(sc - s) ;

Answer: d a) number of characters equal in sc


Explanation: The strpbrk() function is used b) length of string s
to locate the first occurrence in the string str1 c) doesn’t return any value
of any character from the string str2. d) displays string s

9. What will be the output of the following C Answer: b


code? Explanation: The strlen() function is used to
return the length of the string s.
char str1[] = "Helloworld ";
char str2[] = "Hello"; 2. The function strsp() is the complement of
len = strspn(str1, str2); strcspn().
printf("Length of initial segment matchin a) true
g %d\n", len );
b) false
a) 6
Answer: a
b) 5
Explanation: Both strcspn() and strpbrk()
c) 4
perform the same function. Only strspn() the
d) no match
return values differ. The function strspn() is
Answer: b the complement of strcspn() .
Explanation: The length of the maximum
3. What will the following C code do?
initial segment of the string str1 which
consists entirely of characters from the string char * strrchr(const char *s, int c )
str2 is computed by strspn(). char ch = c;
char *sc;
10. The function returns the number for(sc = NULL; ; ++s)
of characters that are present before the if(*s == ch)
SC = 9;
terminating null character.
i f (*s =='\O' )
a) strlength() return (( char *) s);
b) strlen()
c) strlent() a) find last occurrence of c in char s[ ].
d) strchr() b) find first occurrence of c in char s[ ].
c) find the current location of c in char s[ ].
Answer: b d) There is error in the given code
Explanation: The strlen() function is used to
return the number of characters that are Answer: a
present before the terminating null Explanation:The strrchr() function locates
character.size-t strlen(const char *s);The the last occurrence of c (converted to a char)
length of the string pointed to by s is in the string pointed to by s. String contains
computed by strlen(). null character as a terminating part of it.
4. This function offers the quickest way to for (su = s; 0 < n; ++su, --n)
determine whether two character sequences <br>
*su = ch;
of the same known length match character for <br>
the character up to and including any null
character in both. a) store c throughout unsigned char s[n]
a) strcmp() b) store c throughout signed char s[n]
b) memcmp() c) find first occurrence of c in s[n]
c) strncmp() d) find last occurrence of c in s[n]
d) no such function
Answer: a
Answer: c Explanation: This is the safe way to store the
Explanation: The strncmp() function is used same value throughout a contiguous sequence
to compare not more than n characters of elements in a character array.
(characters that follow a null character are not
compared) from the array pointed to by one, 7. Use to determine the null-
to the array pointed to by other. terminated message string that corresponds to
the error code errcode.
5. What will be the output of the following C a) strerror()
code? b) strstr()
c) strxfrm()
char str1[15];
char str2[15];
d) memset()
int mat;
strcpy(str1, "abcdef"); Answer: a
strcpy(str2, "ABCDEF"); Explanation: Use strerror (errcode) to
mat= strncmp(str1, str2, 4); determine the null-terminated message string
if(mat< 0)
that corresponds to the error code errcode.
printf("str1 is not greater than str2");
else if(mat> 0)
printf("str2 is is not greater than str1" 8. What will be the output of the following C
); code?
else
printf("both are equal"); const char str1[10]="Helloworld";
const char str2[10] = "world";
a) str1 is not greater than str2 char *mat;
b) str2 is not greater than str1 mat = strstr(str1, str2);
printf("The substring is:%s\n", mat);
c) both are equal
d) error in given code a) The substring is:world
b) The substring is:Hello
Answer: b
c) The substring is:Helloworld
Explanation: The C library function int d) error in the code
strncmp(const char *str1, const char *str2,
size_t n) is used to compare at most the first n Answer: a
bytes of str1 and str2. Explanation: The C library function char
*strstr(const char *str1, const char *str2) is
6. What will be the output of the following C
used to find the first occurrence of the
code?
substring str2 in the string str1.The
void *memset(void *c, int c, size-t n) terminating ‘\0’ characters are not compared.
unsigned char ch = c;
unsigned char *su;
9. void *memcpy(void *dest, const void *src, Answer: c
size_t n) What does the following code do? Explanation: int isalnum(int c)
a) copies n characters from src to dest The isalnum function tests for any character
b) copies n character from dest to src for which isalpha or isdigit is true.
c) transform n character from dest to src
d) transform n character from src to dest 3. What does the following C code do?

Answer: a int iscntrl( int c);


Explanation: In the C library function
memcpy() is used to copy n characters from a) checks if character is upper case
memory area src to memory area dest. b) checks if character is lower case
c) tests for any control character
10. What will the given C code do? d) no function as such

int memcmp(const void *str1, const void * Answer: c


str2, size_t n) Explanation:
int iscntrl( int c);
a) compares the first n bytes of str1 and str2 The iscntrl function tests for any control
b) copies the first n bytes of str1 to str2 character.
c) copies the first n bytes of str2 to str1
d) invalid function 4. What do the following C function do?

Answer: a int isgraph(int c);


Explanation: In the C library function int
memcmp(const void *str1, const void *str2, a) tests for only space character
size_t n)) is used to compare the first n bytes b) tests for only digit
of memory area str1 and memory area str2. c) tests for only lower case
d) tests for any printing character

1. Which header declares several functions Answer: d


useful for testing and mapping characters? Explanation: int isgraph(int c);
a) assert.h The isgraph function tests for any printing
b) stdio.h character except space.
c) ctype.h
d) errno.h 5. The isdigit function tests for any decimal-
digit character.
Answer: c a) true
Explanation: The header <ctype.h> declares b) false
several functions useful for testing and
mapping characters. Answer: a
Explanation: int isdigit(int c);
2. The function tests for any character This function checks whether the passed
for which isalpha or isdigit is true. character is a decimal digit.
a) isxdigit()
b) isspace() 6. Which function returns true only for the
c) isalnum() characters defined as lowercase letters?
d) isupper() a) islow()
b) islower()
c) isalpa() b) ispunct()
d) isalnum() c) isgraph()
d) isxdigit()
Answer: b
Explanation: int islower( int c); Answer: d
The islower function tests for any character Explanation: int isxdigit( int c) ;
that is a lowercase letter. This function tests for any hexadecimal-digit
character.
7. This function checks whether the passed
character is white-space.
a) ispunct() 1. The function converts an
b) isgraph() uppercase letter to the corresponding
c) isspace() lowercase letter.
d) isalpha() a) islower()
b) isupper()
Answer: c c) toupper()
Explanation: The isspace function tests for d) tolower()
any character that is a standard white-space
character. Answer: d
Explanation: If the argument is a character
8. The standard white-space characters are the for which isupper() is true and islower() is
following: space (' '), form feed (' \ f '), true for another set of character, the tolower()
newline (' \n') , horizontal tab (' \tr), and function returns the corresponding
vertlcal tab (' \v') can be tested with function. character;otherwise, the argument is returned
a) ispunct() unchanged.
b) isalpha()
c) isgraph() 2. The toupper() function converts a
d) isspace() to the corresponding
a) uppercase, lowercase
Answer: d b) lowercase, uppercase
Explanation: The isspace function tests for c) binary, decimal
any character that is a standard white-space d) decimal, binary
character.
Answer: b
9. Which function tests for any character that Explanation: The toupper() function is used
is an uppercase letter. to convert a lowercase letter to the
a) iscntrl() corresponding uppercase letter.
b) ispunct() If the value is a character for which islower()
c) isdigit() is true and isupper () is true for another set of
d) isupper() characters, the toupper() function returns the
corresponding character, otherwise, the value
Answer: d is returned unchanged.
Explanation: isupper() returns true only for
the characters defined as uppercase letters. 3. fgetc, getc, getchar are all declared in

10. The function tests for any a) stdio. h


hexadecimal-digit character. b) ctype. h
a) iscntrl()
c) assert. h used to check whether the passed character
d) stdarg. h can be printed.A control character is not a
printable character.
Answer: a
Explanation: The functions getc, fgetc, 7. What will be the output of the following C
getchar are all declared in stdio.h header file. code?

4. isalpha() function is used to detect char ch[ ] = "0xC";


characters both in uppercase and lowercase. if(isxdigit(ch[ ]))
printf("ch = %s is hexadecimal character
a) true \n",ch);
b) false else
printf("ch = %s is not hexadecimal charac
Answer: a ter \n",ch);
Explanation: isalpha() function is used to test
for letters in the local alphabet. For the locale, a) ch = 0xC is hexadecimal character
the local alphabet consists of 26 English b) ch = 0xC is not hexadecimal character
letters, in each of two cases. c) compile error
d) run-time error
5. What will be the output of the following C
code? Answer: a
Explanation: The C library function isxdigit
int ch= ' '; checks whether the passed character is a
if(isgraph(ch)) hexadecimal digit.
printf("ch = %c can be printed \n",ch);
else
printf("ch=%c cannot be printed \n",ch);
8. Which among the following option is the
full set of character class Hexadecimal digits?
a) ch = ‘ ‘ can be printed a) { 0 1 2 3 4 5 6 7 8 9 A B C D E F }
b) ch = ‘ ‘ cannot be printed b) { 0 1 2 3 4 5 6 7 8 9 a b c d e f }
c) compile error c) { 0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e
d) run-time error f}
d) { 0 1 2 3 4 5 6 7 8 9}
Answer: b
Explanation: void isgraph() function is used Answer: c
to check if the character has graphical Explanation: digit or one of the first letters of
representation. Graphical representations the alphabet in either case, ‘a’ through ‘f’ and
character are all those characters that can be ‘A’ through ‘F’ is contained in the character
printed except for whitespace characters, class Hexadecimal digits.
which is not considered as isgraph characters.
9. What will be the output of the following C
6. The C library function checks whether the code?
passed character is printable.
int i = 0;
a) isgraph() char c;
b) isalpha() char str[ ] = "Little Star";
c) isprint() while(str[i])
d) ispunct() {
putchar (toupper(str[i]));
Answer: c i++;
}
Explanation: int isprint(int c) function is
a) little star c) overflow
b) lITTLE sTAR d) significance loss
c) LITTLE STAR
d) Little Star Answer: d
Explanation: A significance loss occurs
Answer: c when a result has nowhere near the number of
Explanation: The C library function toupper significant digits indicated by its type.
converts the lowercase letter to uppercase.
3. What error occurs when a result is
10. What will be the output of the following undefined for a given argument value?
C code? a) significance loss
b) underflow
int ch = '\t'; c) overflow
if(isprint(ch))
d) domain
printf("ch = |%c| printable \n", ch);
else
printf("ch= |%c| not printable \n",ch); Answer: d
Explanation: A domain error occurs when a
a) ch = |\t| printable result is undefined for a given argument
b) ch = |\t| not printable value.
c) ch = | | printable
d) ch = | | not printable 4. is reported on a domain error.
a) EDOM
Answer: d b) ERANGE
Explanation: isprint() function is used to c) Significance loss
check whether the passed character is d) Underflow
printable. A control character is not a
printable character. Answer: a
Explanation: EDOM is reported on a domain
error.
1. occurs when a result is too large
in magnitude to represent errors as a floating- 5. ERANGE is reported on an overflow or an
point value of the required type. underflow.
a) underflow a) true
b) significance loss b) false
c) domain
d) overflow Answer: a
Explanation: This macro represents a range
Answer: d error, which occurs if an input argument is
Explanation: An overflow occurs when a outside the range, over which the
result is too large in magnitude to represent mathematical function is defined and errno is
errors as a floating-point value of the required set to ERANGE.
type.
6. What will be the output of the following C
2. What occurs when a result has nowhere code?
near the number of significant digits indicated
by its type. errno = 0;
y = sqrt(2);
a) domain if(errno == EDOM)
b) underflow printf(&quot;Invalid value\n&quot;);
else a) true
printf(&quot;Valid value\n&quot;); b) false
a) Invalid value Answer: a
b) Valid value Explanation: Any library function can store
c) No output nonzero values in errno.
d) Compile error
10. tells the compiler that this
Answer: b data is defined somewhere and will be
Explanation: The C library macro EDOM connected with the linker.
represents a domain error, which occurs if an a) errno
input argument is outside the domain, over b) extern
which the mathematical function is defined c) variable
and errno is set to EDOM. d) yvals
7. What will be the output of the following C Answer: b
code? Explanation: The C library macro extern int
errno = 0;
errno is set by system calls and some library
y = sqrt(-10); functions in the event of an error to indicate if
if(errno == EDOM) anything went wrong.
printf(&quot;Invalid value \n&quot;);
else
printf(&quot;Valid value\n&quot;);
TOPIC 2.2 C FUNCTIONS AND
a) Invalid value STRUCTURE OF A PROGRAM
b) Valid value
c) No output
This section on C interview questions and
d) Compile error
answers focuses on “Basics of Functions”.
Answer: a One shall practice these interview questions
Explanation: The C library macro EDOM to improve their C programming skills needed
represents a domain error, which occurs if an for various interviews (campus interviews,
input argument is outside the domain, over walkin interviews, company interviews),
which the mathematical function is defined placements, entrance exams and other
and errno is set to EDOM. competitive exams. These questions can be
attempted by anyone focusing on learning C
8. errno causes trouble in two subtler Programming language. They can be a
ways(vague and explicit). beginner, fresher, engineering graduate or an
a) true experienced IT professional. Our C Interview
b) false questions come with detailed explanation of
the answers which helps in better
Answer: a understanding of C concepts.
Explanation: errno causes trouble in two
subtler ways – sometimes its specification is Here is a listing of C interview questions on
too vague and sometimes it is too explicit. “Basics of Functions” along with answers,
explanations and/or solutions:
9. No library function will store a zero in
errno. 1. What will be the output of the following C
code?
1. #include <stdio.h> 11. void f()

2. int main() 12. {

3. { 13. printf("1 ");

4. void foo(); 14. foo();

5. printf("1 "); 15. }

6. foo(); a) Compile time error as foo is local to main


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

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

10. { 1. #include <stdio.h>

11. printf("2 "); 2. void foo();

12. } 3. int main()

a) Compile time error 4. {


b) 2
5. void foo(int);
c) Depends on the compiler
d) Depends on the standard 6. foo();

Answer: b 7. return 0;
Explanation: None.
8. }
5. What will be the output of the following C
9. void foo()
code?
10. {
1. #include <stdio.h>
11. printf("2 ");
2. void foo();
12. } 6. }

a) 2 7. void main()
b) Compile time error
c) Depends on the compiler 8. {

d) Depends on the standard 9. void m()

Answer: b 10. {
Explanation: None.
11. printf("hi");
7. What will be the output of the following C
code? 12. }

13. }
1. #include <stdio.h>

2. void m() a) hi
b) Compile time error
3. { c) Nothing
d) Varies
4. printf("hi");
Answer: b
5. }
Explanation: None.
6. void main()
This section on C interview questions and
7. { answers focuses on “Basics of Functions”.
8. m();
One shall practice these interview questions
to improve their C programming skills needed
9. } for various interviews (campus interviews,
walkin interviews, company interviews),
a) hi placements, entrance exams and other
b) Run time error competitive exams. These questions can be
c) Nothing attempted by anyone focusing on learning C
d) Varies Programming language. They can be a
beginner, fresher, engineering graduate or an
Answer: a experienced IT professional. Our C Interview
Explanation: None. questions come with detailed explanation of
the answers which helps in better
8. What will be the output of the following C understanding of C concepts.
code?
Here is a listing of C interview questions on
1. #include <stdio.h> “Basics of Functions” along with answers,
explanations and/or solutions:
2. void m();

3. void n() 1. What will be the output of the following C


code?
4. {
1. #include <stdio.h>
5. m();
2. void main()
3. { 3. What will be the output of the following C
code?
4. m();
1. #include <stdio.h>
5. void m()
2. void main()
6. {
3. {
7. printf("hi");
4. static int x = 3;
8. }
5. x++;
9. }
6. if (x <= 5)
a) hi
b) Compile time error 7. {
c) Nothing
d) Varies 8. printf("hi");

9. main();
Answer: b
Explanation: None. 10. }

2. What will be the output of the following C 11. }


code?
a) Run time error
1. #include <stdio.h> b) hi
c) Infinite hi
2. void main()
d) hi hi
3. {
Answer: d
4. m(); Explanation: None.

5. } 4. Which of the following is a correct format


for declaration of function?
6. void m() a) return-type function-name(argument type);
b) return-type function-name(argument type)
7. {
{}
8. printf("hi"); c) return-type (argument type)function-name;
d) all of the mentioned
9. m();
Answer: a
10. } Explanation: None.
a) Compile time error 5. Which of the following function
b) hi declaration is illegal?
c) Infinite hi a) int 1bhk(int);
d) Nothing b) int 1bhk(int a);
c) int 2bhk(int*, int []);
Answer: c d) all of the mentioned
Explanation: None.
Answer: d This section on C questions and puzzles
Explanation: None. focuses on “External Variables”. One shall
practice these questions and puzzles to
6. Which function definition will run improve their C programming skills needed
correctly? for various interviews (campus interviews,
a) walkin interviews, company interviews),
placements, entrance exams and other
int sum(int a, int b) competitive exams. These programming
return (a + b);
puzzles can be attempted by anyone focusing
b) on learning C Programming language. They
can be a beginner, fresher, engineering
int sum(int a, int b) graduate or an experienced IT professional.
{return (a + b);} Our C questions come with detailed
explanation of the answers which helps in
c) better understanding of C concepts.
int sum(a, b) Here is a listing of C questions and puzzles
return (a + b);
on “External Variables” along with answers,
d) none of the mentioned explanations and/or solutions:

Answer: b 1. What will be the output of the following C


Explanation: None. code?

7. Can we use a function as a parameter of 1. #include <stdio.h>


another function? [Eg: void wow(int func())]. 2. void main()
a) Yes, and we can use the function value
conveniently 3. {
b) Yes, but we call the function again to get
the value, not as convenient as in using 4. m();
variable
5. printf("%d", x);
c) No, C does not support it
d) This case is compiler dependent 6. }

Answer: c 7. int x;
Explanation: None.
8. void m()
8. The value obtained in the function is given
back to main by using keyword. 9. {
a) return 10. x = 4;
b) static
c) new 11. }
d) volatile
a) 4
Answer: a b) Compile time error
Explanation: None. c) 0
d) Undefined
Answer: b a) Run time error
Explanation: None. b) 3 3
c) 3 5
2. What will be the output of the following C d) 3 4
code?
Answer: d
1. #include <stdio.h> Explanation: None.
2. int x; 4. What will be the output of the following C
3. void main()
code?

4. { 1. #include <stdio.h>

5. printf("%d", x); 2. int x = 5;

6. } 3. void main()

a) Junk value 4. {

b) Run time error 5. int x = 3;


c) 0
d) Undefined 6. printf("%d", x);

Answer: c 7. {
Explanation: None.
8. int x = 4;
3. What will be the output of the following C 9. }
code?
10. printf("%d", x);
1. #include <stdio.h>
11. }
2. int x = 5;
a) 3 3
3. void main()
b) 3 4
4. { c) 3 5
d) Run time error
5. int x = 3;
Answer: a
6. printf("%d", x); Explanation: None.
7. { 5. Functions in C are always
8. x = 4;
a) Internal
b) External
9. } c) Both Internal and External
d) External and Internal are not valid terms
10. printf("%d", x); for functions
11. } Answer: b
Explanation: None.
6. Global variables are 4. printf("%d", d++);
a) Internal
b) External 5. }
c) Both Internal and External
6. int d = 10;
d) None of the mentioned
a) 9
Answer: b
b) 10
Explanation: None. c) 11
d) Compile time error
7. Which of the following is an external
variable in the following C code? Answer: d
Explanation: None.
1. #include <stdio.h>

2. int func (int a) 9. What will be the output of the following C


code?
3. {
1. #include <stdio.h>
4. int b;
2. double var = 8;
5. return b;
3. int main()
6. }
4. {
7. int main()
5. int var = 5;
8. {
6. printf("%d", var);
9. int c;
7. }
10. func (c);
a) 5
11. }
b) 8
12. int d;
c) Compile time error due to wrong format
identifier for double
a) a d) Compile time error due to redeclaration of
b) b variable with same name
c) c
d) d Answer: a
Explanation: None.
Answer: d
Explanation: None. Sanfoundry’s 1000+ Interview Questions &
Answers on C helps anyone preparing for
8. What will be the output of the following C Cognizant and other companies C interviews.
code? One should practice these 1000+ interview
questions and answers continuously for 2-3
1. #include <stdio.h> months to clear Cognizant interviews on C
2. int main()
Programming language.

3. {
Here is a listing of basic C interview Answer: b
questions on “External Variable” along with Explanation: None.
answers, explanations and/or solutions:
3. Which part of the program address space is
1. What will be the output of the following C p stored in the following C code?
code?
1. #include <stdio.h>
1. #include <stdio.h>
2. int *p;
2. double i;
3. int main()
3. int main()
4. {
4. {
5. int i = 0;
5. printf("%g\n",i);
6. p = &i;
6. return 0;
7. return 0;
7. }
8. }
a) 0
b) 0.000000 a) Code/text segment
c) Garbage value b) Data segment
d) Depends on the compiler c) Bss segment
d) Stack
Answer: a
Explanation: None. Answer: c
Explanation: None.
2. Which part of the program address space is
p stored in the following C code? 4. Can variable i be accessed by functions in
another source file?
1. #include <stdio.h>
1. #include <stdio.h>
2. int *p = NULL;
2. int i;
3. int main()
3. int main()
4. {
4. {
5. int i = 0;
5. printf("%d\n", i);
6. p = &i;
6. }
7. return 0;
a) Yes
8. } b) No
c) Only if static keyword is used
a) Code/text segment d) Depends on the type of the variable
b) Data segment
c) Bss segment Answer: a
d) Stack Explanation: None.
5. Property of the external variable to be 7. return 0;
accessed by any source file is called by the
C90 standard as 8. }
a) external linkage
b) external scope a) true
c) global scope b) true only if NULL value is 0
d) global linkage c) Compile time error
d) Nothing
Answer: a
Explanation: None. Answer: b
Explanation: None.
6. What will be the output of the following C
code? 8. What will be the output of the following C
code?
1. #include <stdio.h>
1. #include <stdio.h>
2. int *i;
2. static int x = 5;
3. int main()
3. void main()
4. {
4. {
5. if (i == NULL)
5. x = 9;
6. printf("true\n");
6. {
7. return 0;
7. int x = 4;
8. }
8. }
a) true
9. printf("%d", x);
b) true only if NULL value is 0
c) Compile time error 10. }
d) Nothing
a) 9
Answer: a b) 4
Explanation: None. c) 5
d) 0
7. What will be the output of the following C
code? Answer: a
Explanation: None.
1. #include <stdio.h>

2. int *i; This section on C programming quiz focuses


on “Scope of a Variable”. One shall practice
3. int main() these quizzes to improve their C
programming skills needed for various
4. {
interviews (campus interviews, walkin
5. if (i == 0) interviews, company interviews), placements,
entrance exams and other competitive exams.
6. printf("true\n");
These questions can be attempted by anyone 3. {
focusing on learning C Programming
language. They can be a beginner, fresher, 4. extern ary1[];
engineering graduate or an experienced IT 5. printf("scope rules\n");
professional. Our C quiz comes with detailed
explanation of the answers which helps in 6. }
better understanding of C concepts.
a) scope rules
Here is a listing of C programming quiz on b) Linking error due to undefined reference
“Scope of a Variable” along with answers, c) Compile time error because size of array is
explanations and/or solutions: not provided
d) Compile time error because datatype of
1. What will be the output of the following C array is not provided
code?
Answer: a
1. #include <stdio.h> Explanation: None.
2. int i;
3. What will be the output of the following C
3. int main() code (after linking to source file having
definition of ary1)?
4. {
1. #include <stdio.h>
5. extern int i;
2. int main()
6. if (i == 0)
3. {
7. printf("scope rules\n")
; 4. extern ary1[];

8. } 5. printf("%d\n", ary1[0]);

a) scope rules 6. }
b) Compile time error due to multiple
declaration a) Value of ary1[0];
c) Compile time error due to not defining type b) Compile time error due to multiple
in statement extern i definition
d) Nothing will be printed as value of i is not c) Compile time error because size of array is
zero because i is an automatic variable not provided
d) Compile time error because datatype of
Answer: a array is not provided
Explanation: None.
Answer: d
2. What will be the output of the following C Explanation: None.
code (without linking the source file in which
ary1 is defined)? 4. What is the scope of an external variable?
a) Whole source file in which it is defined
1. #include <stdio.h> b) From the point of declaration to the end of
the file in which it is defined
2. int main() c) Any source file in a program
d) From the point of declaration to the end of 3. int main()
the file being compiled
4. {
Answer: d
5. int c;
Explanation: None.
6. return 0;
5. What is the scope of a function?
a) Whole source file in which it is defined 7. }
b) From the point of declaration to the end of
the file in which it is defined 8. int a;
c) Any source file in a program
d) From the point of declaration to the end of a) a
the file being compiled b) b
c) c
Answer: d d) Both a and b
Explanation: None.
Answer: b
6. Comment on the output of the following C Explanation: None.
code.
8. Comment on the following 2 C programs.
1. #include <stdio.h>
1. #include <stdio.h> //Program 1
2. int main()
2. int main()
3. {
3. {
4. int i;
4. int a;
5. for (i = 0;i < 5; i++)
5. int b;
6. int a = i;
6. int c;
7. printf("%d", a);
7. }
8. }
8.
a) a is out of scope when printf is called
9. #include <stdio.h> //Program 2
b) Redeclaration of a in same scope throws
error 10. int main()
c) Syntax error in declaration of a
d) No errors, program will show the output 5 11. {

Answer: c 12. int a;


Explanation: None.
13. {
7. Which variable has the longest scope in the 14. int b;
following C code?
15. }
1. #include <stdio.h>
16. {
2. int b;
17. int c; 5. {

18. } 6. int b;

19. } 7. }

a) Both are same 8. }


b) Scope of c is till the end of the main
function in Program 2 a) a->b, a->b
c) In Program 1, variables a, b and c can be b) a->b, b->a
used anywhere in the main function whereas c) b->a, a->b
in Program 2, variables b and c can be used d) b->a, b->a
only inside their respective blocks
d) None of the mentioned Answer: b
Explanation: None.
Answer: c
Explanation: None. 2. Array sizes are optional during array
declaration by using keyword.
a) auto
This section on basic C questions focuses on b) static
“Scope of a Variable”. One shall practice c) extern
these basic interview questions to improve d) register
their C programming skills needed for various
interviews (campus interviews, walkin Answer: c
interviews, company interviews), placements, Explanation: None.
entrance exams and other competitive exams.
These questions can be attempted by anyone 3. What will be the output of the following C
focusing on learning C Programming code?
language. They can be a beginner, fresher,
engineering graduate or an experienced IT 1. #include <stdio.h>
professional. Our C interview questions come
with detailed explanation of the answers 2. void main()
which helps in better understanding of C 3. {
concepts.
4. int x = 3;
Here is a listing of basic C questions on
“Scope of a Variable” along with answers, 5. {
explanations and/or solutions:
6. x = 4;
1. What will be the sequence of allocation
7. printf("%d", x);
and deletion of variables in the following C
code? 8. }

1. #include <stdio.h> 9. }

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

14. void n() 1. #include <stdio.h>

15. { 2. static int x = 5;

16. printf("%d", x); 3. void main()

17. } 4. {

a) 8 3 5. int x = 9;
b) 3 8 6. {
c) 8 5
d) 5 3 7. x = 4;

Answer: a 8. }
Explanation: None.
9. printf("%d", x);
5. What will be the output of the following C
10. }
code?
a) 9
1. #include <stdio.h>
b) 5
c) 4 3. {
d) 0
4. m();
Answer: c
Explanation: None. 5. m();

6. }
7. What will be the output of the following C
code? 7. void m()

1. #include <stdio.h> 8. {

2. void main() 9. static int x = 5;

3. { 10. x++;

4. { 11. printf("%d", x);

5. int x = 8; 12. }

6. } a) 6 7
b) 6 6
7. printf("%d", x);
c) 5 5
8. } d) 5 6

a) 8 Answer: a
b) 0 Explanation: None.
c) Undefined
d) Compile time error 2. What will be the output of the following C
code?
Answer: d
Explanation: None. 1. #include <stdio.h>

2. void main()
Sanfoundry’s 1000+ MCQs on C helps
anyone preparing for placement in Oracle and 3. {
other companies. Anyone looking for Oracle 4. static int x;
placement papers should practice these 1000+
questions continuously for 2-3 months, 5. printf("x is %d", x);
thereby ensuring a top position in placements.
6. }
Here is a listing of C questions and puzzles
on “Static Variables” along with answers, a) 0
explanations and/or solutions: b) 1
c) Junk value
1. What will be the output of the following C d) Run time error
code?
Answer: a
1. #include <stdio.h> Explanation: None.

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

Answer: b 6. Which of following is not accepted in C?


Explanation: None. a) static a = 10; //static as
b) static int func (int); //parameter as static
4. What will be the output of the following C c) static static int a; //a static variable prefixed
code? with static
d) all of the mentioned
1. #include <stdio.h>
Answer: c
2. void main()
Explanation: None.
3. {
7. Which of the following cannot be static in
4. static double x; C?
a) Variables
5. int x; b) Functions
c) Structures
6. printf("x is %d", x);
d) None of the mentioned
7. }
Answer: d
a) Nothing Explanation: None.
b) 0
c) Compile time error Sanfoundry’s 1000+ MCQs on C helps
d) Junkvalue anyone preparing for placement in Cisco and
other companies. Anyone looking for Cisco
Answer: c placement papers should practice these 1000+
Explanation: None. questions continuously for 2-3 months,
thereby ensuring a top position in placements.
5. What will be the output of the following C
code?
Here is a listing of C programming questions c) 10 10
on “Static Variables” along with answers, d) Compilation Error
explanations and/or solutions:
Answer: a
1. What will be the output of the following C Explanation: None.
code if these two files namely test.c and
test1.c are linked and run? 2. Functions have static qualifier for its
declaration by default.
1. -------file test.c------- a) True
b) False
2. #include <stdio.h> c) Depends on the compiler
3. #include ""test.h""
d) Depends on the standard

4. int main() Answer: b


Explanation: None.
5. {
3. Is initialisation mandatory for local static
6. i = 10; variables?
a) Yes
7. printf(""%d "", i);
b) No
8. foo(); c) Depends on the compiler
d) Depends on the standard
9. }
Answer: b
10. Explanation: None.
11. -----file test1.c------
4. What will be the output of the following C
12. #include <stdio.h> code?

13. #include ""test.h"" 1. #include <stdio.h>

14. int foo() 2. int main()

15. { 3. {

16. printf(""%d\n"", i); 4. foo();

17. } 5. foo();

18. 6. }

19. -----file test.h----- 7. void foo()

20. #include <stdio.h> 8. {

21. #include <stdlib.h> 9. int i = 11;

22. static int i; 10. printf("%d ", i);

a) 10 0 11. static int j = 12;


b) 0 0
12. j = j + 1; 1. #include <stdio.h>

13. printf("%d\n", j); 2. void func();

14. } 3. int main()

a) 11 12 11 12 4. {
b) 11 13 11 14
5. static int b = 20;
c) 11 12 11 13
d) Compile time error 6. func();

Answer: b 7. }
Explanation: None.
8. void func()
5. Assignment statements assigning value to
local static variables are executed only once. 9. {
a) True 10. static int b;
b) False
c) Depends on the code 11. printf("%d", b);
d) None of the mentioned
12. }
Answer: b
Explanation: None. a) Output will be 0
b) Output will be 20
6. What is the format identifier for “static a = c) Output will be a garbage value
20.5;”? d) Compile time error due to redeclaration of
a) %s static variable
b) %d
c) %f Answer: a
d) Illegal declaration due to absence of data Explanation: None.
type
This section on C interview questions and
Answer: b answers focuses on “Register Variables”. One
Explanation: None. shall practice these interview questions to
improve their C programming skills needed
7. Which of the following is true for the static for various interviews (campus interviews,
variable? walkin interviews, company interviews),
a) It can be called from another function placements, entrance exams and other
b) It exists even after the function ends competitive exams. These questions can be
c) It can be modified in another function by attempted by anyone focusing on learning C
sending it as a parameter Programming language. They can be a
d) All of the mentioned beginner, fresher, engineering graduate or an
experienced IT professional. Our C Interview
Answer: b questions come with detailed explanation of
Explanation: None. the answers which helps in better
understanding of C concepts.
8. What will be the output of the following C
code?
Here is a listing of C interview questions on 4. register static int i = 10;
“Register Variables” along with answers,
explanations and/or solutions: 5. i = 11;

6. printf("%d\n", i);
1. What will be the output of the following C
code? 7. }

1. #include <stdio.h> a) 10
2. int main()
b) Compile time error
c) Undefined behaviour
3. { d) 11

4. register int i = 10; Answer: b


Explanation: None.
5. int *p = &i;
4. What will be the output of the following C
6. *p = 11;
code?
7. printf("%d %d\n", i, *p);
1. #include <stdio.h>
8. }
2. int main()
a) Depends on whether i is actually stored in
3. {
machine register
b) 10 10 4. register auto int i = 10;
c) 11 11
d) Compile time error 5. i = 11;

Answer: d 6. printf("%d\n", i);


Explanation: None.
7. }

2. register keyword mandates compiler to a) 10


place it in machine register. b) Compile time error
a) True
c) Undefined behaviour
b) False
d) 11
c) Depends on the standard
d) None of the mentioned Answer: b
Explanation: None.
Answer: b
Explanation: None. 5. What will be the output of the following C
code?
3. What will be the output of the following C
code? 1. #include <stdio.h>

1. #include <stdio.h> 2. int main()

2. int main() 3. {

3. { 4. register const int i = 10;


5. i = 11; 8. void m()

6. printf("%d\n", i); 9. {

7. } 10. x++;

a) 10 11. }
b) Compile time error
c) Undefined behaviour a) 6
d) 11 b) 5
c) Junk value
Answer: b d) Compile time error
Explanation: None.
Answer: d
6. Register storage class can be specified to Explanation: None.
global variables.
a) True Sanfoundry’s 1000+ MCQs on C helps
b) False anyone preparing for placement in Samsung
c) Depends on the compiler and other companies. Anyone looking for
d) Depends on the standard Samsung placement papers should practice
these 1000+ questions continuously for 2-3
Answer: b months, thereby ensuring a top position in
Explanation: None. placements.
7. Which among the following is wrong for Here is a listing of C interview questions on
“register int a;”? “Register Variables” along with answers,
a) Compiler generally ignores the request explanations and/or solutions:
b) You cannot take the address of this
variable 1. When compiler accepts the request to use
c) Access time to a is critical the variable as a register?
d) None of the mentioned a) It is stored in CPU
b) It is stored in cache memory
Answer: d c) It is stored in main memory
Explanation: None. d) It is stored in secondary memory
8. What will be the output of the following C Answer: a
code? Explanation: None.
1. #include <stdio.h> 2. Which data type can be stored in register?
a) int
2. void main()
b) long
3. { c) float
d) all of the mentioned
4. register int x = 5;
Answer: d
5. m(); Explanation: None.
6. printf("x is %d", x);
3. Which of the following operation is not
7. } possible in a register variable?
a) Reading the value into a register variable a) Segmentation fault
b) Copy the value from a memory variable b) main is called twice
c) Global declaration of register variable c) main is called once
d) All of the mentioned d) main is called thrice

Answer: d Answer: a
Explanation: None. Explanation: None.

4. Which among the following is the correct 7. What will be the output of the following C
syntax to declare a static variable register? code?
a) static register a;
b) register static a; 1. #include <stdio.h>
c) Both static register a; and register static a;
d) We cannot use static and register together 2. void main()

3. {
Answer: d
Explanation: None. 4. register int x;

5. Register variables reside in 5. printf("%d", x);


a) stack
b) registers 6. }
c) heap
a) 0
d) main memory
b) Junk value
Answer: b c) Compile time error
Explanation: None. d) Nothing

6. What will be the output of the following C Answer: b


code? Explanation: None.

1. #include <stdio.h> 8. What will be the output of the following C


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

4. If storage class is not specified for a local


This section on C quiz focuses on “Automatic
variable, then the default class will be auto.
Variable”. One shall practice these quizzes to
a) True
improve their C programming skills needed
b) False
for various interviews (campus interviews,
c) Depends on the standard
walkin interviews, company interviews),
d) None of the mentioned
placements, entrance exams and other
competitive exams. These questions can be Answer: a
attempted by anyone focusing on learning C Explanation: None.
Programming language. They can be a
beginner, fresher, engineering graduate or an 5. What will be the output of the following C
experienced IT professional. Our C quiz code?
comes with detailed explanation of the
answers which helps in better understanding 1. #include <stdio.h>
of C concepts.
2. void foo(auto int i);
Here is a listing of C quiz on “Automatic
Variable” along with answers, explanations 3. int main()
and/or solutions:
4. {

1. What is the scope of an automatic variable? 5. foo(10);


a) Within the block it appears
b) Within the blocks of the block it appears 6. }
c) Until the end of program
d) Within the block it appears & Within the 7. void foo(auto int i)
blocks of the block it appears
8. {
Answer: d 9. printf("%d\n", i );
Explanation: None.
10. }
2. Automatic variables are allocated space in
the form of a a) 10
a) stack b) Compile time error
b) queue c) Depends on the standard
c) priority queue d) None of the mentioned
d) random
Answer: b
Answer: a Explanation: None.
Explanation: None.
6. Automatic variables are stored in
3. Which of the following is a storage
specifier? a) stack
a) enum b) data segment
b) union c) register
c) auto d) heap
d) volatile
Answer: a 1. Automatic variables are
Explanation: None. a) Declared within the scope of a block,
usually a function
7. What linkage does automatic variables b) Declared outside all functions
have? c) Declared with the auto keyword
a) Internal linkage d) Declared within the keyword extern
b) External linkage
c) No linkage Answer: a
d) None of the mentioned Explanation: None.

Answer: c 2. What is the scope of an automatic variable?


Explanation: None. a) Exist only within that scope in which it is
declared
8. What will be the output of the following C b) Cease to exist after the block is exited
code? c) Exist only within that scope in which it is
declared & exist after the block is exited
1. #include <stdio.h> d) All of the mentioned
2. int main()
Answer: c
3. { Explanation: None.

4. auto i = 10; 3. Automatic variables are allocated memory


in
5. const auto int *p = &i; a) heap
b) Data segment
6. printf("%d\n", i);
c) Code segment
7. } d) stack

a) 10 Answer: d
b) Compile time error Explanation: None.
c) Depends on the standard
d) Depends on the compiler 4. What will be the x in the following C
code?
Answer: a
Explanation: None. 1. #include <stdio.h>

2. void main()
Sanfoundry’s 1000+ Interview Questions &
Answers on C helps anyone preparing for 3. {
Samsung and other companies C interviews. 4. int x;
One should practice these 1000+ interview
questions and answers continuously for 2-3 5. }
months to clear Samsung interviews on C
Programming language. a) automatic variable
b) static variable
Here is a listing of online C test questions on c) register variable
“Automatic Variables” along with answers, d) global variable
explanations and/or solutions:
Answer: a Here is a listing of C questions and puzzles
Explanation: None. on “C Preprocessor” along with answers,
explanations and/or solutions:
5. Automatic variables are initialized to
1. Property which allows to produce different
a) Zero executable for different platforms in C is
b) Junk value called?
c) Nothing a) File inclusion
d) Both Zero & Junk value b) Selective inclusion
c) Conditional compilation
Answer: b d) Recursive macros
Explanation: None.
Answer: c
6. Which of the following storage class Explanation: Conditional compilation is the
supports char data type? preprocessor facility to produce a different
a) register executable.
b) static
c) auto 2. What is #include <stdio.h>?
d) all of the mentioned a) Preprocessor directive
b) Inclusion directive
Answer: d c) File inclusion directive
Explanation: None. d) None of the mentioned
7. A local variable declaration with no storage Answer: a
class specified is by default Explanation: None.
a) auto
b) extern 3. C preprocessors can have compiler specific
c) static features.
d) register a) True
b) False
Answer: a c) Depends on the standard
Explanation: None. d) Depends on the platform

This section on C questions and puzzles Answer: a


focuses on “C Preprocessor”. One shall Explanation: #pragma is compiler specific
practice these questions and puzzles to feature.
improve their C programming skills needed
for various interviews (campus interviews, 4. What will be the output of the following C
walkin interviews, company interviews), code?
placements, entrance exams and other
1. #include <stdio.h>
competitive exams. These programming
puzzles can be attempted by anyone focusing 2. #define foo(m, n) m * n = 10
on learning C Programming language. They
can be a beginner, fresher, engineering 3. int main()
graduate or an experienced IT professional.
Our C questions come with detailed 4. {
explanation of the answers which helps in 5. printf("in main\n");
better understanding of C concepts.
6. } used to select any file.

a) In main 8. What is a preprocessor?


b) Compilation error as lvalue is required for a) That processes its input data to produce
the expression m*n=10 output that is used as input to another
c) Preprocessor error as lvalue is required for program
the expression m*n=10 b) That is nothing but a loader
d) None of the mentioned c) That links various source files
d) All of the mentioned
Answer: a
Explanation: Preprocessor just replaces Answer: a
whatever is given compiler then checks for Explanation: A preprocessor is a program
error at the replaced part of the code. Here it that processes its input data to produce output
is not replaced anywhere. that is used as input to another program.
Output:
$ cc pgm1.c Sanfoundry’s 1000+ MCQs on C helps
$ a.out anyone preparing for placement in Dell and
in main other companies. Anyone looking for Dell
placement papers should practice these 1000+
5. C preprocessor is conceptually the first step
questions continuously for 2-3 months,
during compilation. thereby ensuring a top position in placements.
a) True
b) False Here is a listing of C interview questions on
c) Depends on the compiler “C Preprocessor” along with answers,
d) Depends on the standard explanations and/or solutions:
Answer: a 1. Which of the following are C
Explanation: None. preprocessors?
a) #ifdef
6. Preprocessor feature that supply line b) #define
numbers and filenames to compiler is called? c) #endif
a) Selective inclusion d) all of the mentioned
b) macro substitution
c) Concatenation Answer: d
d) Line control Explanation: None.
Answer: d 2. #include statement must be written
Explanation: None.
a) Before main()
7. #include <somefile.h> are files b) Before any scanf/printf
and #include “somefile.h” files. c) After main()
a) Library, Library d) It can be written anywhere
b) Library, user-created header
c) User-created header, library Answer: b
d) They can include all types of file Explanation: Using these directives before
main() improves readability.
Answer: d
Explanation: Both of these statement can be
3. #pragma exit is primarily used for? Answer: a
a) Checking memory leaks after exiting the Explanation: The C-preprocessors are
program specified with # symbol.
b) Informing Operating System that program
has terminated 6. What is #include directive?
c) Running a function at exiting the program a) Tells the preprocessor to grab the text of a
d) No such preprocessor exist file and place it directly into the current file
b) Statements are not typically placed at the
Answer: c top of a program
Explanation: It is primarily used for running c) All of the mentioned
a function upon exiting the program. d) None of the mentioned

4. What will be the output of the following C Answer: a


code? Explanation: The #include directive tells the
preprocessor to grab the text of a file and
1. #include <stdio.h> place it directly into the current file and are
statements are typically placed at the top of a
2. int main()
program.
3. {
7. The preprocessor provides the ability for
4. int one = 1, two = 2;
a) The inclusion of header files
5. #ifdef next b) The inclusion of macro expansions
c) Conditional compilation and line control
6. one = 2;
d) All of the mentioned
7. two = 1;
Answer: d
8. #endif Explanation: The preprocessor provides the
ability for the inclusion of header files, macro
9. printf("%d, %d", one, two); expansions, conditional compilation, and line
control.
10. }
8. If #include is used with file name in
a) 1, 1
angular brackets.
b) 1, 2
a) The file is searched for in the standard
c) 2, 1
compiler include paths
d) 2, 2
b) The search path is expanded to include the
Answer: b current source directory
Explanation: None. c) The search path will expand
d) None of the mentioned
5. The C-preprocessors are specified with
Answer: a
symbol.
a) # Explanation: With the #include, if the
b) $ filename is enclosed within angle brackets,
c) ” ” the file is searched for in the standard
d) & compiler include paths.
This section on C programming quiz focuses selected
on “File Inclusion”. One shall practice these c) Both the files will be included
quizzes to improve their C programming d) The compiler won’t accept the program
skills needed for various interviews (campus
interviews, walkin interviews, company Answer: b
interviews), placements, entrance exams and Explanation: None.
other competitive exams. These questions can
be attempted by anyone focusing on learning 4. How is search done in #include and
C Programming language. They can be a #include “somelibrary.h” according to C
beginner, fresher, engineering graduate or an standard?
experienced IT professional. Our C quiz a) When former is used, current directory is
comes with detailed explanation of the searched and when latter is used, standard
answers which helps in better understanding directory is searched
of C concepts. b) When former is used, standard directory is
searched and when latter is used, current
Here is a listing of C programming quiz on directory is searched
“File Inclusion” along with answers, c) When former is used, search is done in
explanations and/or solutions: implementation defined manner and when
latter is used, current directory is searched
1. What is the sequence for preprocessor to d) For both, search for ‘somelibrary’ is done
look for the file within <>? in implementation-defined places
a) The predefined location then the current
directory Answer: d
b) The current directory then the predefined Explanation: None.
location
c) The predefined location only 5. How is search done in #include and
d) The current directory location #include”somelibrary.h” normally or
conventionally?
Answer: a a) When former is used, current directory is
Explanation: <> first searches the predefined searched and when latter is used, standard
location for the specified file and then the directory is searched
current directory. b) When former is used, predefined directory
is searched and when latter is used, current
2. Which directory the compiler first looks for directory is searched and then predefined
the file when using #include? directories are searched
a) Current directory where program is saved c) When former is used, search is done in
b) C:COMPILERINCLUDE implementation defined manner and latter is
c) S:SOURCEHEADERS used to search current directory
d) Both C:COMPILERINCLUDE and d) For both, search for somelibrary is done in
S:SOURCEHEADERS simultaneously implementation-defined manner

Answer: b Answer: b
Explanation: None. Explanation: None.

3. What would happen if you create a file 6. Can function definition be present in
stdio.h and use #include “stdio.h”? header files?
a) The predefined library file will be selected a) Yes
b) The user-defined library file will be b) No
c) Depends on the compiler 10. printf("%d\n", foo(2, 3));
d) Depends on the standard
11. }
Answer: a
Explanation: None. a) 23
b) 2 3
7. Comment on the output of the following C c) Compile time error
code. d) Undefined behaviour

1. #include <stdio.h> Answer: a


Explanation: None.
2. #include "test.h"

3. #include "test.h" Sanfoundry’s 1000+ MCQs on C helps


anyone preparing for placement in Tech
4. int main() Mahindra and other companies. Anyone
looking for Tech Mahindra placement papers
5. {
should practice these 1000+ questions
6. //some code
continuously for 2-3 months, thereby
ensuring a top position in placements.
7. }
Here is a listing of online C quiz on “File
a) True Inclusion” along with answers, explanations
b) Compile time error and/or solutions:
c) False
d) Depends on the compiler 1. If the file name is enclosed in double
quotation marks, then
Answer: b a) The preprocessor treats it as a user-defined
Explanation: None. file
b) The preprocessor treats it as a system-
8. What will be the output of the following C defined file
code? c) The preprocessor treats it as a user-defined
file & system-defined file
1. #include <stdio.h> d) None of the mentioned
2. #define foo(m, n) m ## n Answer: a
3. void myfunc();
Explanation: None.

4. int main() 2. If the file name is enclosed in angle


brackets, then
5. { a) The preprocessor treats it as a user-defined
file
6. myfunc(); b) The preprocessor treats it as a system-
7. }
defined file
c) The preprocessor treats it as a user-defined
8. void myfunc() file & system-defined file
d) None of the mentioned
9. {
Answer: b 5. What will be the output of the following C
Explanation: None. code?

3. What will be the output of the following C 1. #include <stdio.h>


code snippet?
2. #include "printf"
1. #include (stdio.h)
3. void main()
2. void main()
4. {
3. {
5. printf("hello");
4. printf("hello");
6. }
5. }
a) hello
a) hello b) Error
b) Nothing c) Depends on compiler
c) Compile time error d) Varies
d) Depends on compiler
Answer: a
Answer: c Explanation: None.
Explanation: File to be included must be
specified either in “” or <>. 6. Which of the following file extensions are
Output: accepted with #include?
$ cc pgm1.c a) .h
pgm1.c:1: error: #include expects b) .in
“FILENAME” or c) .com
pgm1.c: In function ‘main’: d) All of the mentioned
pgm1.c:4: warning: incompatible implicit
Answer: d
declaration of built-in function ‘printf’
Explanation: The preprocessor will include
4. The below two lines are equivalent to whatever file extension you specify in your
#include statement. However, it is not a good
practice as another person debugging it will
1. #define C_IO_HEADER <stdio.h> find it difficult in finding files you have
included.
2. #include C_IO_HEADER
7. Which of the following names for files not
a) #include<stdlib.h> accepted?
b) #include”printf” a) header.h.h
c) #include”C_IO_HEADER” b) 123header.h
d) #include<stdio.h> c) _head_er.h
d) None of the mentioned
Answer: d
Explanation: Since C_IO_HEADER is Answer: d
defined to be <stdio.h>, the second line Explanation: All file names are accepted as
becomes #include<stdio.h>, since for the execution to occur. There are no
C_IO_HEADER is replaced with <stdio.h> constraints on giving file names for inclusion.
depending on the value of NULL
TOPIC 2.3 POINTERS AND
d) p q
ARRAYS IN C
Answer: a
Sanfoundry’s 1000+ MCQs on C helps Explanation: None.
anyone preparing for placement in Persistent
Systems and other companies. Anyone 2. What will be the output of the following C
looking for Persistent placement papers code?
should practice these 1000+ questions
continuously for 2-3 months, thereby 1. #include <stdio.h>
ensuring a top position in placements. 2. int main()

Here is a listing of C quiz on “Pointer 3. {


Addresses” along with answers, explanations
and/or solutions: 4. int i = 10;

1. What will be the output of the following C 5. void *p = &i;


code?
6. printf("%d\n", (int)*p);

1. #include <stdio.h> 7. return 0;

2. int main() 8. }

3. {
a) Compile time error
4. char *p = NULL; b) Segmentation fault/runtime crash
c) 10
5. char *q = 0; d) Undefined behaviour
6. if (p) Answer: a
Explanation: None.
7. printf(" p ");

8. else 3. What will be the output of the following C


code?
9. printf("nullp");
1. #include <stdio.h>
10. if (q)
2. int main()
11. printf("q\n");
3. {
12. else
4. int i = 10;
13. printf(" nullq\n");
5. void *p = &i;
14. }
6. printf("%f\n", *(float*)p);
a) nullp nullq
b) Depends on the compiler 7. return 0;
c) x nullq where x can be p or nullp 8. }
a) Compile time error 2. int *f();
b) Undefined behaviour
c) 10 3. int main()
d) 0.000000 4. {

Answer: d 5. int *p = f();


Explanation: None.
6. printf("%d\n", *p);
4. What will be the output of the following C
code? 7. }

1. #include <stdio.h>
8. int *f()

2. int *f();
9. {

3. int main()
10. int j = 10;

4. {
11. return &j;

5. int *p = f();
12. }

6. printf("%d\n", *p); a) 10
b) Compile time error
7. } c) Segmentation fault/runtime crash
d) Undefined behaviour
8. int *f()
Answer: a
9. {
Explanation: We are returning address of a
10. int *j = (int*)malloc(sizeo local variable which should not be done. In
f(int)); this specific instance, we are able to see the
value of 10, which may not be the case if we
11. *j = 10; call other functions before calling printf() in
main().
12. return j;

13. } 6. Comment on the following pointer


declaration.
a) 10
int *ptr, p;
b) Compile time error
c) Segmentation fault/runtime crash since a) ptr is a pointer to integer, p is not
pointer to local variable is returned b) ptr and p, both are pointers to integer
d) Undefined behaviour c) ptr is a pointer to integer, p may or may not
be
Answer: a
d) ptr and p both are not pointers to integer
Explanation: None.
Answer: a
5. What will be the output of the following C
Explanation: None.
code?
7. What will be the output of the following C
1. #include <stdio.h>
code?
1. #include <stdio.h> 1. Which is an indirection operator among the
following?
2. int main() a) &
3. {
b) *
c) ->
4. int *ptr, a = 10; d) .

5. ptr = &a; Answer: b


Explanation: None.
6. *ptr += 1;
2. Which of the following does not initialize
7. printf("%d,%d/n", *ptr, a);
ptr to null (assuming variable declaration of a
8. } as int a=0;)?
a) int *ptr = &a;
a) 10,10 b) int *ptr = &a – &a;
b) 10,11 c) int *ptr = a – a;
c) 11,10 d) All of the mentioned
d) 11,11
Answer: a
Answer: d Explanation: None.
Explanation: None.
3. What will be the output of the following C
8. Comment on the following C statement. code?

const int *ptr; 1. #include <stdio.h>

a) You cannot change the value pointed by ptr 2. int x = 0;


b) You cannot change the pointer ptr itself
c) You May or may not change the value 3. void main()

pointed by ptr 4. {
d) You can change the pointer as well as the
value pointed by it 5. int *ptr = &x;

Answer: a 6. printf("%p\n", ptr);


Explanation: None.
7. x++;

Sanfoundry’s 1000+ Interview Questions & 8. printf("%p\n ", ptr);


Answers on C helps anyone preparing for HP
and other companies C interviews. One 9. }
should practice these 1000+ interview
a) Same address
questions and answers continuously for 2-3
b) Different address
months to clear HP interviews on C
c) Compile time error
Programming language.
d) Varies
Here is a listing of online C test questions on
Answer: a
“Pointers and Addresses” along with answers,
Explanation: None.
explanations and/or solutions:
4. What will be the output of the following C c) 0xbfd605e8 0xbfd605e9
code? d) Run time error

1. #include <stdio.h> Answer: a


Explanation: None.
2. int x = 0;
6. What will be the output of the following C
3. void main()
code?
4. {
1. #include <stdio.h>
5. int *const ptr = &x;
2. void main()
6. printf("%p\n", ptr);
3. {
7. ptr++;
4. int x = 0;
8. printf("%p\n ", ptr);
5. int *ptr = &5;
9. }
6. printf("%p\n", ptr);
a) 0 1
7. }
b) Compile time error
c) 0xbfd605e8 0xbfd605ec a) 5
d) 0xbfd605e8 0xbfd605e8 b) Address of 5
c) Nothing
Answer: b
d) Compile time error
Explanation: None.
Answer: d
5. What will be the output of the following C
Explanation: None.
code?
7. What will be the output of the following C
1. #include <stdio.h>
code?
2. void main()
1. #include <stdio.h>
3. {
2. void main()
4. int x = 0;
3. {
5. int *ptr = &x;
4. int x = 0;
6. printf("%p\n", ptr);
5. int *ptr = &x;
7. ptr++;
6. printf("%d\n", *ptr);
8. printf("%p\n ", ptr);
7. }
9. }
a) Address of x
a) 0xbfd605e8 0xbfd605ec b) Junk value
b) 0xbfd605e8 0cbfd60520 c) 0
d) Run time error
Answer: c a) 10
Explanation: None. b) Some garbage value
c) Compile time error
This section on C MCQs (multiple choice d) Segmentation fault/code crash
questions) focuses on “Pointers and Function
Answer: c
Arguments”. One shall practice these MCQs
Explanation: None.
to improve their C programming skills needed
for various interviews (campus interviews, 2. What will be the output of the following C
walkin interviews, company interviews), code?
placements, entrance exams and other
competitive exams. These questions can be 1. #include <stdio.h>
attempted by anyone focusing on learning C
Programming language. They can be a 2. void foo(int*);
beginner, fresher, engineering graduate or an
experienced IT professional. Our multiple 3. int main()
choice questions come with detailed 4. {
explanation of the answers which helps in
better understanding of C concepts. 5. int i = 10, *p = &i;

Here is a listing of C multiple choice 6. foo(p++);


questions on “Pointers and Function
Arguments” along with answers, explanations 7. }
and/or solutions:
8. void foo(int *p)

1. What will be the output of the following C 9. {


code?
10. printf("%d\n", *p);
1. #include <stdio.h>
11. }
2. void foo(int*);
a) 10
3. int main() b) Some garbage value
4. {
c) Compile time error
d) Segmentation fault
5. int i = 10;
Answer: a
6. foo((&i)++); Explanation: None.
7. } 3. What will be the output of the following C
code?
8. void foo(int *p)

9. { 1. #include <stdio.h>

10. printf("%d\n", *p); 2. void foo(float *);

11. }
3. int main()

4. {
5. int i = 10, *p = &i; c) Compile time error
d) Segmentation fault/code crash
6. foo(&i);
Answer: a
7. }
Explanation: None.
8. void foo(float *p)
5. What will be the output of the following C
9. { code?

10. printf("%f\n", *p); 1. #include <stdio.h>

11. } 2. int main()

a) 10.000000 3. {
b) 0.000000
c) Compile time error 4. int i = 97, *p = &i;

d) Undefined behaviour 5. foo(&p);

Answer: b 6. printf("%d ", *p);


Explanation: None.
7. return 0;
4. What will be the output of the following C
code? 8. }

9. void foo(int **p)


1. #include <stdio.h>
10. {
2. int main()
11. int j = 2;
3. {
12. *p = &j;
4. int i = 97, *p = &i;
13. printf("%d ", **p);
5. foo(&i);
14. }
6. printf("%d ", *p);

7. } a) 2 2
b) 2 97
8. void foo(int *p) c) Undefined behaviour
d) Segmentation fault/code crash
9. {
Answer: a
10. int j = 2;
Explanation: None.
11. p = &j;
6. What will be the output of the following C
12. printf("%d ", *p); code?

13. } 1. #include <stdio.h>

a) 2 97 2. int main()
b) 2 2
3. { 10. void foo(int **const p)

4. int i = 11; 11. {

5. int *p = &i; 12. int j = 11;

6. foo(&p); 13. *p = &j;

7. printf("%d ", *p); 14. printf("%d ", **p);

8. } 15. }

9. void foo(int *const *p) a) 11 11 11


b) 11 11 Undefined-value
10. {
c) Compile time error
11. int j = 10; d) Segmentation fault/code-crash

12. *p = &j; Answer: b


Explanation: None.
13. printf("%d ", **p);
8. What will be the output of the following C
14. }
code?
a) Compile time error 1. #include <stdio.h>
b) 10 10
c) Undefined behaviour 2. int main()
d) 10 11
3. {
Answer: a
Explanation: None. 4. int i = 10;

5. int *const p = &i;


7. What will be the output of the following C
code? 6. foo(&p);

1. #include <stdio.h> 7. printf("%d\n", *p);

2. int main() 8. }

3. { 9. void foo(int **p)

4. int i = 10; 10. {

5. int *p = &i; 11. int j = 11;

6. foo(&p); 12. *p = &j;

7. printf("%d ", *p); 13. printf("%d\n", **p);

8. printf("%d ", *p); 14. }

9. } a) 11 11
b) Undefined behaviour
c) Compile time error 3. Arguments that take input by user before
d) Segmentation fault/code-crash running a program are called?
a) Main function arguments
Answer: a b) Main arguments
Explanation: None. c) Command-Line arguments
d) Parameterized arguments
9. Which of the following is the correct
syntax to send an array as a parameter to Answer: c
function? Explanation: None.
a) func(&array);
b) func(#array); 4. What is the maximum number of
c) func(*array); arguments that can be passed in a single
d) func(array[size]); function?
a) 127
Answer: a b) 253
Explanation: None. c) 361
d) No limits in number of arguments
Sanfoundry’s 1000+ Interview Questions &
Answers on C helps anyone preparing for Answer: b
Citrix and other companies C interviews. One Explanation: None.
should practice these 1000+ interview
5. What will be the output of the following C
questions and answers continuously for 2-3
code?
months to clear Citrix interviews on C
Programming language. 1. #include <stdio.h>

Here is a listing of C interview questions on 2. void m(int *p, int *q)


“Pointers and Function Arguments”:
3. {
1. Which of the following can never be sent
by call-by-value? 4. int temp = *p; *p = *q; *q
a) Variable = temp;
b) Array 5. }
c) Structures
d) Both Array and Structures 6. void main()

Answer: b 7. {
Explanation: None.
8. int a = 6, b = 5;
2. Which type of variables can have the same
9. m(&a, &b);
name in a different function?
a) Global variables 10. printf("%d %d\n", a, b);
b) Static variables
c) Function arguments 11. }
d) Both static variables and Function
arguments a) 5 6
b) 6 5
Answer: d c) 5 5
Explanation: None. d) 6 6
Answer: a 7. }
Explanation: None.
8. void main()
6. What will be the output of the following C
9. {
code?
10. int a = 6, b = 5;
1. #include <stdio.h>
11. m(a, b);
2. void m(int *p)
12. printf("%d %d\n", a, b);
3. {
13. }
4. int i = 0;

5. for(i = 0;i < 5; i++)


a) 5 6
b) 5 5
6. printf("%d\t", p[i]); c) 6 5
d) 6 6
7. }
Answer: c
8. void main() Explanation: None.
9. {
8. What will be the output of the following C
10. int a[5] = {6, 5, 3}; code?

11. m(&a); 1. #include <stdio.h>

12. } 2. void m(int p, int q)

a) 0 0 0 0 0 3. {
b) 6 5 3 0 0
4. printf("%d %d\n", p, q);
c) Run time error
d) 6 5 3 junk junk 5. }

Answer: b 6. void main()


Explanation: None.
7. {
7. What will be the output of the following C
code? 8. int a = 6, b = 5;

9. m(a);
1. #include <stdio.h>
10. }
2. void m(int p, int q)

3. {
a) 6
b) 6 5
4. int temp = p; c) 6 junk value
d) Compile time error
5. p = q;
Answer: d
6. q = temp; Explanation: None.
9. What will be the output of the following C and/or solutions:
code?
1. What will be the output of the following C
1. #include <stdio.h> code?
2. void m(int p) 1. #include <stdio.h>

3. { 2. void main()

4. printf("%d\n", p); 3. {

5. } 4. int a[3] = {1, 2, 3};

6. void main() 5. int *p = a;

7. { 6. printf("%p\t%p", p, a);

8. int a = 6, b = 5; 7. }

9. m(a, b); a) Same address is printed


10. printf("%d %d\n", a, b);
b) Different address is printed
c) Compile time error
11. } d) Nothing

a) 6 Answer: a
b) 6 5 Explanation: None.
c) 6 junk value
d) Compile time error 2. What will be the output of the following C
code?
Answer: d
Explanation: None. 1. #include <stdio.h>

2. void main()
This section on C quiz focuses on “Pointers
and Arrays”. One shall practice these quizzes 3. {
to improve their C programming skills needed
4. char *s = "hello";
for various interviews (campus interviews,
walkin interviews, company interviews), 5. char *p = s;
placements, entrance exams and other
competitive exams. These questions can be 6. printf("%p\t%p", p, s);
attempted by anyone focusing on learning C
Programming language. They can be a 7. }
beginner, fresher, engineering graduate or an
a) Different address is printed
experienced IT professional. Our C quiz
b) Same address is printed
comes with detailed explanation of the
c) Run time error
answers which helps in better understanding
d) Nothing
of C concepts.
Answer: b
Here is a listing of C quiz on “Pointers and
Explanation: None.
Arrays” along with answers, explanations
3. What will be the output of the following C 5. What will be the output of the following C
code? code?

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

2. void main() 2. void main()

3. { 3. {

4. char *s= "hello"; 4. char *s= "hello";

5. char *p = s; 5. char *p = s;

6. printf("%c\t%c", p[0], s[1] 6. printf("%c\t%c", 1[p], s[1]


); );

7. } 7. }

a) Run time error a) h h


b) h h b) Run time error
c) h e c) l l
d) h l d) e e

Answer: c Answer: d
Explanation: None. Explanation: None.

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

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

2. void main() 2. void foo( int[] );

3. { 3. int main()

4. char *s= "hello"; 4. {

5. char *p = s; 5. int ary[4] = {1, 2, 3, 4};

6. printf("%c\t%c", *(p + 3), 6. foo(ary);


s[1]);
7. printf("%d ", ary[0]);
7. }
8. }
a) h e
b) l l 9. void foo(int p[4])
c) l o 10. {
d) l e
11. int i = 10;
Answer: d
Explanation: None. 12. p = &i;
13. printf("%d ", p[0]); 7. }

14. } a) 2 3
b) Compile time error
a) 10 10 c) 2 4
b) Compile time error d) 2 somegarbagevalue
c) 10 1
d) Undefined behaviour Answer: d
Explanation: None.
Answer: c
Explanation: None.
Sanfoundry’s 1000+ Interview Questions &
7. What will be the output of the following C Answers on C helps anyone preparing for
code? Tech Mahindra and other companies C
interviews. One should practice these 1000+
1. #include <stdio.h> interview questions and answers continuously
for 2-3 months to clear Tech Mahindra
2. int main() interviews on C Programming language.
3. { Here is a listing of C programming questions
4. int ary[4] = {1, 2, 3, 4};
on “Address Arithmetic” along with answers,
explanations and/or solutions:
5. int *p = ary + 3;
1. What will be the output of the following C
6. printf("%d\n", p[-2]); code?
7. } 1. #include <stdio.h>

a) 1 2. void main()
b) 2
c) Compile time error 3. {
d) Some garbage value 4. char *s = "hello";

Answer: b 5. char *p = s * 3;
Explanation: None.
6. printf("%c\t%c", *p, s[1]);
8. What will be the output of the following C
code? 7. }

1. #include <stdio.h> a) h e
b) l e
2. int main() c) Compile time error
d) l h
3. {
Answer: c
4. int ary[4] = {1, 2, 3, 4};
Explanation: None.
5. int *p = ary + 3;
2. What will be the output of the following C
6. printf("%d %d\n", p[-2], ar code?
y[*p]);
1. #include <stdio.h> 4. What will be the output of the following C
code?
2. void main()
1. #include <stdio.h>
3. {
2. int main()
4. char *s= "hello";
3. {
5. char *p = s + 2;
4. void *p;
6. printf("%c\t%c", *p, s[1]);
5. int a[4] = {1, 2, 3, 4};
7. }
6. p = &a[3];
a) l e
b) h e 7. int *ptr = &a[2];
c) l l
d) h l 8. int n = (int*)p - ptr;

Answer: a 9. printf("%d\n", n);

Explanation: None. 10. }

3. What will be the output of the following C a) 1


code? b) Compile time error
c) Segmentation fault
1. #include <stdio.h>
d) 4
2. int main()
Answer: a
3. { Explanation: None.

4. void *p; 5. What will be the output of the following C


code?
5. int a[4] = {1, 2, 3, 8};
1. #include <stdio.h>
6. p = &a[3];
2. int main()
7. int *ptr = &a[2];
3. {
8. int n = p - ptr;
4. int a[4] = {1, 2, 3, 4};
9. printf("%d\n", n);
5. int b[4] = {1, 2, 3, 4};
10. }
6. int n = &b[3] - &a[2];
a) 1
b) Compile time error 7. printf("%d\n", n);
c) Segmentation fault
d) 4 8. }

Answer: b a) -3
Explanation: None. b) 5
c) 4 8. printf("%d\n", *ptr);
d) Compile time error
9. }
Answer: a
Explanation: None. a) 4
b) 3
6. What will be the output of the following C c) Compile time error
code? d) Undefined behaviour

1. #include <stdio.h> Answer: c


Explanation: None.
2. int main()
8. What will be the output of the following C
3. { code?
4. int a[4] = {1, 2, 3, 4};
1. #include <stdio.h>
5. int *p = &a[1];
2. int main()
6. int *ptr = &a[2];
3. {
7. ptr = ptr * 1;
4. int a[4] = {1, 2, 3, 4};
8. printf("%d\n", *ptr);
5. void *p = &a[1];
9. }
6. void *ptr = &a[2];
a) 2 7. int n = 1;
b) 1
c) Compile time error 8. n = ptr - p;
d) Undefined behaviour
9. printf("%d\n", n);
Answer: c
10. }
Explanation: None.
a) 1
7. What will be the output of the following C
b) 4
code?
c) Compile time error
1. #include <stdio.h>
d) Depends on the compiler

2. int main() Answer: b


Explanation: None.
3. {

4. int a[4] = {1, 2, 3, 4}; This section on C interview questions and


answers focuses on “Character Pointers and
5. int *ptr = &a[2]; Functions”. One shall practice these interview
questions to improve their C programming
6. float n = 1; skills needed for various interviews (campus
7. ptr = ptr + n;
interviews, walkin interviews, company
interviews), placements, entrance exams and
other competitive exams. These questions can 3. {
be attempted by anyone focusing on learning
C Programming language. They can be a 4. char *str = "hello world";
beginner, fresher, engineering graduate or an 5. char strc[] = "good morning
experienced IT professional. Our C Interview india\n";
questions come with detailed explanation of
the answers which helps in better 6. strcpy(strc, str);
understanding of C concepts.
7. printf("%s\n", strc);
Here is a listing of C interview questions on
8. return 0;
“Character Pointers and Functions” along
with answers, explanations and/or solutions: 9. }

1. What will be the output of the following C a) hello world


code? b) hello worldg india
c) Compile time error
1. #include <stdio.h> d) Undefined behaviour
2. int main()
Answer: a
3. { Explanation: None.

4. char *str = "hello, world\n 3. What will be the output of the following C
"; code?
5. char *strc = "good morning 1. #include <stdio.h>
\n";
2. int main()
6. strcpy(strc, str);
3. {
7. printf("%s\n", strc);
4. char *str = "hello, world!!
8. return 0;
\n";
9. } 5. char strc[] = "good morning
\n";
a) hello, world
b) Crash/segmentation fault 6. strcpy(strc, str);
c) Undefined behaviour
d) Run time error 7. printf("%s\n", strc);

8. return 0;
Answer: b
Explanation: None. 9. }

2. What will be the output of the following C a) hello, world!!


code? b) Compile time error
c) Undefined behaviour
1. #include <stdio.h> d) Segmenation fault
2. int main()
Answer: c a) hello. world
Explanation: None. b) hello, world
c) Compile error
4. What will be the output of the following C d) Segmentation fault
code?
Answer: a
1. #include <stdio.h> Explanation: None.
2. int main() 6. What will be the output of the following C
3. {
code?

4. char *str = "hello, world\n 1. #include <stdio.h>


";
2. int main()
5. str[5] = '.';
3. {
6. printf("%s\n", str);
4. char *str = "hello world";
7. return 0;
5. char strary[] = "hello worl
8. } d";

a) hello. world 6. printf("%d %d\n", sizeof(st


r), sizeof(strary));
b) hello, world
c) Compile error 7. return 0;
d) Segmentation fault
8. }
Answer: d
Explanation: None. a) 11 11
b) 12 12
5. What will be the output of the following C c) 4 12
code? d) 4 11

1. #include <stdio.h> Answer: c


Explanation: None.
2. int main()
7. What will be the output of the following C
3. {
code?
4. char str[] = "hello, world"
; 1. #include <stdio.h>

5. str[5] = '.'; 2. int main()

6. printf("%s\n", str); 3. {

7. return 0; 4. char *str = "hello world";

8. } 5. char strary[] = "hello worl


d";
6. printf("%d %d\n", strlen(st 1. #include <stdio.h>
r), strlen(strary));
2. void fun(char *k)
7. return 0;
3. {
8. }
4. printf("%s", k);
a) 11 11
b) 12 11 5. }
c) 11 12
6. void main()
d) x 11 where x can be any positive integer.
7. {
Answer: a
Explanation: None. 8. char s[] = "hello";

8. What will be the output of the following C 9. fun(s);


code?
10. }
1. #include <stdio.h>
a) hello
2. void f(char *k) b) Run time error
c) Nothing
3. { d) h
4. k++; Answer: a
Explanation: None.
5. k[2] = 'm';

6. printf("%c\n", *k); Sanfoundry’s 1000+ Interview Questions &


Answers on C helps anyone preparing for
7. }
Ericsson and other companies C interviews.
8. void main() One should practice these 1000+ interview
questions and answers continuously for 2-3
9. { months to clear Ericsson interviews on C
Programming language.
10. char s[] = "hello";
Here is a listing of tough C interview
11. f(s);
questions on “Initialization of Pointer Arrays”
12. } along with answers, explanations and/or
solutions:
a) l
b) e 1. What will be the output of the following C
c) h code?
d) o
1. #include <stdio.h>
Answer: b
2. int main()
Explanation: None.
3. {
9. What will be the output of the following C
code? 4. char *p[1] = {"hello"};
5. printf("%s", (p)[0]); 6. printf("%d", (*a)[0]);

6. return 0; 7. return 0;

7. } 8. }

a) Compile time error a) Compile time error


b) Undefined behaviour b) Undefined behaviour
c) hello c) 0
d) None of the mentioned d) Some garbage value

Answer: c Answer: c
Explanation: None. Explanation: None.

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

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

2. int main() 2. int main()

3. { 3. {

4. char **p = {"hello", "hi", 4. int i = 0, j = 1;


"bye"};
5. int *a[] = {&i, &j};
5. printf("%s", (p)[0]);
6. printf("%d", *a[0]);
6. return 0;
7. return 0;
7. }
8. }
a) Compile time error
b) Undefined behaviour a) Compile time error
c) hello b) Undefined behaviour
d) Address of hello c) 0
d) Some garbage value
Answer: b
Explanation: None. Answer: c
Explanation: None.
3. What will be the output of the following C
code? 5. What will be the output of the following C
code?
1. #include <stdio.h>
1. #include <stdio.h>
2. int main()
2. int main()
3. {
3. {
4. int i = 0, j = 1;
4. int i = 0, j = 1;
5. int *a[] = {&i, &j};
5. int *a[] = {&i, &j}; other competitive exams. These questions can
be attempted by anyone focusing on learning
6. printf("%d", (*a)[1]); C Programming language. They can be a
7. return 0;
beginner, fresher, engineering graduate or an
experienced IT professional. Our C
8. } programming questions come with detailed
explanation of the answers which helps in
a) Compile time error better understanding of C concepts.
b) Undefined behaviour
c) 1 Here is a listing of C programming questions
d) Some garbage value on “Pointers Vs. Multi-dimensional Arrays”
along with answers, explanations and/or
Answer: d solutions:
Explanation: None.
1. Which is true for a, if a is defined as “int
6. Which of the following are generated from a[10][20];”?
char pointer? a) a is true two-dimensional array
a) char *string = “Hello.”; b) 200 int-sized locations have been set aside
b) c) The conventional rectangular subscript
calculation 20 * row + col is used to find the
char *string; element a[row, col].
d) All of the mentioned
scanf("%s", string);
Answer: d
c) char string[] = “Hello.”;
Explanation: None.
d) char *string = “Hello.”; and char string[] =
“Hello.”; 2. Which is true for b, if b is defined as “int
*b[10];”?
Answer: a
a) The definition only allocates 10 pointers
Explanation: None.
and does not initialize them
7. Which of the following declaration are b) Initialization must be done explicitly
illegal? c) The definition only allocates 10 pointers
a) int a[][] = {{1, 2, 3}, {2, 3, 4, 5}}; and does not initialize them & Initialization
b) int *a[] = {{1, 2, 3}, {2, 3, 4, 5}}; must be done explicitly
c) int a[4][4] = {{1, 2, 3}, {2, 3, 4, 5}}; d) Error
d) none of the mentioned
Answer: c
Answer: a Explanation: None.
Explanation: None.
3. What will be the output of the following C
code?
This section on C programming questions and
answers focuses on “Pointers Vs. Multi- 1. #include <stdio.h>
dimensional Arrays”. One shall practice these
questions to improve their C programming 2. void main()
skills needed for various interviews (campus
3. {
interviews, walkin interviews, company
interviews), placements, entrance exams and
4. char a[10][5] = {"hi", "hel 1. #include <stdio.h>
lo", "fellows"};
2. void main()
5. printf("%s", a[2]);
3. {
6. }
4. char a[10][5] = {"hi", "hel
a) fellows lo", "fellows"};
b) fellow
5. printf("%d", sizeof(a[1]));
c) fello
d) fell 6. }

Answer: c a) 2
Explanation: Since every row in the array b) 4
a[10][5] can contain only 5 characters, the c) 5
a[2] element will hold “fello” i.e. 5 d) 10
characters. There will not be any null
character in a[2]. Since, the array is Answer: c
completely intialized, other rows (row a[3] Explanation: None.
will have only null characters. Hence, printf()
using %s specifier will display fello only. 6. What will be the output of the following C
code?
4. What will be the output of the following C
code? 1. #include <stdio.h>

1. #include <stdio.h> 2. int main()

2. void main() 3. {

3. { 4. char a[1][5] = {"hello"};

4. char a[10][5] = {"hi", "hel 5. printf("%s", a[0]);


lo", "fellows"};
6. return 0;
5. printf("%p\n", a);
7. }
6. printf("%p", a[0]);
a) Compile time error
7. } b) hello
c) Undefined behaviour
a) same address is printed d) hellon
b) different address is printed
c) hello Answer: c
d) hi hello fello Explanation: None.
Answer: a 7. What will be the output of the following C
Explanation: None. code?
5. What will be the output of the following C 1. #include <stdio.h>
code?
2. int main()
3. { 1. #include <stdio.h>

4. char *a[1] = {"hello"}; 2. int main()

5. printf("%s", a[0]); 3. {

6. return 0; 4. char *a[2] = {"hello", "hi"


};
7. }
5. printf("%d", sizeof(a));
a) Compile time error
b) hello 6. return 0;
c) Undefined behaviour 7. }
d) hellon
a) 9
Answer: b b) 4
Explanation: None. c) 8
d) 10
8. Which of the following statements are
true? Answer: c
Explanation: None.
P. Pointer to Array

Q. Multi-dimensional array 2. What will be the output of the following C


code?
a) P are static, Q are static
b) P are static, Q are dynamic 1. #include <stdio.h>
c) P are dynamic, Q are static
2. int main()
d) P are dynamic, Q are dynamic
3. {
Answer: c
Explanation: None. 4. char a[2][6] = {"hello", "h
i"};
Sanfoundry’s 1000+ Interview Questions & 5. printf("%d", sizeof(a));
Answers on C helps anyone preparing for
Intel and other companies C interviews. One 6. return 0;
should practice these 1000+ interview
questions and answers continuously for 2-3 7. }
months to clear Intel interviews on C
a) 9
Programming language.
b) 12
Here is a listing of C programming questions c) 8
on “Pointers Vs. Multi-dimensional Arrays” d) 10
along with answers, explanations and/or
Answer: b
solutions:
Explanation: None.
1. What will be the output of the following C
code (considering sizeof char is 1 and pointer 3. What will be the output of the following C
is 4)? code?
1. #include <stdio.h> b) Input can be taken from user
c) Faster Access
2. int main() d) All of the mentioned
3. {
Answer: d
4. char a[2][6] = {"hello", "h Explanation: None.
i"};
6. Which of the following operation is
5. printf("%s", *a + 1); possible using a pointer char? (Assuming the
declaration is char *a;)
6. return 0;
a) Input via %s
7. }
b) Generation of the multidimensional array
c) Changing address to point at another
a) hello location
b) hi d) All of the mentioned
c) ello
d) ello hi Answer: c
Explanation: None.
Answer: c
Explanation: None. 7. Comment on the following two operations.

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


4. What will be the output of the following C //- 1
code? int b[4][4] = {{1, 2, 3}, {1, 2, 3,
4}};//- 2
1. #include <stdio.h>
a) 1 will work, 2 will not
2. int main() b) 1 and 2, both will work
c) 1 won’t work, 2 will work
3. {
d) Neither of them will work
4. char *a[2] = {"hello", "hi"
}; Answer: c
Explanation: None.
5. printf("%s", *(a + 1));
8. Comment on the following two operations.
6. return 0;
int *a[] = {{1, 2, 3}, {1, 2, 3, 4}};
7. } //- 1
int b[][] = {{1, 2, 3}, {1, 2, 3,
a) hello 4}}; //- 2
b) ello
c) hi a) 1 works, 2 doesn’t
d) ello hi b) 2 works, 1 doesn’t
c) Both of them work
Answer: c d) Neither of them work
Explanation: None.
Answer: d
5. What is the advantage of a Explanation: None.
multidimensional array over pointer array?
a) Predefined size
This section on C interview questions and Answer: a
answers focuses on “Command Line Explanation: None.
Arguments”. One shall practice these
interview questions to improve their C 3. In linux, argv[0] by command-line
programming skills needed for various argument can be occupied by
interviews (campus interviews, walkin a) ./a.out
interviews, company interviews), placements, b) ./test
entrance exams and other competitive exams. c) ./fun.out.out
These questions can be attempted by anyone d) all of the mentioned
focusing on learning C Programming
language. They can be a beginner, fresher, Answer: d
engineering graduate or an experienced IT Explanation: All the options mentioned
professional. Our C Interview questions come (./a.out, ./test, ./fun.out.out) are simply the
with detailed explanation of the answers command without any argument. A command
which helps in better understanding of C is always stored as argument vector zero i.e.,
concepts. argv[0] always contain the command where
as argv[1], argv[2], etc. contains the
Here is a listing of C interview questions on arguments to the commands, if any.
“Command Line Arguments” along with
answers, explanations and/or solutions: 4. What type of array is generally generated
in Command-line argument?
1. What does argc and argv indicate in a) Single dimension array
command-line arguments? b) 2-Dimensional Square Array
(Assuming: int main(int argc, char *argv[]) ) c) Jagged Array
a) argument count, argument variable d) 2-Dimensional Rectangular Array
b) argument count, argument vector
c) argument control, argument variable Answer: c
d) argument control, argument vector Explanation: None.

Answer: b 5. What will be the output of the following C


Explanation: None. statement? (assuming the input is “cool
brother in city”)
2. Which of the following syntax is correct
for command-line arguments? printf(“%s\n”, argv[argc]);
a)
a) (null)
int main(int var, char *varg[]) b) City
c) In
b) d) Segmentation Fault

int main(char *argv[], int argc) Answer: a


Explanation: None.
c)
6. What is the first argument in command line
int main() arguments?
{
int argv, char *argc[];
a) The number of command-line arguments
} the program was invoked with;
b) A pointer to an array of character strings
d) none of the mentioned that contain the arguments
c) Nothing Answer: c
d) None of the mentioned Explanation: None.

Answer: a 2. What is the index of the last argument in


Explanation: None. command line arguments?
a) argc – 2
7. What is the second argument in command b) argc + 1
line arguments? c) argc
a) The number of command-line arguments d) argc – 1
the program was invoked with;
b) A pointer to an array of character strings Answer: d
that contain the arguments, one per string Explanation: None.
c) Nothing
d) None of the mentioned 3. What will be the output of the following C
code (if run with no options or arguments)?
Answer: b
Explanation: None. 1. #include <stdio.h>

8. What is argv[0] in command line 2. int main(int argc, char *argv[]


)
arguments?
a) The name by which the program was 3. {
invoked
b) The name of the files which are passed to 4. printf("%d\n", argc);
the program
c) Count of the arguments in argv[] vector 5. return 0;
d) None of the mentioned 6. }

Answer: a a) 0
Explanation: None. b) 1
c) Depends on the platform
Sanfoundry’s 1000+ MCQs on C helps d) Depends on the compiler
anyone preparing for placement in Huawei
and other companies. Anyone looking for Answer: b
Huawei placement papers should practice Explanation: None.
these 1000+ questions continuously for 2-3
months, thereby ensuring a top position in 4. What will be the output of the following C
placements. code (run without any command line
arguments)?
Here is a listing of C interview questions on
“Command Line Arguments” along with 1. #include <stdio.h>
answers, explanations and/or solutions:
2. int main(int argc, char *argv[]
)
1. A program that has no command line
arguments will have argc 3. {
a) Zero
b) Negative 4. while (argc--)
c) One
d) Two 5. printf("%s\n", argv[argc]);
6. return 0; 6. return 0;

7. } 7. }

a) Compile time error a) Segmentation fault/code crash


b) Executablefilename b) Executable file name
c) Segmentation fault c) Depends on the platform
d) Undefined d) Depends on the compiler

Answer: b Answer: a
Explanation: None. Explanation: None.

5. What will be the output of the following C 7. What will be the output of the following C
code (run without any command line code (run without any command line
arguments)? arguments)?

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

2. int main(int argc, char *argv[] 2. int main(int argc, char *argv[]
) )

3. { 3. {

4. printf("%s\n", argv[argc]); 4. while (*argv != NULL)

5. return 0; 5. printf("%s\n", *(argv++));

6. } 6. return 0;

a) Segmentation fault/code crash 7. }


b) Executable file name
c) Depends on the platform a) Segmentation fault/code crash
d) Depends on the compiler b) Executable file name
c) Depends on the platform
Answer: a d) Depends on the compiler
Explanation: None.
Answer: b
6. What will be the output of the following C Explanation: None.
code (run without any command line
arguments)? 8. What will be the output of the following C
code (run without any command line
1. #include <stdio.h> arguments)?

2. int main(int argc, char *argv[] 1. #include <stdio.h>


)
2. int main(int argc, char *argv[]
3. { )

4. while (*argv++ != NULL) 3. {

5. printf("%s\n", *argv); 4. while (argv != NULL)


5. printf("%s\n", *(argv++)); 8. first();

6. return 0; 9. }

7. } 10. void third()

a) Segmentation fault/code crash 11. {


b) Executable file name
c) Depends on the platform 12. second();

d) Depends on the compiler 13. }

Answer: a 14. void main()


Explanation: None.
15. {

This section on C quiz focuses on “Pointer to 16. void (*ptr)();


Functions”. One shall practice these quizzes
to improve their C programming skills needed 17. ptr = third;
for various interviews (campus interviews,
walkin interviews, company interviews), 18. ptr();
placements, entrance exams and other 19. }
competitive exams. These questions can be
attempted by anyone focusing on learning C a) Function first
Programming language. They can be a b) Function second
beginner, fresher, engineering graduate or an c) Function third
experienced IT professional. Our C quiz d) None of the mentioned
comes with detailed explanation of the
answers which helps in better understanding Answer: d
of C concepts. Explanation: None.
Here is a listing of C quiz on “Pointer to 2. How to call a function without using the
Functions” along with answers, explanations function name to send parameters?
and/or solutions: a) typedefs
b) Function pointer
1. Which function is not called in the c) Both typedefs and Function pointer
following C program? d) None of the mentioned
1. #include <stdio.h> Answer: b
Explanation: None.
2. void first()

3. { 3. Which of the following is a correct syntax


to pass a Function Pointer as an argument?
4. printf("first"); a) void pass(int (*fptr)(int, float, char)){}
b) void pass(*fptr(int, float, char)){}
5. } c) void pass(int (*fptr)){}
d) void pass(*fptr){}
6. void second()

7. { Answer: a
Explanation: None.
4. Which of the following is not possible in 4. return a * b * c;
C?
a) Array of function pointer 5. }
b) Returning a function pointer
6. void main()
c) Comparison of function pointer
d) None of the mentioned 7. {

Answer: d 8. int (*function_pointer)(int


Explanation: None. , int, int);

5. What will be the output of the following C 9. function_pointer = mul;


code? 10. printf("The product of thre
e numbers is:%d",
1. #include <stdio.h>
11. function_pointer(2, 3, 4));
2. void first()
12. }
3. {
a) The product of three numbers is:24
4. printf("Hello World");
b) Run time error
5. } c) Nothing
d) Varies
6. void main()
Answer: a
7. { Explanation: None.
8. void *ptr() = first; 7. What will be the output of the following C
9. ptr++
code?

10. ptr(); 1. #include <stdio.h>

11. } 2. int mul(int a, int b, int c)

a) Illegal application of ++ to void data type 3. {


b) pointer function initialized like a variable 4. return a * b * c;
c) Illegal application of ++ to void data type
& pointer function initialized like a variable 5. }
d) None of the mentioned
6. void main()
Answer: c
Explanation: None. 7. {

8. int (function_pointer)(int,
6. What will be the output of the following C int, int);
code?
9. function_pointer = mul;
1. #include <stdio.h>
10. printf("The product of thre
2. int mul(int a, int b, int c) e numbers is:%d",

3. { 11. function_pointer(2, 3, 4));


12. } Answer: b
Explanation: None.
a) The product of three numbers is:24
b) Compile time error
Sanfoundry’s 1000+ Interview Questions &
c) Nothing
Answers on C helps anyone preparing for
d) Varies
Honeywell and other companies C interviews.
Answer: b One should practice these 1000+ interview
Explanation: None. questions and answers continuously for 2-3
months to clear Honeywell interviews on C
8. What will be the output of the following C Programming language.
code?
Here is a listing of C programming questions
1. #include <stdio.h> on “Pointers to Functions” along with
answers, explanations and/or solutions:
2. void f(int (*x)(int));
1. What will be the output of the following C
3. int myfoo(int); code?
4. int (*fooptr)(int); 1. #include <stdio.h>
5. int ((*foo(int)))(int); 2. int mul(int a, int b, int c)
6. int main() 3. {
7. { 4. return a * b * c;
8. fooptr = foo(0); 5. }
9. fooptr(10); 6. void main()
10. }
7. {
11. int ((*foo(int i)))(int)
8. int *function_pointer;
12. {
9. function_pointer = mul;
13. return myfoo; 10. printf("The product of thre
e numbers is:%d",
14. }
11. function_pointer(2, 3, 4));
15. int myfoo(int i)
12. }
16. {
a) The product of three numbers is:24
17. printf("%d\n", i + 1);
b) Compile time error
18. } c) Nothing
d) Varies
a) 10
b) 11 Answer: b
c) Compile time error Explanation: None.
d) Undefined behaviour
2. What will be the output of the following C 2. void f(int);
code?
3. void (*foo)() = f;
1. #include <stdio.h>
4. int main(int argc, char *argv[]
2. int sub(int a, int b, int c) )

3. { 5. {

4. return a - b - c; 6. foo(10);

5. } 7. return 0;

6. void main()
8. }

7. { 9. void f(int i)

8. int (*function_pointer)(int 10. {


, int, int);
11. printf("%d\n", i);
9. function_pointer = &sub;
12. }
10. printf("The difference of t
hree numbers is:%d", a) Compile time error
b) 10
11. (*function_pointer)(2, 3, 4 c) Undefined behaviour
)); d) None of the mentioned
12. }
Answer: b
a) The difference of three numbers is:1 Explanation: None.
b) Run time error
c) The difference of three numbers is:-5 5. What will be the output of the following C
d) Varies code?

Answer: c 1. #include <stdio.h>

Explanation: None. 2. void f(int);

3. One of the uses for function pointers in C 3. void (*foo)(void) = f;


is
a) Nothing 4. int main(int argc, char *argv[]
b) There are no function pointers in c )
c) To invoke a function 5. {
d) To call a function defined at run-time
6. foo(10);
Answer: d
Explanation: None. 7. return 0;

4. What will be the output of the following C 8. }


code? 9. void f(int i)

1. #include <stdio.h>
10. { 2. void f(int (*x)(int));

11. printf("%d\n", i); 3. int myfoo(int i);

12. } 4. int (*foo)(int) = myfoo;

a) Compile time error 5. int main()


b) 10
6. {
c) Undefined behaviour
d) None of the mentioned 7. f(foo(10));

Answer: a 8. }
Explanation: None.
9. void f(int (*i)(int))
6. What will be the output of the following C
code? 10. {

11. i(11);
1. #include <stdio.h>
12. }
2. void f(int);
13. int myfoo(int i)
3. void (*foo)(float) = f;
14. {
4. int main()
15. printf("%d\n", i);
5. {
16. return i;
6. foo(10);
17. }
7. }

8. void f(int i)
a) Compile time error
b) Undefined behaviour
9. { c) 10 11
d) 10 Segmentation fault
10. printf("%d\n", i);
Answer: d
11. } Explanation: None.
a) Compile time error 8. What will be the output of the following C
b) 10 code?
c) 10.000000
d) Undefined behaviour 1. #include <stdio.h>

Answer: d 2. void f(int (*x)(int));


Explanation: None.
3. int myfoo(int);
7. What will be the output of the following C
code? 4. int (*foo)() = myfoo;

5. int main()
1. #include <stdio.h>
6. { answers, explanations and/or solutions:
7. f(foo); 1. What will be the output of the following C
code?
8. }

9. void f(int(*i)(int ))
1. #include <stdio.h>

10. {
2. void main()

11. i(11);
3. {

12. }
4. struct student

13. int myfoo(int i)


5. {

14. {
6. int no;

7. char name[20];
15. printf("%d\n", i);
8. };
16. return i;

17. }
9. struct student s;

10. no = 8;
a) 10 11
b) 11 11. printf("%d", no);
c) 10
d) Undefined behaviour 12. }

Answer: b a) Nothing
Explanation: None. b) Compile time error
c) Junk
d) 8
This section on C question bank focuses on
“Complicated Declarations”. One shall Answer: b
practice these questions to improve their C Explanation: None.
programming skills needed for various
interviews (campus interviews, walkin 2. What will be the output of the following C
interviews, company interviews), placements, code?
entrance exams and other competitive exams.
These questions can be attempted by anyone 1. #include <stdio.h>
focusing on learning C Programming
language. They can be a beginner, fresher, 2. struct student
engineering graduate or an experienced IT
professional. Our C question bank comes 3. {

with detailed explanation of the answers 4. int no;


which helps in better understanding of C
concepts. 5. char name[20];

Here is a listing of C question bank on 6. };


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

7. void main()
Answer: c
Explanation: None. 8. {

3. What will be the output of the following C 9. student s;


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

8. { 5. What will be the output of the following C


code?
9. struct student s;
1. #include <stdio.h>
10. s.no = 8;
2. void main()
11. printf("hello");
3. {
12. }
4. struct student
a) Nothing
b) Compile time error 5. {
c) hello
d) Varies 6. int no;

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

6. printf("after x\n");
Answer: c
Explanation: None. 7. }

6. What will be the output of the following C 8. int *((*x)())[2]


code?
9. {
1. #include <stdio.h>
10. int **str;
2. struct student
11. str = (int*)malloc(sizeof(i
3. { nt)* 2);

4. int no; 12. return str;

5. char name[20]; 13. }

6. }; a) Compile time error


b) Undefined behaviour
7. struct student s; c) After x
d) None of the mentioned
8. void main()

9. {
Answer: a
Explanation: None.
10. s.no = 8;
8. What will be the output of the following C
11. printf("%s", s.name); code?
12. } 1. #include <stdio.h>

a) Nothing 2. void (*(f)())(int, float);


b) Compile time error
c) Junk 3. void (*(*x)())(int, float) = f;
d) 8
4. void ((*y)(int, float));
Answer: a 5. void foo(int i, float f);
Explanation: None.
6. int main()
7. { 2. void (*(f)())(int, float);

8. y = x(); 3. typedef void (*(*x)())(int, flo


at);
9. y(1, 2);
4. void foo(int i, float f);
10. }
5. int main()
11. void (*(f)())(int, float)
6. {
12. {
7. x = f;
13. return foo;
8. x();
14. }
9. }
15. void foo(int i, float f)
10. void (*(f)())(int, float)
16. {
11. {
17. printf("%d %f\n", i, f);
12. return foo;
18. }
13. }
a) 1 2.000000
b) 1 2 14. void foo(int i, float f)

c) Compile time error 15. {


d) Segmentation fault/code crash
16. printf("%d %f\n", i, f);
Answer: a
Explanation: None. 17. }

9. What does this declaration say? a) Compile time error


b) Undefined behaviour
int (*(*y)())[2]; c) 1 2.000000
d) Nothing
a) y is pointer to the function which returns
pointer to integer array Answer: a
b) y is pointer to the function which returns Explanation: None.
array of pointers
c) y is function which returns function pointer 11. What will be the output of the following
which in turn returns pointer to integer array C code?
d) y is function which returns array of
integers 1. #include <stdio.h>

Answer: a 2. void (*(f)())(int, float);


Explanation: None.
3. typedef void (*(*x)())(int, flo
at);
10. What will be the output of the following
C code? 4. void foo(int i, float f);

1. #include <stdio.h>
5. int main() a) ptr is pointer to int that converts its type to
void
6. { b) ptr is pointer to function passing int
7. x p = f;
returning void
c) ptr is pointer to void that converts its type
8. p(); to int
d) ptr is pointer to function passing void
9. } returning int
10. void (*(f)())(int, float) Answer: b
Explanation: None.
11. {

12. return foo; 2. Which of the following expression is true


for the following C statement?
13. }
ptr is array with 3 elements of pointer t
14. void foo(int i, float f) o function returning pointer of int

15. { a) int **ptr[3]();


b) int *(*ptr[3])();
16. printf("%d %f\n", i, f); c) int (*(*ptr[3])());
d) None of the mentioned
17. }

a) Compile time error Answer: b


Explanation: None.
b) Undefined behaviour
c) 1 2.000000
3. What makes the following declaration
d) Nothing
denote?
Answer: d int **ptr;
Explanation: None.
a) ptr is a function pointer that returns pointer
Sanfoundry’s 1000+ Interview Questions & to int type
Answers on C helps anyone preparing for b) ptr is a pointer to an int pointer
EMC and other companies C interviews. One c) ptr is a pointer to pointer to type int
should practice these 1000+ interview d) none of the mentioned
questions and answers continuously for 2-3
months to clear EMC interviews on C Answer: b
Programming language. Explanation: None.

Here is a listing of C multiple choice 4. What makes the following declaration


questions on “Complicated Declarations” denote?
along with answers, explanations and/or
char *str[5];
solutions:
a) str is an array of 5 element pointer to type
1. Read the following expression?
char
void (*ptr)(int); b) str is a pointer to an array of 5 elements
c) str is a function pointer of 5 elements
returning char Answer: a
d) none of the mentioned Explanation: None.

Answer: a 7. What will be the output of the following C


Explanation: None. code?

5. Comment on the following declaration. 1. #include <stdio.h>

int (*ptr)(); // i) 2. struct student


char *ptr[]; // ii)
3. {
a) Both i) and ii) and cannot exist due to same
name 4. int no = 5;
b) i) is legal, ii) is illegal 5. char name[20];
c) i) is illegal, ii) is legal
d) Both i) and ii) will work legal and 6. };
flawlessly
7. void main()
Answer: d
Explanation: None. 8. {

9. struct student s;
6. What will be the output of the following C
code? 10. s.no = 8;

1. #include <stdio.h> 11. printf("hello");

2. struct student 12. }

3. { a) Nothing
b) Compile time error
4. int no;
c) hello
5. char name[20]; d) Varies

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

11. printf("hello");
a) True
b) False
12. } c) Undefined behaviour
d) Depends on the standard
a) Nothing
b) hello Answer: b
c) Compile time error Explanation: None.
d) Varies

Answer: c
Explanation: None.

9. What will be the output of the following C


UNIT IV STRUCTURES
code?
TOPIC 4.1 STRUCTURES,
1. #include <stdio.h>
UNIONS AND BIT-FIELDS IN C
2. void main()
This section on online C quiz focuses on
3. { “Basics of Structures”. One shall practice
4. struct student
these online quizzes to improve their C
programming skills needed for various
5. { interviews (campus interviews, walkin
interviews, company interviews), placements,
6. int no; entrance exams and other competitive exams.
These questions can be attempted by anyone
7. char name[20];
focusing on learning C Programming
8. }; language. They can be a beginner, fresher,
engineering graduate or an experienced IT
9. struct student s; professional. Our C quiz comes with detailed
explanation of the answers which helps in
10. s.no = 8; better understanding of C concepts.
11. printf("%d", s.no);
Here is a listing of online C quiz on “Basics
12. } of Structures” along with answers,
explanations and/or solutions:
a) Nothing
b) Compile time error 1. Which of the following are themselves a
c) Junk collection of different data types?
d) 8 a) string
b) structures
c) char struct temp s;
d) all of the mentioned struct temp{};
main(){}
Answer: b
d) None of the mentioned
Explanation: None.
Answer: d
2. User-defined data type can be derived
Explanation: None.
by
a) struct 6. What will be the output of the following C
b) enum code?
c) typedef
d) all of the mentioned 1. #include <stdio.h>

Answer: d 2. struct student


Explanation: None.
3. {
3. Which operator connects the structure
name to its member name? 4. int no;
a) – 5. char name[20];
b) <-
c) . 6. }
d) Both <- and .
7. void main()
Answer: c
Explanation: None. 8. {

9. struct student s;
4. Which of the following cannot be a
structure member? 10. s.no = 8;
a) Another structure
b) Function 11. printf("hello");
c) Array
d) None of the mentioned 12. }

Answer: b a) Compile time error


Explanation: None. b) Nothing
c) hello
5. Which of the following structure d) Varies
declaration will throw an error?
a) Answer: a
Explanation: None.
struct temp{}s;
main(){} 7. What will be the output of the following C
code?
b)
1. #include <stdio.h>
struct temp{};
struct temp s; 2. struct student
main(){}
3. {
c)
4. int no = 5; a) Nothing
b) hello
5. char name[20]; c) Compile time error
6. };
d) Varies

7. void main() Answer: c


Explanation: None.
8. {
9. What will be the output of the following C
9. struct student s; code?
10. s.no = 8;
1. #include <stdio.h>
11. printf("hello");
2. void main()
12. } 3. {

a) Nothing 4. struct student


b) Compile time error
c) hello 5. {
d) Varies
6. int no;
Answer: b
7. char name[20];
Explanation: None.
8. };
8. What will be the output of the following C
code? 9. struct student s;

1. #include <stdio.h> 10. s.no = 8;

2. struct student 11. printf("%d", s.no);

3. { 12. }

4. int no; a) Nothing


b) Compile time error
5. char name[20]; c) Junk
6. };
d) 8

7. void main() Answer: d


Explanation: None.
8. {
10. Can the following C code be compiled
9. student s; successfully?
10. s.no = 8;
1. #include <stdio.h>
11. printf("hello"); 2. struct p
12. } 3. {
4. int k; 6. int no;

5. char c; 7. char name[20];

6. float f; 8. };

7. }; 9. struct student s;

8. int main() 10. no = 8;

9. { 11. printf("%d", no);

10. struct p x = {.c = 97, .f = 12. }


3, .k = 1};
a) Nothing
11. printf("%f\n", x.f);
b) Compile time error
12. } c) Junk
d) 8
a) Yes
b) No Answer: b
c) Depends on the standard Explanation: None.
d) Depends on the platform
2. How many bytes in memory taken by the
Answer: c following C structure?
Explanation: None.
1. #include <stdio.h>

Sanfoundry’s 1000+ Interview Questions & 2. struct test


Answers on C helps anyone preparing for
Juniper Networks and other companies C 3. {
interviews. One should practice these 1000+ 4. int k;
interview questions and answers continuously
for 2-3 months to clear Juniper interviews on 5. char c;
C Programming language.
6. };
Here is a listing of C multiple choice
questions on “Basics of Structures” along a) Multiple of integer size
with answers, explanations and/or solutions: b) integer size+character size
c) Depends on the platform
1. What will be the output of the following C d) Multiple of word size
code?
Answer: a
1. #include <stdio.h> Explanation: None.

2. void main() 3. What will be the output of the following C


code?
3. {
1. #include <stdio.h>
4. struct student
2. struct
5. {
3. { 12. }

4. int k; a) Compile time error


b) 10 10
5. char c;
c) Depends on the standard
6. }; d) Depends on the compiler

7. int main() Answer: a


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

8. int main()
4. What will be the output of the following C
code? 9. {

1. #include <stdio.h> 10. struct p x;

2. struct 11. x.k = 10;

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

4. int k; 13. }

5. char c; a) Compile time error


b) 10 10
6. } p;
c) Depends on the standard
7. int p = 10; d) Depends on the compiler

8. int main() Answer: b


Explanation: None.
9. {
6. What will be the output of the following C
10. p.k = 10;
code?
11. printf("%d %d\n", p.k, p);
1. #include <stdio.h>
2. struct p
a) 3.000000
3. { b) Compile time error
c) Undefined behaviour
4. int k; d) 1.000000
5. char c; Answer: a
6. float f;
Explanation: None.

7. }; 8. What will be the output of the following C


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

12. }
1. #include <stdio.h>

2. struct p a) 3.000000
b) 0.000000
3. { c) Compile time error
d) Undefined behaviour
4. int k;
Answer: b
5. char c;
Explanation: None.
6. float f;

7. };

8. int main()

9. {
10. p x = {.c = 97, .f = 3,
.k = 1};

11. printf("%f\n", x.f);

12. }
9. What will be the output of the following C Here is a listing of C programming questions
code according to C99 standard? on “Structures and Functions” along with
answers, explanations and/or solutions:
1. #include <stdio.h>
1. What will be the output of the following C
2. struct p code?
3. {
1. #include <stdio.h>
4. int k;
2. struct student
5. char c;
3. {
6. float f;
4. char *name;
7. };
5. };
8. int main()
6. struct student s;
9. {
7. struct student fun(void)
10. struct p x = {.c = 97};
8. {
11. printf("%f\n", x.f);
9. s.name = "newton";
12. }
10. printf("%s\n", s.name);
a) 0.000000 11. s.name = "alan";
b) Somegarbagevalue
c) Compile time error 12. return s;
d) None of the mentioned
13. }
Answer: a
14. void main()
Explanation: None.
15. {
This section on C programming questions and
answers focuses on “Structures and 16. struct student m = fun();
Functions”. One shall practice these questions 17. printf("%s\n", m.name);
to improve their C programming skills needed
for various interviews (campus interviews, 18. m.name = "turing";
walkin interviews, company interviews),
placements, entrance exams and other 19. printf("%s\n", s.name);
competitive exams. These questions can be
20. }
attempted by anyone focusing on learning C
Programming language. They can be a a) newton alan alan
beginner, fresher, engineering graduate or an b) alan newton alan
experienced IT professional. Our C c) alan alan newton
programming questions come with detailed d) compile time error
explanation of the answers which helps in
better understanding of C concepts.
Answer: a 1. #include <stdio.h>
Explanation: None.
2. struct temp
2. What will be the output of the following C
3. {
code?
4. int a;
1. #include <stdio.h>
5. } s;
2. struct student
6. void func(struct temp s)
3. {
7. {
4. char *name;
8. s.a = 10;
5. };
9. printf("%d\t", s.a);
6. void main()
10. }
7. {
11. main()
8. struct student s, m;
12. {
9. s.name = "st";
13. func(s);
10. m = s;
14. printf("%d\t", s.a);
11. printf("%s%s", s.name, m.na
me); 15. }

12. }
a) 10 (Garbage Value)
a) Compile time error b) 0 10
b) Nothing c) 10 0
c) Junk values d) (Garbage Value) 10
d) st st
Answer: c
Answer: d Explanation: None.
Explanation: None.
5. Which of the following is not possible
3. Which of the following return-type cannot under any scenario?
be used for a function in C? a) s1 = &s2;
a) char * b) s1 = s2;
b) struct c) (*s1).number = 10;
c) void d) None of the mentioned
d) none of the mentioned
Answer: d
Answer: d Explanation: None.
Explanation: None.
6. Which of the following operation is illegal
4. What will be the output of the following C in structures?
code? a) Typecasting of structure
b) Pointer to a variable of the same structure
c) Dynamic allocation of memory for 17. }
structure
d) All of the mentioned a) alan
b) Turing
Answer: a c) Compile time error
Explanation: None. d) Nothing

7. Presence of code like “s.t.b = 10” indicates Answer: c


Explanation: None.
a) Syntax Error
b) Structure
Sanfoundry’s 1000+ Interview Questions &
c) double data type
Answers on C helps anyone preparing for
d) An ordinary variable name
Huawei and other companies C interviews.
Answer: b One should practice these 1000+ interview
Explanation: None. questions and answers continuously for 2-3
months to clear Huawei interviews on C
8. What will be the output of the following C Programming language.
code?
Here is a listing of C interview questions on
1. #include <stdio.h> “Structures and Functions” along with
answers, explanations and/or solutions:
2. struct student
1. What will be the output of the following C
3. { code?
4. char *name; 1. #include <stdio.h>

5. }; 2. struct point

6. struct student fun(void) 3. {

7. { 4. int x;

8. struct student s; 5. int y;

9. s.name = "alan"; 6. };

10. return s; 7. int main()

11. } 8. {

12. void main() 9. struct point p = {1};

13. { 10. struct point p1 = {1};

14. struct student m = fun(); 11. if(p == p1)

15. s.name = "turing"; 12. printf("equal\n");

16. printf("%s", m.name); 13. else


14. printf("not equal\n"); 20. struct point foo()

15. } 21. {

a) Compile time error 22. struct point temp = {1, 2};


b) equal
c) depends on the standard 23. return temp;

d) not equal 24. }

Answer: a a) Compile time error


Explanation: None. b) 1
c) 2
2. What will be the output of the following C d) Undefined behaviour
code?
Answer: a
1. #include <stdio.h>
Explanation: None.
2. struct point
3. What will be the output of the following C
3. { code?

4. int x; 1. #include <stdio.h>

5. int y; 2. struct point

6. }; 3. {

7. struct notpoint 4. int x;

8. { 5. int y;

9. int x; 6. };

10. int y; 7. struct notpoint

11. }; 8. {

12. struct point foo(); 9. int x;

13. int main() 10. int y;

14. { 11. };

15. struct point p = {1}; 12. int main()

16. struct notpoint p1 = {2, 3} 13. {


;
14. struct point p = {1};
17. p1 = foo();
15. struct notpoint p1 = p;
18. printf("%d\n", p1.x);
16. printf("%d\n", p1.x);
19. }
17. } 21. }

a) Compile time error a) Compile time error


b) 1 b) 1
c) 0 c) 0
d) Undefined d) Undefined

Answer: a Answer: a
Explanation: None. Explanation: None.

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

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

2. struct point 2. struct point

3. { 3. {

4. int x; 4. int x;

5. int y; 5. int y;

6. }; 6. };

7. struct notpoint 7. void foo(struct point*);

8. { 8. int main()

9. int x; 9. {

10. int y; 10. struct point p1 = {1, 2};

11. }; 11. foo(&p1);

12. void foo(struct point); 12. }

13. int main() 13. void foo(struct point *p)

14. { 14. {

15. struct notpoint p1 = {1, 2} 15. printf("%d\n", *p.x++);


;
16. }
16. foo(p1);
a) Compile time error
17. }
b) Segmentation fault/code crash
18. void foo(struct point p) c) 2
d) 1
19. {
Answer: a
20. printf("%d\n", p.x); Explanation: None.
6. What will be the output of the following C 5. {
code?
6. char *name;
1. #include <stdio.h>
7. };
2. struct point
8. struct student s;
3. {
9. s.name = "alan";
4. int x;
10. return s;
5. int y;
11. }
6. };
12. void main()
7. void foo(struct point*);
13. {
8. int main()
14. struct student m = fun();
9. {
15. printf("%s", m.name);
10. struct point p1 = {1, 2};
16. }
11. foo(&p1);
a) Compile time error
12. } b) alan
c) Nothing
13. void foo(struct point *p) d) Varies
14. {
Answer: a
15. printf("%d\n", *p->x++); Explanation: None.

16. } 8. What will be the output of the following C


code?
a) Compile time error
b) 1 1. #include <stdio.h>
c) Segmentation fault/code crash
d) 2 2. struct student

3. {
Answer: a
Explanation: None. 4. char *name;

7. What will be the output of the following C 5. };


code?
6. struct student fun(void)
1. #include <stdio.h>
7. {
2. struct student fun(void)
8. struct student s;
3. {
9. s.name = "alan";
4. struct student
10. return s; a) s.b.[i];
b) s.[i].b;
11. } c) s.b[i];
12. void main()
d) s[i].b;

13. { Answer: d
Explanation: None.
14. struct student m = fun();
2. Comment on the output of the following C
15. printf("%s", m.name); code.
16. }
1. #include <stdio.h>

a) Nothing 2. struct temp


b) alan
c) Run time error 3. {
d) Varies
4. int a;
Answer: b
5. int b;
Explanation: None.
6. int c;
This section on C interview questions and
7. };
answers focuses on “Arrays of Structures”.
One shall practice these interview questions 8. main()
to improve their C programming skills needed
for various interviews (campus interviews, 9. {
walkin interviews, company interviews),
placements, entrance exams and other 10. struct temp p[] = {{1, 2, 3
}, {4, 5, 6}, {7, 8, 9}};
competitive exams. These questions can be
attempted by anyone focusing on learning C 11. }
Programming language. They can be a
beginner, fresher, engineering graduate or an a) No Compile time error, generates an array
experienced IT professional. Our C Interview of structure of size 3
questions come with detailed explanation of b) No Compile time error, generates an array
the answers which helps in better of structure of size 9
understanding of C concepts. c) Compile time error, illegal declaration of a
multidimensional array
Here is a listing of C interview questions on d) Compile time error, illegal assignment to
“Arrays of Structures” along with answers, members of structure
explanations and/or solutions:
Answer: a
1. The correct syntax to access the member of Explanation: None.
the ith structure in the array of structures is?
3. Which of the following uses structure?
Assuming: struct temp a) Array of structures
{
int b; b) Linked Lists
}s[50]; c) Binary Tree
d) All of the mentioned
Answer: d 1. #include <stdio.h>
Explanation: None.
2. struct student
4. What is the correct syntax to declare a
3. {
function foo() which receives an array of
structure in function? 4. char *name;
a) void foo(struct *var);
b) void foo(struct *var[]); 5. };
c) void foo(struct var);
d) none of the mentioned 6. struct student s[2];

7. void main()
Answer: a
Explanation: None. 8. {

5. What will be the output of the following C 9. s[0].name = "alan";


code? (Assuming size of int be 4)
10. s[1] = s[0];
1. #include <stdio.h>
11. printf("%s%s", s[0].name, s
2. struct temp [1].name);

3. { 12. s[1].name = "turing";

4. int a; 13. printf("%s%s", s[0].name, s


[1].name);
5. int b;
14. }
6. int c;
a) alan alan alan turing
7. } p[] = {0}; b) alan alan turing turing
c) alan turing alan turing
8. main() d) run time error
9. {
Answer: a
10. printf("%d", sizeof(p)); Explanation: None.

11. } 7. What will be the output of the following C


code?
a) 4
b) 12 1. #include <stdio.h>
c) 16
d) Can’t be estimated due to ambiguous 2. struct student
initialization of array 3. {

Answer: b 4. char *name;


Explanation: None.
5. };
6. What will be the output of the following C
code? 6. struct student s[2], r[2];
7. void main() Answer: c
Explanation: None.
8. {
9. What will be the output of the following C
9. s[0].name = "alan";
code?
10. s[1] = s[0];
1. #include <stdio.h>
11. r = s;
2. struct student
12. printf("%s%s", r[0].name, r
[1].name); 3. {

13. } 4. };

a) alan alan 5. void main()


b) Compile time error 6. {
c) Varies
d) Nothing 7. struct student s[2];

Answer: b 8. printf("%d", sizeof(s));


Explanation: None.
9. }
8. What will be the output of the following C
code? a) 2
b) 4
1. #include <stdio.h> c) 8
d) 0
2. struct student
Answer: d
3. { Explanation: None.
4. char *name;
Sanfoundry’s 1000+ Interview Questions &
5. }; Answers on C helps anyone preparing for
Philips and other companies C interviews.
6. void main()
One should practice these 1000+ interview
7. {
questions and answers continuously for 2-3
months to clear Philips interviews on C
8. struct student s[2], r[2]; Programming language.

9. s[1] = s[0] = "alan"; Here is a listing of C interview questions on


“Arrays of Structures” along with answers,
10. printf("%s%s", s[0].name, s
explanations and/or solutions:
[1].name);

11. } 1. What will be the output of the following C


code?
a) alan alan
b) Nothing 1. #include <stdio.h>
c) Compile time error 2. struct point
d) Varies
3. { 8. int main()

4. int x; 9. {

5. int y; 10. struct point p1[] = {1, 2,


3, 4};
6. };
11. foo(p1);
7. void foo(struct point*);
12. }
8. int main()
13. void foo(struct point p[])
9. {
14. {
10. struct point p1[] = {1, 2
, 3, 4}; 15. printf("%d\n", p->x);

11. foo(p1); 16. }

12. } a) 1
b) 2
13. void foo(struct point p[])
c) 3
14. { d) Compile time error

15. printf("%d\n", p[1].x); Answer: a


Explanation: None.
16. }
3. What will be the output of the following C
a) Compile time error code?
b) 3
c) 2 1. #include <stdio.h>
d) 1
2. struct point
Answer: b
Explanation: None. 3. {

4. int x;
2. What will be the output of the following C
code? 5. int y;

1. #include <stdio.h> 6. };

2. struct point 7. void foo(struct point*);

3. { 8. int main()

4. int x; 9. {

5. int y; 10. struct point p1[] = {1, 2,


3, 4};
6. };
11. foo(p1);
7. void foo(struct point*);
12. } a) 1 0
b) Compile time error
13. void foo(struct point p[]) c) 1 somegarbagevalue
d) Undefined behaviour
14. {

15. printf("%d %d\n", p->x, ++p Answer: a


->x); Explanation: None.

16. } 5. What will be the output of the following C


code?
a) 1 2
b) 2 2 1. #include <stdio.h>
c) Compile time error
d) Undefined behaviour 2. struct point

Answer: b 3. {

Explanation: None. 4. int x;

4. What will be the output of the following C 5. int y;


code?
6. };
1. #include <stdio.h>
7. void foo(struct point*);
2. struct point
8. int main()
3. {
9. {
4. int x;
10. struct point p1[] = {1, 2,
5. int y; 3, 4, 5};

6. } p[] = {1, 2, 3, 4, 5}; 11. foo(p1);

7. void foo(struct point*); 12. }

8. int main() 13. void foo(struct point p[])

9. { 14. {

10. foo(p); 15. printf("%d %d\n", p->x, p[3


].y);
11. }
16. }
12. void foo(struct point p[])
a) Compile time error
13. { b) 1 0
c) 1 somegarbagevalue
14. printf("%d %d\n", p->x, p[2 d) None of the mentioned
].y);

15. } Answer: c
Explanation: None.
6. What will be the output of the following C 4. int x;
code?
5. int y;
1. #include <stdio.h>
6. };
2. struct point
7. void foo(struct point*);
3. {
8. int main()
4. int x;
9. {
5. int y;
10. struct point p1[] = {1, 2,
6. }; 3, 4, 5};

7. void foo(struct point*);


11. foo(p1);

8. int main()
12. }

9. { 13. void foo(struct point p[])

10. struct point p1[] = {1, 2, 14. {


3, 4, 5};
15. printf("%d %d\n", p->x, (p
11. foo(p1); + 2)->y);

12. } 16. }

13. void foo(struct point p[]) a) Compile time error


b) 1 0
14. { c) 1 somegarbagevalue
d) undefined behaviour
15. printf("%d %d\n", p->x, (p
+ 2).y);
Answer: b
16. } Explanation: None.

a) Compile time error 8. What will be the output of the following C


b) 1 0 code?
c) 1 somegarbagevalue
d) Undefined behaviour 1. #include <stdio.h>

2. struct student
Answer: a
Explanation: None. 3. {

7. What will be the output of the following C 4. char *c;


code?
5. };
1. #include <stdio.h>
6. void main()
2. struct point
7. {
3. {
8. struct student s[2]; 8. {

9. printf("%d", sizeof(s)); 9. struct p p1[] = {1, 92, 3,


94, 5, 96};
10. }
10. struct p *ptr1 = p1;
a) 2
b) 4 11. int x = (sizeof(p1) / 3);
c) 16 12. if (x == sizeof(int) + size
d) 8 of(char))

Answer: d 13. printf("%d\n", ptr1->x)


Explanation: None. ;

14. else
This section on C quiz focuses on “Pointer to
Structures”. One shall practice these quizzes 15. printf("falsen");
to improve their C programming skills needed
for various interviews (campus interviews, 16. }
walkin interviews, company interviews),
a) Compile time error
placements, entrance exams and other
b) 1
competitive exams. These questions can be
c) Undefined behaviour
attempted by anyone focusing on learning C
d) false
Programming language. They can be a
beginner, fresher, engineering graduate or an Answer: d
experienced IT professional. Our C quiz Explanation: None.
comes with detailed explanation of the
answers which helps in better understanding 2. What will be the output of the following C
of C concepts. code?
Here is a listing of C quiz on “Pointer to 1. #include <stdio.h>
Structures” along with answers, explanations
and/or solutions: 2. struct p

1. What will be the output of the following C 3. {


code?
4. int x;
1. #include <stdio.h>
5. char y;
2. struct p
6. };
3. {
7. int main()
4. int x;
8. {
5. char y;
9. struct p p1[] = {1, 92, 3,
94, 5, 96};
6. };
10. struct p *ptr1 = p1;
7. int main()
11. int x = (sizeof(p1) / sizeo a) Compile time error
f(ptr1)); b) 1
c) Undefined behaviour
12. if (x == 1)
d) Segmentation fault
13. printf("%d\n", ptr1->x)
; Answer: a
Explanation: None.
14. else
4. What will be the output of the following C
15. printf("false\n"); code?
16. }
1. #include <stdio.h>

a) Compile time error 2. struct p


b) 1
c) false 3. {
d) Undefined behaviour
4. int x;
Answer: c
5. char y;
Explanation: None.
6. };
3. What will be the output of the following C
code? 7. void foo(struct p* );

1. #include <stdio.h> 8. int main()

2. struct p 9. {

3. { 10. typedef struct p* q;

4. int x; 11. struct p p1[] = {1, 92, 3,


94, 5, 96};
5. char y;
12. foo(p1);
6. };
13. }
7. typedef struct p* q*;
14. void foo(struct p* p1)
8. int main()
15. {
9. {
16. q ptr1 = p1;
10. struct p p1[] = {1, 92, 3,
94, 5, 96}; 17. printf("%d\n", ptr1->x);

11. q ptr1 = p1; 18. }

12. printf("%d\n", ptr1->x); a) Compile time error


b) 1
13. }
c) Segmentation fault
d) Undefined behaviour
Answer: a d) none of the mentioned
Explanation: None.
Answer: d
5. Which of the following is an incorrect Explanation: None.
syntax for pointer to structure?
8. Which option is not possible for the
(Assuming struct temp{int b;}*my_struct;) following function call?
a) *my_struct.b = 10; 1. func(&s.a); //where s is a variable
b) (*my_struct).b = 10; of type struct and a is the member
c) my_struct->b = 10; of the struct.
d) Both *my_struct.b = 10; and
(*my_struct).b = 10; a) Compiler can access entire structure from
the function
Answer: a b) Individual member’s address can be
Explanation: None. displayed in structure
c) Individual member can be passed by
6. Which of the following is an incorrect reference in a function
syntax to pass by reference a member of a d) None of the mentioned
structure in a function?
Answer: a
(Assume: struct temp{int a;}s;) Explanation: None.

a) func(&s.a); 9. What will be the output of the following C


b) func(&(s).a); code?
c) func(&(s.a));
d) none of the mentioned 1. #include <stdio.h>

Answer: d 2. struct temp


Explanation: None.
3. {
7. Which of the following structure 4. int a;
declaration doesn’t require pass-by-reference?
a) 5. } s;

struct{int a;}s; 6. void change(struct temp);


main(){}
7. main()
b)
8. {
struct temp{int a;};
main(){ 9. s.a = 10;
struct temp s;
} 10. change(s);

c) 11. printf("%d\n", s.a);

struct temp{int a;}; 12. }


main(){}
struct temp s; 13. void change(struct temp s)
14. { c) Undefined behaviour
d) false
15. s.a = 1;
Answer: d
16. }
Explanation: None.
a) Output will be 1
b) Output will be 10 Sanfoundry’s 1000+ MCQs on C helps
c) Output varies with machine anyone preparing for placement in Citrix and
d) Compile time error other companies. Anyone looking for Citrix
placement papers should practice these 1000+
Answer: b questions continuously for 2-3 months,
Explanation: None. thereby ensuring a top position in placements.

10. What will be the output of the following Here is a listing of C programming questions
C code? on “Pointer to Structures” along with
answers, explanations and/or solutions:
1. #include <stdio.h>
1. What will be the output of the following C
2. struct p code?
3. {
1. #include <stdio.h>
4. int x;
2. struct student
5. int y;
3. {
6. };
4. char *c;
7. int main()
5. };
8. {
6. void main()
9. struct p p1[] = {1, 92, 3,
94, 5, 96};
7. {

10. struct p *ptr1 = p1;


8. struct student m;

11. int x = (sizeof(p1) / 5);


9. struct student *s = &m;

12. if (x == 3)
10. s->c = "hello";

13. printf("%d %d\n", ptr1-


11. printf("%s", s->c);
>x, (ptr1 + x - 1)->x);
12. }
14. else
a) hello
15. printf("false\n"); b) Run time error
c) Nothing
16. } d) Depends on compiler
a) Compile time error Answer: a
b) 1 5 Explanation: None.
2. What will be the output of the following C 10. s->c = "hello";
code?
11. printf("%s", m.c);
1. #include <stdio.h>
12. }
2. struct student
a) Run time error
3. { b) Nothing
c) hello
4. char *c; d) Varies
5. };
Answer: c
6. void main() Explanation: None.

7. { 4. What will be the output of the following C


code?
8. struct student *s;
1. #include <stdio.h>
9. s->c = "hello";
2. struct student
10. printf("%s", s->c);
3. {
11. }
4. char *c;
a) hello
b) Segmentation fault 5. };
c) Run time error
d) Nothing 6. void main()

7. {
Answer: b
Explanation: None. 8. struct student m;

3. What will be the output of the following C 9. struct student *s = &m;


code?
10. (*s).c = "hello";
1. #include <stdio.h>
11. printf("%s", m.c);
2. struct student
12. }
3. {
a) Run time error
4. char *c; b) Nothing
c) Varies
5. }; d) hello
6. void main()
Answer: d
7. { Explanation: None.

8. struct student m; 5. What will be the output of the following C


code?
9. struct student *s = &m;
1. #include <stdio.h> 10. int main()

2. struct student 11. {

3. { 12. struct p p1 = {1, 2};

4. char *c; 13. struct q *ptr1;

5. }; 14. ptr1->x = (struct q*)&p1.x;

6. void main() 15. printf("%d\n", ptr1->x[1]);

7. { 16. }

8. struct student n; a) Compile time error


b) Segmentation fault/code crash
9. struct student *s = &n;
c) 2
10. (*s).c = "hello"; d) 1

11. printf("%p\n%p\n", s, &n); Answer: b


Explanation: None.
12. }
7. What will be the output of the following C
a) Different address code?
b) Run time error
c) Nothing 1. #include <stdio.h>
d) Same address
2. struct p
Answer: d
Explanation: None. 3. {

4. int x[2];
6. What will be the output of the following C
code? 5. };

1. #include <stdio.h> 6. struct q

2. struct p 7. {

3. { 8. int *x;

4. int x[2]; 9. };

5. }; 10. int main()

6. struct q 11. {

7. { 12. struct p p1 = {1, 2};

8. int *x; 13. struct q *ptr1 = (struct q*


)&p1;
9. };
14. ptr1->x = (struct q*)&p1.x;
15. printf("%d\n", ptr1->x[0]); 1. #include <stdio.h>

16. } 2. struct p

a) Compile time error 3. {


b) Undefined behaviour
4. int x;
c) Segmentation fault/code crash
d) 1 5. char y;

Answer: b 6. };
Explanation: None.
7. int main()
8. What will be the output of the following C
code? 8. {

9. struct p p1[] = {1, 92, 3,


1. #include <stdio.h> 94, 5, 96};
2. struct p 10. struct p *ptr1 = p1;

3. { 11. int x = (sizeof(p1) / sizeo


f(struct p));
4. int x;
12. printf("%d %d\n", ptr1->x,
5. int y; (ptr1 + x - 1)->x);

6. }; 13. }

7. int main()
a) Compile time error
8. {
b) Undefined behaviour
c) 1 3
9. struct p p1[] = {1, 2, 3, 4 d) 1 5
, 5, 6};
Answer: d
10. struct p *ptr1 = p1; Explanation: None.
11. printf("%d %d\n", ptr1->x,
(ptr1 + 2)->x); Sanfoundry’s 1000+ Interview Questions &
Answers on C helps anyone preparing for
12. }
AMD and other companies C interviews. One
should practice these 1000+ interview
a) 1 5
questions and answers continuously for 2-3
b) 1 3
months to clear AMD interviews on C
c) Compile time error
Programming language.
d) 1 4
Here is a listing of C question bank on “Self
Answer: a
Referential Structures” along with answers,
Explanation: None.
explanations and/or solutions:
9. What will be the output of the following C
1. What will be the output of the following C
code?
code?
1. #include <stdio.h> 7. void main()

2. struct student 8. {

3. { 9. struct student s;

4. char *c; 10. struct student m;

5. struct student *point; 11. m.point = s;

6. }; 12. (m.point)->c = "hey";

7. void main() 13. printf("%s", s.c);

8. { 14. }

9. struct student s; a) Nothing


b) Compile time error
10. struct student m;
c) hey
11. s.c = m.c = "hi"; d) Varies

12. m.point = &s; Answer: b


Explanation: None.
13. (m.point)->c = "hey";
3. What will be the output of the following C
14. printf("%s\t%s\t", s.c, m.c
code?
);

15. } 1. #include <stdio.h>

a) hey hi 2. struct student

b) hi hey 3. {
c) Run time error
d) hey hey 4. char *c;

Answer: a 5. struct student point;


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

9. struct student s;
4. What will be the output of the following C
code? 10. struct student *m = &s;

1. #include <stdio.h> 11. printf("%d", sizeof(student


));
2. struct student
12. }
3. {
a) Compile time error
4. char *c; b) 8
c) 5
5. struct student *point;
d) 16
6. };
Answer: a
7. void main() Explanation: None.
8. { 6. What will be the output of the following C
code?
9. struct student s;

10. printf("%d", sizeof(s));


1. #include <stdio.h>

11. }
2. struct p

3. {
a) 5
b) 9 4. int x;
c) 8
d) 16 5. char y;

Answer: c 6. struct p *ptr;


Explanation: None. 7. };

5. What will be the output of the following C 8. int main()


code?
9. {
1. #include <stdio.h>
10. struct p p = {1, 2, &p};
2. struct student
11. printf("%d\n", p.ptr->x);
3. {
12. return 0;
4. char *c;
13. }
5. struct student *point;
a) Compile time error of every other node
b) Undefined behaviour c) Comparing the the value stored in a node
c) 1 by a value in every other node
d) 2 d) None of the mentioned

Answer: c Answer: b
Explanation: None. Explanation: None.

7. What will be the output of the following C This section on C interview questions and
code? answers focuses on “Self-Referential
1. #include <stdio.h>
Structures”. One shall practice these
interview questions to improve their C
2. typedef struct p *q; programming skills needed for various
interviews (campus interviews, walkin
3. struct p interviews, company interviews), placements,
entrance exams and other competitive exams.
4. {
These questions can be attempted by anyone
5. int x; focusing on learning C Programming
language. They can be a beginner, fresher,
6. char y; engineering graduate or an experienced IT
professional. Our C Interview questions come
7. q ptr; with detailed explanation of the answers
8. };
which helps in better understanding of C
concepts.
9. int main()
Here is a listing of C interview questions on
10. { “Self-Referential Structures” along with
answers, explanations and/or solutions:
11. struct p p = {1, 2, &p};
1. What will be the output of the following C
12. printf("%d\n", p.ptr->x);
code?
13. return 0;
1. #include <stdio.h>
14. }
2. typedef struct p *q;
a) Compile time error
3. int main()
b) 1
c) Undefined behaviour 4. {
d) Address of p
5. struct p
Answer: b
Explanation: None. 6. {

8. Presence of loop in a linked list can be 7. int x;

tested by 8. char y;
a) Traveling the list, if NULL is encountered
no loop exists 9. q ptr;
b) Comparing the address of nodes by address
10. }; Answer: b
Explanation: None.
11. struct p p = {1, 2, &p};
3. What will be the output of the following C
12. printf("%d\n", p.ptr->x);
code?
13. return 0;
1. #include <stdio.h>
14. }
2. typedef struct p *q;
a) Compile time error
3. struct p
b) 1
c) Depends on the compiler 4. {
d) None of the mentioned
5. int x;
Answer: a
Explanation: None. 6. char y;

7. q ptr;
2. What will be the output of the following C
code? 8. };

1. #include <stdio.h> 9. int main()

2. int main() 10. {

3. { 11. struct p p = {1, 2, &p};

4. typedef struct p *q; 12. printf("%d\n", p.ptr->ptr->


x);
5. struct p
13. return 0;
6. {
14. }
7. int x;
a) Compile time error
8. char y;
b) Segmenation fault
9. q ptr; c) Undefined behaviour
d) 1
10. };
Answer: d
11. struct p p = {1, 2, &p}; Explanation: None.
12. printf("%d\n", p.ptr->x);
4. The number of distinct nodes the following
13. return 0; struct declaration can point to is

14. }
1. struct node
a) Compile time error
b) 1 2. {

c) Depends on the compiler 3. struct node *left;


d) Depends on the standard
4. struct node *centre; while (p != NULL)
{
5. struct node *right; p = p->next;
}
6. };
b)
a) 1
b) 2 while (p->next != NULL)
{
c) 3 p = p->next;
d) All of the mentioned }

Answer: d c)
Explanation: None.
while (1)
5. Which of the following is not possible {
regarding the structure variable? p = p->next;
if (p == NULL)
a) A structure variable pointing to itself break;
b) A structure variable pointing to another }
structure variable of same type
c) 2 different type of structure variable d) All of the mentioned
pointing at each other
d) None of the mentioned Answer: b
Explanation: None.
Answer: d
Explanation: None. This section on C interview questions and
answers focuses on “Unions”. One shall
6. Which of the following technique is faster
practice these interview questions to improve
for travelling in binary trees?
their C programming skills needed for various
a) Iteration
interviews (campus interviews, walkin
b) Recursion
interviews, company interviews), placements,
c) Both Iteration and Recursion
entrance exams and other competitive exams.
d) Depends from compiler to compiler
These questions can be attempted by anyone
Answer: b focusing on learning C Programming
Explanation: None. language. They can be a beginner, fresher,
engineering graduate or an experienced IT
7. Which of the following will stop the loop professional. Our C Interview questions come
at the last node of a linked list in the with detailed explanation of the answers
following C code snippet? which helps in better understanding of C
concepts.
1. struct node
Here is a listing of C interview questions on
2. { “Unions” along with answers, explanations
and/or solutions:
3. struct node *next;
1. The size of a union is determined by the
4. };
size of the
a) First member in the union
a)
b) Last member in the union
c) Biggest member in the union a) 4
d) Sum of the sizes of all members b) 8
c) 40
Answer: c d) 80
Explanation: None.
Answer: c
2. Which member of the union will be active Explanation: None.
after REF LINE in the following C code?
4. What type of data is holded by variable u
1. #include <stdio.h> int in the following C code?
2. union temp 1. #include <stdio.h>

3. { 2. union u_tag

4. int a; 3. {

5. float b; 4. int ival;

6. char c; 5. float fval;

7. }; 6. char *sval;

8. union temp s = {1,2.5,’A’}; //R 7. } u;


EF LINE
a) Will be large enough to hold the largest of
a) a
the three types;
b) b
b) Will be large enough to hold the smallest
c) c
of the three types;
d) Such declaration are illegal
c) Will be large enough to hold the all of the
Answer: a three types;
d) None of the mentioned
Explanation: None.
Answer: a
3. What would be the size of the following
Explanation: None.
union declaration? (Assuming size of double
= 8, size of int = 4, size of char = 1)
5. Members of a union are accessed
1. #include <stdio.h>
as
a) union-name.member
2. union uTemp b) union-pointer->member
c) both union-name.member & union-pointer-
3. { >member
d) none of the mentioned
4. double a;

5. int b[10];
Answer: c
Explanation: None.
6. char c;
6. In the following C code, we can access the
7. }u; 1st character of the string sval by using
1. #include <stdio.h> c) 4
d) 32
2. struct
Answer: c
3. {
Explanation: None.
4. char *name;
8. What will be the output of the following C
5. union code?
6. { 1. #include <stdio.h>

7. char *sval; 2. union stu

8. } u; 3. {

9. } symtab[10]; 4. int ival;

a) *symtab[i].u.sval 5. float fval;


b) symtab[i].u.sval[0].
c) You cannot have union inside structure 6. };
d) Both *symtab[i].u.sval & 7. void main()
symtab[i].u.sval[0].
8. {
Answer: d
Explanation: None. 9. union stu r;

7. What will be the output of the following C 10. r.ival = 5;


code (Assuming size of int and float is 4)? 11. printf("%d", r.ival);

1. #include <stdio.h> 12. }

2. union
a) 9
3. { b) Compile time error
c) 16
4. int ival; d) 5
5. float fval; Answer: d
Explanation: None.
6. } u;

7. void main() This section on C interview questions and


answers focuses on “Unions”. One shall
8. {
practice these interview questions to improve
9. printf("%d", sizeof(u)); their C programming skills needed for various
interviews (campus interviews, walkin
10. } interviews, company interviews), placements,
entrance exams and other competitive exams.
a) 16 These questions can be attempted by anyone
b) 8 focusing on learning C Programming
language. They can be a beginner, fresher,
engineering graduate or an experienced IT 4. int x;
professional. Our C Interview questions come
with detailed explanation of the answers 5. char y;
which helps in better understanding of C 6. }p;
concepts.
7. int main()
Here is a listing of C interview questions on
“Unions” along with answers, explanations 8. {
and/or solutions:
9. p.y = 60;
1. What will be the output of the following C
10. printf("%d\n", sizeof(p));
code?
11. }
1. #include <stdio.h>
a) Compile time error
2. union
b) sizeof(int) + sizeof(char)
3. { c) Depends on the compiler
d) sizeof(char)
4. int x;
Answer: c
5. char y; Explanation: None.
6. }p;
3. What will be the output of the following C
7. int main() code?

8. { 1. #include <stdio.h>

9. p.x = 10; 2. union p

10. printf("%d\n", sizeof(p)); 3. {

11. } 4. int x;

a) Compile time error 5. char y;


b) sizeof(int) + sizeof(char) 6. };
c) Depends on the compiler
d) sizeof(int) 7. int main()

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

Answer: c 9. printf("%d\n", k.y);


Explanation: None.
10. }
4. What will be the output of the following C
code? a) Compile time error
b) 97
1. #include <stdio.h> c) a
d) Depends on the standard
2. union p

3. {
Answer: b
Explanation: None.
4. int x;
6. What will be the output of the following C
5. char y; code?
6. }k = {1, 97}; 1. #include <stdio.h>

7. int main() 2. union p

8. { 3. {

9. printf("%d\n", k.y); 4. int x;

10. } 5. float y;

a) Compile time error 6. };


b) 97
c) a 7. int main()
d) 1
8. {

Answer: d 9. union p p, b;
Explanation: None.
10. p.x = 10;
5. What will be the output of the following C
code? 11. printf("%f\n", p.y);

1. #include <stdio.h> 12. }

2. union p a) Compile time error


b) Implementation dependent
3. { c) 10.000000
d) 0.000000
4. int x;

5. char y;
Answer: b
Explanation: None.
7. Which of the following share a similarity
in syntax?
1. Union, 2. Structure, 3. Arrays and 4.
Pointers
UNIT V FILE
PROCESSING
a) 3 and 4
b) 1 and 2
c) 1 and 3 TOPIC 5.1 INPUT AND OUTPUT
d) 1, 3 and 4 IN C
Answer: b This section on C programming interview
Explanation: None. questions and answers focuses on “File
Access”. One shall practice these interview
8. What will be the output of the following C
questions to improve their C programming
code? (Assuming size of char = 1, int = 4,
skills needed for various interviews (campus
double = 8)
interviews, walkin interviews, company
1. #include <stdio.h>
interviews), placements, entrance exams and
other competitive exams. These questions can
2. union utemp be attempted by anyone focusing on learning
C Programming language. They can be a
3. { beginner, fresher, engineering graduate or an
experienced IT professional. Our C
4. int a;
programming interview questions come with
5. double b; detailed explanation of the answers which
helps in better understanding of C concepts.
6. char c;
Here is a listing of C programming interview
7. }u; questions on “File Access” along with
answers, explanations and/or solutions:
8. int main()

9. { 1. What are the first and second arguments of


fopen?
10. u.c = 'A'; a) A character string containing the name of
the file & the second argument is the mode
11. u.a = 1; b) A character string containing the name of
the user & the second argument is the mode
12. printf("%d", sizeof(u));
c) A character string containing file pointer &
13. } the second argument is the mode
d) None of the mentioned
a) 1
b) 4 Answer: a
c) 8 Explanation: None.
d) 13
2. For binary files, a must be appended to
Answer: c the mode string.
Explanation: None. a) Nothing
b) “b”
c) “binary” fp = fopen("Random.txt", "a");
d) “01”
a) Attach
Answer: b b) Append
Explanation: None. c) Apprehend
d) Add
3. What will fopen will return, if there is any
error while opening a file? Answer: b
a) Nothing Explanation: None.
b) EOF
c) NULL 8. Which of the following mode argument is
d) Depends on compiler used to truncate?
a) a
Answer: c b) f
Explanation: None. c) w
d) t
4. What is the return value of getc()?
a) The next character from the stream is not Answer: c
referred by file pointer Explanation: None.
b) EOF for end of file or error
c) Nothing 9. Which type of files can’t be opened using
d) None of the mentioned fopen()?
a) .txt
Answer: b b) .bin
Explanation: None. c) .c
d) none of the mentioned
5. When a C program is started, O.S
environment is responsible for opening file Answer: d
and providing pointer for that file? Explanation: None.
a) Standard input
b) Standard output Sanfoundry’s 1000+ Interview Questions &
c) Standard error Answers on C helps anyone preparing for
d) All of the mentioned Adaptec and other companies C interviews.
One should practice these 1000+ interview
Answer: d questions and answers continuously for 2-3
Explanation: None. months to clear Adaptec interviews on C
Programming language.
6. In C language, FILE is of which data type?
a) int Here is a listing of C programming questions
b) char * on “File Access” along with answers,
c) struct explanations and/or solutions:
d) None of the mentioned
1. Which of the following fopen() statements
Answer: c are illegal?
Explanation: None. a) fp = fopen(“abc.txt”, “r”);
b) fp = fopen(“/home/user1/abc.txt”, “w”);
7. What is meant by ‘a’ in the following C c) fp = fopen(“abc”, “w”);
operation? d) none of the mentioned
Answer: d Answer: c
Explanation: None. Explanation: None.

2. What does the following segment of C 5. What will be the output of the following C
code do? code?
fprintf(fp, "Copying!"); 1. #include <stdio.h>

a) It writes “Copying!” into the file pointed 2. #include <stdlib.h>


by fp
b) It reads “Copying!” from the file and prints 3. int main()
on display 4. {
c) It writes as well as reads “Copying!” to and
from the file and prints it 5. FILE *fp = stdout;
d) None of the mentioned
6. int n;
Answer: a
Explanation: None. 7. fprintf(fp, "%d", 45);

8. }
3. What is FILE reserved word?
a) A structure tag declared in stdio.h a) Compilation error
b) One of the basic data types in c b) 45
c) Pointer to the structure defined in stdio.h c) Nothing
d) It is a type name defined in stdio.h d) Depends on the standard
Answer: d Answer: b
Explanation: None. Explanation: None.
4. What will be the output of the following C 6. stdout, stdin and stderr are
code? a) File pointers
b) File descriptors
1. #include <stdio.h>
c) Streams
2. int main() d) Structure

3. { Answer: a
Explanation: None.
4. FILE *fp = stdin;
7. Which of the following statements about
5. int n;
stdout and stderr are true?
6. fprintf(fp, "%d", 45); a) Same
b) Both connected to screen always
7. } c) Both connected to screen by default
d) stdout is line buffered but stderr is
a) Compilation error unbuffered
b) 45
c) Nothing Answer: c
d) Depends on the standard Explanation: None.
8. What will be the output of the following C c) 65
code? d) Compilation error

1. #include <stdio.h> Answer: a


Explanation: None.
2. int main()
10. What will be the output of the following
3. {
C code?
4. FILE *fp = stdout;
1. #include <stdio.h>
5. int n;
2. int main()
6. fprintf(fp, "%d ", 45);
3. {
7. fprintf(stderr, "%d ", 65);
4. FILE *fp = stdout;
8. return 0;
5. int n;
9. }
6. fprintf(fp, "%d ", 45);
a) 45 65
7. fflush(stdout);
b) 65 45
c) 65 8. fprintf(stderr, "%d", 65);
d) Compilation error
9. return 0;
Answer: b
Explanation: None. 10. }

9. What will be the output of the following C a) 45 65


code? b) 65 45
c) 45
1. #include <stdio.h> d) Compilation error

2. int main() Answer: a


Explanation: None.
3. {

4. FILE *fp = stdout; This section on C MCQs (multiple choice


questions) focuses on “Error Handling”. One
5. int n; shall practice these MCQs to improve their C
programming skills needed for various
6. fprintf(fp, "%d\n ", 45);
interviews (campus interviews, walkin
7. fprintf(stderr, "%d ", 65); interviews, company interviews), placements,
entrance exams and other competitive exams.
8. return 0; These questions can be attempted by anyone
focusing on learning C Programming
9. }
language. They can be a beginner, fresher,
engineering graduate or an experienced IT
a) 45 65
professional. Our multiple choice questions
b) 65 45
come with detailed explanation of the
answers which helps in better understanding 3. {
of C concepts.
4. FILE *fp;
Here is a listing of C multiple choice
5. char c;
questions on “Error Handling” along with
answers, explanations and/or solutions: 6. int n = 0;

1. What is the output of the following C code 7. fp = fopen("newfile1.txt",


if there is no error in stream fp? "r");

1. #include <stdio.h> 8. while (!feof(fp))

2. int main() 9. {

3. { 10. c = getc(fp);

4. FILE *fp; 11. putc(c, stdout);

5. fp = fopen("newfile", "w"); 12. }

6. printf("%d\n", ferror(fp)); 13. }

7. return 0; a) Compilation error


b) Prints to the screen content of newfile1.txt
8. } completely
c) Prints to the screen some contents of
a) Compilation error newfile1.txt
b) 0 d) None of the mentioned
c) 1
d) Any nonzero value Answer: d
Explanation: None.
Answer: b
Explanation: None. 4. What will be the output of the following C
code?
2. Within main, return expr statement is
equivalent to 1. #include <stdio.h>
a) abort(expr)
b) exit(expr) 2. int main()
c) ferror(expr)
d) none of the mentioned 3. {

4. FILE *fp = stdout;


Answer: b
Explanation: None. 5. stderr = fp;

3. What will be the output of the following C 6. fprintf(stderr, "%s", "hell


code? o");

1. #include <stdio.h>
7. }

2. int main() a) Compilation error


b) hello
c) Undefined behaviour output file
d) Depends on the standard c) The line which caused error is compiled
again
Answer: b d) The program is immediately aborted
Explanation: None.
Answer: a
5. What will be the output of the following C Explanation: None.
code?
8. Which of the following function can be
1. #include <stdio.h> used to terminate the main function from
another function safely?
2. int main()
a) return(expr);
3. { b) exit(expr);
c) abort();
4. char buf[12]; d) both exit(expr); and abort();
5. stderr = stdin; Answer: b
Explanation: None.
6. fscanf(stderr, "%s", buf);

7. printf("%s\n", buf); Sanfoundry’s 1000+ Interview Questions and


Answers on C helps Freshers preparing for
8. }
Job Interviews. Freshers’s should practice
these 1000+ questions continuously for 2-3
a) Compilation error
months, thereby ensuring a top position in
b) Undefined behaviour
campus interviews or fresher’s hiring
c) Whatever user types
programs.
d) None of the mentioned
Here is a listing of C interview questions on
Answer: c
“Error Handling” along with answers,
Explanation: None.
explanations and/or solutions:
6. stderr is similar to?
1. Which of the following causes an error?
a) stdin
a) Trying to read a file that doesn’t exist
b) stdout
b) Inability to write data in a file
c) both stdout and stdin
c) Failure to allocate memory with the help of
d) none of the mentioned
malloc
Answer: a d) All of the mentioned
Explanation: None.
Answer: d
7. What happens when we use the following Explanation: None.
C statement?
2. What is the purpose of the C function?
fprintf(stderr, "error: could not open fi
int ferror(FILE *fp)
len");

a) The diagnostic output is directly displayed a) They check for input errors
in the output b) They check for output errors
b) The diagnostic output is pipelined to the
c) They check for all types of errors malloc
d) They check for error in accessing the file d) All of the mentioned

Answer: b Answer: d
Explanation: None. Explanation: None.

3. stderr is similar to? 7. What is the purpose of the C function?


a) stdin
b) stdout int ferror(FILE *fp)
c) Both stdout and stdin
d) None of the mentioned a) They check for input errors
b) They check for output errors
Answer: b c) They check for all types of errors
Explanation: stderr is not exactly the same as d) They check for error in accessing the file
stdout, but similar in the sense that both puts
the output or error to the monitor. Answer: b
Explanation: None.
4. What will be the output of the following C
statement? This section on advanced C interview
questions focuses on “Line Input & Output”.
fprintf(stderr, "error: could not open fi One shall practice these advanced C questions
len");
to improve their C programming skills needed
a) The diagnostic output is directly displayed for various interviews (campus interviews,
in the output walkin interviews, company interviews),
b) The diagnostic output is pipelined to the placements, entrance exams and other
output file competitive exams. These questions can be
c) The line which caused error is compiled attempted by anyone focusing on learning C
again Programming language. They can be a
d) The program is immediately aborted beginner, fresher, engineering graduate or an
experienced IT professional. Our advanced C
Answer: a questions come with detailed explanation of
Explanation: None. the answers which helps in better
understanding of C concepts.
5. Which of the following function can be
used to terminate the main() function from Here is a listing of advanced C interview
another function safely? questions on “Line Input & Output” along
a) return(expr); with answers, explanations and/or solutions:
b) exit(expr);
c) abort(); 1. The syntax of fgets is char *fgets(char
d) both exit(expr); and abort(); *line, int maxline, FILE *fp). Which is true
for fgets?
Answer: b a) Returns line on success
Explanation: None. b) On end of file or error it returns NULL
c) Nothing
6. Which of the following causes an error? d) Both returns line on success & On end of
a) Trying to read a file that doesn’t exist file or error it returns NULL
b) Inability to write data in a file
c) Failure to allocate memory with the help of
Answer: d 3. int main()
Explanation: None.
4. {
2. fputs() function writes a string to a file that
5. char line[3];
only ends with a newline.
a) True 6. FILE *fp;
b) False
c) Depends on the standard 7. fp = fopen("newfile.txt",
d) Depends on the compiler "r");

Answer: b 8. while (fgets(line, 3, fp))


Explanation: None. 9. fputs(line, stdout);

3. What will be the output of the following C 10. return 0;


code?
11. }
1. #include <stdio.h>
a) Compilation error
2. #include <string.h> b) Infinite loop
c) Segmentation fault
3. int main()
d) No.of lines present in file newfile
4. {
Answer: c
5. char line[3]; Explanation: None.

6. fgets(line, 3, stdin); 5. What will be the output of the following C


code if 2 characters is typed by the user?
7. printf("%d\n", strlen(line)
);
1. #include <stdio.h>
8. return 0;
2. #include <string.h>
9. }
3. int main()

a) 3 4. {
b) 1
c) Any length since line did not end with null 5. char line[3];
character
d) Depends on the standard 6. fgets(line, 3, stdin);

7. printf("%d\n", line[2]);
Answer: b
Explanation: None. 8. return 0;

4. What will be the output of the following C 9. }


code?
a) Compilation error
1. #include <stdio.h> b) Undefined behaviour
c) 0
2. #include <string.h> d) 10(ascii value of newline character)
Answer: c focusing on learning C Programming
Explanation: None. language. They can be a beginner, fresher,
engineering graduate or an experienced IT
6. fputs() adds newline character. professional. Our C programming questions
a) True come with detailed explanation of the
b) False answers which helps in better understanding
c) Depends on the standard of C concepts.
d) Undefined behaviour
Here is a listing of tough C questions on
Answer: b “Line Input & Output” along with answers,
Explanation: None. explanations and/or solutions:
7. puts() function adds newline character. 1. What is the size of array “line” used in
a) True fgets(line, maxline, *fp) function?
b) False a) maxline – 1
c) Depends on the standard b) maxline
d) Undefined behaviour c) maxline + 1
d) Size is dynamic
Answer: a
Explanation: None. Answer: b
Explanation: None.
8. gets() function checks overflow run.
a) True 2. What will be the output of the following C
b) False function when EOF returns?
c) Depends on the standard
d) Undefined behaviour int fputs(char *line, FILE *fp)

Answer: b a) ‘�’ character of array line is encountered


Explanation: None. b) ‘n’ character in array line is encountered
c) ‘t’ character in array line is encountered
9. puts() does the following when it writes to d) When an error occurs
stdout.
a) Deletes everything Answer: d
b) Adds ‘t’ to the line written Explanation: None.
c) Deletes the terminating ‘n’
d) Adds ‘n’ to the line written 3. Identify X library function for line input
and output in the following C code?
Answer: d
Explanation: None. 1. #include <stdio.h>

2. int X(char *s, FILE *iop)


This section on tough C questions focuses on
“Line Input & Output”. One shall practice 3. {
these questions to improve their C
programming skills needed for various 4. int c;
interviews (campus interviews, walkin 5. while (c = *s++)
interviews, company interviews), placements,
entrance exams and other competitive exams. 6. putc(c, iop);
These questions can be attempted by anyone
7. return ferror(iop) ? EOF : c) stderr
0; d) nothing
8. } Answer: a
Explanation: None.
a) getc
b) putc 8. gets() does the following when it reads
c) fgets from stdin.
d) fputs a) Deletes the ‘t’
b) Puts adds it.
Answer: d
c) Deletes the terminating ‘n’
Explanation: None.
d) Nothing
4. Which function has a return type as char Answer: c
pointer?
Explanation: None.
a) getline
b) fputs
c) fgets 1. Which one of the following is correct
d) all of the mentioned syntax for opening a file.
a) FILE *fopen(const *filename, const char
Answer: c *mode)
Explanation: None. b) FILE *fopen(const *filename)
c) FILE *open(const *filename, const char
5. Which of the following is the right
*mode)
declaration for fgets() inside the library?
d) FILE open(const*filename)
a) int *fgets(char *line, int maxline, FILE
*fp); Answer: a
b) char *fgets(char *line, int maxline, FILE Explanation: fopen() opens the named file,
*fp); and returns a stream, or NULL of the attempt
c) char *fgets(char *line, FILE *fp); fails.
d) int *fgets(char *line, FILE *fp);
2. What is the function of the mode ‘ w+’?
Answer: b a) create text file for writing, discard previous
Explanation: None. contents if any
b) create text file for update, discard previous
6. what is the return value of fputs()?
contents if any
a) EOF if an error occurs
c) create text file for writing, do not discard
b) Non-negative if no error
previous contents if any
c) EOF if an error occurs & Non-negative if
d) create text file for update, do not discard
no error
previous contents if any
d) None of the mentioned
Answer: b
Answer: c
Explanation: w+ is a mode used to open a
Explanation: None.
text file for update (i. e., writing and reading),
7. gets() and puts() operate on discard previous contents if any.
a) stdin and stdout
3. If the mode includes b after the initial
b) files
letter, what does it indicates?
a) text file 7. What does tmpfile() returns when it could
b) big text file not create the file?
c) binary file a) stream and NULL
d) blueprint text b) only stream
c) only NULL
Answer: c d) does not return anything
Explanation: If the mode consists of letter b
after the first letter as in, “rb” or “w+b”, it Answer: a
indicates binary file. Explanation: tmpfile() returns a stream or
NULL if it could not create the file.
4. fflush(NULL) flushes all
a) input streams 8. Choose the right statement for fscanf() and
b) output streams scanf()
c) previous contents a) fscanf() can read from standard input
d) appended text whereas scanf() specifies a stream from
which to read
Answer: b b) fscanf() can specifies a stream from which
Explanation: fflush(FILE *stream) – fflush() to read whereas scanf() can read only from
causes any buffered but unwritten to be standard input
written on an Output stream. On an input c) fscanf() and scanf() has no difference in
stream, the effect is undefined. fflush(NULL) their functions
flushes all output streams. d) fscanf() and scanf() can read from
specified stream
5. removes the named file, so that a
subsequent attempt to open it will fail. Answer: b
a) remove(const *filename) Explanation: The fscanf() is similar to the
b) remove(filename) scanf() function, except that the first
c) remove() argument of fscanf() specifies a stream from
d) fclose(filename) which to read whereas scanf() can read from
standard input.
Answer: a
Explanation: remove(const *filename) 9. EOF is an integer type defined in stdio.
removes the named file, so that a subsequent hand has a value
attempt to open it will fail. It returns non-zero a) 1
of the attempt fails. b) 0
c) NULL
6. What is the function of FILE d) – 1
*tmpfile(void)?
a) creates a temporary file of mode “wb+” Answer: d
b) creates a temporary file of mode “wb” Explanation: EOF is an integer type defined
c) creates a temporary file of mode ” w” in stdio. hand has a value – 1.
d) creates a temporary file of mode “w+”
10. fwrite() can be used only with files that
Answer: a are opened in binary mode.
Explanation: A temporary file is created by a) true
tmpfile() function of mode “wb+” that will be b) false
automatically removed when closed or when
the program terminates normally.
Answer: a int fgetpos(FILE *stream, fpos_t *s)
Explanation: fwrite() can be used to write
characters, integers, or structures to a file. a) records the current position in stream in *s
However, fwrite() can be used only with files b) sets the file position for stream in *s
opened in binary mode. c) positions stream at the position recorded in
*s
d) reads from stream into the array ptr
1. what is the function of fputs()?
a) read a line from a file Answer: a
b) read a character from a file Explanation:fgetpos() records the current
c) write a character to a file position in stream in *s, for subsequent use
d) write a line to a file by fsetpos() . The type fpost_t is suitable for
recording such values.
Answer: d
Explanation: The fputs() is used to write a 5. Which functions is declared in <errno. h>?
line to a file. fputs() syntax can be written as a) fseek()
int fputs(const char *str, FILE *stream); b) ftell()
c) ferror()
2. What does the following C code snippet d) fsetpos()
mean?
Answer: c
char *gets(char *s) Explanation: ferror() is declared under
<errno. h>. ferror() returns non-zero if the
a) reads the next input line into the array s error indicator for stream is set.
b) writes the line into the array s
c) reads the next input character into the array 6. setvbuf() and setbuf() function controls
s buffering for the stream.
d) write a character into the array a) true
b) false
Answer: a
Explanation: gets() reads the next input line Answer: a
into the array s, terminating newline is Explanation: setvbuf() and setbuf() controls
replaced with ‘\0’.It returns s, or NULL if end buffering for the stream. If buff is NULL,
of file or error occurs. buffering is turned off for the stream.

3. Which function will return the current file 7. The functions vprintf(), vfprintf(), and
position for stream? vsprintf() are not equivalent to the
a) fgetpos() corresponding printf() functions except the
b) fseek() variable argument list.
c) ftell() a) true
d) fsetpos() b) false
Answer: c Answer: b
Explanation: The current file position is Explanation: The functions vprintf() ,
returned by ftell() function for stream, or -1L vfprintf() , and vsprintf() are similar to the
on error. corresponding printf() functions except that
the variable argument list is replaced by arg.
4. Select the right explanation for the
following C code snippet.
8. The function reads atmost one less c) reads frequency of c in stream
than the number of characters specified by d) no action is taken by the command
size from the given stream and it is stored in
the string str. Answer: a
a) fget() Explanation: ungetc() pushes c back onto
b) fgets() stream, where it will be returned on the next
c) fput() read. Only one character of pushback per
d) fputs() stream is Guaranteed.

Answer: b 10. Choose the correct difference between


getc() and fgetc().
Explanation: The fgets() function reads one
a) If it is not a macro, it may evaluate stream
less than the number of characters indicated
more than once
by the size from the given stream and it is
b) if it is amacro, it may not evaluate stream
stored in the string str. The fgets() terminates
as soon as it encounters either a newline more than once
character, EOF, or other error. c) if it is a macro, it may evaluate stream
more than once
9. What does the following C code snippet d) no difference between fgetc() and getc()
mean?
Answer: c
int ungetc(int c, FILE *stream) Explanation: getc() is equivalent to fgetc()
except that if it is a macro, it may evaluate
a) pushes c back onto a stream more than once.
b) deletes c from the stream

You might also like