Technical Aptitude
Technical Aptitude
Subject: C Fundamentals
1. Point out the type of error, if any in the following program.
main( )
{
extern int i ;
i = 20 ;
printf ( "%d", sizeof ( i ) ) ;
}
a) Compile Time Error b) Run Time Error c) No Error d) Linker Error
2. Consider the following program segment.
main( )
{
int a = 5, b = 10 ;
P(a);
P(b);
}
Define the macro P such that the output of the program comes out to be a = 5 b = 10
void main ()
{
int i = 0 , a[3] ;
a[i] = i++;
printf (%d",a[i]) ;
}
8. Predict the output or error
#define clrscr() 100
main()
{
clrscr();
printf("%d\n",clrscr());
}
10. enum colors {BLACK,BLUE,GREEN}
main()
{
printf("%d..%d..%d",BLACK,BLUE,GREEN);
return(1);
}
11. main()
{
int i;
printf("%d",scanf("%d",&i)); // value 10 is given as input here
}
12. main()
{
int a=3, b = 4, c, d;
c = a,b;
d = (a,b);
printf(%d %d,c,d);
}
13. What will be the output of the following program :
void main()
{
char str[]="C For Swimmers";
printf("%d",-sizeof(str));
}
(a)14 (b)Compile-Time Error (c)-15 (d)-14
14. What will be the output of the following program :
void main()
{
printf("%d",!(100==100)+1);
}
(a)100 (b)0 (c)1 (d)2
15. What will be the output of the following program :
void main()
{
printf("%d %d %d",5,!5,25-!25);
}
(a)5 10 22 (b)5 5 25 (c)5 0 25 (d)5 1 24
16) What will be the output of the following program:
int main()
{
int a=500,b=100,c=30,d=40,e=19;
a+=b-=c*=d/=e%=5;
printf("%d %d %d %d %d",a,b,c,d,e);
}
(a)500 100 30 40 4 (b)Run-Time Error (c)700 200 300 10 4 (d)300 -200 300 10 4
17) What will be the output of the following program:
void main()
{
int a,b,c;
scanf("%1d %2d %3d",&a,&b,&c);
printf("Sum=%d",a+b+c);
}
[NOTE : THE USER INPUT IS :123456 44 544]
(a)Sum=480 (b)Sum=594 (c)Sum=589 (d)None of these
18. What happens when the following program is executed?
void main()
{
char line[80];
scanf("%[^1234567890\n]",line);
}
(a) Accepts the string that contains DIGITS only.
(b) Accepts the string that contains DIGITS and NEWLINE characters.
(c) Accepts the string that contains anything other than the DIGITS and NEWLINE
characters.
(d)None of these
20.
void main()
{
char *s="\12345s\n";
printf("%d",sizeof(s));
}
a) 8 b) 7 c) 6 d)5
Answer:
1) Linker Error : Undefined Symbol i.
By 'extern int i' we mean that the variable i has been declared before the main( )
function or in some other file. Here i is not declared before main( ) function, therefore it
is assumed that i has been defined in some other file. Since the file is not included by
#include, hence the Linker Error.
4) 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, in directing it with * is same as
s[i]. i[s] may be surprising. But in the case of C it is same as s[i].
5) I hate U
Explanation:
For floating point numbers (float, double, long double) the values cannot
be predicted exactly. Depending on the number of bytes, the precession with of the value
represented varies. Float takes 4 bytes and long double takes 10 bytes. So float stores 0.9
with less precision than long double.
6) 54321
Explanation:
When static storage class is given, it is initialized once. The change in the
value of a static variable is retained even between the function calls. Main is also treated
like any other ordinary function, which can be called recursively.
7) A garbage value.
Explanation:
In the statement a[i] = i++; the value of the variable i would get assigned first to
a[i] i.e. a[0] and then the value of i would get incremented by 1. Since a[i] i.e. a[1] has
not been initialized, a[i] will have a garbage value.
8) 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);
}
100; is an executable statement but with no action. So it doesn't give any problem
9) 0..1..2
Explanation:
enum assigns numbers starting from 0, if not explicitly defined.
11. 1
Explanation:
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.
12. 3 4
13. d
14. c
15. c
16. d
17. a
18. c
19. c
20. c