You are on page 1of 131

A Comprehensive C Question Bank and Answers.

Shriram

K Vasudevan

Technical

Manager

Lets Swim in the C.

10/7/16

Question..
1. What is the output of following program?
main()
{
int x = -10;
printf("%d\n", ~x+1);
}
Choices:

A)11

b)-8

c)10

d)Size of the integer mandatory to get answer.

Answer ?
2

Lets Swim in the C.

10/7/16

Answer

-10 is stored as 8 4 2 1
1 0 1 0(+10)
0 1 0 1 (1s comp)
+ 1 will get 2s comp
--------------------------0 1 1 0 (-10)
Negation = 1 0 0 1 + 1 = 9 + 1 = 10.

Lets Swim in the C.

10/7/16

Question..
2. What is the output of following program?
main()
{
printf("%c\n", '1' + 1);
}

Choices:
a)ASCII value of '1' is required to find out the answer
b)2
c)50
d)Syntax Error
4

Lets Swim in the C.

10/7/16

Answer

B. '1' + 1 is evaluated as 0x31 + 1. Which is


nothing but the ASCII value of '2'

Lets Swim in the C.

10/7/16

Question..
3. What is output of following
program?
main()
{
int x = 1;
Choices:
int y = 2;
a)6
switch(x | y)
b)4
{
c)5
case 1: x = 2;
d)3
case 2: x = 3;
case 3: x = 4;
case 4: x = 5;
default:x = 6;
}
printf("%d\n", x);
}

Lets Swim in the C.

10/7/16

Answer
a. There are no break statements so,
irrespective of the value of x, last case
(default) will be executed. This is an example
of fall-through situation.

Lets Swim in the C.

10/7/16

Question..
4. What is the output of the following program?
main()
{
printf("%d\n", 100 / 10 / 10);
}

a)1
b)100
c)10
d)0
8

Lets Swim in the C.

10/7/16

Answer

a. For most of operators in C, the order of


evaluation is from left to right. (exceptions:
unary, ternary and assignment operators).
So, 100/10/10 is evaluated as (100/10)/10
which is equal to 1.

Lets Swim in the C.

10/7/16

Question..
5.

main()

{
int a=5;
if (a=1)
{
printf("%d", a);
}
}
In the above code

a)The printf statement will never get executed


b)The printf statement will always get executed and the value of a
will be printed as 5
c)The program will encounter syntax error
d)The printf statement will always get executed and the value of a
will be printed as 1
10

Lets Swim in the C.

10/7/16

Answer

Tricky!! D is the answer. The printf statement


will always get executed and the value of a
will be printed as 1

11

Lets Swim in the C.

10/7/16

Question..
6. int main()
{
int a[10];
int i;
for (i=0; i<10; i++)
a[i] = i;
printf("%d", a[-1]);
}
The above program

a)Will encounter a compilation error


b)Will encounter segmentation violation when run
c)Will have unpredictable behavior
d)Will get into infinite loop

12

Lets Swim in the C.

10/7/16

Answer

the correct answer is c. The array a[10] is in


stack. So, a[-1] will point to some location
within stack and its content is unpredictable.

13

Lets Swim in the C.

10/7/16

Question..
7. What is the output of the following program?
main()
{
int a=3, b=5, c=1;
int x=8;
x = a < (b < c);
printf("%d", x);
}

a)1
b)8
c)Will encounter run time error
d)0

14

Lets Swim in the C.

10/7/16

Answer

d. The expression (b < c) is evaluated first. It


is equivalent to (5 < 1) and this expression
will return 0. So, the next part of expression
will become a < 0 which is equivalent to 3 <
0 and hence result is 0. This result is assigned
to x and hence value of x becomes 0.

15

Lets Swim in the C.

10/7/16

Question..
8.

20 is the answer.. Scope matters!

16

Lets Swim in the C.

10/7/16

Question
9.

17

20, 40 are the answers

Lets Swim in the C.

10/7/16

Question
10.

Declaration

18

Lets Swim in the C.

10/7/16

Question
11.

19

Lets Swim in the C.

10/7/16

Question
12.

Hold your breath.. 20!!

20

Lets Swim in the C.

10/7/16

Question
13.

How are the two different?

Simple, former states that the fun () could be in another


source file!

21

Lets Swim in the C.

10/7/16

Question
14.

When partial initialization is done, you are all set to


0

22

Lets Swim in the C.

10/7/16

Question
15.

23

Lets Swim in the C.

10/7/16

Question
16.

Nope!! Am sorry!

24

Lets Swim in the C.

10/7/16

Question
17.
OUTPUT??
D is the answer.. And the
output is 12345678910

25

Lets Swim in the C.

10/7/16

Question
18
1. What is while (1) ???
2. What will be the output?
Answers:
1. Infinite loop
2. 12345678910

26

Lets Swim in the C.

10/7/16

Question
19.

A is the
answer Boss!

27

Lets Swim in the C.

10/7/16

Question
20,21 and 22

for (i=1;i<=10;i++); is similar to


for (i=1;i<=10;i++)
{
}
28

Lets Swim in the C.

10/7/16

Question
24.
S!! Of
course .
. Case
j

AM I WRONG SOMEWHERE????

29

Lets Swim in the C.

10/7/16

Question
25.

No.. Its perfect.


What could be the
output?
It is 7!!

Do I have a mistake here?

30

Lets Swim in the C.

10/7/16

Question
26.

Anything wrong
here???????????????
Not at all, Switch is no harmful!

31

Lets Swim in the C.

10/7/16

Question
27.

Ouput???
Sorry.. No output

32

Lets Swim in the C.

10/7/16

Question
28.
Answer:
7. Printf wont
get run!!!

What will be the output.. ???


33

Lets Swim in the C.

10/7/16

Question
29.

No difference from compiler point of


view!!

34

Lets Swim in the C.

10/7/16

Question
30.

Why not!!! But if-else would be a better


choice for sure!

35

Lets Swim in the C.

10/7/16

Question
30.

36

Lets Swim in the C.

10/7/16

Question
31, 32.
#include <stdio.h>
main( )
{
int i=7;
printf ("\n %d %d",++i,++i);
getchar();
}

Answer:
Compiler will go mad! Order of
evaluation will not be clear and the
output can be 9 8

37

Lets Swim in the C.

#include
<stdio.h>
main( )
{
int i=7;
printf ("\n %d",+
+i);
printf ("\n %d",+
+i);
getchar();
Now it would be
}
perfect!! 8,9

10/7/16

Question
33
#include <stdio.h>
main( )
{
int x=10, y=20, z=5,i;
i=x<y<z;
printf ("\n %d", i);
getchar();
}

Answer:
(10<20)<5
So answer is 1.

Options
1, 0

38

Lets Swim in the C.

10/7/16

Question
34
What will this do ?
z = (a > b) ? a : b;

Answer:
Thus to set z to the maximum of a and b,
z = (a > b) ? a : b; /* assign z the maximum of a and
b */

39

Lets Swim in the C.

10/7/16

Question
35
Answer:
3
0
1

Note:
! Is evaluated before && which is evaluated
before ||

40

Lets Swim in the C.

10/7/16

Question
36

C.
Simple!!!

41

Lets Swim in the C.

10/7/16

Question
37

Tell me
your
opinion?

42

Lets Swim in the C.

10/7/16

Question
38

Type casting .. It is
A

43

Lets Swim in the C.

10/7/16

Question
39

c. You cannot re
declare dude!
44

Lets Swim in the C.

10/7/16

Question
40

45

Lets Swim in the C.

10/7/16

Question
41, 42
Y

Till
overflow!

46

Lets Swim in the C.

10/7/16

Question
43,44,45

What is the return value of printf (SHRIRAM);

What is the return value of scanf (%d,&i);


(Assuming I is entered with 6)

Can we include a header file which does not


exist?

47

NOPE.

Lets Swim in the C.

10/7/16

Question
46, 47
Re arrange this #define in
such a way u will get 25
as output.
# define SQR (x)
((x)*(x))

Simple!!
3+(2*3)+2
3+6+2=11

48

Lets Swim in the C.

10/7/16

Question
48

49

Lets Swim in the C.

10/7/16

Question
50, 51

51

Lets Swim in the C.

10/7/16

Question
52,53,54

52

Lets Swim in the C.

10/7/16

Question
55

53

Lets Swim in the C.

10/7/16

Question
56

54

Lets Swim in the C.

10/7/16

Question
57

55

Lets Swim in the C.

10/7/16

Question
58
#define MAKEDOUBLE(x) x+x
a = MAKEDOUBLE(4) * 3;
VALUE OF a?

a)24
b)16
c)8
d)None

The correct answer is b. MAKEDOUBLE(4) * 3 translates 4 + 4


* 3. Since, * has higher precedence than +, it becomes 4 +
12, i.e 16. To avoid such problems, the macro should be
defined as MAKEDOUBLE(x) ((x) + (x))
56

Lets Swim in the C.

10/7/16

Question
59
What is the output of the following program?
int x[100];
main()
{
printf("%d\n",x[99]);
}

a)Unpredicatable
b)Runtime error
c)0
d)99

The correct answer is c. In C, it is guaranteed that all the uninitialized


global variables are initialized to 0.

57

Lets Swim in the C.

10/7/16

Question
63
How many times is "Hello world" is printed in following program.
main()
{
unsigned int i = 5;
while (--i >= 0)
{
printf("Hello World\n");
}
}

a)5
b)6
c)Infinite
d)Program will not compile

The correct answer is c. Being an unsigned number, value of i cannot be less than 0.
So, the statement i >= 0 will be always true. Hence, this program goes into an
infinite loop.
61

Lets Swim in the C.

10/7/16

Question
64
The preprocessor can trap simple errors like missing
declarations, nested comments or mismatch of braces

a)True
b)False
c)Depends on the compiler
d)None of the above

The correct answer is b. The only function of preprocessor is to expand macros. They can be thought of
as intelligent text processors. Only compilers have the
capability to perform syntax checking.
62

Lets Swim in the C.

10/7/16

Question
65
What is the output of the following program.
main()
{
char str[7] = "Strings";
printf("%s", str);
}

a)Strings
b)Not predicatable
c)Compilation error
d)None of the above
The correct answer is b. "Strings" is internally stored as "Strings\0"
which needs 8 bytes of storage space. Since only 7 bytes are allocated
for the str, '\0' is written into some location in stack after 7 bytes and
this results in stack corruption. So, the behavior is not predictable.
63

Lets Swim in the C.

10/7/16

Question
66
What is the output of following program?
main()
{
int y = 100;
const int x = y;
printf("%d\n", x);
}

a)100
b)Garbage value
c)Error
d)0

The correct answer is a. It is ok to initialise a const variable with another


variable. The keyword const indicates that once initialised, it shouldn't be
changed.
64

Lets Swim in the C.

10/7/16

Question
67
In a C file, the following line is present:
#define FAST_COMPUTE
When trying to compile the program,

a)The C preprocessor gives an error


b)The C compiler gives an error
c)The program compiles normally
d)The assembler gives an error

Sorry, the correct answer is c. As far as the pre-processor is


concerned, FAST_COMPUTE is defined but doesn't have a
value. So, the statements like ifdef FAST_COMPUTE will
succeed.
65

Lets Swim in the C.

10/7/16

Question
68
In a C Program, stack is used for

a)Storing local variables only


b)Storing local variables and passing parameters
c)Passing parameters only
d)Storing local static variables

correct answer is b. Stack is used for storing local


variables as well as for passing parameters. Calling
function pushes the arguments into stack before control
transfers to the called function. In addition, return address
and values of registers also get pushed on to the stack.
66

Lets Swim in the C.

10/7/16

Question
69
Memory leak in a program is mostly due to

a)RAM becoming bad during program execution


b)linker defects
c)Improper use of dynamic memory in the code or the libraries
used
d)Compiler Defects

The correct answer is c. Whenever memory is allocated


dynamically and not freed, this situation is called as memory
leak. Memory leak is a serious problem and difficult to locate.
Tools like purify are used to detect the memory leaks faster. It is
possible to avoid memory leak by following certain coding
guidelines.
67

Lets Swim in the C.

10/7/16

Question
70
int main()
{
int i = 0;
int sizeint;
sizeint = sizeof i++;
printf("%d\n", i);
return 0;
}
Considering that integer requires 4 bytes of memory, the value of
i printed by the above program is:

a)1
b)4
c)0
d)The above program encounters a compilation error.
The correct answer is c. sizeof is an operator. Once the sizeof the type int is determined, the expression is
not evaluated any further since sizeof(i++) and sizeof(i) both are same as sizeof(int)

68

Lets Swim in the C.

10/7/16

Question
71
int main()
{
int i = 0;
int sizeint;
sizeint = sizeof(i++);
printf("%d\n", sizeint);
return 0;
}

a)1
b)2
c)4
d)The output will be machine dependent
The correct answer is d. In the above program, i is of integer type. An integer
could occupy either 2 or 4 or more bytes depending on the architecture of
the machine.
69

Lets Swim in the C.

10/7/16

Question
72
int main()
{
int j = 6;
int i;
i = 5, j++;
printf("%d\n", i);
return 0;
}
Which of the following statements are true about the code snippet given above

a)The code does not compile.


b)5
c)6
d)The result in machine dependent.
The correct answer is b. Comma is a series operator. In a statement like what is
shown above, first assignment is performed and then next part of the statement
is evaluated.
70

Lets Swim in the C.

10/7/16

Question
73
int main()
{
int j=6;
int i;
i = (5, j);
printf("%d %d\n", i, j);
return 0;
}
The output from the above program is:

a)5 6
b)6 6
c)6 7
d)The output is machine dependent

The correct answer is b. (5, j) is first evaluated as (5, 6). When placed in
parenthesis (5,6) would return 6 which gets assigned to i.
71

Lets Swim in the C.

10/7/16

Question
74
int main()
{
int j=6;
int i;
i = (5, j++);
printf("%d %d\n", i, j);
return 0;
}
The output from the above program is:

a)5 6
b)6 6
c)6 7
d)The output is machine dependent

The correct answer is c. (5, j++) is first evaluated as (5, 6). When placed in
parenthesis (5,6) would return 6 which gets assigned to i. Then j is incremented.
72

Lets Swim in the C.

10/7/16

Question
70
What is the output of following program?
main()
{
int x = 0;
int y = 0;
if (x++ && ++y)
{ ++x; }
printf("%d %d\n",x, y);
}
a)0 0
b)1 0
c)2 0
d)2 1

The correct answer is b. x++ translates to 0 (and then x is incremented to 1). Since FALSE && (anything)
would become zero, the second part is not evaluated (short-circuited). And overall condition is false and
hence ++x is not executed.

73

Lets Swim in the C.

10/7/16

Question
75
What is the output of following program?
main()
{
int x = 0;
int y = 0;
if (++x || ++y)
{
x++;
}
printf("%d %d\n",x, y);
}

a)1 1
b)1 0
c)2 1
d)2 0

74

Lets Swim in the C.

10/7/16

Question
76
What is the output of following program?
main()
{
int x = 0;
int y = 0;
if (!(++x && ++y))
{
x++;
}
printf("%d %d\n",x, y);
}

a)1 1
b)1 0
c)2 1
d)2 0

Sorry,

the correct answer is a. ++x translates to 1. ++y also needs to be evaluated as we have && operator. ++y translates to 1. !(TRUE &&
TRUE) translates to FALSE. Since overall condition translates to FALSE, x++ is not executed.

75

Lets Swim in the C.

10/7/16

Question
77
What is the output of following program?
main()
{
int x = 0;
int y = 0;
if (!(++x || ++y))
{
x++;
}
printf("%d %d\n",x, y);
}

a)1 0
b)1 1
c)2 1
d)2 0

The correct answer is a. ++x translates to 1. The second part need to be evaluated as (TRUE ||
anything) would be TRUE. Since there is an overall NOT, condition translates to FALSE, x++ is
not executed.

76

Lets Swim in the C.

10/7/16

Question
78
What is the output of following program?
main()
{
int x = 10;
int y = 20;
if (x <= y == 1)
{
++x;
}
printf("%d\n", x);
}

a)11
b)10
c)Compiler Dependent
d)Syntax Error

Sorry, the correct answer is a. Precedence: <= followed by ==. The given expression is
equivalent to (x <= y) == 1.

77

Lets Swim in the C.

10/7/16

Question
79
What is the output of following program?
main()
{
int x = 1;
int y = 0;
if (x | y++)
{
++y;
}
printf("%d %d\n", x, y);
}

a)Compiler Dependent
b)1 1
c)1 0
d)1 2

Sorry, the correct answer is d. The expression (x | y++) has bit-wise OR. i.e the expression would always be evaluated
fully. Therefore y would get incremented twice (unlike logical OR ||, where evaluation of expression may be done partially
based on partial result).

78

Lets Swim in the C.

10/7/16

Question
80
What is the output of following program?
#define DOUBLE(x) x + x
main()
{
printf("%d\n", DOUBLE(10)*2);
}

a)20
b)30
c)40
d)Syntax error

Sorry, the correct answer is b. DOUBLE(10)*2 would expand as 10 + 10


* 2. Since * has more precedence than +, this expression will be
evaluated as 10 + 20 (30).
79

Lets Swim in the C.

10/7/16

Question
81
What is the output of following program?
#define SQUARE(x) (x * x)
main()
{
int x = 10;
int y = 5;
printf("%d\n", SQUARE(x +y));
}

a)225
b)45
c)65
d)Syntax error

The correct answer is c. SQUARE(x+y) would expand as (10 + 5 * 10 + 5). Since


* has more precedence than +, this expression will be evaluated as 10 + 50 + 5
(65).
80

Lets Swim in the C.

10/7/16

Question
82
What is the output of following program?
#define ABC(x) DEF(x)
#define DEF(x) GHI(x)
#define GHI(x) printf x
main()
{
int x = 100;
ABC(("value of x is %d\n", x));
}

a)value of x is 100
b)Syntax error
c)Linker error
d)Runtime error

Sorry, the correct answer is a. Because of additional paranthesis in ABC(("value of x is %d\n",


x)), the entire ("value of x is %d\n", x) (including the paranthesis) goes as an argument to
macro ABC. Eventually, it is expanded to printf ("value of x is %d\n", x). Note the space
between printf and ( in the last sentence as well as space between printf and x in the macro
GHI.

81

Lets Swim in the C.

10/7/16

Question
83 just know

## -- "token paste operator"


# - "stringizer operator"

82

Lets Swim in the C.

10/7/16

Question
84
Assuming the code below is stored in a file "a.c" and the command "gcc a.c" is used to compile
the file, what is the output of this program:

#ifdef DEBUG
#define DEBUG_PRINT(x) printf("debug :%s\n", (x));
#else
#define DEBUG_PRINT(x)
#endif
main()
{
int x;
#if 0
x=10;
#endif
printf("%d\n", x);
DEBUG_PRINT("Exiting main");
}

a)Program encounters compilation error


b)The program prints some junk value
c)Program gives run-time error
d)Program prints 10
Sorry, the correct answer is b. Here the assignment statement gets compiled out since text following #if 0 is discarded. This is
equivalent to commenting the code between #if 0 and #endif

83

Lets Swim in the C.

10/7/16

Question
85
What is the output of following program?
main()
{
char **p = 0;
printf("%d\n", ++p);
}

a)4
b)Syntax Error
c)Runtime Error
d)Unpredictable output

Sorry, the correct answer is a. p is double pointer (pointer to pointer).


Sizeof any pointer in 32 bit machines is 4 bytes. So, by incrementing p, it
moves 4 bytes. Since, p is initialised to zero, value of p after increment is 4.

84

Lets Swim in the C.

10/7/16

Question
86
What is the output of following program?
main()
{
int x, y;
int *p1 = &x;
int *p2 = &y;
printf("%d\n", p1-p2);
}

a)4
b)1
c)-4
d)Syntax or Runtime error

the correct answer is b. All the local variables are stored in stack. So, x, y, p1 and p2, all
these variables will be part of the stack. Since, stack follows "last in first out" principle, the
first local variable is pushed into the stack. i.e x is pushed first, followed by y, followed by p1
and p2. So, &p2 and &p1 would differ by 4 bytes. Since p1 is pushed earlier than p2, it will
have higher memory address than p2. However, subtracting two pointers of the same type,
will give the "number of elements" between them and hence output is 1.

85

Lets Swim in the C.

10/7/16

Question
87
What is the output of the following program?
main()
{
int x = 10;
int *p = &x;
printf("%d %d %d\n", x, *(&x), **(&p));
}

a)Syntax Error
b)Runtime Error
c)10 10 10
d)Unpredictable output as addresss of x and p are not known.

answer is c. Value of x is 10. *&x is nothing but x. *(&p) is not but p. **(&p)
is not but *p. So output would be 10 10 10.
86

Lets Swim in the C.

10/7/16

Question
88
What is the output of the following program:
main()
{
int i=15;
char *str;
strcpy(str, "Hello, World!");
printf("i=%d\n", i--);
}

a)15
b)14
c)The program does not compile
d)Program behaviour is unpredictable

the correct answer is d. str being a pointer has only 4-bytes allocated for it to
store an address. Copying a string into it will corrupt the stack
87

Lets Swim in the C.

10/7/16

Question
89
What is the output of the following program:
#include <stdio.h>
int i=0;
main()
{
int i=5;
printf("%d\n", i++);
}

a)0
b)1
c)5
d)6

Correct answer is c. Though i is a global variable, the local value of i holds within
the block in which it is declared. So printf would print 5, and then a postincrement would take place.
88

Lets Swim in the C.

10/7/16

Question
90
What is the output of the following program:
int i=5;
int f1(int i)
{
i=i/2;
return i;
}
main()
{
int i=10;
printf("%d, %d\n", f1(i), i);
}

a)5, 5
b)5, 10
c)10, 10
d)None of the above
Sorry, the correct answer is b. When f1() is called, the local copy of i which f1 receives as
argument(10) holds. The on-stack value of i is changed to 5 in f1 and 5 is returned from f1.
This is first printed in the printf statement when f1(i) is called, and then the on-stack variable i
which is assigned 10 is printed.

89

Lets Swim in the C.

10/7/16

Question
91
What is wrong with the following program:
main()
{
char *s = "Hello World\n";
printf("%s", s);
}

a)s has memory for only 4-bytes so this will not compile.
b)Its behaviour is unpredictable since s has memory for only 4 bytes
c)Program will encounter segmentation violation
d)It will work without any problem

Sorry, the correct answer is d. "Hello World\n" is allocated in read


only data segment of memory. The address of that gets assigned to
90

Lets Swim in the C.

10/7/16

Question
92
A loader is

a)Independent of the OS and hence loaders can work across OS


platforms
b)It is dependent on the OS and understands the OS specific
process loading requirements
c)Is part of the compiler and takes care of the last phase of
compilation
d)None of the above

Sorry, the correct answer is b. Loader is responsible for loading
the executable file generated into the memory to prepare it for
running. Hence it needs to understand the OS specifics to
enable running of the program.
91

Lets Swim in the C.

10/7/16

Question
93
What happens during "dynamic linking"?

a)Symbols in shared libraries are resolved.


b)Absolute references of a location are changed to
virtual addresses.
c)Allocation for stack and heap for the program.
d)Moving executable image from hard disk to main
memory.

Sorry, the correct answer is a. Dynamic linking is the


phase when symbols from shared libraries are resolved
92

Lets Swim in the C.

10/7/16

Question
94
By overwriting into the stack area, which of the
following are you likely to corrupt:

a)Other local variables in the current function


b)Return address
c)local variables of calling functions
d)All of the above

Sorry, the correct answer is d. Depending on the


extent to which stack over-writing happened, it is
likely that all of the above can get corrupted.
93

Lets Swim in the C.

10/7/16

Question
95
By overwriting memory in strings that are global
variables, which of the following are you likely to
corrupt?

a)Return address
b)Other global variables
c)local variables of calling functions
d)All of the above

the correct answer is b. Since all the global variables


are allocated from the data section, you are likely to
corrupt the data section.
94

Lets Swim in the C.

10/7/16

Question
96
Which of the following statements is true

a)The C-compiler checks for array bounds and prevents overwriting of


memory that is not part of the array if the array is on the stack
b)The C-compiler checks for sizes of various variables, and prevents a
particular variable's memory being illegally overwritten when
another variable is being written to.
c)The C-compiler checks for array bounds and prevents overwriting of
memory that is not part of the array if the array is on the heap
d)None of the above

The correct answer is d. As per the C-standard, there are no such


checks mandated and hence compilers do not take care of violations
of memory access. Hence it is all the more important for
programmers to take care of this.
95

Lets Swim in the C.

10/7/16

Question
97, 98, 99

Is the NULL pointer same as an uninitialized pointer ? Yes/No.

In which header file the NULL macro defined?

NO
STDIO.H, STDLIB.H

What is NULL pointer?

int a ; is declared outside any function, Ansi compilers will initialize it to


zero.
Likewise an uninitialized pointer variable is initialized to a value
guaranteed in such a way that it is certain and not to point any C
object or function.
A Pointer initialized in this manner is called a null pointer.
A Null pointer is a special pointer that points nowhere. That is no other
pointer will be equal to Null pointer.
The most easier and straight forward way to get a null pointer in your
program is to use the predefined constant NULL, which is defined

by several standard header files including <stdio.h>


<stdlib.h>.
96

Lets Swim in the C.

10/7/16

Question
100 Century!!

What is a void ()
pointer?

A void pointer is a special


type of pointer.

// An Example for VOID pointer


# include <stdio.h>
int main ()
{

It can point to any data type,


from an integer value to float
to anything!
Its sole limitation is that it
can be referenced directly .
(* cannot be used on them as
the length is always un
determined.
Therefore type casting is
required must be used to
turn the void pointer to a
pointer of a concrete data
type to which we can refer.
An example would be helpful.
97

Lets Swim in the C.

int a = 5;
double b = 3.233;
void *vp; // void pointer
vp = &a;
printf ("\n a = %d", *((int *)vp));
vp = &b;
printf ("\n b = %f", *((double *)vp));
return 0;
}
O/P
shri@ubuntu:~$ ./file1
a=5
b = 3.233000

10/7/16

Question
101
Assuming that starting address is 1000, what would be the
output?
# include <stdio.h>
main ()
{
int array[] = {2,4,5,6,7};
printf ("%d %d", array,sizeof(array));
}
Answer:
1000, 20
(Note: 4 bytes for int in my compiler)
98

Lets Swim in the C.

10/7/16

Question
102
Assuming that starting address is 1000, what
would be the output?
# include <stdio.h>
main ()
{
int arr[]={1,2,3,34,4};
printf ("%u %u", arr,&arr);
}
1000,1000
99

Lets Swim in the C.

10/7/16

Question
103

Yes!!!

100

Lets Swim in the C.

10/7/16

Question
104
What is the output of following code?
# include <stdio.h>
main ()
{
printf (10+"SACHIN TENDULKAR");
}

Output:
DULKAR

101

Lets Swim in the C.

10/7/16

Question
105
# include <stdio.h>
main ()
{
printf ("%c","abcdefgh"[4]);
}

What is the output?

Answer:

102

Lets Swim in the C.

10/7/16

Question
106
What is the output of following program?
main()
{
char x = 0x80;
int y = x;
printf("%d\n", y);
}

a)0
b)128
c)-128
d)Unpredictable

The correct answer is c. x is defined as a signed character. With 8 bits, data range
that can be represented is -128 to +127. So, 0x80 represents -128. When assigned
to an integer, -128 remains as is. Since the MSB is the sign bit, when value of
signed char is 0x80, sign bit is set. When this value is assigned to a signed integer,
the sign bit would get extended. Internally the value stored in y would be
0xFFFFFF80 which is the value of -128. Note that sign extension happens only for
signed data tyes. The value would show up as 128 if y was declared as unsigned.

103

Lets Swim in the C.

10/7/16

Question
107

What is the output of following program?


main()
{
char y[10] = "abcdefghi";
char *p = y;
p = p + 9;
printf("%c\n",*p);
}

a)i
b)Program will have runtime error
c)Unpredictable
d)No visible output

the correct answer is d. To begin with p points to first character. Adding 9 to it,
makes it point to '0' since the string is terminated by a NULL value. So when 9 is
added to p which is a char pointer it points to an empty string and hence no
output will be printed on the screen.

104

Lets Swim in the C.

10/7/16

Question
108
What is the output of following program?
main()
{
int y[2][2] = { {1,2}, {3,4} };
int *p = &y[1];
p = p + 1;
printf("%d\n",*p);
}

a)4
b)3
c)The program doesn't compile
d)Output is unpredicatable

the correct answer is a. Note that array indexing starts with 0. So, &y[1]
points to 3. After incrementing the integer pointer p by 1, it would point to
4.

105

Lets Swim in the C.

10/7/16

Question
109
main()
{
printf("%d\n",f1(10,10));
}
Assuming f1 is defined in another file, which of the following statements are acceptable by
compiler as function prototype of f1.
1. extern int f1();
2. extern f1(int, int);
3. extern f1();
4. extern int f1(int, int);

a)All of them
b)4
c)3
d)1 & 4

the correct answer is a. To keep compiler happy, just an extern f1() is enough. However,
it is good practice to give complete prototype extern int f1(int, int).

the correct answer is a. To keep compiler happy, just an extern f1() is enough. However,
it is good practice to give complete prototype extern int f1(int, int).

106

Lets Swim in the C.

10/7/16

Question
110
9)What is output of following program?
int y = 10;
main()
{
int x = 10;
int y = 20;
x = x + y;
if (x >= 30)
{
int y = 30;
x = x + y;
}
else
{
int y = 40;
x = x + y;
}
printf("%d\n", x);
}

a)40
b)50
c)60
d)70
Sorry, the correct answer is c.

To begin with, value of x is 10. After x = x + y, it


value becomes 30. Control will then get into the if block since x is 30. Within the
if block, value of y is 30 and hence x becomes 60.

107

Lets Swim in the C.

10/7/16

Question
111
What is output of following program?
main()
{
int x = 10;
int y = 20;
if (x & y)
if(x | y) x = 20;
else x = 30;
printf("%d\n", x);
}

a)10
b)20
c)30
d)Unpredicatable

Sorry, the correct answer is a. Note that else belongs to second if. It is always a good
practice to put even single line blocks also within braces to avoid such confusion. Also,
appropriate indentation would enhance readability significantly giving lesser scope to
misinterpretation of code.

108

Lets Swim in the C.

10/7/16

Question
113
What is the output of the following program?
int x[100];
main()
{
printf("%d\n",x[99]);
}

a)Unpredicatable
b)Runtime error
c)0
d)99

Sorry, the correct answer is c. In C, it is guaranteed that all the


uninitialized global variables are initialized to 0.

110

Lets Swim in the C.

10/7/16

Question
114
How many times is "Hello world" is printed in following program.
main()
{
unsigned int i = 5;
while (--i >= 0)
{
printf("Hello World\n");
}
}

a)5
b)6
c)Infinite
d)Program will not compile

Sorry, the correct answer is c. Being an unsigned number, value of i cannot be less than 0.
So, the statement i >= 0 will be always true. Hence, this program goes into an infinite
loop. When i becomes 0 and decremented further, it wraps around and becomes MAXINT.

111

Lets Swim in the C.

10/7/16

Question
116
What is the output of the following program:
#include <stdio.h>
int i=0;
main()
{
int i=5;
printf("%d\n", i++);
}

a)0
b)1
c)5
d)6

Sorry, the correct answer is c. Though i is a global variable, the local value of i holds
within the block in which it is declared. So printf would print 5, and then a postincrement would take place.

113

Lets Swim in the C.

10/7/16

Question
117
What is the size of the following structure?
typedef struct
{
int ctrl1;
char flag;
int ctrl2;
char flag2;
int ctrl3;
} MYREG;

a)14
b)20
c)24
d)16

Sorry, the correct answer is b. There is a 3 byte padding added after flag and flag2 to
align the integers ctrl2 and ctrl3. Therefore size of this structure will be 20 bytes.

114

Lets Swim in the C.

10/7/16

Question
118
What is the size of the following structure?
typedef struct
{
int ctrl1;
char flag;
char flag2;
int ctrl2;
int ctrl3;
} MYREG;

a)14
b)20
c)24
d)16

the correct answer is d. There is a 2-byte padding added before ctrl2 to align ctrl2
to a 4-byte boundary. Hence the size of this structure is 16.
115

Lets Swim in the C.

10/7/16

Question
119

Having access to the memory allocated through the pointer


returned is important because:

a)The address returned by malloc can be accessed only


through it
b)To check the return value of malloc
c)Freeing that memory is completely dependent on this
address
d)All of the above

Sorry, the correct answer is d. Memory allocated through


pointer returned is the only means for the program to have
access to the chunk allocated, for subsequently freeing it.

116

Lets Swim in the C.

10/7/16

Question
120

10)
A loader is

a)Independent of the OS and hence loaders can work across OS


platforms
b)It is dependent on the OS and understands the OS specific
process loading requirements
c)Is part of the compiler and takes care of the last phase of
compilation
d)None of the above
The correct answer is b. Loader is responsible for loading the
executable file generated into the memory to prepare it for
running. Hence it needs to understand the OS specifics to
enable running of the program.
117

Lets Swim in the C.

10/7/16

Question
121

118

Lets Swim in the C.

10/7/16

Question
122

Nothin
g

119

Lets Swim in the C.

10/7/16

Q 123
void main(){
int const * p=5;
printf("%d",++(*p));
}
What is the answer?
Compiler error: Cannot modify a constant value.

120

Lets Swim in the C.

10/7/16

Q 124
main(){
float me = 1.1;
double you = 1.1;
if(me==you)
printf("I love U");
else
printf("I hate U");
}

121

Lets Swim in the C.

10/7/16

Answer
Answer:
I hate U
Explanation:
For floating point numbers (float, double, long
double) the values cannot be predicted
exactly. Depending on the number of bytes,
the precision with of the value represented
varies. Float takes 4 bytes and long double
takes 10 bytes. So float stores 0.9 with less
precision than long double.
Rule of Thumb:
Never compare or at-least be cautious when
122
Swim in the C.
using floatingLets
point
numbers with 10/7/16
relational

Q125
main() {
static int var = 5;
printf("%d ",var--);
if(var)
main();
}

123

Lets Swim in the C.

10/7/16

Answer

Answer:
54321
Explanation:
When static storage class is given, it is
initialized once. The change in the value of a
static variable is retained even between the
function calls. Main is also treated like any
other ordinary function, which can be called
recursively.

124

Lets Swim in the C.

10/7/16

Q126

main(){
extern int i;
i=20;
printf("%d",i);
}

Answer:

Linker Error : Undefined symbol '_i'

Explanation:

extern storage class in the following declaration,


extern int i;

specifies to the compiler that the memory for i is allocated in some other
program and that address will be given to the current program at the time
of linking. But linker finds that no other variable of name i is available in
any other program with memory space allocated for it. Hence a linker error
has occurred .

125

Lets Swim in the C.

10/7/16

Q127
main()
{
int i=-1,j=-1,k=0,l=2,m;
m=i++&&j++&&k++||l++;
printf("%d %d %d %d %d",i,j,k,l,m);
}

Answer:
00131

Explanation :

Logical operations always give a result of 1 or 0 . And also the logical AND (&&)
operator has higher priority over the logical OR (||) operator. So the expression i++
&& j++ && k++ is executed first. The result of this expression is 0 (-1 && -1 && 0
= 0). Now the expression is 0 || 2 which evaluates to 1 (because OR operator always
gives 1 except for 0 || 0 combination- for which it gives 0). So the value of m is 1. The
values of other variables are also incremented by 1.
126

Lets Swim in the C.

10/7/16

Q128

main(){
char *p;
printf("%d %d ",sizeof(*p),sizeof(p));
}

Answer:

12

Explanation:

The sizeof() operator gives the number of bytes taken by its


operand. P is a character pointer, which needs one byte for storing
its value (a character). Hence sizeof(*p) gives a value of 1. Since it
needs two bytes to store the address of the character pointer
sizeof(p) gives 2.

127

Lets Swim in the C.

10/7/16

Q129
main(){
int i=3;
switch(i) {
default:printf("zero");
case 1: printf("one");
break;
case 2:printf("two");
break;
case 3: printf("three");
break;
}
}

Answer :

three

Explanation :

The default case can be placed anywhere inside the loop. It is executed only when all other cases
doesn't match.

128

Lets Swim in the C.

10/7/16

Q130
main(){
char string[]="Hello World";
display(string);
}
void display(char *string){
printf("%s",string);
}

Answer:
Compiler Error : Type mismatch in redeclaration of function display
Explanation :
In third line, when the function display is encountered, the compiler doesn't know
anything about the function display. It assumes the arguments and return types to be
integers, (which is the default type). When it sees the actual function display, the
arguments and type contradicts with what it has assumed previously. Hence a compile
time error occurs.
129
Lets Swim in the C.
10/7/16

q131

main(){
int c=- -2;
printf("c=%d",c);
}

Answer:

c=2;

Explanation:

Here unary minus (or negation) operator is used twice. Same


maths rules applies, ie. minus * minus= plus.

Note:

However you cannot give like --2. Because -- operator can only be
applied to variables as a decrement operator (eg., i--). 2 is a
constant and not a variable.
130

Lets Swim in the C.

10/7/16

q132
#define int char
main(){

int i=65;

printf("sizeof(i)=%d",sizeof(i));
}
Answer:
sizeof(i)=1
Explanation:
Since the #define replaces the string int by
the macro char

131

Lets Swim in the C.

10/7/16

q133
main()
{
printf("\nab");
printf("\bsi");
printf("\rha");

Answer:

hai

Explanation:

\n - newline

\b - backspace

\r - linefeed

132

Lets Swim in the C.

10/7/16

q134

#define a 10
main()
{
#define a 50
printf("%d",;
}
Answer:
50
Explanation:
The preprocessor directives can be redefined anywhere in
the program. So the most recently assigned value will be
taken.

133

Lets Swim in the C.

10/7/16

q135
#define clrscr() 100
main(){
clrscr();
printf("%d\n",clrscr());
}

Answer:

100

Explanation:

Preprocessor executes as a seperate pass before the execution of the compiler. So textual
replacement of clrscr() to 100 occurs.The input program to compiler looks like this :

main()

{
100;

printf("%d\n",100);
}

134

Lets Swim in the C.

10/7/16

q136

main(){
printf("%p",main);
}
Answer:
Some address will be printed.
Explanation:
Function names are just addresses (just like
array names are addresses).main() is also a
function. So the address of function main will
be printed. %p in printf specifies that the
argument is an address. They are printed as
hexadecimal numbers.

135

Lets Swim in the C.

10/7/16

q137

void main(){
int i=5;
printf("%d",i+++++i);
}
Answer:
Compiler Error
Explanation:
The expression i+++++i is parsed as i ++ +
+ + i which is an illegal combination of
operators.

136

Lets Swim in the C.

10/7/16

Enough with C!!

Next is C PLUS PLUS

137

Lets Swim in the C.

10/7/16

You might also like