Sei sulla pagina 1di 165

INDEX

1. Expressions
2. Declarations and Initializations
3. Control Instructions
4. Floating Point Issues
5. Functions
6. C Preprocessor
7. Pointers
8. Arrays
9. Strings
10. Structures, Unions, Enums
11. Typedef
12. Bitwise Operators
13. Command Line Arguments

Page 1
EXPRESSIONS
1. Which of the following is the correct order of evaluation for the below expression?
z=x+y*z/4%2-1

A.* / % + - = B. = * %/ + -
C. +*/%-= D.* /% - + =

2. Which of the following correctly shows the hierarchy of arithmetic operations in C?

A./ + * - B. * - / +
C. + - / * D./ * + -

3. Which of the following is the correct usage of conditional operators used in C?

A.a>b ? c=30 : c=40; B. a>b ? c=30;


C. max = a>b ? a>c?a:c:b>c?b:c D.return (a>b)?(a:b)
Answer & Explanation

4. Which of the following are unary operators in C?

1. !
2. sizeof
3. ~
4. &&
A.1, 2 B. 1, 3
C. 2, 4 D.1, 2, 3

5. In which order do the following gets evaluated

1. Relational
2. Arithmetic
3. Logical
4. Assignment
A.2134 B. 1234
C. 4321 D.3214

6. What will be the output of the program?

#include<stdio.h>

Page 2
int main()
{
int i=-3, j=2, k=0, m;
m = ++i && ++j && ++k;
printf("%d, %d, %d, %d\n", i, j, k, m);
return 0;
}
A.-2, 3, 1, 1 B. 2, 3, 1, 2
C. 1, 2, 3, 1 D.3, 3, 1, 2

7. Assunming, integer is 2 byte, What will be the output of the program?

#include<stdio.h>

int main()
{
printf("%x\n", -2<<2);
return 0;
}
A.ffff B. 0
C. fff8 D.Error

8. What will be the output of the program?

#include<stdio.h>
int main()
{
int i=-3, j=2, k=0, m;
m = ++i || ++j && ++k;
printf("%d, %d, %d, %d\n", i, j, k, m);
return 0;
}
A.2, 2, 0, 1 B. 1, 2, 1, 0
C. -2, 2, 0, 0 D.-2, 2, 0, 1

9. What will be the output of the program?

#include<stdio.h>
int main()
{
int x=2, y=70, z;
z = x!=4 || y == 2;
printf("z=%d\n", z);
return 0;
}

Page 3
A.z=0 B. z=1
C. z=4 D.z=2

Answer & Explanation

10. What will be the output of the program?

#include<stdio.h>
int main()
{
static int a[5];
int i = 0;
a[i] = i ;
printf("%d, %d, %d\n", a[0], a[1], i);
return 0;
}
A.1, 0, 1 B. 1, 1, 1
C. 0, 0, 0 D.0, 1, 0

11. What will be the output of the program?

#include<stdio.h>
int main()
{
int i=4, j=-1, k=0, w, x, y, z;
w = i || j || k;
x = i && j && k;
y = i || j &&k;
z = i && j || k;
printf("%d, %d, %d, %d\n", w, x, y, z);
return 0;
}
A.1, 1, 1, 1 B. 1, 1, 0, 1
C. 1, 0, 0, 1 D.1, 0, 1, 1

12. What will be the output of the program?

#include<stdio.h>
int main()
{
int i=-3, j=2, k=0, m;
m = ++i && ++j || ++k;
printf("%d, %d, %d, %d\n", i, j, k, m);
return 0;
}
A.1, 2, 0, 1 B. -3, 2, 0, 1
C. -2, 3, 0, 1 D.2, 3, 1, 1

Page 4
13. What will be the output of the program?

#include<stdio.h>
int main()
{
int x=4, y, z;
y = --x;
z = x--;
printf("%d, %d, %d\n", x, y, z);
return 0;
}
A.4, 3, 3 B. 4, 3, 2
C. 3, 3, 2 D.2, 3, 3

14. What will be the output of the program?

#include<stdio.h>
int main()
{
int x=55;
printf("%d, %d, %d\n", x<=55, x=40, x>=10);
return 0;
}
A.1, 40, 1 B. 1, 55, 1
C. 1, 55, 0 D.1, 1, 1

15. What will be the output of the program?

#include<stdio.h>
int main()
{
int i=2;
printf("%d, %d\n", ++i, ++i);
return 0;
}
A.3, 4
B. 4, 3
C. 4, 4
D.Output may vary from compiler to compiler

16. What will be the output of the program?


#include<stdio.h>
int main()
{
int k, num=30;

Page 5
k = (num>5 ? num <=10 ? 100 : 200: 500);
printf("%d\n", num);
return 0;
}
A.200 B. 30
C. 100 D.500

17. What will be the output of the program?

#include<stdio.h>
int main()
{
char ch;
ch = 'A';
printf("The letter is");
printf("%c", ch >= 'A' && ch <= 'Z' ? ch + 'a' - 'A':ch);
printf("Now the letter is");
printf("%c\n", ch >= 'A' && ch <= 'Z' ? ch : ch + 'a' - 'A');
return 0;
}
The letter is a The letter is A
A. B.
Now the letter is A Now the letter is a
C. Error D.None of above

18. What will be the output of the program?

#include<stdio.h>
int main()
{
int i=20;
int j = i + (1, 2, 3, 4, 5);
printf("%d\n", j);
return 0;
}

A.21 B. 25
C. Garbage value D.error

19. Associativity has no role to play unless the precedence of operator is same.

A.True B.False

20. The expression of the right hand side of || operators doesn't get evaluated if the left hand side
determines the outcome.

Page 6
A.True B.False

21. In the expression a=b=c=50 the order of Assignment is NOT decided by Associativity of
operators

A.True B.False

22. Associativity of an operator is either Left to Right or Right to Left.

A.True B.False

Page 7
Declarations & Initializations
1. Which of the following statements should be used to obtain a remainder after dividing 3.5 by
2.5 ?
A. rem = 3.5 % 2.5;
B. rem = modf(3.5, 2.5);
C. rem = fmod(3.5, 2.5);
D. Remainder cannot be obtain in floating point division.

2. What are the types of linkages?

A.Internal and External B. External, Internal and None


C. External and None D.Internal

3. Which of the following special symbol allowed in a variable?


A.* (asterisk) B. $ (Dollar)
C. # (Hash) D._ (underscore)

4. Is there any difference between following declarations?

1 : extern int f1();


2 : int f1();
A.Both are identical
B. No difference, except extern int f1(); is probably in another file
C. int f1(); is overrided with extern int f1();
D.None of these

5. How would you round off a value from 2.75 to 3.0?

A.ceil(2.75) B. floor(2.75)
C. roundup(2.75) D.roundto(2.75)

6. By default a real number is treated as a

A.float B. double
C. long double D.far double

7. Which of the following is not user defined data type?

1 : struct book
{
char name[10];
float price;
int pages;

Page 8
};

long int l = 2L;


2:
3: enum day {Sun, Mon, Tue, Wed};

4: union employee
{
int id;
Char name[36];
int sal;
};

A. 1 B. 2 C. 3 D. 4

8. Is the following statement a declaration or definition?


extern int i;

A.Declaration B. Definition &Defination


C. Function D. Defination

9. Identify which of the following are declarations


1 : extern int exe;
2 : void square ( float x ) { ... }
3 : double pow(double, double);
A.1 B. 2
C. 1 and 3 D.3

10. In the following program where is the variable a getting defined and where it is getting
declared?
#include<stdio.h>
int main()
{
extern int a;
printf("%d\n", a);
return 0;
}
int a=10;
A.extern int a is declaration, int a = 10 is the definition
B. int a = 10 is declaration, extern int a is the definition
C. int a = 10 is definition, a is not defined
D.a is declared, a is not defined

11. When we mention the prototype of a function?


A.Defining B. Declaring
C. Prototyping D. invoking

Page 9
12. What is the output of the program
#include<stdio.h>
int main()
{
enum status { pass, fail, atkt};
enum status stud1, stud2, stud3;
stud1 = pass;
stud2 = atkt;
stud3 = fail;
printf("%d, %d, %d\n", stud1, stud2, stud3);
return 0;
}
A.0, 2, 1 B. 1, 2, 3
C. error D. 0,0,0

13. What will be the output of the program?


#include<stdio.h>
int main()
{
extern int i;
i = 20;
printf("%d\n", i);
return 0;
}
A.20
B. Garbage value
C. Compiler error
D.Linker Error : Undefined symbol 'i'

14. What is the output of the program?

#include<stdio.h>
int main()
{
extern int exe;
printf("%d\n", exe);
return 0;
}
int exe=10;
A.10 B. 0
C. Garbage Value D.Linker Error

15. What is the output of the program

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

Page 10
char *s1;
char far *s2;
char huge *s3;
printf("%d, %d, %d\n", sizeof(s1), sizeof(s2), sizeof(s3));
return 0;
}
A.2, 4, 6 B. 4, 4, 2
C. 2, 4, 4 D.2, 2, 2

16. What is the output of the program

#include<stdio.h>
int main()
{
struct emp
{
char name[20];
int age;
float sal;
};
struct emp e = {"Tiger"};
printf("%d, %f\n", e.age, e.sal);
return 0;
}
A.0, 0.000000 B. Garbage values
C. Error D.None of above

17. What will be the output of the program?


#include<stdio.h>
int X=10;
int main()
{
int X=20;
printf("%d\n", X);
return 0;
}
A. 20 B. 10
Error due to conflict between local and global
C. 0 D.
variable

18. #include<stdio.h>
int main()
{
int x = 10, y = 20, z = 5, i;
i = x < y < z;

Page 11
printf("%d\n", i);
return 0;
}
A.0 B. 1
C. Error D. Garbage value

19. What is the output of the program

#include<stdio.h>
int main()
{
extern int fun(float);
int a;
a = fun(3.14);
printf("%d\n", a);
return 0;
}
int fun(aa)
float aa;
{
return ((int)aa);
}
A.3 B. 3.14
C. 0 D.Error

20. What is the output of the program

#include<stdio.h>
int main()
{
int a[5] = {1,2};
printf("%d, %d, %d\n", a[2], a[3], a[4]);
return 0;
}
A.Garbage Values B. 2, 3, 3
C. Array should be completely initialized D.0, 0, 0

21. What is the output of the program

#include<stdio.h>
int main()
{
union a
{
int i;
char ch[2];
};

Page 12
union a u;
u.ch[0] = 3;
u.ch[1] = 2;
printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
return 0;
}
A.3, 2, 515 B. 515, 2, 3
C. 3, 2, 5 D. Garbage values

22. In the following program how long will the for loop get executed?

#include<stdio.h>
int main()
{
int i;
for(;scanf("%s", &i); printf("%d\n", i));
return 0;
}
A.The for loop would not get executed at all
B. The for loop would get executed only once
C. The for loop would get executed 5 times
D.The for loop would get executed infinite times

23. What will be the output of the program?

#include<stdio.h>
int main()
{
int X=40;
{
int X=20;
printf("%d ", X);
}
printf("%d\n", X);
return 0;
}
A.40 40 B. 20 40
C. 20 20 D.Error

24. Point out the error in the following program.

#include<stdio.h>
int main()
{
display();
return 0;
}

Page 13
void display()
{
printf("Disp fun");
}
A.No error
B. Disp fun
C. Compilation error: Type mismatch in redeclaration of function display()
D.None of these

25. Point out the error in the following program.

#include<stdio.h>
int main()
{
void v = 0;

printf("%d", v);

return 0;
}
A.Error: Declaration syntax error 'v' (or) Size of v is unknown or zero.
B. Program terminates abnormally.
C. No error.
D.None of these.

26. Point out the error in the following program.

#include<stdio.h>
struct emp
{
char name[20];
int age;
};
int main()
{
emp int xx;
int a;
printf("%d\n", &a);
return 0;
}
A.Error: in printf B. Error: in emp int xx;
C. No error. D.None of these.

Answer: Option B

Explanation:

Page 14
There is an error in the line emp int xx;

To overcome this error, remove the int and add the struct at the begining of emp int xx;

#include<stdio.h>
struct emp
{
char name[20];
int age;
};
int main()
{
struct emp xx;
int a;
printf("%d\n", &a);
return 0;
}

27. Which of the following is correct about err used in the declaration given below?

typedef enum error { warning, test, exception } err;


A.It is a typedef for enum error.
B. It is a variable of type enum error.
C. The statement is erroneous.
D.It is a structure.

28. Point out the error in the following program.

#include<stdio.h>
int main()
{
int (*p)() = fun;
(*p)();
return 0;
}
int fun()
{
printf("Ind Test\n");
return 0;
}
A.Error: in int(*p)() = fun;
B. Error: fun() prototype not defined
C. No error
D.None of these

Page 15
29. Which of the definition is correct?
A.int len; B. char int;
C. int long; D.float double;

30. Which of the following operations are INCORRECT?

A. int i = 35; i = i%5;


B. short int j = 255; j = j;
C. long int k = 365L; k = k;
D. float a = 4.14; a = a%3;

31. Which of the following correctly represents a double constant?

A.6.68 B. 6.68L
C. 6.68f D.6.68LF

32. Which of the structure is incorrcet?


struct aa
{
int a=10;
1:
float b=3.14f;
};

struct aa
{
int a;
2:
float b;
struct aa var;
};
struct aa
{
int a;
3:
float b;
struct aa *var;
};
A.1 B. 2
C. 3 D. None of the above

Answer: Option D

Explanation:

Page 16
1. In 1st structure there is a compilation error, we cannot assign values in structures for
structure members so 1st structure is invalid
2. Structure cannot conation variable of same type so 2nd structure is invalid.

33. which of the following are correct?

typedef long a;
1:
extern int a c;
typedef long a;
2:
extern a int c;
typedef long a;
3:
extern a c;
A.1 correct B. 2 correct
C. 3 correct D.1, 2, 3 are correct

Answer: Option C

Explanation:

typedef long a;
extern int a c; while compiling this statement becomes extern int long c;. This will result in to
"Declaration syntax error".

typedef long a;
extern a int c; while compiling this statement becomes extern long int c;. This will result in to
"Too many types in declaration error".

typedef long a;
extern a c; while compiling this statement becomes extern long c;. This is a valid c declaration
statement. It says variable c is long data type and defined in some other file or module.

So, Option C is the correct answer.

34. A long double can be used if range of a double is not enough to accommodate a real number.

A.False B.True

Answer: Option B

Explanation:

Page 17
35. A float is 4 bytes wide, whereas a double is 8 bytes wide. long double is 10 byte wide

A.True B.False

Answer: Option A

Explanation:

True,
float = 4 bytes.
double = 8 bytes.

long double=10 bytes .

36. If the definition of the external variable occurs in the source file before its use in a particular
function, then there is no need for an extern declaration in the function.

A.True B.False

Answer: Option A

Explanation:

True, When a function is declared inside the source file, that function(local function) get a
priority than the extern function. So there is no need to declare a function as extern inside the
same source file.

37. Size of short integer and long integer can be verified using the sizeof() operator.

A.True B.False
Answer: Option A
Explanation:
True, we can find the size of short integer and long integer using the sizeof() operator.

Example:

#include<stdio.h>
int main()
{
printf("short int is %d bytes.,\nlong int is %d bytes.",
sizeof(short),sizeof(long));
return 0;
}

Page 18
Output:
short int is 2 bytes.
long int is 4 bytes.

38. Range of double is -1.7e-308 to 1.7e+308

A.True B.False

Answer: Option A

Explanation:

False, The range of double is -1.7e-308 to 1.7e+308.

39. Size of short integer and long integer would vary from one platform to another.

A.True B.False

40. Range of float id -2.25e-38 to 2.25e+38

A.True B.False

41. Is there any difference int the following declarations?


int myfun(int a[]);
int myfun(a[20]);

A.Yes B.No

42. Suppose a program is divided into three files f1, f2 and f3, and a variable is defined in the file
f1 but used in files f2 and f3. In such a case would we need the extern declaration for the
variables in the files f2 and f3?

A.Yes B.No

43. Global variable are available to all functions. Does there exist a mechanism by way of which
it available to some and not to others.

A.Yes B.No

44. Is it true that a global variable may have several declarations, but only one definition?

A.Yes B.No

45. Is it true that a function may have several declarations, but only one definition?

A.Yes B.No

Page 19
Control Instructions
1.How many times "Ind Test" is get printed?

#include<stdio.h>
int main()
{
int x;
for(x=-1; x<=10; x++)
{
if(x < 5)
continue;
else
break;
printf("Ind Test ");
}
return 0;
}
A.Infinite times B. 11 times
C. 0 times D.10 times

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

A.I Love U B. I hate U C. no output D.none of the above

3. How many times the while loop will get executed if a short int is 2 byte wide?

#include<stdio.h>
int main()
{
int j=1;
while(j <= 256)
{
printf("%c %d\n", j, j);
j++;

Page 20
}
return 0;
}
A.Infinite times B. 255 times
C. 256 times D.254 times

3. Which of the following is not logical operator?

A.& B. &&
C. || D.!
4.void main()
{
int i=3;
switch(i)
{
default:printf("zero");
case 1: printf("one");
break;
case 2:printf("two");
break;
case 3: printf("three");
break;
}
}
05.void main()
{
printf("%x",-1<<4);
}
A. ffff B. fffo C. -1 D.

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

A. -2 B. 2 C. 0 D. Error
06.void main()
{
int i=10;
i=!i>14;
Printf ("i=%d",i);
}
A. 10 B. 1 C. 0 D. 14

Page 21
07.. Which is the correct order of mathematical operators ?

A.Addition, Subtraction, Multiplication, Division


B. Division, Multiplication, Addition, Subtraction
C. Multiplication, Addition, Division, Subtraction
D.Addition, Division, Modulus, Subtraction

8. Which of the following cannot be checked in a switch-case statement?


A.Character B. Integer
C. Float D.enum

09.void main()
{
printf("\nab");
printf("\bsi");
printf("\rha");
}

A.absiha B.hai C.garbage value D.Error

10. void main()


{
int i=5;
printf("%d%d%d%d%d%d",i++,i--,++i,--i,i);
}
A. 5 5 5 5 5 B. 4 4 4 4 4 C. 4 5 5 4 5 D. 5 6 4 3 3

11. What will be the output of the program?

#include<stdio.h>
int main()
{
int i=0;
for(; i<=5; i++);
printf("%d,", i);
return 0;

}
A.0, 1, 2, 3, 4, 5 B. 5
C. 1, 2, 3, 4 D.6

12. void main()


{
int i=400,j=300;
printf("%d..%d");
}

Page 22
A.garbage value garbage value B.400..300 C.0..0
D.Error

13. What will be the output of the program?

#include<stdio.h>
int main()
{
char str[]="C-program";
int a = 5;
printf(a >1?"Ind Test\n":"%s\n", str);
return 0;
}
A.C-program B. Ind Test
C. Error D.None of above

14. Will the following code compile


void main()
{
int i=1;
while (i<=5)
{
printf("%d",i);
if (i>2)
goto here;
i++;
}
}
fun()
{
here:
printf("PP");
}
A. Yes B. No

15. What will be the output of the program?

#include<stdio.h>
int main()
{
int a = 50, b = 10, c;
if(!a >= 400)
b = 30;
c = 200;
printf("b = %d c = %d\n", b, c);
return 0;
}

Page 23
A.b = 30 c = 200 B. b = 10 c = garbage
C. b = 30 c = garbage D.b = 10 c = 200

16. void main()


{

int i=5;
printf("%d",i++ + ++i);
}

17.Will the following code compile


void main()
{
int i=5;
printf("%d",i+++++i);
}

A. Yes B. No
18. #include<stdio.h>
main()
{
int i=1,j=2;
switch(i)
{
case 1: printf("GOOD");
break;
case j: printf("BAD");
break;
}
}
A. BAD B.GOOD C.GOODBAD D.error
19. Will the following code compile
void main()
{
int i;
printf("%d",scanf("%d",&i)); // value 10 is given as input here
}

A. Yes B. No

20. What will be the output of the program?

#include<stdio.h>
int main()
{
unsigned int i = 65535; /* Assume 2 byte integer*/

Page 24
while(i++ >= 0)
printf("%d",i);
printf("\n");
return 0;
}
A.Infinite loop
B. 0 1 2 ... 65535
C. 0 1 2 ... 32767 - 32766 -32765 -1 0
D.No output

21.void main()
{
int i=0;
for(;i++;printf("%d",i)) ;
printf("%d",i);
}

A. 0 B.1 C. infinite loop D.error

22. What will be the output of the program?

#include<stdio.h>
int main()
{
int x = 3;
float y = 3.0;
if(x == y)
printf("I love you");
else
printf("I hate you");
return 0;
}
A.I love you B. I hate you
C. Unpredictable D.No output

23. What will be the output of the program?

#include<stdio.h>
int main()
{
char ch;
if(ch = printf(""))
printf("It matters\n");
else
printf("It doesn't matters\n");
return 0;
}

Page 25
A.It matters B. It doesn't matters
C. matters D.No output
24.void main( )
{
char *q;
int j;
for (j=0; j<3; j++) scanf(%s ,(q+j));
for (j=0; j<3; j++) printf(%c ,*(q+j));
for (j=0; j<3; j++) printf(%s ,(q+j));
}

25. What will be the output of the program?

#include<stdio.h>
int main()
{
float a = 0.7;
if(0.7 > a)
printf("Hi if\n");
else
printf("Hello else\n");
return 0;
}
A.Hi if B. Hello else
C. Hi if Hello else D.None of above

26.Will the following code compile


void main()
{
int k=1;
printf("%d==1 is ""%s",k,k==1?"TRUE":"FALSE");
}
A. Yes B. No

27. The program is used to check whether the given year is leap or not
void main()
{
int y;
scanf("%d",&y); // input given is 2000
if( (y%4==0 && y%100 != 0) || y%100 == 0 )
printf("%d is a leap year");
else
printf("%d is not a leap year");
}
A. Yes B. No

28. What will be the output of the program?

Page 26
#include<stdio.h>
int main()
{
int a=0, b=1, c=3;
*((a) ? &b : &a) = a ? b: c;
printf("%d, %d, %d\n", a, b, c);
return 0;
}
A.0, 1, 3 B. 1, 2, 3
C. 3, 1, 3 D.1, 3, 1

29. What will be the output of the program?

#include<stdio.h>
int main()
{
int i = 5;
while(i-- >= 0)
printf("%d,", i);
i = 5;
printf("\n");
while(i-- >= 0)
printf("%i,", i);
while(i-- >= 0)
printf("%d,", i);
return 0;
}

5, 4, 3, 2, 1, 0
4, 3, 2, 1, 0, -1
A. B. 5, 4, 3, 2, 1, 0
4, 3, 2, 1, 0, -1

5, 4, 3, 2, 1, 0
5, 4, 3, 2, 1, 0
C. Error D.
5, 4, 3, 2, 1, 0

30.Will the following code compilemain()


{
int i=-1;
-i;
printf("i = %d, -i = %d \n",i,-i);
}

A. Yes B. No
31.Will the following code compile
#include<stdio.h>
void main()

Page 27
{
const int i=4;
float j;
j = ++i;
printf("%d %f", i,++j);
}

A. Yes B. No

32. Will the following code compile


void main()
{
int i=-1;
-i;
printf("i = %d, -i = %d \n",i,-i);
}

B. Yes B. No

33.Will the following code compile


#include<stdio.h>
void main()
{
const int i=4;
float j;
j = ++i;
printf("%d %f", i,++j);
}

B. Yes B. No

34. What will be the output of the program?

#include<stdio.h>
int main()
{
int i=3;
switch(i)
{
case 1:
printf("Hello\n");
case 2:
printf("Hi\n");
case 3:
printf("Bye\n");
default:

Page 28
continue;

}
return 0;
}
A.Error: Misplaced continue B. Bye
C. No output D.Hello Hi

35. What will be the output of the program?


#include<stdio.h>
int main()
{
int x = 1, y = 2;
if(!(!x) && x)
printf("x = %d\n", x);
else
printf("y = %d\n", y);
return 0;
}
A.y =20 B. x = 0
C. x = 1 D.x = 1
36. void main()
{
int i=5,j=6,z;
printf("%d",i+++j);
}

37. What will be the output of the program?

#include<stdio.h>
int main()
{
int i=4;
switch(i)
{
default:
printf("This is default\n");
case 1:
printf("This is case 1\n");

case 2:
printf("This is case 2\n");
break;
case 3:
printf("This is case 3\n");
}
return 0;

Page 29
}
This is default
This is case 1 This is case 3
A. B.
This is case 2 This is default

This is case 1
C. D.This is default
This is case 3

38. void main()


{
int i =0;j=0;
if(i && j++)
printf("%d..%d",i++,j);
printf("%d..%d,i,j);
}

A. 0..0 B. 0..1 C. 1..1 D.error

39.
void main(){
int a= 0;int b = 20;char x =1;char y =10;
if(a,b,x,y)
printf("hello");
}
A.hello B.nooutput C.error D.none of the above

40. What will be the output of the program?

#include<stdio.h>
int main()
{
int i = 1;
switch(i)
{
printf("Hello\n");
case 1:
printf("Hi\n");
break;
case 2:
printf("\nBye\n");
break;
}
return 0;
}
Hello
Hello
A. B. Bye
Hi

Page 30
C. Hi D.Bye

41. Will the following code compile


void main()
{
int i=-1;
+i;
printf("i = %d, +i = %d \n",i,+i);
}

A. Yes B. No

42. What will be the output of the program?

#include<stdio.h>
int main()
{
char j=1;
while(j < 5)
{
printf("%d, ", j);
j = j+1;
}
printf("\n");
return 0;
}
A.1 2 3 ... 127
B. 1 2 3 ... 255
C.1 2 3 ... 127 128 0 1 2 3 ... infinite times
D.1, 2, 3, 4

43. What will be the output of the program?

#include<stdio.h>
int main()
{
int x, y, z;
x=y=z=1;
z = ++x || ++y && ++z;
printf("x=%d, y=%d, z=%d\n", x, y, z);
return 0;
}
A.x=2, y=1, z=1 B. x=2, y=2, z=1
C. x=2, y=2, z=2 D.x=1, y=2, z=1

Page 31
44. Point out the error, if any in the for loop.

#include<stdio.h>
int main()
{
int i=1;
for(;;)
{
printf("%d\n", i++);
if(i>10)
break;
}
return 0;
}
A.There should be a condition in the for loop
B. The two semicolons should be dropped
C. The for loop should be replaced with while loop.
D.No error

45.. #include<stdio.h>
int main()
{
int a = 1000;
switch(a)
{
}
printf("This is c program.");
return 0;
}
A.Error: No case statement specified
B. Error: No default specified
C. No Error
D.Error: infinite loop occurs

46. Point out the error, if any in the program.

#include<stdio.h>
void main()
{
int i ;
i=1;
switch(i)
{
printf("This is c program.");
case 1:
printf("Case1");

Page 32
break;
case 2:
printf("Case2");
break;
}
}
A.Error: No default specified
B. Error: Invalid printf statement after switch statement
C. No Error and prints "Case1"
D.None of above

47. Point out the error, if any in the while loop.

#include<stdio.h>
int main()
{
int i=0;
while()
{
printf("%d\n", i++);
if(i>10)
break;
}
return 0;
}
A.There should be a condition in the while loop
B. There should be at least a semicolon in the while
C. The while loop should be replaced with for loop.
D.No error

48. Which of the following errors would be reported by the compiler on compiling the program
given below?

#include<stdio.h>
int main()
{
int a = 4;
switch(a)
{
case 1:
printf("First");

case 2:
printf("Second");

case 3 + 1:

Page 33
printf("Third");

case 4:
printf("Final");
break;

}
return 0;
}
A.There is no break statement in each case.
B. Expression as in case 3 + 1 is not allowed.
C. Duplicate case case 4:
D.No error will be reported.

49. Point out the error, if any in the program.

#include<stdio.h>
int main()
{
int j = 100;
switch(j)
{
case 10:
printf("Case 1");

case 20:
printf("Case 2");
break;

case j:
printf("Case j");
break;
}
return 0;
}
A.Error: No default value is specified
B. Error: Constant expression required at line case j:
C. Error: There is no break statement in each case.
D.No error will be reported.

50. Point out the error, if any in the program.

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

Page 34
int i = 10;
switch(i)
{
case 1:
printf("Case10");
break;
case 1*2+4:
printf("Case2");
break;
}
return 0;
}
A.Error: in case 1*2+4 statement
B. Error: No default specified
C. Error: in switch statement
D.No Error

51. Point out the error, if any in the while loop.

#include<stdio.h>
int main()
{
void fun();
int i = 1;
while(i <= 5)
{
printf("%d\n", i);
if(i>2)
goto here;
}
return 0;
}
void fun()
{
here:
printf("It works");
}
A.No Error: prints "It works"
B. Error: fun() cannot be accessed
C. Error: goto cannot takeover control to other function
D.No error

Answer: Option C

Explanation:

Page 35
goto is unconditional construct which transfer the control form one position to another position
unconditionally within the function. A label is used as the target of a goto statement, and that
label must be within the same function as the goto statement.

There are two type of jumps a) forward jump b) backward jump

Point out the error, if any in the program.

52. #include<stdio.h>
int main()
{
int a = 10, b;
a >=5 ? b=100: b=200;
printf("%d\n", b);
return 0;
}
A.100 B. 200
C. Error: L value required for b D.Garbage value

53. Which of the following statements are correct about the below program?

#include<stdio.h>
int main()
{
int i = 10, j = 20;
if(i = 5) && if(j = 10)
printf("Have a nice day");
return 0;
}
A.Output: Have a nice day
B. No output
C. Error: Expression syntax
D.Error: Undeclared identifier if

54. Which of the following statements are correct about the below program?

#include<stdio.h>
int main()
{
int i = 10, j = 15;
if(i % 2 = j % 3)
printf("IndiaBIX\n");
return 0;
}
A.Error: Expression syntax B. Error: Lvalue required

Page 36
C. Error: Rvalue required D.The Code runs successfully

55. Point out the correct statements are correct about the program below?

#include<stdio.h>
int main()
{
char x;
while(x=0;x<=255;x++)
printf("ASCII value of %d character %c\n", x, x);
return 0;
}
A.The code generates an infinite loop
B. The code prints all ASCII values and its characters
C. Error: x undeclared identifier
D.Error: while statement missing

56. Which of the following statements are correct about the below program?

#include<stdio.h>
int main()
{
int i = 0;
i++;
if(i++ <= 5)
{
printf("Ind Test\n");
exit();
main();
}
return 0;
}
A.The program prints ' Ind Test ' 5 times
B. The program prints ' Ind Test ' one time
C. The call to main() after exit() doesn't materialize.
D.The compiler reports an error since main() cannot call itself.

57. Which of the following statements are correct about the below C-program?

#include<stdio.h>
int main()
{
int x = 10, y = 100%90, i;
for(i=1; i<10; i++)

Page 37
if(x != y);
printf("x = %d y = %d\n", x, y);
return 0;
}
1 : The printf() function is called 10 times.
2 : The program will produce the output x = 10 y = 10
3 : The ; after the if(x!=y) will NOT produce an error.
4 : The program will not produce output.
A.1 B. 2, 3
C. 3, 4 D.4

58. The way the break is used to take control out of switch can continue to take control of the
beginning of the switch?

A.True B.False

59. Can we use a switch statement to switch on strings?

A.True B.False

60.We want to test whether a value lies in the range 1 to 3 or 9 to 11. Can we do this using a
switch?

A.True B.False

Page 38
Floating Point Concepts
1. What are the different types of real data type in C ?

A.float, double,long B. short int, double, long int,char


C. float, double, long double D.double, long int, float

2. What will you do to treat the constant 3.14 as a long double?

A.use 3.14LD B. use 3.14L


C. use 3.14DL D.use 3.14LF

3. Which statement will you add in the following program to work it correctly?

#include<stdio.h>
int main()
{
printf("%f\n", log(36.0));
return 0;
}
A.#include<conio.h> B. #include<math.h>
C. #include<stdlib.h> D.#include<dos.h>

4. What will be the output of the program?

#include<stdio.h>
int main()
{
float *p;
printf("%d\n", sizeof(p));
return 0;
}
A.2 in 16bit compiler, 4 in 32bit compiler
B. 4 in 16bit compiler, 2 in 32bit compiler
C. 4 in 16bit compiler, 4 in 32bit compiler
D.2 in 16bit compiler, 2 in 32bit compiler

5. What will be the output of the program?

#include<stdio.h>
int main()
{
float fval=7.987;

Page 39
printf("%d\n", (int)fval);
return 0;
}
A.0 B. 0.0
C. 7.0 D.7

6. What will be the output of the program?

#include<stdio.h>
#include<math.h>
int main()
{
printf("%f\n", sqrt(49.0));
return 0;
}
A.7.0 B. 7
C. 7.000000 D.Error: Prototype sqrt() not found.

Answer & Explanation

Answer: Option C

7. What will be the output of the program?

#include<stdio.h>
#include<math.h>
int main()
{
printf("%d, %d, %d\n", sizeof(3.14f), sizeof(3.14), sizeof(3.14l));
return 0;
}
A.4, 4, 4 B. 4, 8, 8
C. 4, 8, 10 D.4, 8, 12

8. What will be the output of the program?

#include<stdio.h>
int main()
{
float f=43.20;
printf("%e, ", f);
printf("%f, ", f);
printf("%g", f);
return 0;
}
A.4.320000e+01, 43.200001, 43.2 B. 4.3, 43.22, 43.21

Page 40
C. 4.3e, 43.20f, 43.00 D.Error

9. What will be the output of the program?

#include<stdio.h>
#include<math.h>
int main()
{
float n=1.54;
printf("%f, %f\n", ceil(n), floor(n));
return 0;
}
A.2.000000, 1.000000 B. 1.500000, 1.500000
C. 1.550000, 2.000000 D.1.000000, 2.000000

10. What will be the output of the program?

#include<stdio.h>
int main()
{
float d=2.25;
printf("%e,", d);
printf("%f,", d);
printf("%g,", d);
printf("%lf", d);
return 0;
}
A.2.2, 2.50, 2.50, 2.5
B. 2.2e, 2.25f, 2.00, 2.25
C. 2.250000e+000, 2.250000, 2.25, 2.250000
D.Error

Page 41
Functions
1. The keyword used to transfer control from a function back to the calling function is

A.switch B. goto
C. go back D.return

2.void main()
{
static int var = 5;
printf("%d ",var--);
if(var)
main();
}
3.void main()
{
extern int i;
i=20;
printf("%d",i);
}

4. What is the notation for following functions?

1. int f(int a, float b)


{
/* Some code */
}

2. int f(a, b)
int a; float b;
{
/* Some code */
}
1. KR Notation 1. Pre ANSI C Notation
A. B.
2. ANSI Notation 2. KR Notation
1. ANSI Notation 1. ANSI Notation
C. D.
2. KR Notation 2. Pre ANSI Notation

5.Will the following code compile


void main()
{
extern out;
printf("%d", out);
}
int out=100;
A. Yes B. No

Page 42
6. Will the following code compile
main()
{
show();
}
void show()
{
printf("I'm the greatest");

}
A. Yes B. No

7. How many times the program will print "Ind Test" ?

#include<stdio.h>

int main()
{
printf("Ind Test");
main();
return 0;
}
A.Infinite times B. 32767 times
C. 65535 times D.Till stack doesn't overflow

8.Will the following code compile


void main()
{
printf("%d", out);
}
int out=100;

A. Yes B. No

9. What will be the output of the program?

#include<stdio.h>
int main()
{
int fun();
int i;
i = fun();
printf("%d\n", i);
return 0;
}

Page 43
int fun()
{
_AX = 19;
}
A.Garbage value B. 0 (Zero)
C. 19 D.No output

10. Will the following code compile


void main()
{
main();
}

A. Yes B. No

11. What will be the output of the program?

#include<stdio.h>
void fun(int*, int*);
int main()
{
int i=5, j=2;
fun(&i, &j);
printf("%d, %d", i, j);
return 0;
}
void fun(int *i, int *j)
{
*i = *i**i;
*j = *j**j;
}
A.5, 2 B. 10, 4
C. 2, 5 D.25, 4
12. int i=10;
void main()
{
extern int i;
{
int i=20;
{
unsigned i=30;
printf("%d",i);
}
printf("%d",i);
}
printf("%d",i);
}

Page 44
A. 10 20 30 B. 30 20 10 C. 10 10 10 D. 20 20 20
13. What will be the output of the program?
#include<stdio.h>
int i;
int fun();
int main()
{
while(i)
{
fun();
main();
}
printf("Hello\n");
return 0;
}
int fun()
{
printf("Hi");
}
A.Hello B. Hi Hello
C. No output D.Infinite loop
14 #include<stdio.h>
void main()
{
register i=5;
char j[]= "hello";
printf("%s %d",j,i);
}

A. hello 5 B. 5 hello C. error D. none of the above

15. What will be the output of the program?

#include<stdio.h>
int reverse(int);
int main()
{
int no=5;
reverse(no);
return 0;
}
int reverse(int no)
{
if(no == 0)
return 0;
else
printf("%d,", no);

Page 45
reverse (no--);
}
A.Print 5, 4, 3, 2, 1 B. Print 1, 2, 3, 4, 5
C. Print 5, 4, 3, 2, 1, 0 D.Infinite loop
16.Will the following code compile
void main()
{
int i=_l_abc(10);
printf("%d\n",--i);
}
int _l_abc(int i)
{
return(i++);
}
A. Yes B. No

17. What will be the output of the program?


#include<stdio.h>
void fun(int);
int main()
{
int a=3;
fun(a);
return 0;
}
void fun(int n)
{
if(n > 0)
{
fun(--n);
printf("%d,", n);
fun(--n);
}
}
18. Will the code convert lower case to upper case
void main()
{
char c=' ',x,convert(z);
getc(c);

if((c>='a') && (c<='z'))


x=convert(c);
printf("%c",x);
}
convert(z)
{

Page 46
return z-32;
}

A. Yes B. No

19. void main(int argc, char **argv)


{
printf("enter the character");
getchar();
sum(argv[1],argv[2]);
}
sum(num1,num2)
int num1,num2;
{
return num1+num2;
}

Answer:
20. # include <stdio.h>
int one_d[]={1,2,3};
main()
{
int *ptr;
ptr=one_d;
ptr+=3;
printf("%d",*ptr);
}

A. 1 B.2 C.3 D. garbage value


21. # include<stdio.h>
aaa() {
printf("hi");
}
bbb(){
printf("hello");
}
ccc(){
printf("bye");
}
main()
{
int (*ptr[3])();
ptr[0]=aaa;
ptr[1]=bbb;
ptr[2]=ccc;
ptr[2]();
}

Page 47
A.hi B.hello C.bye D.error
22. Will the following code compile
main()
{
int i;
i = abc();
printf("%d",i);
}
abc()
{
_AX = 1000;
}

A. Yes B. No
23. void main()
{
extern int i;
i=20;
printf("%d",sizeof(i));
}

24. What will be the output of the program?

#include<stdio.h>
int sumdig(int);
int main()
{
int a, b;
a = sumdig(123);
b = sumdig(123);
printf("%d, %d\n", a, b);
return 0;
}
int sumdig(int n)
{
int s, d;
if(n!=0)
{
d = n%10;
n = n/10;
s = d+sumdig(n);
}
else
return 0;
return s;
}

Page 48
A. 4, 4 B. 3, 3 C. 6, 6 D. 12, 12

25. What will be the output of the program?


#include<stdio.h>
int main()
{
void fun(char*);
char a[100];
a[0] = 'A'; a[1] = 'B';
a[2] = 'C'; a[3] = 'D';
fun(&a[0]);
return 0;
}
void fun(char *a)
{
a++;
printf("%c", *a);
a++;
printf("%c", *a);
}
[A].AB [B]. BC
[C].CD [D].No output

26. What will be the output of the program?

#include<stdio.h>
int fun(int, int);
typedef int (*pf) (int, int);
int proc(pf, int, int);
int main()
{
printf("%d\n", proc(fun, 6, 6));
return 0;
}
int fun(int a, int b)
{
return (a==b);
}
int proc(pf p, int a, int b)
{
return ((*p)(a, b));
}
[A].6 [B]. 1
[C].0 [D].-1

27. What will be the output of the program?


#include<stdio.h>

Page 49
int main()
{
int i=1;
if(!i)
printf("Ind Test");
else
{
i=0;
printf("C-Program");
main();
}
return 0;
}
A.prints " Ind Test, C-Program" infinitely
B. prints "C-Program" infinetly
C. prints "C-Program, Ind Test " infinitely
D.Error: main() should not inside else statement

28. What will be the output of the program?


#include<stdio.h>

int main()
{
int i=3, j=4, k, l;
k = addmult(i, j);
l = addmult(i, j);
printf("%d %d\n", k, l);
return 0;
}
int addmult(int ii, int jj);
{
int kk, ll;
kk = ii + jj;
ll = ii * jj;
return (kk, ll);
}
A.Function addmult()return 7 and 12
B. No output
C. Error: Compile error
D.None of above

29. What will be the output of the program?


#include<stdio.h>
int i;
int fun1(int);
int fun2(int);

Page 50
int main()
{
extern int j;
int i=3;
fun1(i);
printf("%d,", i);
fun2(i);
printf("%d", i);
return 0;
}
int fun1(int j)
{
printf("%d,", ++j);
return 0;
}
int fun2(int i)
{
printf("%d,", ++i);
return 0;
}
int j=1;
A.3, 4, 4, 3 B. 4, 3, 4, 3
C. 3, 3, 4, 4 D.3, 4, 3, 4
30. What will be the output of the program?
#include<stdio.h>
int func1(int);

int main()
{
int k=35;
k = func1(k=func1(k=func1(k)));
printf("k=%d\n", k);
return 0;
}
int func1(int k)
{
k++;
return k;
}
A.k=35 B. k=36
C. k=37 D.k=38

31. What will be the output of the program?

#include<stdio.h>
int fun(int(*)());

Page 51
int main()
{
fun(main);
printf("Hi\n");
return 0;
}
int fun(int (*p)())
{
printf("Hello ");
return 0;
}
[A].Infinite loop [B]. Hi
[C].Hello Hi [D].Error

32. What will be the output of the program?


#include<stdio.h>
int main()
{
int fun(int);
int i=3;
fun(i=fun(fun(i)));
printf("%d\n", i);
return 0;
}
fun(int i)
{
i++;
return i;
}
A.5 B. 4
C. Error D.Garbage value

33.Point out the error in the program


f(int a, int b)
{
int a;
a = 20;
return a;
}
A.Missing parenthesis in return statement
B. The function should be defined as int f(int a, int b)
C. Redeclaration of a
D.None of above

34. Which of the following statements are correct about the program?
#include<stdio.h>

Page 52
int main()
{
printf("%p\n", main());
return 0;
}
A.It prints garbage values infinitely
B. Runs infinitely without printing anything
C. Error: main() cannot be called inside printf()
D.No Error and print nothing

35. There is a error in the below program. Which statement will you add to remove it?
#include<stdio.h>
int main()
{
int a;
a = f(10, 3.14);
printf("%d\n", a);
return 0;
}
float f(int aa, float bb)
{
return ((float)aa + bb);
}
A.Add prototype: float f(aa, bb)
B. Add prototype: float f(int, float)
C. Add prototype: float f(float, int)
D.Add prototype: float f(bb, aa)

36. Which of the following statements are correct about the function?
long fun(int num)
{
int i;
long f=1;
for(i=1; i<=num; i++)
f = f * i;
return f;
}
A.The function calculates the value of 1 raised to power num.
B. The function calculates the square root of an integer
C. The function calculates the factorial value of an integer
D.None of above

22. A function cannot be defined inside another function


A.True B.False

37. Functions cannot return more than one value at a time

Page 53
A.True B.False

Answer & Explanation

Answer: Option A

Explanation:

True, A function cannot return more than one value at a time. because after returning a value the
control is given back to calling function.

But if you want return multiple values from function return structure type.

37. If return type for a function is not specified, it defaults to int

A.True B.False

Answer & Explanation

Answer: Option A

Explanation:

True, The default return type for a function is int.

38. In C all functions except main() can be called recursively.

A.True B.False

Answer & Explanation

Answer: Option B

Explanation:

Any function including main() can be called recursively.

39. Functions can be called either by value or reference

A.True B.False

Answer & Explanation

Answer: Option A

Explanation:

Page 54
True, A function can be called either call by value or call by reference.

Example:

Call by value means c = sub(a, b); here value of a and b are passed.

Call by reference or address or pointer means c = sub(&a, &b); here address of a and b are
passed.

40. Names of functions in two different files linked together must be unique
A.True B.False

Answer & Explanation

Answer: Option A

Explanation:

True, If two function are declared in a same name, it gives "Error: Multiple declaration of
function_name())".

41. A function may have any number of return statements each returning different values.

A.True B.False

Answer: Option A
Explanation:

True, A function may have any number of return statements each returning different values and
each return statements will not occur successively.

Eg:
int fun(int a,int b)
{
if(a>b)
return a;
else
return b;
}

42. Names of functions in two different files linked together must be unique

A.True B.False

Answer & Explanation

Page 55
Answer: Option A

Explanation:

True, If two function are declared in a same name, it gives "Error: Multiple declaration of
function_name())".

Library Functions
1.What will the function rewind() do?
A.Reposition the file pointer to a character reverse.
B. Reposition the file pointer stream to end of file.
C. Reposition the file pointer to begining of that line.
D.Reposition the file pointer to begining of file.

Answer & Explanation

Answer: Option D

Explanation:

Page 56
rewind() takes the file pointer to the beginning of the file. so that the next I/O operation will take
place at the beginning of the file.
Example: rewind(FilePointer);

2. Input/output function prototypes and macros are defined in which header file?

A.conio.h B. stdlib.h
C. stdio.h D.dos.h

Answer & Explanation

Answer: Option C

Explanation:

stdio.h, which stands for "standard input/output header", is the header in the C standard library
that contains macro definitions, constants, and declarations of functions and types used for
various standard input and output operations.

3. Which standard library function will you use to find the last occurance of a character in a
string in C?

A.strnchar() B. strchar()
C. strrchar() D.strrchr()

Answer & Explanation

Answer: Option D

Explanation:

strrchr() returns a pointer to the last occurrence of character in a string.

Example:

#include <stdio.h>
#include <string.h>

int main()
{
char str[30] = "1234567891011121345567";
printf("The last position of '2' is %d.\n",
strrchr(str, '2') - str);
return 0;
}

Page 57
Output: The last position of '2' is 14.

4. Does there any function exist to convert the int or float to a string?

A.Yes B.No

Answer & Explanation

Answer: Option A

Explanation:

1. itoa() converts an integer to a string.


2. ltoa() converts a long to a string.
3. ultoa() converts an unsigned long to a string.
4. sprintf() sends formatted output to a string, so it can be used to convert any type of values to
string type.

#include<stdio.h>
#include<stdlib.h>

int main(void)
{
int num1 = 12345;
float num2 = 5.12;
char str1[20];
char str2[20];

itoa(num1, str1, 10); /* 10 radix value */


printf("integer = %d string = %s \n", num1, str1);

sprintf(str2, "%f", num2);


printf("float = %f string = %s", num2, str2);

return 0;
}

// Output:
// integer = 12345 string = 12345
// float = 5.120000 string = 5.120000

5. Can you use the fprintf() to display the output on the screen?

A.Yes B.No

Answer & Explanation

Page 58
Answer: Option A

Explanation:

Do like this fprintf(stdout, "%s %d %f", hello,35,10.23);

6. What will the function randomize() do?

A.returns a random number.


B. returns a random number generator in the specified range.
C. returns a random number generator with a random value based on time.
D.return a random number with a given seed value.

Answer & Explanation

Answer: Option C

Explanation:

The randomize() function initializes the random number generator with a random value based on
time. You can try the sample program given below in Turbo-C, it may not work as expected in
other compilers.

/* Prints a random number in the range 0 to 99 */

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main(void)
{
randomize();
printf("Random number in the 0-99 range: %d\n", random (100));
return 0;
}

7. What will be the output of the program?


#include<stdio.h>
int main()
{
int i;
i = printf("How r u\n");
i = printf("%d\n", i);
printf("%d\n", i);
return 0;
}

Page 59
How r u How r u
A.7 B. 8
2 2
How r u
C. 1 D.Error: cannot assign printf to variable
1

Answer & Explanation

Answer: Option B

Explanation:

In the program, printf() returns the number of charecters printed on the console

i = printf("How r u\n"); This line prints "How r u" with a new line character and returns the
length of string printed then assign it to variable i.
So i = 8 (length of '\n' is 1).

i = printf("%d\n", i); In the previous step the value of i is 8. So it prints "8" with a new line
character and returns the length of string printed then assign it to variable i. So i = 2 (length of
'\n' is 1).

printf("%d\n", i); In the previous step the value of i is 2. So it prints "2"

8. What will be the output of the program?

#include<stdio.h>
#include<stdlib.h>

int main()
{
char *i = "55.555";
int result1 = 10;
float result2 = 11.111;
result1 = result1+atoi(i);
result2 = result2+atof(i);
printf("%d, %f", result1, result2);
return 0;
}
A.55, 55.555 B. 66, 66.666600
C. 65, 66.666000 D.55, 55

Answer & Explanation

Answer: Option C

Page 60
Explanation:

Function atoi() converts the string to integer.


Function atof() converts the string to float.

result1 = result1+atoi(i);
Here result1 = 10 + atoi(55.555);
result1 = 10 + 55;
result1 = 65;

result2 = result2+atof(i);
Here result2 = 11.111 + atof(55.555);
result2 = 11.111 + 55.555000;
result2 = 66.666000;
So the output is "65, 66.666000"

9. What will be the output of the program?

#include<stdio.h>
#include<string.h>

int main()
{
char dest[] = {98, 98, 0};
char src[] = "bbb";
int i;
if((i = memcmp(dest, src, 2))==0)
printf("Got it");
else
printf("Missed");
return 0;
}
A.Missed B. Got it
C. Error in memcmp statement D.None of above

Answer & Explanation

Answer: Option B

Explanation:

memcmp compares the first 2 bytes of the blocks dest and src as unsigned chars. So, the ASCII
value of 98 is 'b'.

if((i = memcmp(dest, src, 2))==0) When comparing the array dest and src as unsigned chars, the
first 2 bytes are same in both variables.so memcmp returns '0'.
Then, the if(0=0) condition is satisfied. Hence the output is "Got it".

Page 61
10. What will function gcvt() do?

A.Convert vector to integer value


B. Convert floating-point number to a string
C. Convert 2D array in to 1D array.
D.Covert multi Dimensional array to 1D array

Answer & Explanation

Answer: Option B

Explanation:

The gcvt() function converts a floating-point number to a string. It converts given value to a null-
terminated string.

#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char str[25];
double num;
int sig = 5; /* significant digits */
/* a regular number */
num = 9.876;
gcvt(num, sig, str);
printf("string = %s\n", str);
/* a negative number */
num = -123.4567;
gcvt(num, sig, str);
printf("string = %s\n", str);
/* scientific notation */
num = 0.678e5;
gcvt(num, sig, str);
printf("string = %s\n", str);

return(0);
}

Output:
string = 9.876
string = -123.46
string = 67800

11. What will be the output of the program?

Page 62
#include<stdio.h>

int main()
{
int i;
char c;
for(i=1; i<=5; i++)
{
scanf("%c", &c); /* given input is 'a' */
printf("%c", c);
ungetc(c, stdin);
}
return 0;
}
A.aaaa B. aaaaa
C. Garbage value. D.Error in ungetc statement.

Answer & Explanation

Answer: Option B

Explanation:

for(i=1; i<=5; i++) Here the for loop runs 5 times.

Loop 1:
scanf("%c", &c); Here we give 'a' as input.
printf("%c", c); prints the character 'a' which is given in the previous "scanf()" statement.
ungetc(c, stdin); "ungetc()" function pushes character 'a' back into input stream.

Loop 2:
Here the scanf("%c", &c); get the input from "stdin" because of "ungetc" function.
printf("%c", c); Now variable c = 'a'. So it prints the character 'a'.
ungetc(c, stdin); "ungetc()" function pushes character 'a' back into input stream.

This above process will be repeated in Loop 3, Loop 4, Loop 5.

12. It is necessary that for the string functions to work safely the strings must be terminated with
'\0'.

A.True B.False

Answer & Explanation

Answer: Option A

Explanation:

Page 63
C string is a character sequence stored as a one-dimensional character array and terminated with
a null character('\0', called NULL in ASCII).
The length of a C string is found by searching for the (first) NULL byte.

13.The prototypes of all standard library string functions are declared in the file string.h.

A.Yes B.No

Answer & Explanation

Answer: Option A

Explanation:

string.h is the header in the C standard library for the C programming language which contains
macro definitions, constants, and declarations of functions and types used not only for string
handling but also various memory handling functions.

14. scanf() or atoi() function can be used to convert a string like "436" in to integer.

A.Yes B.No

Answer & Explanation

Answer: Option A

Explanation:

scanf is a function that reads data with specified format from a given string stream source.
scanf("%d",&number);

atoi() convert string to integer.


var number;
number = atoi("string");

15. Which header file should be included to use functions like malloc() and calloc()?

A.memory.h B. stdlib.h
C. string.h D.dos.h

Answer & Explanation

Answer: Option B

Explanation:

Page 64
Stdlib means standard library function. Which contains malloc(),calloc(),realloc(),free() function
declarations.

16. What will be the output of the program?

#include<stdio.h>
#include<stdlib.h>

int main()
{
int *p;
p = (int *)malloc(20); /* Assume p has address of 1318 */
free(p);
printf("%u", p);
return 0;
}
[A].1318 [B]. Garbage value
[C].1316 [D].Random address

Answer: Option A

Explanation:

free(p) means it deletes the memory pointed by p but not p so it prints 1318

17. What will be the output of the program?

#include<stdio.h>
#include<stdlib.h>

int main()
{
int *p;
p = (int *)malloc(20);
printf("%d\n", sizeof(p));
free(p);
return 0;
}
A.4 B. 2
C. 8 D.Garbage value

Answer & Explanation

Answer: Option B

Explanation:

Page 65
The sizeof any pointer on 16 bit dos is 2 bytes.
If it under gcc then it is 4 bytes.

18. What will be the output of the program?


#include<stdio.h>
int main()
{
char *s;
char *fun();
s = fun();
printf("%s\n", s);
return 0;
}
char *fun()
{
char buffer[30];
strcpy(buffer, "RAM");
return (buffer);
}
A.0xffff B. Garbage value
C. 0xffee D.Error

Answer & Explanation

Answer: Option B

Explanation:

The output is unpredictable since buffer is an auto array and will die when the control go back to
main. Thus s will be pointing to an array , which not exists. This is also called dangling pointer.

Page 66
Pointers
1.Can you combine the following two statements into one?
char *p;
p = (char*) malloc(10);
[A]. char p = *malloc(10);
[B]. char *p = (char) malloc(10);
[C]. char *p = (char*)malloc(10);
[D]. char *p = (char *)(malloc*)(10);
Answer: Option C

Explanation:
Malloc will allocate space dynamically by default it returns void* type so inorder to get character
pointer we need to typecast it char *.

2. what is the sizeof near, far and huge pointers in DOS?


A. near=2 far=4 huge=4 B. near=4 far=8 huge=8
C. near=2 far=4 huge=8 D. near=4 far=4 huge=4
Answer & Explanation
Answer: Option A
Explanation:
near=2, far=4 and huge=4 pointers exist only under DOS. Under windows and Linux every
pointers is 4 bytes long.

3. If a variable is a pointer to a structure, then which of the following operator is used to access
data members of the structure through the pointer variable?

Page 67
A. '.' B. '&'
C. '*' D. '->'
Answer & Explanation
Answer: Option D

Explanation: ->operator is pointer to structure member

4. void main()
{
int const * p=5;
printf("%d",++(*p));
}
A.6 B.5 C.ERROR D.GARBAGE VALUE
Answer:
Compiler error: Cannot modify a constant value.
Explanation:
p is a pointer to a "constant integer". But we tried to change the value of the
"constant integer".

5. What would be the equivalent pointer expression for referring the array element
a[i][j][k][l][m]

[A].((((a+i)+j)+k)+l) [B]. *(*(*(*(*(a+i)+j)+k)+l)+m


[C].(((a+i)+j)+k+l) [D].((a+i)+j+k+l)

Answer: Option B

Explanation:

For every array a const poiner with the array name will be created internally.
So arrays can be written as

a[i]---->*(a+i);---*(i+a)-- i[a]
a[i][j]---->*(*(a+i)+j);
a[i][j][k]---->*(*(*(a+i)+j)+k);

6. What will be the output of the program ?

#include<stdio.h>
int main()
{
int x=30, *y, *z;
y=&x; /* Assume address of x is 500 and integer is 4 byte size */
z=y;

Page 68
*y++=*z++;
x++;
printf("x=%d, y=%d, z=%d\n", x, y, z);
return 0;
}
[A].x=31, y=502, z=502 [B]. x=31, y=500, z=500
[C].x=31, y=498, z=498 [D].x=31, y=504, z=504

Answer: Option D

Explanation:

X=30
Y=&x(y<=500)
Z contains y that the address in y is copied to z(500)
*y++=*z++ means contents at *z copied to *y .after that z and y are incremented to 504
X++ means 30 is incrementd by 1 and becomes 31

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

A . -1 -1 0 2 garbage value B. 0 0 1 3 1 C.0 0 1 2 1 D. -1 -1 0 2 1


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.

7. What will be the output of the program If the integer is 4 bytes long?

#include<stdio.h>
int main()
{
int ***r, **q, *p, i=8;
p = &i;
q = &p;

Page 69
r = &q;
printf("%d, %d, %d\n", *p, **q, ***r);
return 0;
}
A.8, 8, 8 B. 4000, 4002, 4004
C. 4000, 4004, 4008 D.4000, 4008, 4016

Answer & Explanation

Answer: Option A

Explanation:

p is pointer to an integer so it contains addres of i

q is pointer to an integer pointer .so it conatins address of p

r is pointer to pointer to an integer pointer which stores address of q

8. What will be the output of the program ?

#include<stdio.h>
int main()
{
static char *s[] = {"black", "white", "pink", "violet"};
char **ptr[] = {s+3, s+2, s+1, s}, ***p;
p = ptr;
++p;
printf("%s", **p+1);
return 0;
}
A.ink B. ack
C. ite D.let

Answer & Explanation

Answer: Option A

Explanation:

s is array character pointer


s[0]=black and ptr[0]=violet
s[1]=white ptr[1]=pink
s[2]=pink ptr[2]=white
s[3]=violet ptr[3]=black

Page 70
here p=ptr means p,ptr represents same address
++p means pointing to second element in the ptr array that is it contains the address of pink
**p+1 =>> here **p means pink addres and +1 means pointing to ink so the output is ink.

9.void main()
{
char *p;
printf("%d %d ",sizeof(*p),sizeof(p));
}
Answer:
12

A. 1 2 B. 1 1 C. 2 2 D. error
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.

10. What will be the output of the program ?

#include<stdio.h>
int main()
{
char str[20] = "Hello";
char *const p=str;
*p='M';
printf("%s\n", str);
return 0;
}
A.Mello B. Hello
C. HMello D.MHello

Answer: Option A

Explanation:

Here p contains the base address.char *const p means p should not be modifed but *p can be
modified
So *p=M means H sholud be replaced by M.so out put is Mello

11. What will be the output of the program ?


#include<stdio.h>
void fun(void *p);
int i=100;

Page 71
int main()
{
void *vptr;
vptr = &i;
fun(vptr);
return 0;
}
void fun(void *p)
{
int **q;
q = (int**)&p;
printf("%d\n", **q);
}
A.Error: cannot convert from void** to int**
B. Garbage value
C. 100
D. 0

Answer: Option C

Explanation:

Here vptr contains the address of I after that,it is passed to function.the address of vptr(void **
type) is converted to (int**) and assigned to q. so **q will give the value of i that is 100

12. What will be the output of the program ?


#include<stdio.h>
int main()
{
char *str;
str = "%s";
printf(str, "K\n");
return 0;
}
A.Error B. No output
C. K D.%s

Answer & Explanation

Answer: Option C

Explanation:

Here the printf 1st arugment is replaced with %s


Prinf(%s,K\n); that is how the compiler is treated.

Page 72
So the output is K

13. What will be the output of the program?

#include<stdio.h>
int main()
{
int arr[2][2][2] = {10, 2, 3, 4, 5, 6, 7, 8};
int *p, *q;
p = &arr[1][1][1];
q = (int*) arr;
printf("%d, %d\n", *p, *q);
return 0;
}
[A].8, 10 [B]. 10, 2
[C].8, 1 [D].Garbage values

Answer: Option A

Explanation:

arr[1][1][1]=8,
*p,
p=&arr[1][1][1],
*p means value at address i.e=8.
(int*)arr always gives first value addres ,
o/p= 8 10

14. The program wont compile


void main()
{
clrscr();
}
clrscr();

A. Ture B.False

Answer:
B.false
No output/error
Explanation:
The first clrscr() occurs inside a function. So it becomes a function call. In the
second clrscr(); is a function declaration (because it is not inside any
function).

Page 73
15. What will be the output of the program assuming that the array begins at the location 1002
and size of an integer is 4 bytes?

#include<stdio.h>
int main()
{
int a[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
printf("%u, %u, %u\n", a[0]+1, *(a[0]+1), *(*(a+0)+1));
return 0;
}
[A].448, 4, 4 [B]. 520, 2, 2
[C].1006, 2, 2 [D].Error

Answer: Option C

Explanation:

The array begins at the location 1002 and each integer occupies 4bytes. a[0]+1 =(1002+1(is
nothing but 4 bytes)) = 1006

*(a[0]+1) means a[0][1] so 2

*(*(a+0)+1) means a[0][1] so 2

16.void main()
{
char *p;
p="Hello";
printf("%c\n",*&*p);
}

A. Hello B. H C. Garbage Value D.Error


Answer:
H
Explanation:
* is a dereference operator & is a reference operator. They can be applied
any number of times provided it is meaningful. Here p points to the first
character in the string "Hello". *p dereferences it and so its value is H. Again
& references it to an address and * dereferences it to the value H.

17. What will be the output of the program ?

#include<stdio.h>
int main()
{
char *str;
str = "%d\n";

Page 74
str++;
str++;
printf(str-2, 3000);
return 0;
}
A.No output B. 30
C. 3 D.3000

Answer & Explanation

Answer: Option D

Explanation:

str[0] str[1] str[2]


%d \n \0
500 501 502
Str contains 500
++str means 501
++str means 502
Str-2 menas 500
Printf(str-2,300)is treatd as printf(500 address,300)=>printf(%d\n,3000);
So the output is 3000

18.void main()
{
int *j;
{
int i=10;
j=&i;
}
printf("%d",*j);
}
A. 0 B. 10 C. Garbage Value D.error
Answer:
10
Explanation:
The variable i is a block level variable and the visibility is inside that block
only. But the lifetime of i is lifetime of the function so it lives upto the exit of
main function. Since the i is still allocated space, *j prints the value stored in i
since j points i.

19.What will be the output of the program ?

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

Page 75
printf("%c\n", 5["IndTest"]);
return 0;
}
A.Error: in printf B. Nothing will print
C. print "E" of IndTest D.print "5"

Answer & Explanation

Answer: Option C

Explanation:

a[i] can be written as i[a]

so 5["IndTest"] can be written as "IndTest"[5] that is value at 5th index

20. Will the following code compile


void main()
{
char *cptr,c;
void *vptr,v;
c=10; v=0;
cptr=&c; vptr=&v;
printf("%c%v",c,v);
}

A. Yes B. No

Answer:
B. No
Compiler error (at line number 4): size of v is Unknown.
Explanation:
You can create a variable of type void * but not of type void, since void is an
empty type. In the second line you are creating variable vptr of type void * and
v of type void hence an error.

21.What will be the output of the program ?

#include<stdio.h>
int main()
{
char str[] = "peace wolrd";
char *s = str;
printf("%s\n", s++ +3);
return 0;
}
A.Peace wolrd B. Eace world

Page 76
C. Ace world D.ce world

Answer & Explanation

Answer: Option D

Explanation:

S contains starting address of string so s++ +3 gives the addres from the thrid index so the
output is ce world

22.What will be the output of the program ?

#include<stdio.h>
int main()
{
char *p;
p="hell";
printf("%s\n", *&*&p);
return 0;
}
A.ll B. hell
C. ell D.h

Answer & Explanation

Answer: Option B

In printf * &*& will cancel out and it will be tread as printf(%s\n,p)=>printf(%s\n,hell);

23.void main()
{
char *p;
int *q;
long *r;
p=q=r=0;
p++;
q++;
r++;
printf("%p...%p...%p",p,q,r);
}

A . 0001...0002...0004 B.00020002.0002
C. 0001...0001...0001 D.0004...0004...0004
Answer:
A.0001...0002...0004
Explanation:

Page 77
++ operator when applied to pointers increments address according to their
corresponding data-types.

24.What will be the output of the program ?

#include<stdio.h>
power(int**);
int main()
{
int a=6, *aa; /* Address od 'a' is 1000 */
aa = &a;
a = power(&aa);
printf("%d\n", a);
return 0;
}
power(int **ptr)
{
int b;
b = **ptr***ptr;
return (b);
}
A.6 B. 36
C. 216 D.Garbage value

Answer & Explanation

Answer: Option B

Explanation:

aa = &a; // *aa = 6;
power(&aa) is of int** type
**ptr = &aa; ptr is a pointer to a pointer aa.

Hence value stored in a can be accessed using **ptr(which is **ptr = 6).

So the b = **ptr ***ptr means b = 6*6which is 36.

25. What will be the output of the program ?

#include<stdio.h>
int main()
{
int i, a[] = {2, 4, 6, 8, 10};
change(a, 5);
for(i=0; i<=4; i++)
printf("%d, ", a[i]);

Page 78
return 0;
}
change(int *b, int n)
{
int i;
for(i=0; i<n; i++)
*(b+i) = *(b+i)+5;
}
[A].7, 9, 11, 13, 15 [B]. 2, 15, 6, 8, 10
[C].2 4 6 8 10 [D].3, 1, -1, -3, -5

Answer: Option A

Explanation:

*(b+i) = *(b+i)+5;
1. i = 0 => *(b+1) ie 4 is replaced by *(b+0)+5 ie 2+5
2. i = 1 => *(b+1) ie 4 is replaced by *(b+1)+5 ie 4+5
3. i = 2 => *(b+1) ie 4 is replaced by *(b+2)+5 ie 6+5
4. i = 3 => *(b+1) ie 4 is replaced by *(b+3)+5 ie 8+5
5. i = 4 => *(b+1) ie 4 is replaced by *(b+4)+5 ie 10+5

26. Point out the error in the program

#include<stdio.h>
int main()
{
int a[] = {1, 2, 3, 4, 5};
int j;
for(j=0; j<5; j++)
{
printf("%d\n", a);
a++;
}
return 0;
}
A.Error: Declaration syntax B. Error: Expression syntax
C. Error: LValue required D.Error: Rvalue required

Answer & Explanation

Answer: Option C

Explanation:

Page 79
The error at statement a++ a is the array name treated as constatn pointer which should
not be modifed

27. Which of the following statements correctly declare a function that receives a pointer to
pointer to a pointer to a float and returns a pointer to a pointer to a pointer to a pointer to a float?

[A].float **fun(float***); [B]. float *fun(float**);


[C].float fun(float***); [D].float ****fun(float***);

Answer: Option D

Explanation:

From the given question,

"... receives a pointer to pointer to a pointer to a float ***

"... returns a pointer to a pointer to a pointer to a pointer...so the return type is float ****

Therefore, float ****fun (float ***) is the correct answer.

28. Which of the statements is correct about the program?

#include<stdio.h>

int main()
{
int i=10;
int *j=&i;
return 0;
}
A.j and i are pointers to an int
B. i is a pointer to an int and stores address of j
C. j is a pointer to an int and stores address of i
D.j is a pointer to a pointer to an int and stores address of i

Answer & Explanation

Answer: Option C

29. In the following program add a statement in the function fun() such that address of a gets
stored in j?

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

Page 80
int *j;
void fun(int**);
fun(&j);
return 0;
}
void fun(int **k)
{
int a=10;
/* Add a statement here */
}
A.**k=a; B. k=&a;
C. *k=&a D.&k=*a

Answer & Explanation

Answer: Option C

Explanation:
Here in the function call k conatins address of j
Where k is of int ** and contains js address so any modification done on k will be effected on j.
Inorder to get as address in j
Add *k=&a
Where *k int of int *type and &a is also int * type

30. Which of the statements is correct about the program?

#include<stdio.h>
int main()
{
int arr[3][3] = {1, 2, 3, 4};
printf("%d\n", *(*(*(arr))));
return 0;
}
A.Output: Garbage value B. Output: 1
C. Output: 3 D.Error: Invalid indirection

Answer & Explanation

Answer: Option D

Explanation:

Here arr is 2D array so at max we can go for pointer to pointer.


Here it is asked to print value at pointer to pointer to pointer

Page 81
31. Which statement will you add to the following program to ensure that the program outputs
"Ind Test" on execution?

#include<stdio.h>
int main()
{
char s[] = " Ind Test ";
char t[25];
char *ps, *pt;
ps = s;
pt = t;
while(*ps)
*pt++ = *ps++;

/* Add a statement here */


printf("%s\n", t);
return 0;
}
A.*pt=''; B. pt='\0';
C. pt='\n'; D.*pt='\0';

Answer & Explanation

Answer: Option D

Explanation:

In the loop the data is copied by character by character basis so null character is not copied so,
after the loop terminates t conations only Ind Test so explicitly null character should be
appended.

36. Are the expression *ptr++ and ++*ptr are same?

A.True B.False

Answer & Explanation

Answer: Option B

Explanation:

*ptr++ increments the pointer and not the value, whereas the ++*ptr increments the value being
pointed by ptr

37. The program will compile?

Page 82
#include<stdio.h>
int main()
{
char s[5] = "Ind Test";
return 0;
}
A.True B.False

Answer & Explanation

Answer: Option B

Explanation:

C doesn't do array bounds checking at compile time, hence this compiles.

But, the modern compilers like Turbo C++ detects this as 'Error: Too many initializers'.

GCC would give you a warning.

38. The following program reports an error on compilation.

#include<stdio.h>
int main()
{
float i=100, *j;
void *k;
k=&i;
j=k;
printf("%f\n", *j);
return 0;
}
A.True B.False

Answer & Explanation

Answer: Option B

Explanation:

This program will NOT report any error. (Tested in Turbo C under DOS and GCC under Linux)

The output: 100.000000

39. Are the three declarations char **a, char *a[], and char a[][] same?

A.True B.False

Page 83
Answer & Explanation

Answer: Option B

Explanation:

char **a is pointer to pointer to character

char *a[] is array of character pointer

char a[][] is 2D character array.

40. Is there any difference between the following two statements?


char *a=0;
char *b=NULL

A.Yes B.No

Answer & Explanation

Answer: Option B

Explanation:

NULL is #defined as 0 in the 'stdio.h' file. Thus, both a and b are NULL pointers

41. Is the NULL pointer same as an uninitialised pointer?

A.Yes B.No

Answer: Option B

Explanation:

A Null pointer is one which holds 0 (zero) as its value and uninitialized pointer may have some
garbage value.

Page 84
Arrays
1.void main()
{
int c[ ]={2.8,3.4,4,6.7,5};
int j,*p=c,*q=c;
for(j=0;j<5;j++) {
printf(" %d ",*c);
++q; }
for(j=0;j<5;j++){
printf(" %d ",*p);
++p; }
}
Answer:
2222223465

A. 2 3 4 6 5 5 5 5 5 5 B. 2 2 2 2 2 2 3 4 6 5 C. garbage value D. error


Explanation:
Initially pointer c is assigned to both p and q. In the first loop, since only q is
incremented and not c , the value 2 will be printed 5 times. In second loop p itself is
incremented. So the values 2 3 4 6 5 will be printed.

2.What does the following declaration mean?


int (*ptr)[10]

A.ptr is array of pointers to 10 integers


B. ptr is a pointer to an array of 10 integers
C. ptr is an array of 10 integers
D.ptr is an pointer to array

Answer & Explanation

Answer: Option B

Explanation:

If its is written as int *ptr[10] then it is treated as array of 10 pointer

Page 85
3. In C, if you pass an array as an argument to a function, what actually gets passed?

A.Value of elements in array


B. First element of the array
C. Base address of the array
D.Address of the last element of array

Answer & Explanation

Answer: Option C

Explanation:

The statement 'C' is correct. When we pass an array as a funtion argument, the base address of
the array will be passed.

Eg:
int fun(int*);
void main()
{
int a[5];
fun(a);//here it base addres of the array.
}
int fun(int *p1)
{
}

4. #include<stdio.h>
void main()
{
int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} };
int *p,*q;
p=&a[2][2][2];
*q=***a;
printf("%d----%d",*p,*q);
}
Answer:
SomeGarbageValue---1

A. 10 1 B. garbage value - -1 C. 10 5D. Error


Explanation:
p=&a[2][2][2] you declare only two 2D arrays, but you are trying to access the
third 2D(which you are not declared) it will print garbage values. *q=***a starting address of
a is assigned integer pointer. Now q is pointing to starting address of a. If you print *q, it will
print first element of 3D array.

Page 86
6. What will happen if in a C program you assign a value to an array element whose subscript
exceeds the size of array?

A.The element will be set to 0.


B. The compiler would report an error.
C. The program may crash if some important data gets overwritten.
D.The array size would appropriately grow.

Answer & Explanation

Answer: Option C

Explanation:

If the index of the array size is exceeded, the program will crash. Hence "option c" is the correct
answer.

Example: Run the below program, it will crash in Windows (TurboC Compiler)

#include<stdio.h>

int main()
{
int arr[10];
arr[100]=100;
printf("%d",arr[100]);
return 0;
}

In C there is no bounadary checking Since C is a compiler dependent language, it may give


different outputs at different platforms. We have given the Turbo-C Compiler (Windows) output.

Please try the above programs in Windows (Turbo-C Compiler) and Linux (GCC Compiler), you
will understand the difference better.

7. void main( )
{
int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}};//base address is 100
printf(%u %u %u %d \n,a,*a,**a,***a);
printf(%u %u %u %d \n,a+1,*a+1,**a+1,***a+1);
}
Answer:
100, 100, 100, 2
114, 104, 102, 3
Explanation:
The given array is a 3-D one. It can also be viewed as a 1-D array.
247834222334

Page 87
100 102 104 106 108 110 112 114 116 118 120 122
thus, for the first printf statement a, *a, **a give address of first element .
since the indirection ***a gives the value. Hence, the first line of the output.
for the second printf a+1 increases in the third dimension thus points to value at 114, *a+1
increments in second dimension thus points to 104, **a +1 increments the first dimension thus
points to 102 and ***a+1 first gets the value at first location and then increments it by 1. Hence,
the output.

8.void main( )
{
int a[ ] = {10,20,30,40,50},j,*p;
for(j=0; j<5; j++)
{
printf(%d ,*a);
a++;
}
p = a;
for(j=0; j<5; j++)
{
printf(%d ,*p);
p++;
}
}
A 10 20 30 40 50 10 20 30 40 50 B 10 20 30 40 50 C Error
D. None of the above.
Answer:
Compiler error: lvalue required.
Explanation:
Error is in line with statement a++. The operand must be an lvalue and may
be of any of scalar type for the any operator, array name only when
subscripted is an lvalue. Simply array name is a non-modifiable lvalue.

9.void main( )
{
static int a[ ] = {0,1,2,3,4};
int *p[ ] = {a,a+1,a+2,a+3,a+4};
int **ptr = p;
ptr++;
printf(\n %d %d %d, ptr-p, *ptr-a, **ptr);
*ptr++;
printf(\n %d %d %d, ptr-p, *ptr-a, **ptr);
*++ptr;
printf(\n %d %d %d, ptr-p, *ptr-a, **ptr);
++*ptr;
printf(\n %d %d %d, ptr-p, *ptr-a, **ptr);
}

Page 88
Answer:
111
222
333
344
Explanation:
Let us consider the array and the two pointers with some address
a
01234
100 102 104 106 108
p
100 102 104 106 108
1000 1002 1004 1006 1008
ptr
1000
2000
After execution of the instruction ptr++ value in ptr becomes 1002, if scaling
factor for integer is 2 bytes. Now ptr p is value in ptr starting location of
array p, (1002 1000) / (scaling factor) = 1, *ptr a = value at address
pointed by ptr starting value of array a, 1002 has a value 102 so the value
is (102 100)/(scaling factor) = 1, **ptr is the value stored in the location
pointed by the pointer of ptr = value pointed by value pointed by 1002 = value
pointed by 102 = 1. Hence the output of the firs printf is 1, 1, 1.
After execution of *ptr++ increments value of the value in ptr by scaling factor,
so it becomes1004. Hence, the outputs for the second printf are ptr p = 2,
*ptr a = 2, **ptr = 2.
After execution of *++ptr increments value of the value in ptr by scaling factor,
so it becomes1004. Hence, the outputs for the third printf are ptr p = 3, *ptr
a = 3, **ptr = 3.
After execution of ++*ptr value in ptr remains the same, the value pointed by
the value is incremented by the scaling factor. So the value in array p at
location 1006 changes from 106 10 108,. Hence, the outputs for the fourth
printf are ptr p = 1006 1000 = 3, *ptr a = 108 100 = 4, **ptr = 4.

10. What will be the output of the program ?

#include<stdio.h>
int main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf("%d, %d, %d", i, j, m);
return 0;
}

Page 89
A.2, 1, 15 B. 1, 2, 5
C. 3, 2, 15 D.2, 3, 20

Answer & Explanation

Answer: Option C

Explanation:

Step 1: int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size of
5 and it is initialized to
a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25 .
Step 2: int i, j, m; The variable i,j,m are declared as an integer type.
Step 3: i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2
Step 4: j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.
Step 5: m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++
so i=3)
Step 6: printf("%d, %d, %d", i, j, m); It prints the value of the variables i, j, m
Hence the output of the program is 3, 2, 15

11 void main()
{
char name[10],s[12];
scanf(" \"%[^\"]\"",s);
}
How scanf will execute?
Answer:
First it checks for the leading white space and discards it.Then it matches with
a quotation mark and then it reads all character upto another quotation mark.

12. What will be the output of the program if the array begins at 65486 and each integer occupies
2 bytes?

#include<stdio.h>
int main()
{
int arr[] = {12, 14, 15, 23, 45};
printf("%u, %u\n", arr+1, &arr+1);
return 0;
}
A.65488, 65490 B. 64490, 65492
C. 65488, 65496 D.64490, 65498

Answer & Explanation

Answer: Option C

Page 90
Explanation:

Step 1: int arr[] = {12, 14, 15, 23, 45}; The variable arr is declared as an integer array and
initialized.
Step 2: printf("%u, %u\n", arr+1, &arr+1);
Here, the base address(also the address of first element) of the array is 65486.
=> Here, arr is reference to arr has type "pointer to int". Therefore, arr+1 is pointing to second
element of the array arr memory location. Hence 65486 + 2 bytes = 65488
=> Then, &arr is "pointer to array of 5 ints". Therefore, &arr+1 denotes "5 ints * 2 bytes * 1 = 10
bytes".
Hence, begining address 65486 + 10 = 65496. So, &arr+1 = 65496
Hence the output of the program is 65486, 65496

13. #include<stdio.h>
void main()
{
int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} };
int *p,*q;
p=&a[2][2][2];
*q=***a;
printf("%d..%d",*p,*q);
}
Answer:
garbagevalue..1
Explanation:
p=&a[2][2][2] you declare only two 2D arrays. but you are trying to access the third 2D(which
you are not declared) it will print garbage values. *q=***a starting address of a is assigned
integer pointer. now q is pointing to starting address of a.if you print *q meAnswer:it will print
first element of 3D array.

14. What will be the output of the program ?


#include<stdio.h>
int main()
{
static int a[2][2] = {1, 2, 3, 4};
int i, j;
static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("%d, %d, %d, %d\n", *(*(p+i)+j), *(*(j+p)+i),
*(*(i+p)+j), *(*(p+j)+i));
}
}
return 0;

Page 91
}

1, 1, 1, 1 1, 2, 1, 2
2, 3, 2, 3 2, 3, 2, 3
A. B.
3, 2, 3, 2 3, 4, 3, 4
4, 4, 4, 4 4, 2, 4, 2

1, 1, 1, 1 1, 2, 3, 4
2, 2, 2, 2 2, 3, 4, 1
C. D.
2, 2, 2, 2 3, 4, 1, 2
3, 3, 3, 3 4, 1, 2, 3

Answer: Option C

Explanation:
step1: p[3]={a,a+1,a+2}
step2:i=0,j=0
*(*(p+i)+j)=a[0]=1
*(*(j+p)+i)=a[0]=1
*(*(i+p)+j)=a[0]=1
*(*(p+j)+i)=a[0]=1
step2:i=0,j=1
*(*(p+i)+j)=a[1]=2
*(*(j+p)+i)=a[1]=2
*(*(i+p)+j)=a[1]=2
*(*(p+j)+i)=a[1]=2
step3:i=1,j=0
*(*(p+i)+j)=a[1]=2
*(*(j+p)+i)=a[1]=2
*(*(i+p)+j)=a[1]=2
*(*(p+j)+i)=a[1]=2
step4:i=1,j=1
*(*(p+i)+j)=a[2]=3
*(*(j+p)+i)=a[2]=3
*(*(i+p)+j)=a[2]=3
*(*(p+j)+i)=a[2]=3

15. What will be the output of the program ?


#include<stdio.h>
int main()
{
static int arr[] = {0, 1, 2, 3, 4};
int *p[] = {arr, arr+1, arr+2, arr+3, arr+4};
int **ptr=p;
ptr++;
printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);

Page 92
*ptr++;
printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
*++ptr;
printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
++*ptr;
printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
return 0;
}
0, 0, 0 1, 1, 2
1, 1, 1 2, 2, 3
A. B.
2, 2, 2 3, 3, 4
3, 3, 3 4, 4, 1
1, 1, 1 0, 1, 2
2, 2, 2 1, 2, 3
C. D.
3, 3, 3 2, 3, 4
3, 4, 4 3, 4, 5

Answer & Explanation

Answer: Option C

16. What will be the output of the program ?

#include<stdio.h>

int main()
{
int arr[1]={100};
printf("%d\n", 0[arr]);
return 0;
}
A.1 B. 100
C. 0 D.6

Answer & Explanation

Answer: Option B

Explanation:

Step 1: int arr[1]={100}; The variable arr[1] is declared as an integer array with size '2' and it's
first element is initialized to value '10'(means arr[0]=100)

Step 2: printf("%d\n", 0[arr]); It prints the first element value of the variable arr.

Hence the output of the program is 100.

Page 93
17. What will be the output of the program?

#include<stdio.h>
int main()
{
float arr[] = {1.4, 0.3, 4.50, 6.70};
printf("%d\n", sizeof(arr)/sizeof(arr[0]));
return 0;
}
A.5 B. 4
C. 6 D.7

Answer & Explanation

Answer: Option B

Explanation:
The sizeof function return the given variable. Example: float a=10; sizeof(a) is 4 bytes
Step 1: float arr[] = {2.4, 0.3, 4.50, 6.70}; The variable arr is declared as an floating point array
and it is initialized with the values.
Step 2: printf("%d\n", sizeof(arr)/sizeof(arr[0]));
The variable arr has 4 elements. The size of the float variable is 4 bytes.
Hence 4 elements x 4 bytes = 16 bytes
sizeof(arr[0]) is 4 bytes
Hence 16/4 is 4 bytes
Hence the output of the program is '4'.

18. What will be the output of the program if the array begins 1200 in memory?
#include<stdio.h>
int main()
{
int a[]={2, 3, 4, 1, 6};
printf("%u, %u, %u\n", a, &a[0], &a);
return 0;
}
A.1200, 1202, 1204 B. 1200, 1200, 1200
C. 1200, 1204, 1208 D.1200, 1202, 1200

Answer & Explanation

Answer: Option B

Explanation:

Step 1: int a[]={2, 3, 4, 1, 6}; The variable a is declared as an integer array and initialized.
Step 2: printf("%u, %u, %u\n", a, &a[0], &a); Here,
The base address of the array is 1200.

Page 94
=> a, &a is pointing to the base address of the array a.
=> &a[0] is pointing to the address of the first element array a. (ie. base address)
Hence the output of the program is 1200, 1200, 1200

19. What will be the output of the program if the array begins at address 65486?
#include<stdio.h>
int main()
{
int a[] = {1, 2, 3, 4, 5};
printf("%u, %u\n", a, &a);
return 0;
}
A.65486, 65488 B. 65486, 65486
C. 65486, 65490 D.65486, 65487

Answer & Explanation

Answer: Option B

Explanation:
Step 1: int a[] = {12, 14, 15, 23, 45}; The variable a is declared as an integer array and
initialized.
Step 2: printf("%u, %u\n", a, a); Here,
The base address of the array is 65486.
=> a, &a is pointing to the base address of the array a.
Hence the output of the program is 65486, 65486

20. Which of the following is correct way to define the function fun() in the below program?
#include<stdio.h>
int main()
{
int a[3][4];
fun(a);
return 0;
}
void fun(int p[][4]) void fun(int *p[4])
[A].{ [B]. {
} }
void fun(int *p[][4]) void fun(int *p[3][4])
[C].{ [D].{
} }

Answer: Option A

Explanation:

Page 95
void fun(int p[][4]){ } is the correct way to write the function fun(). while the others are
considered only the function fun() is called by using call by reference.

21. Which of the following statements are correct about the program below?

#include<stdio.h>
int main()
{
int size, i;
scanf("%d", &size);
int arr[size];
for(i=1; i<=size; i++)
{
scanf("%d", arr[i]);
printf("%d", arr[i]);
}
return 0;
}
A.The code is erroneous since the subscript for array used in for loop is in the range 1 to size.
B. The code is erroneous since the values of array are getting scanned through the loop.
C. The code is erroneous since the statement declaring array is invalid.
D.The code is correct and runs successfully.

Answer & Explanation

Answer: Option C

Explanation:

The statement int arr[size]; produces an error, because we cannot initialize the size of array
dynamically. Constant expression is required here.

Example: int arr[10];

One more point is there, that is, usually declaration is not allowed after calling any function in a
current block of code. In the given program the declaration int arr[10]; is placed after a function
call scanf().

22. Which of the following statements are correct about 6 used in the program?
int num[6];
num[6]=21;

In the first statement 6 specifies a particular element, whereas in the second statement it
A.
specifies a type.

Page 96
In the first statement 6 specifies a array size, whereas in the second statement it specifies a
B.
particular element of array.
In the first statement 6 specifies a particular element, whereas in the second statement it
C.
specifies a array size.
D.In both the statement 6 specifies array size.

Answer & Explanation

Answer: Option B

Explanation:

The statement 'B' is correct, because int num[6]; specifies the size of array and num[6]=21;
designates the particular element(7th element) of the array.

23. Which of the following statements are correct about an array?

1: The array int num[26]; can store 26 elements.


2: The expression num[1] designates the very first element in the array.
3: It is necessary to initialize the array at the time of declaration.
4: The declaration num[SIZE] is allowed if SIZE is a macro.
A.1 B. 1,4
C. 2,3 D.2,4

Answer & Explanation

Answer: Option B

Explanation:

1. The array int num[26]; can store 26 elements. This statement is true.
2. The expression num[1] designates the very first element in the array. This statement is false,
because it designates the second element of the array.
3. It is necessary to initialize the array at the time of declaration. This statement is false.
4. The declaration num[SIZE] is allowed if SIZE is a macro. This statement is true, because the
MACRO just replaces the symbol SIZE with given value.
Hence the statements '1' and '4' are correct statements.

24. A pointer to a block of memory is effectively same as an array


A.True B.False

Answer & Explanation

Answer: Option A

Page 97
Explanation:

Yes, It is possible to allocate a block of memory (of arbitrary size) at run-time, using the
standard library's malloc or calloc or reaclloc function, and treat it as an array.

25. Does this mentioning array name gives the base address in all the contexts?

A.Yes B.No

Answer & Explanation

Answer: Option B

Explanation:

No, Mentioning the array name in C or C++ gives the base address in all contexts except one.
Syntactically, the compiler treats the array name as a pointer to the first element. You can
reference elements using array syntax, a[n], or using pointer syntax, *(a+n), and you can even
mix the usages within an expression.
When you pass an array name as a function argument, you are passing the "value of the pointer",
which means that you are implicitly passing the array by reference, even though all parameters in
functions are "call by value".

26. Is there any difference int the following declarations?


int f1(int arr[]);
int f1(int arr[100]);

A.Yes B.No

Answer & Explanation

Answer: Option B

Explanation:

No, both the statements are same. It is the prototype for the function f1() that accepts one integer
array as an parameter and returns an integer value.

27. Are the expressions arr and &arr same for an array of 10 integers?

A.Yes B.No

Answer & Explanation

Answer: Option B

Page 98
Explanation:

Both mean two different things. arr gives the address of the first int, whereas the &arr gives the
address of array of ints.

Strings

Page 99
1. How will you print \n on the screen?

A.printf("\n"); B. echo "\\n";


C. printf('\n'); D.printf("\\n");

Answer & Explanation

Answer: Option D

Explanation:

The statement printf("\\n"); prints '\n' on the screen.

2. The library function used to reverse a string is

A.strstr() B. strrev()
C. revstr() D.strreverse()

Answer & Explanation

Answer: Option B

Explanation:

strrev(s) Reverses all characters in s

3. Which of the following function sets first n characters of a string to a given character?

A.strinit() B. strnset()
C. strset() D.strcset()

Answer & Explanation

Answer: Option B

Explanation:

Declaration:

char *strnset(char *s, int ch, size_t n); Sets the first n characters of s to ch

4. #include <stdio.h>
#include <string.h>
int main(void)

Page 100
{
char *string = "abcdefghijklmnopqrstuvwxyz";
char letter = 'x';

printf("string before strnset: %s\n", string);


strnset(string, letter, 13);
printf("string after strnset: %s\n", string);

return 0;
}
Output:
string before strnset: abcdefghijklmnopqrstuvwxyz
string after strnset: xxxxxxxxxxxxxnopqrstuvwxyz

5.void main()
{
char s[ ]="man";
int i;
for(i=0;s[ i ];i++)
printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);
}
A.man
man
man
man
B. mmmm
aaaa
nnnn

C. Error

D. None of the above


Answer:
mmmm
aaaa
nnnn
Explanation:
s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea.
Generally array name is the base address for that array. Here s is the base address. i is the
index number/displacement from the base address. So, indirecting it with * is same as s[i].
i[s] may be surprising. But in the case of C it is same as s[i].

6.void main()
{
char string[]="Hello World";
display(string);

Page 101
}
void display(char *string)
{
printf("%s",string);
}
Answer:
Compiler Error : Type mismatch in redeclaration of function display

A. Hello World B. Compiler Error C. H D. None of the above

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.

7. #include<stdio.h>
void main()
{
char s[]={'a','b','c','\n','c','\0'};
char *p,*str,*str1;
p=&s[3];
str=p;
str1=s;
printf("%d",++*p + ++*str1-32);
}
Answer:
77

A.
Explanation:
p is pointing to character '\n'. str1 is pointing to character 'a' ++*p. "p is pointing to '\n'
and that is incremented by one." the ASCII value of '\n' is 10, which is then incremented to
11. The value of ++*p is 11. ++*str1, str1 is pointing to 'a' that is incremented by 1 and it
becomes 'b'. ASCII value of 'b' is 98.
Now performing (11 + 98 32), we get 77("M");
So we get the output 77 :: "M" (Ascii is 77).

8. If the two strings are identical, then strcmp() function returns

A.-1 B. 1
C. 0 D.Yes

Page 102
Answer & Explanation

Answer: Option C

Explanation:

Declaration: strcmp(const char *s1, const char*s2);

The strcmp return an int value that is


if s1 < s2 returns a value < 0
if s1 == s2 returns 0
if s1 > s2 returns a value > 0

9.void main()
{
char *p="hai friends",*p1;
p1=p;
while(*p!='\0') ++*p++;
printf("%s %s",p,p1);
}

A. haifriends B. ibj!gsjfoet C. friendshai D.error


Answer:
ibj!gsjfoet
Explanation:
++*p++ will be parse in the given order
*p that is value at the location currently pointed by p will be taken
++*p the retrieved value will be incremented
when ; is encountered the location will be incremented that is p++ will be executed
Hence, in the while loop initial value pointed by p is h, which is changed to i by executing
++*p and pointer moves to point, a which is similarly changed to b and so on. Similarly
blank space is converted to !. Thus, we obtain value in p becomes ibj!gsjfoet and since p
reaches \0 and p1 points to p thus p1doesnot print anything.

10. Which of the following function is used to find the first occurrence of a given string in
another string?

A.strchr() B. strrchr()
C. strstr() D.strnset()

Answer & Explanation

Answer: Option C

Explanation:

Page 103
The function strstr() Finds the first occurrence of a substring in another string

Declaration: char *strstr(const char *s1, const char *s2);

Return Value:
On success, strstr returns a pointer to the element in s1 where s2 begins (points to s2 in s1).
On error (if s2 does not occur in s1), strstr returns null.

Example:

#include <stdio.h>
#include <string.h>
int main(void)
{
char *str1 = "Ind Test", *str2 = "Ex", *ptr;

ptr = strstr(str1, str2);


printf("The substring is: %s\n", ptr);
return 0;
}

Output: The substring is: Test

11. Will the following code compile


void main()
{
static char names[5][20]={"pascal","ada","cobol","fortran","perl"};
int i;
char *t;
t=names[3];
names[3]=names[4];
names[4]=t;
for (i=0;i<=4;i++)
printf("%s",names[i]);
}

A. Yes B. No

Answer:
Compiler error: Lvalue required in function main
Explanation:
Array names are pointer constants. So it cannot be modified.

12. #include<stdio.h>
void main()
{

Page 104
char s[]={'a','b','c','\n','c','\0'};
char *p,*str,*str1;
p=&s[3];
str=p;
str1=s;
printf("%c",++*p + ++*str1-32);
}

A. a B. M C.X D.Error
Answer:
M
Explanation:
p is pointing to character '\n'.str1 is pointing to character 'a' ++*p meAnswer:"p
is pointing to '\n' and that is incremented by one." the ASCII value of '\n' is 10.
then it is incremented to 11. the value of ++*p is 11. ++*str1 meAnswer:"str1
is pointing to 'a' that is incremented by 1 and it becomes 'b'. ASCII value of 'b'
is 98. both 11 and 98 is added and result is subtracted from 32.
i.e. (11+98-32)=77("M");

13. Which of the following function is more appropriate for reading in a multi-word string?

A.printf(); B. scanf();
C. gets(); D.puts();

Answer & Explanation

Answer: Option C

Explanation:

gets(); collects a string of characters terminated by a new line from the standard input stream
stdin

#include <stdio.h>
int main(void)
{
char string[60];

printf("Enter a string:");
gets(string);
printf("The string input was: %s\n", string);
return 0;
}

Output:

Enter a string: Ind Test

Page 105
The string input was: Ind Test
51) main( )
{
void *vp;
char ch = g, *cp = goofy;
int j = 20;
vp = &ch;
printf(%c, *(char *)vp);
vp = &j;
printf(%d,*(int *)vp);
vp = cp;
printf(%s,(char *)vp + 3);
}

A. g20fy B. goofy 20fy C. g2fy D.error


Answer:
g20fy
Explanation:
Since a void pointer is used it can be type casted to any other type pointer.
vp = &ch stores address of char ch and the next statement prints the value
stored in vp after type casting it to the proper data type pointer. the output is g. Similarly the
output from second printf is 20. The third printf statement type casts it to print the string from
the 4th value hence the output is fy.

14 void main ( )
{
static char *s[ ] = {black, white, yellow, violet};
char **ptr[ ] = {s+3, s+2, s+1, s}, ***p;
p = ptr;
**++p;
printf(%s,*--*++p + 3);
}
A. ck B .te C.ow D.et
Answer:
ck
Explanation:
In this problem we have an array of char pointers pointing to start of 4 strings. Then we have ptr
which is a pointer to a pointer of type char and a variable p which is a pointer to a pointer to a
pointer of type char. p hold the initial value of ptr, i.e. p = s+3. The next statement increment
value in p by 1 , thus now value of p = s+2. In the printf statement the expression is evaluated
*++p causes gets value s+1 then the pre decrement is executed and we get s+1 1 = s . the
indirection operator now gets the value from the array of s and adds 3 to the starting address. The
string is printed starting from this position. Thus, the output is ck.

15 void main()
{
int i, n;

Page 106
char *x = girl;
n = strlen(x);
*x = x[n];
for(i=0; i<n; ++i)
{
printf(%s\n,x);
x++;
}
}
A.girl
irl
rl
l
(blank space)

B.
(blank space)
irl
rl
l

C. error

D. none of the above

Answer:

B.
(blank space)
irl
rl
l
Explanation:
Here a string (a pointer to char) is initialized with a value girl. The strlen
function returns the length of the string, thus n has a value 4. The next
statement assigns value at the nth location (\0) to the first location. Now the string becomes
\0irl . Now the printf statement prints the string after each iteration it increments it starting
position. Loop starts from 0 to 4. The first time x[0] = \0 hence it prints nothing and pointer
value is incremented. The second time it prints from x[1] i.e irl and the third time it prints rl
and the last time it prints l and the loop terminates.

16. What are the files which are automatically opened when a C file is executed?
Answer:
stdin, stdout, stderr (standard input,standard output,standard error).

17. what will be the position of the file marker?


a: fseek(ptr,0,SEEK_SET);

Page 107
b: fseek(ptr,0,SEEK_CUR);
Answer :
a: The SEEK_SET sets the file position marker to the starting of the file.
b: The SEEK_CUR sets the file position marker to the current position
of the file.

18. Which of the following function is correct that finds the length of a string?

int xstrlen(char *s) int xstrlen(char s)


{ {
int length=0; int length=0;
A. while(*s!='\0') B. while(*s!='\0')
{ length++; s++; } length++; s++;
return (length); return (length);
} }
int xstrlen(char *s) int xstrlen(char *s)
{ {
int length=0; int length=0;
C. while(*s!='\0') D. while(*s!='\0')
length++; s++;
return (length); return (length);
} }

Answer & Explanation

Answer: Option A

Explanation:

Option A is the correct function to find the length of given string.

Example:

#include<stdio.h>

int xstrlen(char *s)


{
int length=0;
while(*s!='\0')
{ length++; s++; }
return (length);
}

int main()
{
char d[] = "Ind Test";

Page 108
printf("Length = %d\n", xstrlen(d));
return 0;
}

Output: Length = 11

20. What will be the output of the program ?

#include<stdio.h>
#include<string.h>
int main()
{
char str1[20] = "Hello", str2[20] = " World";
printf("%s\n", strcpy(str2, strcat(str1, str2)));
return 0;
}
A.Hello B. World
C. Hello World D.WorldHello

Answer & Explanation

Answer: Option C

Explanation:

Step 1: char str1[20] = "Hello", str2[20] = " World"; The variable str1 and str2 is declared as an
array of characters and initialized with value "Hello" and " World" respectively.

Step 2: printf("%s\n", strcpy(str2, strcat(str1, str2)));

=> strcat(str1, str2)) it append the string str2 to str1. The result will be stored in str1. Therefore
str1 contains "Hello World".

=> strcpy(str2, "Hello World") it copies the "Hello World" to the variable str2.

Hence it prints "Hello World".

25. void main()

{
char *str1="abcd";
char str2[]="abcd";
printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));
}

A. 4 4 4 B. 2 5 5 C. 2 2 2 d.error

Page 109
Answer:
B.2 5 5
Explanation:
In first sizeof, str1 is a character pointer so it gives you the size of the pointer
variable. In second sizeof the name str2 indicates the name of the array
whose size is 5 (including the '\0' termination character). The third sizeof is
similar to the second one.

26. What will be the output of the program ?

#include<stdio.h>
int main()
{
char p[] = "%d\n";
p[1] = 'c';
printf(p, 65);
return 0;
}
A.A B. a
C. c D.65

Answer & Explanation

Answer: Option A

Explanation:

Step 1: char p[] = "%d\n"; The variable p is declared as an array of characters and initialized
with string "%d".

Step 2: p[1] = 'c'; Here, we overwrite the second element of array p by 'c'. So array p becomes
"%c".

Step 3: printf(p, 65); becomes printf("%c", 65);

Therefore it prints the ASCII value of 65. The output is 'A'.

27. What will be the output of the program ?

#include<stdio.h>
#include<string.h>
int main()
{
printf("%d\n", strlen("12345"));
return 0;
}
A.5 B. 12

Page 110
C. 6 D.2

Answer & Explanation

Answer: Option A

Explanation:

The function strlen returns the number of characters in the given string.

Therefore, strlen("12345") returns 5.

Hence the output of the program is "5".

28. What will be the output of the program ?

#include<stdio.h>
int main()
{
printf(5+"Good Morning\n");
return 0;
}
A.Good Morning B. Good
C. M D.Morning

Answer & Explanation

Answer: Option D

Explanation:

printf(5+"Good Morning\n"); It skips the 5 characters and prints the given string.

Hence the output is "Morning"

This can also be written as printf("Good Morning\n"+5); or

printf(%s,5+"Good Morning\n");

29. What will be the output of the program ?

#include<stdio.h>
#include<string.h>
int main()
{
char str[] = "Ind\0\ROCKS\0";
printf("%s\n", str);

Page 111
return 0;
}
A.ROCKS B. Ind
C. Ind ROCKS D.Ind\0ROCKS

Answer & Explanation

Answer: Option B

Explanation:

A string is a collection of characters terminated by '\0'.

Step 1: char str[] = "Ind\0\ROCKS\0"; The variable str is declared as an array of characters and
initialized with value "Ind"

Step 2: printf("%s\n", str); It prints the value of the str.

The output of the program is "Ind".

30. What will be the output of the program If characters 'a', 'b' ,'c',d ans enter are supplied as
input?

#include<stdio.h>
int main()
{
void fun();
fun();
printf("\n");
return 0;
}
void fun()
{
char c;
if((c = getchar())!= '\n')
fun();
printf("%c", c);
}
A.abcd abcd B. dbca
C. Infinite loop D.dcba

Answer & Explanation

Answer: Option D

Explanation:

Page 112
Step 1: void fun(); This is the prototype for the function fun().

Step 2: fun(); The function fun() is called here.

The function fun() gets a character input and the input is terminated by an enter key(New line
character). Due to recursion ,it prints the given character in the reverse order.

The given input characters are "abcd"and \n

Output: dcba

31.What will be the output of the program ?

#include<stdio.h>

int main()
{
printf("Ind", "ROCKS\n");
return 0;
}
A.Error B. Ind ROCKS
C. Ind D.ROCKS

Answer & Explanation

Answer: Option C

Explanation:

printf("Ind", "ROCKS\n"); It prints "Ind". Because ,(comma) operator has Left to Right
associativity. After printing "Ind", the statement got terminated.

32. What will be the output of the program ?

#include<stdio.h>
int main()
{
char *names[] = { "Suresh", "Siva", "Sona", "Baiju", "Ritu"};
int i;
char *t;
t = names[3];
names[3] = names[4];
names[4] = t;
for(i=0; i<=4; i++)
printf("%s,", names[i]);
return 0;
}

Page 113
A.Suresh, Siva, Sona, Baiju, Ritu
B. Suresh, Siva, Sona, Ritu, Baiju
C. Suresh, Siva, Baiju, Sona, Ritu
D.Suresh, Siva, Ritu, Sona, Baiju

Answer & Explanation

Answer: Option B

Explanation:

Step 1: char *names[] = { "Suresh", "Siva", "Sona", "Baiju", "Ritu"}; The variable names is
declared as an pointer to a array of strings.
Step 2: int i; The variable i is declared as an integer type.
Step 3: char *t; The variable t is declared as pointer to a string.
Step 4: t = names[3]; names[3] = names[4]; names[4] = t; These statements the swaps the 4 and
5 element of the array names.
Step 5: for(i=0; i<=4; i++) printf("%s,", names[i]); These statement prints the all the value of the
array names.
Hence the output of the program is "Suresh, Siva, Sona, Ritu, Baiju".

33. What will be the output of the program ?


#include<stdio.h>
#include<string.h>
int main()
{
static char str1[] = "dills";
static char str2[20];
static char str3[] = "Daffo";
int i;
i = strcmp(strcat(str3, strcpy(str2, str1)), "Daffodills");
printf("%d\n", i);
return 0;
}
[A].0 [B]. 1
[C].2 [D].4

Answer: Option A

Explanation:

step 1: strcpy(str2,str1) cpoy the str1="dills" into str2.

step 2: strcat(str3,str2) append the dills at the end of Daffo.

step 3: strcmp(str3,"Daffodils") returns the no. of charecters of str3 which are not find in

Page 114
Daffodills.

step 4: since both strings are same so results will be 0(zero)

34.. What will be the output of the program ?


#include<stdio.h>
#include<string.h>
int main()
{
static char s[] = "Hello!";
printf("%d\n", *(s+strlen(s)));
return 0;
}
A.8 B. 0
C. 16 D.Error

Answer & Explanation

Answer: Option B

Explanation:

Length of the string will be 6. 's' is a pointer which is pointing to the first character of the string.
After getting incremented its value by 6 it will point to null character, having ASCII value 0. So
0 will be printed.

35. What will be the output of the program ?


#include<stdio.h>
int main()
{
static char s[25] = "The cocaine man";
int i=0;
char ch;
ch = s[++i];
printf("%c", ch);
ch = s[i++];
printf("%c", ch);
ch = i++[s];
printf("%c", ch);
ch = ++i[s];
printf("%c", ch);
return 0;
}

Page 115
A.hhe! B. he c
C. The c D.Hhec

Answer & Explanation

Answer: Option A

36. What will be the output of the program ?

#include<stdio.h>
int main()
{
int i;
char a[] = "\0";
if(printf("%s", a))
printf("The string is empty\n");
else
printf("The string is not empty\n");
return 0;
}
A.The string is empty B. The string is not empty
C. No output D.0

Answer & Explanation

Answer: Option B

Explanation:

The function printf() returns the number of charecters printed on the console.

Step 1: char a[] = "\0"; The variable a is declared as an array of characters and it initialized with
"\0". It denotes that the string is empty.

Step 2: if(printf("%s", a)) The printf() statement does not print anything, so it returns '0'(zero).
Hence the if condition is failed.

In the else part it prints "The string is not empty".

37.If char=1, int=4, and float=4 bytes size, What will be the output of the program ?

#include<stdio.h>
int main()
{
char ch = 'A';
printf("%d, %d, %d", sizeof(ch), sizeof('A'), sizeof(3.14f));
return 0;

Page 116
}
A.1, 2, 4 B. 1, 4, 4
C. 2, 2, 4 D.2, 4, 8

Answer & Explanation

Answer: Option B

Explanation:
Step 1: char ch = 'A'; The variable ch is declared as an character type and initialized with value
'A'.
Step 2: printf("%d, %d, %d", sizeof(ch), sizeof('A'), sizeof(3.14));
The sizeof function returns the size of the given expression.
sizeof(ch) becomes sizeof(char). The size of char is 1 byte.
sizeof('A') becomes sizeof(65). The size of int is 4 bytes (as mentioned in the question).
sizeof(3.14f). The size of float is 4 bytes.
Hence the output of the program is 1, 4, 4

38. If the size of pointer is 32 bits What will be the output of the program ?
#include<stdio.h>
int main()
{
char a[] = "Visual C++";
char *b = "Visual C++";
printf("%d, %d\n", sizeof(a), sizeof(b));
printf("%d, %d", sizeof(*a), sizeof(*b));
return 0;
}
10, 2 10, 4
A. B.
2, 2 1, 2
11, 4 12, 2
C. D.
1, 1 2, 2

Answer & Explanation

Answer: Option C

Explanation:

Char a[] has 10 letters and 1 escape character \0(null) so size of will give 11, size of pointer is 32
bit = 4 byte (1 byte = 8 bits), *a points to char V so sizeof(V) = 1 similarly *b also points to V.

39. What will be the output of the program ?

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

Page 117
char str1[] = "Hello";
char str2[10];
char *t, *s;
s = str1;
t = str2;
while(*t=*s)
*t++ = *s++;
printf("%s\n", str2);
return 0;
}
[A].Hello [B]. HelloHello
[C].No output [D].ello

Answer: Option A

Explanation:

Here s will points to the beginig of string str1.


And t will points to the begining address of string str2.
In while loop we are copying the elements from str1 to str2 using pointers s and t untill end of
string is reached. At last when s[n] == '\0' then ( *t=*s ) == zero, so it is false, so the loop will
exit.The above can be written in one step while(*t++=*s); most of the time asked in interviews
.this is copying of string in single line.

40. What will be the output of the program ?

#include<stdio.h>
int main()
{
char str[50] = "Ind Test";
printf("%s\n", &str+2);
return 0;
}
A.Garbage value B. Error
C. No output D.rga Test

Answer & Explanation

Answer: Option A

Explanation:

Step 1: char str[50] = "Ind Test"; The variable str is declared as an array of characteres and
initialized with a string "Ind Test".

Page 118
Step 2: printf("%s\n", &str+2);

=> In the printf statement %s is string format specifier tells the compiler to print the string in the
memory of &str+2

=> &str is a location of string "Ind Test". Therefore &str+2 is another memory location.

Hence it prints the Garbage value.

41. What will be the output of the program ?

#include<stdio.h>
int main()
{
char str = "Ind Test";
printf("%s\n", str);
return 0;
}
A.Error B. Ind Test
C. Base address of str D.No output

Answer & Explanation

Answer: Option A

Explanation:

The line char str = "Ind Test"; generates "Non portable pointer conversion" error.

To eliminate the error, we have to change the above line to

char *str = "Ind Test"; (or) char str[] = "Ind Test";

Then it prints "Ind Test".

42. What will be the output of the program ?

#include<stdio.h>
int main()
{
char str[] = "Nagpur";
str[0]='K';
printf("%s, ", str);
str = "Kanpur";
printf("%s", str+1);
return 0;
}

Page 119
A.Kagpur, Kanpur B. Nagpur, Kanpur
C. Kagpur, anpur D.Error

Answer & Explanation

Answer: Option D

Explanation:

The statement str = "Kanpur"; generates the LVALUE required error. We have to use strcpy
function to copy a string.To remove error we have to change this statement str = "Kanpur"; to
strcpy(str, "Kanpur");The program prints the string "anpur"

43. What will be the output of the program ?

#include<stdio.h>
#include<string.h>
int main()
{
char sentence[80];
int i;
printf("Enter a line of text\n");
gets(sentence);
for(i=strlen(sentence)-1; i >=0; i--)
putchar(sentence[i]);
return 0;
}
[A].The sentence will get printed in same order as it entered
[B]. The sentence will get printed in reverse order
[C].Half of the sentence will get printed
[D].None of above

Answer: Option B

Explanation:

In for loop we initializing i with 1 less the length of the string, so i points to the last element of
the string(before null character), and we printing each character and decrementing i by 1 which
willl print the characters in reverse order.

44. What will be the output of the program ?

#include<stdio.h>
void swap(char *, char *);
int main()
{

Page 120
char *pstr[2] = {"Hello", "Ind Test"};
swap(pstr[0], pstr[1]);
printf("%s\n%s", pstr[0], pstr[1]);
return 0;
}
void swap(char *t1, char *t2)
{
char *t;
t=t1;
t1=t2;
t2=t;
}
Ind Test
A. B. Address of "Hello" and "Ind Test"
Hello
Hello Dello
C. D.
Ind Test Hurga Test

Answer & Explanation

Answer: Option C

Explanation:

Step 1: void swap(char *, char *); This prototype tells the compiler that the function swap accept
two strings as arguments and it does not return anything.
Step 2: char *pstr[2] = {"Hello", "Ind Test"}; The variable pstr is declared as an pointer to the
array of strings. It is initialized to
pstr[0] = "Hello", pstr[1] = "Ind Test"
Step 3: swap(pstr[0], pstr[1]); The swap function is called by "call by value". Hence it does not
affect the output of the program.
If the swap function is "called by reference" it will affect the variable pstr.
Step 4: printf("%s\n%s", pstr[0], pstr[1]); It prints the value of pstr[0] and pstr[1].
Hence the output of the program is
Hello
Ind Test

45. If the size of pointer is 4 bytes then What will be the output of the program ?
#include<stdio.h>
int main()
{
char *str[] = {"Frogs", "Do", "Not", "Die", "They", "Croak!"};
printf("%d, %d", sizeof(str), strlen(str[0]));
return 0;
}
A.22, 4 B. 25, 5
C. 24, 5 D.20, 2

Page 121
Answer & Explanation

Answer: Option C

Explanation:

Step 1: char *str[] = {"Frogs", "Do", "Not", "Die", "They", "Croak!"}; The variable str is
declared as an pointer to the array of 6 strings.
Step 2: printf("%d, %d", sizeof(str), strlen(str[0]));
sizeof(str) denotes 6 * 4 bytes = 24 bytes. Hence it prints '24'
strlen(str[0])); becomes strlen(Frogs)). Hence it prints '5';
Hence the output of the program is 24, 5

46. What will be the output of the program ?


#include<stdio.h>
#include<string.h>
int main()
{
char str1[5], str2[5];
int i;
gets(str1);
gets(str2);
i = strcmp(str1, str2);
printf("%d\n", i);
return 0;
}
A.Unpredictable integer value B. 0
C. -1 D.Error

Answer & Explanation

Answer: Option A

Explanation:

gets() gets collects a string of characters terminated by a new line from the standard input stream
stdin.
The gets(str1) read the input string from user and store in variable str1.
The gets(str2) read the input string from user and store in variable str2.
The code i = strcmp(str1, str2); The strcmp not only returns -1, 0 and +1, but also other negative
or positive values. So the value of i is "unpredictable integer value".
printf("%d\n", i); It prints the value of variable i.

47. What will be the output of the program ?

#include<stdio.h>

Page 122
int main()
{
char str1[] = "Hello";
char str2[] = "Hello";
if(str1 == str2)
printf("Equal\n");
else
printf("Unequal\n");
return 0;
}
A.Equal B. Unequal
C. Error D.None of above

Answer & Explanation

Answer: Option B

Explanation:
Step 1: char str1[] = "Hello"; The variable str1 is declared as an array of characters and
initialized with a string "Hello".
Step 2: char str2[] = "Hello"; The variable str2 is declared as an array of characters and
initialized with a string "Hello".
We have to use strcmp(s1,s2) function to compare strings.
Step 3: if(str1 == str2) here the address of str1 and str2 are compared. The address of both
variable is not same. Hence the if condition is failed.
Step 4: At the else part it prints "Unequal"

48.What will be the output of the program ?


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

char *p1 = "Ind", *p2;


p2=p1;
p1 = "TEST";
printf("%s %s\n", p1, p2);
return 0;
}
A.Ind TEST B. TEST Ind
C. Ind Ind D.TEST TEST

Answer & Explanation

Answer: Option B

Explanation:

Page 123
Step 1: char *p1 = "Ind", *p2; The variable p1 and p2 is declared as an pointer to a character
value and p1 is assigned with a value "Ind".
Step 2: p2=p1; The value of p1 is assigned to variable p2. So p2 contains "Ind".
Step 3: p1 = "TEST"; The p1 is assigned with a string "TEST"
Step 4: printf("%s %s\n", p1, p2); It prints the value of p1 and p2.
Hence the output of the program is "TEST Ind".

49. What will be the output of the program ?

#include<stdio.h>
#include<string.h>
int main()
{
printf("%c\n", "abcdefgh"[4]);
return 0;
}
A.Error B. d
C. e D.abcdefgh

Answer & Explanation

Answer: Option C

Explanation:

printf("%c\n", "abcdefgh"[4]); It prints the 5 character of the string "abcdefgh".

Hence the output is 'e'.

50. What will be the output of the program ?

#include<stdio.h>
int main()
{
printf("%u %s\n", &"Hello", &"Hello");
return 0;
}
A.65530 Hello B. Hello 65530
C. Hello Hello D.Error

Answer & Explanation

Answer: Option A

Explanation:

In printf("%u %s\n", &"Hello", &"Hello");.

Page 124
The %u format specifier tells the compiler to print the memory address of the "Hello".
The %s format specifier tells the compiler to print the string "Hello".
Hence the output of the program is "65530 Hello". (Note: 65530 is memory address it may
change.)

51. Which of the following statements are correct about the program below?

#include<stdio.h>
int main()
{
char str[20], *s;
printf("Enter a string\n");
scanf("%s", str);
s=str;
while(*s != '\0')
{
if(*s >= 97 && *s <= 122)
*s = *s-32;
s++;
}
printf("%s",str);
return 0;
}
A.The code converts a string in to an integer
B. The code converts lower case character to upper case
C. The code converts upper case character to lower case
D.Error in code

Answer & Explanation

Answer: Option B

Explanation:

This program converts the given string to upper case string.


Output:
Enter a string: Ind Test
IND TEST

52. Which of the following statements are correct about the below declarations?
char *p = "Ind";
char a[] = "Ind";
1: There is no difference in the declarations and both serve the same purpose.
p is a non-const pointer pointing to a non-const string, whereas a is a const pointer pointing to
2:
a non-const pointer.

Page 125
The pointer p can be modified to point to another string, whereas the individual characters
3:
within array a can be changed.
4: In both cases the '\0' will be added at the end of the string "Ind".
A.1, 2 B. 2, 3, 4
C. 3, 4 D.2, 3

Answer & Explanation

Answer: Option B

53. Which of the following statements are correct ?


1: A string is a collection of characters terminated by '\0'.
2: The format specifier %s is used to print a string.
3: The length of the string can be obtained by strlen().
4: The pointer CAN work on string.
A.A, B B. A, B, C,D
C. B, D D.C, D

Answer & Explanation

Answer: Option B

Explanation:

Clearly, four statements is correct.

54. Which of the following statement is correct?

A.strcmp(s1, s2) returns a number less than 0 if s1>s2


B. strcmp(s1, s2) returns a number greater than 0 if s1<s2
C. strcmp(s1, s2) returns 0 if s1==s2
D.strcmp(s1, s2) returns 1 if s1==s2

Answer & Explanation

Answer: Option C

Explanation:

The strcmp return an int value that is

if s1 < s2 returns a value < 0

if s1 == s2 returns 0

Page 126
if s1 > s2 returns a value > 0

From the above statements, that the third statement is only correct.

55.will the program compile successfully?

#include<stdio.h>
int main()
{
char a[] = "Ind";
char *p = "TEST";
a = "TEST";
p = "Ind";
printf("%s %s\n", a, p);
return 0;
}
A.Yes B.No

Answer & Explanation

Answer: Option B

Explanation:
Because we can assign a new string to a pointer but not to an array a.

56. For the following statements will arr[3] and ptr[3] fetch the same character?
char arr[] = "Ind Test";
char *ptr = "Ind Test";

A.Yes B.No

Answer & Explanation

Answer: Option A

Explanation:

Yes, both the statements print the same character 'g'.

Page 127
Structures ,Unions ,Enums
1.What is the similarity between a structure, union and enumeration?

A.All of them let you define new values


B. All of them let you define new data types
C. All of them let you define new pointers
D.All of them let you define new structures

Answer & Explanation

Answer: Option B

Explanation:

structures unions and enumeration is used to create a new datatype .

2. #include<stdio.h>
void main()
{
struct xx
{
int x=3;
char name[]="hello";
};
struct xx *s;
printf("%d",s->x);
printf("%s",s->name);
}

Page 128
A. 3 hello B.Compiler Error C. hello 3 D. none of the above

Answer:
Compiler Error

Explanation:
You should not initialize variables in declaration

3. The following code will compile

#include<stdio.h>
void main()
{
struct xx
{
int x;
struct yy
{
char s;
struct xx *p;
};
struct yy *q;
};
}
A. Ture B.False
Answer:
B.False
Compiler Error
Explanation:
The structure yy is nested within structure xx. Hence, the elements are of yy
are to be accessed through the instance of structure xx, which needs an instance of yy to be
known. If the instance is created after defining the structure the compiler will not know about
the instance relative to xx. Hence for nested structure yy you have to declare member.

4. What will be the output of the program ?

#include<stdio.h>
int main()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0]=3;

Page 129
u.ch[1]=2;
printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
return 0;
}
A.3, 2, 515 B. 515, 2, 3
C. 3, 2, 5 D.515, 515, 4

Answer & Explanation

Answer: Option A

Explanation:

The system will allocate 2 bytes for the union.

The statements u.ch[0]=3; u.ch[1]=2; store data in memory as given below

5. What will be the output of the program ?

#include<stdio.h>
int main()
{
union var
{
int a, b;
};
union var v;
v.a=10;
v.b=20;
printf("%d\n", v.a);
return 0;

Page 130
}
A.10 B. 20
C. 30 D.0

Answer & Explanation

Answer: Option B

Explanation:
In union the memory will be allocated for the highest data type.
Here the size of union is two bytes not four bytes. Hence 10 will be overwritten by 20. So the
output is 20

6. What will be the output of the program ?

#include<stdio.h>
int main()
{
struct value
{
int bit1:1;
int bit3:4;
int bit4:4;
}bit={1, 2, 13};

printf("%d, %d, %d\n", bit.bit1, bit.bit3, bit.bit4);


return 0;
}
A.1, 2, 13 B. 1, 4, 4
C. -1, 2, -3 D.-1, -2, -13

Answer & Explanation

Answer: Option C

Explanation:

Note the below statement inside the struct:


int bit1:1; --> 'int' indicates that it is a SIGNED integer.
For signed integers the leftmost bit will be taken for +/- sign.
If you store 1 in 1-bit field:
The left most bit is 1, so the system will treat the value as negative number.
The 2's complement method is used by the system to handle the negative values.
Therefore, the data stored is 1. The 2's complement of 1 is also 1 (negative).
Therefore -1 is printed.
If you store 2 in 4-bits field:

Page 131
Binary 2: 0010 (left most bit is 0, so system will treat it as positive value)
0010 is 2
Therefore 2 is printed.
If you store 13 in 4-bits field:
Binary 13: 1101 (left most bit is 1, so system will treat it as negative value)
Find 2's complement of 1101:
1's complement of 1101 : 0010
2's complement of 1101 : 0011 (Add 1 to the result of 1's complement)
0011 is 3 (but negative value)
Therefore -3 is printed.

7. void main()
{
char far *farther,*farthest;
printf("%d..%d",sizeof(farther),sizeof(farthest));
}

A. 4..2 B. 2..2 C.8.. 8 D.2..4


Answer:
4..2
Explanation:
the second pointer is of char type and not a far pointer

8. What will be the output of the program ?

#include<stdio.h>
int main()
{
struct value
{
int bit1:2;
int bit3:5;
int bit4:4;
}bit;
printf("%d\n", sizeof(bit));
return 0;
}
A.1 B. 2
C. 4 D.11

Answer & Explanation

Answer: Option B

Explanation:

Page 132
Since C is a compiler dependent language, in Turbo C (Windows) the output will be 2, but in
GCC (Linux) the output will be 4.

9. Will the following code compile


#include<stdio.h>
void main()
{
struct xx
{
int x=3;
char name[]="hello";
};
struct xx *s=malloc(sizeof(struct xx));
printf("%d",s->x);
printf("%s",s->name);
}

A. Yes B. No

Answer:
Compiler Error
Explanation:
Initialization should not be done for structure members inside the structure
Declaration

10. What will be the output of the program ?

#include<stdio.h>
int main()
{
enum days {MON=-1, TUE, WED=6, THU, FRI, SAT};
printf("%d, %d, %d, %d, %d, %d\n", MON, TUE, WED, THU, FRI, SAT);
return 0;
}
A.-1, 0, 1, 2, 3, 4 B. -1, 2, 6, 3, 4, 5
C. -1, 0, 6, 2, 3, 4 D.-1, 0, 6, 7, 8, 9

Answer: Option D

Explanation:

In Enum by default cur=prev+1, here mon=-1 so tue will be assigned to 0(tue=mon+1) even
you can overwrite with your own value for eg wed=6 so thu will become 7.

11.The following code is for single linked lists


struct aaa{

Page 133
struct aaa *prev;
int i;
struct aaa *next;
};
main()
{
struct aaa abc,def,ghi,jkl;
int x=100;
abc.i=0;
abc.prev=&jkl;
abc.next=&def;
def.i=1;
def.prev=&abc;
def.next=&ghi;
ghi.i=2;
ghi.prev=&def;
ghi.next=&jkl;
jkl.i=3;
jkl.prev=&ghi;
jkl.next=&abc;
x=abc.next->next->prev->next->i;
printf("%d",x);
}

A. Yes B. No

Answer:
B. No

Output: 2
Explanation:
above all statements form a double circular linked list;
abc.next->next->prev->next->i
this one points to "ghi" node the value of at particular node is 2.

12.Will the following code compile


struct point
{
int x;
int y;
};
struct point origin,*pp;
main()
{
pp=&origin;
printf("origin is(%d%d)\n",(*pp).x,(*pp).y);
printf("origin is (%d%d)\n",pp->x,pp->y);

Page 134
}
A. Yes B. No

Answer:
Yes
Output:
origin is(0,0)
origin is(0,0)
Explanation:
pp is a pointer to structure. we can access the elements of the structure either
with arrow mark or with indirection operator.
Note:
Since structure point is globally declared x & y are initialized as zeroes

13. What will be the output of the program ?

#include<stdio.h>
int main()
{
struct employee
{
char *n;
int age;
};
struct employee e1 = {"Dravid", 23};
struct employee e2 = e1;
strupr(e2.n);
printf("%s\n", e1.n);
return 0;
}
A.Error: Invalid structure assignment
B. DRAVID
C. Dravid
D.No output

Answer & Explanation

Answer: Option C

Explanation:
The value of e1 is passed in e2 and then changes are made to the value present in e2 and then we
want to print the values of e1 then dravid will come in lower case

14. What will be the output of the program in 16-bit platform (under DOS)?

#include<stdio.h>

Page 135
int main()
{
struct node
{
int data;
struct node *link;
};
struct node *p, *q;
p = (struct node *) malloc(sizeof(struct node));
q = (struct node *) malloc(sizeof(struct node));
printf("%d, %d\n", sizeof(p), sizeof(q));
return 0;
}
A.2, 2 B. 8, 8
C. 5, 5 D.4, 4

Answer & Explanation

Answer: Option A

Explanation:

Under 16 bit DOS the size of any pointer is 2 bytes.

15. What will be the output of the program ?

#include<stdio.h>
int main()
{
struct byte
{
int one:1;
};
struct byte var = {1};
printf("%d\n", var.one);
return 0;
}
[A].1 [B]. -1
[C].0 [D].Error

Answer: Option B

Explanation:

If you store 1 in 1-bit field:


The left most bit is 1, so the system will treat the value as negative number.

Page 136
The 2's complement method is used by the system to handle the negative values.
Therefore, the data stored is 1. The 2's complement of 1 is also 1 (negative).
Therefore -1 is printed.

11. What will be the output of the program ?

#include<stdio.h>
int main()
{
enum days {MON=-1, TUE, WED=6, THU, FRI, SAT};
printf("%d, %d, %d, %d, %d, %d\n", ++MON, TUE, WED, THU, FRI, SAT);
return 0;
}
A.-1, 0, 1, 2, 3, 4 B. Error:L value required
C. 0, 1, 6, 3, 4, 5 D.0, 0, 6, 7, 8, 9

Answer & Explanation

Answer: Option B

Explanation:

Because ++ or -- cannot be done on enum value. Because these are treated as constants

12. What will be the output of the program ?

#include<stdio.h>

struct course
{
int courseno;
char coursename[25];
};
int main()
{
struct course c[] = { {102, "Java"},
{103, "PHP"},
{104, "DotNet"} };

printf("%d", c[1].courseno);
printf("%s\n", (*(c+2)).coursename);
return 0;
}
[A].103 Dotnet [B]. 102 Java
[C].103 PHP [D].104 DotNet

Page 137
Answer: Option A

Explanation:

When you do c[1].courseno it will print the 'c[1]' array 2nd variable ie(2 nd index) 103 then
(*(c+2)).coursename it will go into c[2] array's coursename contents ie DOTNET

13. What will be the output of the program ?

#include<stdio.h>
int main()
{
enum value{VAL1=0, VAL2, VAL3, VAL4, VAL5} var;
printf("%d\n", sizeof(var));
return 0;
}
[A].1 [B]. 2
[C].4 [D].10

Answer: Option B

Explanation:

If we run this program in DOS (compiled with Turbo C), it will show the output as 2. Because it
is a 16-bit platform. If you compile in a 32-bit platform like Linux (compiled with GCC
compiler), it will show 4 as the output.

14. Point out the error in the program?

struct employee
{
int ecode;
struct employee *e;
};
A.Error: in structure declaration
B. Linker Error
C. No Error
D.None of above

Answer & Explanation

Answer: Option C

Page 138
Explanation:

This type of declaration is called as self-referential structure. Here *e is pointer to a struct


employee.

15. Point out the error in the program?

typedef struct smalldata mystruct;


struct smalldata
{
int x;
mystruct *b;
};
A.Error: in structure declaration
B. Linker Error
C. No Error
D.None of above

Answer & Explanation

Answer: Option C

Explanation:

Here the type name mystruct is known at the point of declaring the structure, as it is already
defined.

16. Point out the error in the program?

#include<stdio.h>
int main()
{
struct a
{
float category:5;
char scheme:4;
};
printf("size=%d", sizeof(struct a));
return 0;
}
A.Error: invalid structure member in printf
B. Error in this float category:5; statement
C. No error
D.None of above

Answer & Explanation

Page 139
Answer: Option B

Explanation:

Bit field type must be signed int or unsigned int.


The char type: char scheme:2; is also a valid statement.

17. Point out the error in the program?

struct employee
{
int ecode;
struct employee e;
};
A.Error: in structure declaration
B. Linker Error
C. No Error
D.None of above

Answer & Explanation

Answer: Option A

Explanation:

The structure employee contains a member e of the same type.(i.e) struct employee. At this stage
compiler does not know the size of structure.

18.Point out the error in the program?

#include<stdio.h>
int main()
{
struct emp
{
char name[20];
float sal;
};
struct emp e[10];
int i;
for(i=0; i<=9; i++)
scanf("%s %f", e[i].name, &e[i].sal);
return 0;
}
A.Error: invalid structure member
B. Error: Floating point formats not linked
C. No error

Page 140
D.None of above

Answer & Explanation

Answer: Option B

Explanation:

At run time it will show an error then program will be terminated.

Sample output: Turbo C (Windows)

c:\>myprogram

Sample
12.123

scanf : floating point formats not linked


Abnormal program termination

In order to remove error add following code in the program


void linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}

19. Point out the error in the program?

#include<stdio.h>
int main()
{
struct bits
{
int i:40;
}bit;

printf("%d\n", sizeof(bit));
return 0;
}
A.4
B. 2
C. Error: Bit field too large
D.Error: Invalid member access in structure

Page 141
Answer & Explanation

Answer: Option C

Explanation

The width of int is 4 bytes (32 bits) or 2 bytes (16 bits) depending upon the machine, so the
allocation for int is upto 32 bits. The declaration int i:40; exceeds the width of int so the compiler
generates the error:width of 'i' exceeds its type.

20. Point out the error in the program?

#include<stdio.h>
int main()
{
union a
{
int i;
char ch[2];
};
union a z1 = {512};
union a z2 = {0, 2};
return 0;
}
A.Error: invalid union declaration
B. Error: in Initializing z2
C. No error
D.None of above

Answer & Explanation

Answer: Option B

Explanation:
In union at a time, only one variable can be initialised.. so union a z2={0,2} is an error.

21. Point out the error in the program?

#include<stdio.h>

int main()
{
struct emp
{
char name[25];
int age;

Page 142
float bs;
};
struct emp e;
e.name = "Ind";
e.age = 25;
printf("%s %d\n", e.name, e.age);
return 0;
}
A.Error: Lvalue required/incompatible types in assignment
B. Error: invalid constant expression
C. Error: Rvalue required
D.No error, Output: Ind 25

Answer & Explanation

Answer: Option A

Explanation:

We cannot assign a string to a struct variable like e.name = "Ind"; in C.


We have to use strcpy(char *dest, const char *source) function to assign a string.

Ex: strcpy(e.name, "Ind");

22. A union cannot be nested in a structure


[A].True [B].False

Answer: Option B

Explanation

The below code is legal


struct emp
{
int z;
union fac
{
int a;
};
};

23. If a char is 1 byte wide, an integer is 2 bytes wide and a long integer is 4 bytes wide then will
the following structure always occupy 7 bytes?

struct ex
{

Page 143
char ch;
int i;
long int a;
};
A.Yes B.No

Answer & Explanation

Answer: Option B

Explanation:

A compiler may leave holes in structures by padding the first char in the structure with another
byte just to ensures that the integer that follows is stored at an location. Also, there might be
2extra bytes after the integer to ensure that the long integer is stored at an address, which is
multiple of 4. Such alignment is done by machines to improve the efficiency of accessing values.

Page 144
Typedef
1.In the following code, the P2 is Integer Pointer or Integer?

typedef int *ptr;


ptr p1, p2;
A.Integer B. Integer pointer
C. Error in declaration D.None of above

Answer & Explanation

Answer: Option B

Explanation:

Here ptr is of int * type so ptr is alias to int *. P1,p2 are of ptr type so p1,p2 are int * type. Ie pte
is integer pointer or pointer to an integer.

2. What will be the output of the program?

#include<stdio.h>

int main()
{
typedef int arr[5];
arr iarr = {1, 2, 3, 4, 5};
int i;
for(i=0; i<=4; i++)
printf("%d,", iarr[i]);
return 0;
}
[A].1, 2, 3, 4
[B]. 1, 2, 3, 4, 5,
[C].No output
[D].Error: Cannot use typedef with an array

Answer: Option B

Explanation:

Typedef can be used even for arryas also. Arr is alias to int[5] so irr is integer array
arr iarr = {1, 2, 3, 4, 5}; is treated as

Page 145
int iarr[5]={1,2,3,4,5};
3.Will the following code compile
#define max 5
#define int arr1[max]
main()
{
typedef char arr2[max];
arr1 list={0,1,2,3,4};
arr2 name="name";
printf("%d %s",list[0],name);
}

A. Yes B. No

Answer:
B. No
Compiler error (in the line arr1 list = {0,1,2,3,4})
Explanation:
arr2 is declared of type array of size 5 of characters. So it can be used to
declare the variable name of the type arr2. But it is not the case of arr1.
Hence an error.
Rule of Thumb:
#defines are used for textual replacement whereas typedefs are used for
declaring new types.

4. What will be the output of the program?

#include<stdio.h>

int main()
{
typedef float f;
f *fptr;
float fval = 900;
fptr = &fval;
printf("%f\n", *fptr);
return 0;
}
A.9 B. 0
C. 900.000000 D.90

Answer & Explanation

Answer: Option C

Explanation:

Page 146
f is the new name of float. So fptr is of f* (ie float * type) so it can store float address of fval.

5. Is the following declaration acceptable?

typedef long no, *ptrtono;


no n;
ptrtono p;
A.Yes B.NO

Answer & Explanation

Answer: Option A

Here no is long type and ptrtono is of long* type and the declaration is correct.

6. typedef's have the advantage that they obey scope rules, that is they can be declared local to a
function or a block whereas #define's always have a global effect.

[A].Yes [B].No

Answer: Option A

Explanation:

Lets us see simple example


Void main()
{
typedef int x;
#define a 100
}
Here x is having local scope.
Where as a is having global scope even though it is written in main()

7. What will be the output of the program?

#include<stdio.h>
int main()
{
int y=12;
const int x=y;
printf("%d\n", x);
return 0;
}
A.12 B. Garbage value
C. Error D.0

Page 147
Answer & Explanation

Answer: Option A

Explanation:

Step 1: int y=12; The variable 'y' is declared as an integer type and initialized to value "12".

Step 2: const int x=y; The constant variable 'x' is declared as an integer and it is initialized with
the variable 'y' value.

Step 3: printf("%d\n", x); It prints the value of variable 'x'.

Hence the output of the program is "12"

8. What will be the output of the program?

#include<stdio.h>
int main()
{
const int x=5;
const int *ptrx;
ptrx = &x;
*ptrx = 10;
printf("%d\n", x);
return 0;
}
A.5 B. 10
C. Error D.Garbage value

Answer & Explanation

Answer: Option C

Explanation:

Step 1: const int x=5; The constant variable x is declared as an integer data type and initialized
with value '5'.

Step 2: const int *ptrx; The constant variable ptrx is declared as an integer pointer.

Step 3: ptrx = &x; The address of the constant variable x is assigned to integer pointer variable
ptrx.

Step 4: *ptrx = 10; Here we are indirectly trying to change the value of the constant vaiable x.
This will result in an error.

Page 148
To change the value of const variable x we have to use *(int *)&x = 10;

9. What will be the output of the program?

#include<stdio.h>
int main()
{
const char *s = "";
char str[] = "Hello";
s = str;
while(*s)
printf("%c", *s++);

return 0;
}
A.Error B. H
C. Hello D.Hel

Answer & Explanation

Answer: Option C

Explanation:

Step 1: const char *s = ""; The constant variable s is declared as an pointer to an array of
characters type and initialized with an empty string.

Step 2: char str[] = "Hello"; The variable str is declared as an array of charactrers type and
initialized with a string "Hello".

Step 3: s = str; The value of the variable str is assigned to the variable s. Therefore str contains
the text "Hello".

Step 4: while(*s){ printf("%c", *s++); } Here the while loop got executed untill the value of the
variable s is available and it prints the each character of the variable s.

Hence the output of the program is "Hello".

10. What will be the output of the program?

#include<stdio.h>
int get();
int main()
{
const int x = get();
printf("%d", x);
return 0;

Page 149
}
int get()
{
return 2;
}
A.Garbage value B. Error
C. 2 D.0

Answer & Explanation

Answer: Option C

Explanation:

Step 1: int get(); This is the function prototype for the funtion get(), it tells the compiler returns
an integer value and accept no parameters.

Step 2: const int x = get(); The constant variable x is declared as an integer data type and
initialized with the value "2".

The function get() returns the value "2".

Step 3: printf("%d", x); It prints the value of the variable x.

Hence the output of the program is "2".

11. What will be the output of the program (in Turbo C)?

#include<stdio.h>
int fun(int *f)
{
*f = 100;
return 0;
}
int main()
{
const int arr[5] = {1, 2, 3, 4, 5};
printf("Before modification arr[3] = %d", arr[3]);
fun(&arr[3]);
printf("\nAfter modification arr[3] = %d", arr[3]);
return 0;
}
Before modification arr[3] = 4
A.
After modification arr[3] = 100
B. Error: cannot convert parameter 1 from const int * to int *
C. Error: Invalid parameter

Page 150
Before modification arr[3] = 4
D.
After modification arr[3] = 4

Answer & Explanation

Answer: Option A

Explanation:

Step 1: const int arr[5] = {1, 2, 3, 4, 5}; The constant variable arr is declared as an integer array
and initialized to

arr[0] = 1, arr[1] = 2, arr[2] = 3, arr[3] = 4, arr[4] = 5

Step 2: printf("Before modification arr[3] = %d", arr[3]); It prints the value of arr[3] (ie. 4).

Step 3: fun(&arr[3]); The memory location of the arr[3] is passed to fun() and arr[3] value is
modified to 100.

A const variable can be indirectly modified by a pointer.

Step 4: printf("After modification arr[3] = %d", arr[3]); It prints the value of arr[3] (ie. 100).

Hence the output of the program is

Before modification arr[3] = 4

After modification arr[3] = 100

Preprocessor
1. #define square(x) x*x
void main()

Page 151
{
int i;
i = 64/square(4);
printf("%d",i);
}

A. 64 B.4 C. 16 D. error
Answer:
64
Explanation:

the macro call square(4) will substituted by 4*4 so the expression becomes i =64/4*4 . Since /
and * has equal priority the expression will be evaluated as (64/4)*4 i.e. 16*4= 64

2. What will the SWAP macro in the following program be expanded to on preprocessing? will
the code compile?

#include<stdio.h>
#define SWAP(a, b, c)(c t; t=a, a=b, b=t)
int main()
{
int x=10, y=20;
SWAP(x, y, int);
printf("%d %d\n", x, y);
return 0;
}
A. It compiles
B. Compiles with an warning
C. Not compile
D. Compiles and print nothing
Answer & Explanation
Answer: Option C

Explanation:

The code won't compile since declaration of t cannot occur within parenthesis.

3. #define int char


void main()
{
int i=65;
printf("sizeof(i)=%d",sizeof(i));
}
Answer:
sizeof(i)=1

Page 152
A. 2 B. 1 C. 0 D. none of the above
Explanation:
Since the #define replaces the string int by the macro char

4.In which stage the following code


#include<conio.h>
gets replaced by the contents of the file conio.h

A. During editing B. During linking


C. During execution D.During preprocessing
Answer & Explanation
Answer: Option D

Explanation:

The preprocessor replaces the line #include <conio.h> with the system header file of that name.
More precisely, the entire text of the file 'conio.h' replaces the #include directive

4. Point out the error in the program

#include<stdio.h>
int main()
{
int i;
#if A
printf("Enter any number:");
scanf("%d", &i);
#elif B
printf("The number is odd");
return 0;
}
A. Error: unexpected end of file because there is no matching #endif
B. The number is odd
C. Garbage values
D. None of above
Answer & Explanation
Answer: Option A

Explanation:

The conditional macro #if must have an #endif. In this program there is no #endif statement
written.

5. #include <stdio.h>
#define a 10
main()
{

Page 153
#define a 50
printf("%d",a);
}

A. 0 B.50 C.garbagevalue D.error

Answer:
50
Explanation:
The preprocessor directives can be redefined anywhere in the program. So
the most recently assigned value will be taken.

6. #define clrscr() 100


void main()
{
clrscr();
printf("%d\n",clrscr());
}

A 100 B. 0 C.garbagevalue D.error


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);
}
Note:
100; is an executable statement but with no action. So it doesn't give any
problem

7.Will the following code compile


#define f(g,g2) g##g2
void main()
{
int var12=100;
printf("%d",f(var,12));
}

A. Yes B. No

Answer:
100

Page 154
Explanation:
#define f(g,g2) g##g2
g##g2 means token pasting so it will become gg2
similarly f(var,12) will become var12
so printf(%d,var12);give 100as output

8. Will the following code compile


void main()
{
char not;
not=!2;
printf("%d",not);
}

A. Yes B. No

Answer:
A.Yes

Output: 0
Explanation:
! is a logical operator. In C the value 0 is considered to be the boolean value
FALSE, and any non-zero value is considered to be the boolean value TRUE.
Here 2 is a non-zero value so TRUE. !TRUE is FALSE (0) so it prints 0.

9) #define FALSE -1
#define TRUE 1
#define NULL 0
main() {
if(NULL)
puts("NULL");
else if(FALSE)
puts("TRUE");
else
puts("FALSE");
}

A.TRUE B.FALSE C.NULL D.error

Answer:
A. TRUE
Explanation:

Page 155
The input program to the compiler after processing by the preprocessor is,
main(){
if(0)
puts("NULL");
else if(-1)
puts("TRUE");
else
puts("FALSE");
}
Preprocessor doesn't replace the values given inside the double quotes. The
check by if condition is boolean value false so it goes to else. In second if -1
is boolean value true hence "TRUE" is printed.

Bitwise Operator
1. In which numbering system can the binary number 011111000101 be easily converted to?

A.Decimal system B. Hexadecimal system


C. Octal system D.No need to convert

Answer & Explanation

Page 156
Answer: Option B

Explanation:

Hexadecimal system is better, because each 4-digit binary represents one Hexadecimal digit.

2. Which bitwise operator is suitable for turning off a particular bit in a number?

A.|| operator B. & operator


C. && operator D.! operator

Answer & Explanation

Answer: Option B

Explanation
Any bit AND(&) with 0 will give a zero .i.e. will turn that particular bit OFF.

3.Assunming, integer is 2 byte, What will be the output of the program?


#include<stdio.h>
int main()
{
printf("%x\n", -1>>1);
return 0;
}
A.ffff B. 0fff
C. 0000 D.fff0

Answer & Explanation

Answer: Option A

Explanation:

Negative numbers are treated with 2's complement method.

1's complement: Inverting the bits ( all 1s to 0s and all 0s to 1s)


2's complement: Adding 1 to the result of 1's complement.

Binary of 1(2byte) : 0000 0000 0000 0001


Representing -1:
1s complement of 1(2byte) : 1111 1111 1111 1110
Adding 1 to 1's comp. result : 1111 1111 1111 1111
Right shift 1bit(-1>>1): 1111 1111 1111 1111 (carry out 1)
Hexadecimal :f f f f
(Filled with 1s in the left side in the above step)

Page 157
Note:

1. Fill with 1s in the left side for right shift for negative numbers.
2. Fill with 0s in the right side for left shift for negative numbers.
3. Fill with 0s in the left side for right shift for positive numbers.
4. Fill with 0s in the right side for left shift for positive numbers.

4. If an unsigned int is 2 bytes wide then, What will be the output of the program ?

#include<stdio.h>
int main()
{
unsigned int m = 32;
printf("%x\n", ~m);
return 0;
}
A.ffff B. 0000
C. ffdf D.ddfd

Answer & Explanation

Answer: Option C

Explanation:

32 can be written as 0000 0000 0010 0000 in 2 bytes and complement operator convert this to
1111 1111 1101 1111 and it is printed with %x format hence option c.

5.If an unsigned int is 2 bytes wide then, What will be the output of the program ?

#include<stdio.h>
int main()
{
unsigned int a=0xffff;
~a;
printf("%x\n", a);
return 0;
}
A.ffff B. 0000
C. 00ff D.ddfd

Answer & Explanation

Answer: Option A

Explanation:

Page 158
Here the modification done to a is not reassigned so a value is not affected.

6.What will be the output of the program?

#include<stdio.h>
int main()
{
unsigned char i = 0x80;
printf("%d\n", i<<1);
return 0;
}
A.0 B. 256
C. 100 D.80

Answer & Explanation

Answer: Option B

Explanation:

i = 0x80 = 00000000 10000000 in binary form.


After i<<1 it becomes 00000001 00000000. Its decimal equivallent is 256.

7. What will be the output of the program?

#define P printf("%d\n", -1^~0);


#define M(P) int main()\
{\
P\
return 0;\
}
M(P)
[A].1 [B]. 0
[C].-1 [D].2

Answer: Option B

Explanation:

After the First macro function executed


#define P printf("%d\n", -1^~0);
#define M(P) int main()\
{\
P\
return 0;\
}

Page 159
M(printf("%d\n", -1^~0);)

After the second Macro function executed


#define P printf("%d\n", -1^~0);
#define M(P) int main()\
{\
P\
return 0;\
}
int main()
{
printf("%d\n", -1^~0);
return 0;
}
~0 is -1
-1^-1 is 0

8. Which of the following statements are correct about the program?

#include<stdio.h>
int main()
{
unsigned int num;
int i;
scanf("%u", &num);
for(i=0; i&lt16; i++)
{
printf("%d", (num<<i & 1<<15)?1:0);
}
return 0;
}
A.It prints all even bits from num
B. It prints all odd bits from num
C. It prints binary equivalent num
D.Error

Answer & Explanation

Answer: Option C

Explanation:

If we give input 4, it will print 00000000 00000100 ;


If we give input 3, it will print 00000000 00000011 ;
If we give input 511, it will print 00000001 11111111 ;

9.w hich of the following statements are correct about the program?

Page 160
#include<stdio.h>
int main()
{
unsigned int num;
int c=0;
scanf("%u", &num);
for(;num;num>>=1)
{
if(num & 1)
c++;
}
printf("%d", c);
return 0;
}
A.It counts the number of bits that are ON (1) in the number num.
B. It counts the number of bits that are OFF (0) in the number num.
C. It sets all bits in the number num to 1
D.Error

Answer & Explanation

Answer: Option A

Explanation:

If we give input 4, it will print 1.


Binary-4 == 00000000 00000100 ; Total number of bits = 1.
If we give input 3, it will print 2.
Binary-3 == 00000000 00000011 ; Total number of bits = 2.
If we give input 511, it will print 9.
Binary-511 == 00000001 11111111 ; Total number of bits = 9.

Command Line Arguments


1.According to ANSI specifications which is the correct way of declaring main when it receives
command-line arguments?
A.int main(int argc, char *argv[]){}
int main(argc, argv){
B.
int argc; char *argv;}
int main()
{
C.
int argc; char *argv;
}
D.None of above

Page 161
Answer & Explanation

Answer: Option A

2. The maximum combined length of the command-line arguments including the spaces between
adjacent arguments is

A.128 characters
B. 256 characters
C. 64 characters
D.It may vary from one operating system to another

Answer & Explanation

Answer: Option D

Explanation:

The command promt is based on OS.

3. What will be the output of the program (my.c) given below if it is executed from the command
line?
cmd> my one two three

/* my.c */
#include<stdio.h>

int main(int argc, char **argv)


{
printf("%c\n", **++argv);
return 0;
}
A.my one two three B. my one
C. o D.two

Answer & Explanation

Answer: Option C

Explanation
argv[0] = my
argv[1] = one

So ++argv=> argv[1]. That is it points to 2 nd word ie one


**++argv gives first char in the second word

Page 162
4. What will be the output of the program (sam.c) given below if it is executed from the
command line?
cmd> sam 1 2 3

/* sam.c */
#include<stdio.h>

int main(int argc, char *argv[])


{
int j;
j = argv[1] + argv[2] + argv[3];
printf("%d", j);
return 0;
}
[A].6 [B]. sam 6
[C].Error [D].Garbage value

Answer: Option C

Explanation:

Here argv[1],argv[2],argv[3] are strings so we cannot add. use atoi() to convert string to int
As shown below
atoi(argv[1])+atoi(argv[2])

5. What will be the output of the program (sam.c) given below if it is executed from the
command line?
cmd> sam "*.c"

/* sam.c */
#include<stdio.h>
int main(int argc, int *argv)
{
int i;
for(i=1; i<argc; i++)
printf("%s\n", argv[i]);
return 0;
}
A.*.c
B. "*.c"
C. sam *.c
D.List of all files and folders in the current directory

Answer & Explanation

Answer: Option A

Page 163
*.c is the 1st argument so it is printed on the screen

6.What will be the output of the program if it is executed like below?


cmd> sam

/* sam.c */
#include<stdio.h>
int main(int argc, char **argv)
{
printf("%s\n", argv[argc-1]);
return 0;
}
A.0 B. sam
C. samp D.No output

Answer & Explanation

Answer: Option B

Explanation:

argc by default contain 1. So argv[argc-1] is taken as argv[0] the 1 st argument is by default the
program name so the output is sam
7. What will be the output of the program (sam.c) given below if it is executed from the
command line?
cmd> sam one two three

/* sam.c */
#include<stdio.h>
int main(int argc, char *argv[])
{
int i=0;
i+=strlen(argv[1]);
while(i>0)
{
printf("%c", argv[1][--i]);
}
return 0;
}
[A].three two one [B]. owt
[C].eno [D].eerht

Answer: Option C

Explanation:

Page 164
Argv[1] contains one

i=i+strlen(argv[1]); //i=0+3;
while(i>0) //while(3>0)
argv[1][--i] //argv[1][3] =>e and
i value decremented to 2[--i]
argv[1][2]=>n
and i=1[--i]
argv[1][1]=>o

o/p:eno

8. Every time we supply new set of values to the program at command prompt. we need to
recompile the program.

[A].True [B].False

Answer: Option B

Explanation:

The command line arguments are entered after compilation and execution we are not changing
the program code so there is no need to recompile .

9. Even if integer/float arguments are supplied at command prompt they are treated as strings.

A.True B.False

Answer & Explanation

Answer: Option A

Explanation:

The command line arguments are by default strings


As char*[]argv that is array of string addresses.

Page 165

Potrebbero piacerti anche