Some C Aptitude questions that are often questioned in interviews are listed below.
You can find the answers at the bottom of the page.
Questions :
1. What can be
said of the following program ?
main()
{
enum Months {JAN =1,FEB,MAR,APR};
Months X = JAN;
if(X==1)
{
printf("Jan is the first month");
}
}
a) Does not print anything
b) Prints : Jan is the first month
c) Generates compilation error
d) Results in runtime error
main()
{
enum Months {JAN =1,FEB,MAR,APR};
Months X = JAN;
if(X==1)
{
printf("Jan is the first month");
}
}
a) Does not print anything
b) Prints : Jan is the first month
c) Generates compilation error
d) Results in runtime error
2.main()
{
extern
int i;
i=20;
printf("%d",i);
}
a) 20
b) Garbage Value
c) Compilation error
d) Linker error
a) 20
b) Garbage Value
c) Compilation error
d) Linker error
3. enum colors {BLACK,BLUE,GREEN}
main()
{
printf("%d..%d..%d",BLACK,BLUE,GREEN);
return(1);
}
a) 5..4..5
b) Garbage Value
c) 0..1..2
d) Error
a) 5..4..5
b) Garbage Value
c) 0..1..2
d) Error
4.
Given the following statement enum day = { jan = 1 ,feb=4, april, may} What is
the value of may?
(a) 4
(b) 5
(c) 6
(d) 11
(e) None of the above
(a) 4
(b) 5
(c) 6
(d) 11
(e) None of the above
5. What is the output of the following program?
main()
{
int l=6;
switch(l)
{ default : l+=2;
case 4: l=4;
case 5: l++;
break;
}
printf("%d",l);
}
a)8 b)6 c)5 d)4 e)none
6. main()
{
int i=3;
switch(i)
{
default:printf("zero");
case 1: printf("one");
break;
case 2:printf("two");
break;
case 3: printf("three");
break;
}
}
a) zero
b) three
c) Compiler error
d) Runtime error
a) zero
b) three
c) Compiler error
d) Runtime error
7. #include<stdio.h>
main()
{
int i=1,j=2;
switch(i)
{
case 1:
printf("GOOD");
break;
case j:
printf("BAD");
break;
}
}
a) GOOD
b) BAD
c) Compiler error
d) Runtime error
8. main()
{
float
i=1.5;
switch(i)
{
case
1: printf("1");
case
2: printf("2");
default
: printf("0");
}
}
a) 1
b) 0
c) Compiler error
d) Runtime error
a) 1
b) 0
c) Compiler error
d) Runtime error
main()
{
int i=0;
for(i=0;i<20;i++)
{
switch(i)
case 0:i+=5;
case 1:i+=2;
case 5:i+=5;
default i+=4;
break;
}
printf("%d,",i);
}
}
a) 0,5,9,13,17
b) 5,9,13,17
c) 12,17,22
d) 16,21
e) Syntax error
10. main()
{
int i;
for(i=0;i<3;i++)
switch(i)
{
case 1:
printf("%d",i);
case 2 :
printf("%d",i);
default:
printf("%d"i);
}
}
a) 0111222
b) Error
c)012
d)011122
a) 0111222
b) Error
c)012
d)011122
11. what is printed when the following program is compiled and executed?
int
func (int x)
{
if (x<=0)
return(1);
return func(x -1) +x;
}
main()
{
printf("%d\n",func(5));
}
a) 12
b) 16
c) 15
d) 11
{
if (x<=0)
return(1);
return func(x -1) +x;
}
main()
{
printf("%d\n",func(5));
}
a) 12
b) 16
c) 15
d) 11
12. What is the
output of the following program?
main()
{
int x=20;
int y=10;
swap(x,y);
printf("%d %d",y,x+2);
}
swap(int x,int y)
{
int temp;
temp =x;
x=y;
y=temp;
}
main()
{
int x=20;
int y=10;
swap(x,y);
printf("%d %d",y,x+2);
}
swap(int x,int y)
{
int temp;
temp =x;
x=y;
y=temp;
}
a) 10,20 b) 20,12 c) 22,10
d)10,22 e)none
13. Which of the following about the following two
declaration is true
i ) int *F()
ii) int (*F)()
Choice :
a) Both are identical
b) The first is a correct declaration and the second is wrong
c) The first declaraion is a function returning a pointer to an integer and the second is a pointer to function returning int
d) Both are different ways of declarin pointer to a function
i ) int *F()
ii) int (*F)()
Choice :
a) Both are identical
b) The first is a correct declaration and the second is wrong
c) The first declaraion is a function returning a pointer to an integer and the second is a pointer to function returning int
d) Both are different ways of declarin pointer to a function
14. main()
{
printf("%p",main);
}
a) Compiler error
b) Stack Overflow
c) Some address will be printed
d) Garbage value printed recursively
b) Stack Overflow
c) Some address will be printed
d) Garbage value printed recursively
15. main()
{
clrscr();
}
clrscr();
a) Syntax error
b) No output/error
c) Stack Overflow
d) Linker error
a) Syntax error
b) No output/error
c) Stack Overflow
d) Linker error
16. main()
{
int
i;
printf("%d",scanf("%d",&i)); // value 10 is given as input here
}
a) Error
b) 1
c) 2
d) 4
a) Error
b) 1
c) 2
d) 4
17. main()
{
show();
}
void
show()
{
printf("I'm the greatest");
}
a) prints I'm the greatest
b) Compiler error
c) Runtime error
d) prints 1
a) prints I'm the greatest
b) Compiler error
c) Runtime error
d) prints 1
18. main()
{
main();
}
a) Syntax error
b) No output/error
c) Stack Overflow
d) Linker error
19. What are the following notations of
defining functions known as?
i. int abc(int a,float b)
{
/* some code */
}
ii. int abc(a,b)
int a; float b;
{
/*
some code*/
}
20.
What is printed when this program is executed
main()
{
printf ("%d\n",f(7));
}
f(X)
{
if ( x<= 4)
return x;
return f(--x);
}
a) 4
b)5
c) 6
d)
7
Answers :
1 . b
2. Linker Error : Undefined symbol '_i'
extern
storage class in the following declaration,
extern int i;
specifies
to the compiler that the memory for i
is allocated in some other program and that address will be given to the
current program at the time of linking. But linker finds that no other variable
of name i is available in any other
program with memory space allocated for it. Hence a linker error has occurred .
3. 0..1..2
enum
assigns numbers starting from 0, if not explicitly defined.
4. c
5. c
6. three
The
default case can be placed anywhere inside the loop. It is executed only when
all other cases doesn't match.
7. Compiler
Error: Constant expression required in function main.
The
case statement can have only constant expressions (this implies that we cannot
use variable names directly so an error). Enumerated
types can be used in case statements.
8. Compiler Error: switch
expression not integral
Switch statements can be applied only to
integral types.
9. d
10. 011122
11. b
12. d
13. c
14. Some address will be
printed.
Function names are just addresses (just like array names are addresses).
Function names are just addresses (just like array names are addresses).
main() is also a
function. So the address of function main will be printed. %p in printf
specifies that the argument is an address. They are printed as hexadecimal
numbers.
15. No
output/error
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).
16. 1
Scanf returns number of items successfully read and not 1/0. Here 10 is given as input which should have been scanned successfully. So number of items read is 1.
Scanf returns number of items successfully read and not 1/0. Here 10 is given as input which should have been scanned successfully. So number of items read is 1.
17. Compier
error: Type mismatch in redeclaration of show.
When
the compiler sees the function show it doesn't know anything about it. So the
default return type (ie, int) is assumed. But when compiler sees the actual
definition of show mismatch occurs since it is declared as void. Hence the
error.
The
solutions are as follows:
1.
declare void show() in main() .
2.
define show() before main().
3.
declare extern void show() before the use of show().
18. Runtime error : Stack overflow.
main
function calls itself again and again. Each time the function is called its
return address is stored in the call stack. Since there is no condition to
terminate the function call, the call stack overflows at runtime. So it
terminates the program and results in an error.
19. i. ANSI C notation
ii. Kernighan & Ritche notation
Hope this is useful :)
No comments:
Post a Comment