You are on page 1of 6

some tricky c questions

How do you write a program which produces its own source code as its output?
How can I find the day of the week given the date?
Why doesn't C have nested functions?
What is the most efficient way to count the number of bits which are set in a value?
How can I convert integers to binary or hexadecimal?
How can I call a function, given its name as a string?
How do I access commandline arguments?
How can I return multiple values from a function?
How can I invoke another program from within a C program?
How can I access memory located at a certain address?
How can I allocate arrays or structures bigger than !"#?
How can I find out how much memory is available?
How can I read a directory in a C program?
How can I increase the allowable number of simultaneously open files?
What's wrong with the call fopen$%c&'newdir'file(dat%, %r%)?
c/c++ q's
What is the output of printf$%*d%)
What will happen if I say delete this
+ifference between %C structure% and %C,, structure%(
+iffrence between a %assignment operator% and a %copy constructor%
What is the difference between %overloading% and %overridding%?
-xplain the need for %.irtual +estructor%(
Can we have %.irtual Constructors%?
What are the different types of polymorphism?
What are .irtual /unctions? How to implement virtual functions in %C%
What are the different types of 0torage classes?
What is 1amespace?
What are the types of 023 containers?(
+ifference between %vector% and %array%?
How to write a program such that it will delete itself after exectution?
Can we generate a C,, source code from the binary file?
What are inline functions?
What is %strstream% ?
-xplain %passing by value%, %passing by pointer% and %passing by reference%
Have you heard of %mutable% keyword?
What is a %422I%?
Is there something that I can do in C and not in C,,?
What is the difference between %calloc% and %malloc%?
What will happen if I allocate memory using %new% and free it using %free% or allocate sing %calloc% and free it
using %delete%?
+ifference between %printf% and %sprintf%(
What is %map% in 023?
When shall I use 5ultiple Inheritance?
-xplain working of printf(
2alk sometiming about profiling?
How many lines of code you have written for a single program?
How to write 5ultithreaded applications using C,,?
Write any small program that will compile in %C% but not in %C,,%
What is 5emory 6lignment?
Why preincrement operator is faster than postincrement?
What are the techni7ues you use for debugging?
How to reduce a final si8e of executable?
9ive : examples of a code optimi8ation(
What is ;olymorphism
';olymorphism' is an ob<ect oriented term( ;olymorphism may be defined as the ability of related ob<ects to
respond to the same message with different, but appropriate actions( In other words, polymorphism means
taking more than one form( ;olymorphism leads to two important aspects in =b<ect =riented terminology
/unction =verloading and /unction =verriding( =verloading is the practice of supplying more than one
definition for a given function name in the same scope( 2he compiler is left to pick the appropriate version of
the function or operator based on the arguments with which it is called( =verriding refers to the modifications
made in the sub class to the inherited methods from the base class to change their behaviour(
What is =perator overloading
When an operator is overloaded, it takes on an additional meaning relative to a certain class( >ut it can still
retain all of its old meanings(
?) 2he operators @@ and AA may be used for IB= operations because in the header, they are overloaded(
:) In a stack class it is possible to overload the , operattor so that it appends the contents of one stack to the
contents of another( >ut the , operator still retains its original meaning relative to other types of data(
What are 2emplates
C,, 2emplates allow u to generate families of functions or classes that can operate on a variety of different
data types, freeing you from the need to create a separate function or class for each type( Csing templates, u
have the convenience of writing a single generic function or class definition, which the compiler automatically
translates into a specific version of the function or class, for each of the different data types that your program
actually uses( 5any data structures and algorithms can be defined independently of the type of data they work
with( Dou can increase the amount of shared code by separating datadependent portions from data
independent portions, and templates were introduced to help you do that(
What is the difference between run time binding and compile time binding?
+ynamic >inding &
2he address of the functions are determined at runtime rather than E compile time( 2his is also known as
%3ate >inding%(
0tatic >inding &
2he address of the functions are determined at compile time rather than E run time( 2his is also known as
%-arly >inding%
What is +ifference >etween CBC,,
C does not have a classBob<ect concept(
C,, provides data abstraction, data encapsulation, Inheritance and ;olymorphism(
C,, supports all C syntax(
In C passing value to a function is %Call by .alue% whereas in C,, its %Call by 4eference%
/ile extention is (c in C while (cpp in C,,($C,, compiler compiles the files with (c extention but C compiler can
In C structures can not have contain functions declarations( In C,, structures are like classes, so declaring
functions is legal and allowed(
C,, can have inlineBvirtual functions for the classes(
c,, is C with Classes hence C,, while in c the closest u can get to an Cser defined data type is struct and
What will be the output of the following code?
void main $)
G int i H I , aJKL M
a = i++;
printf ("%d",a) ;
The output for the above code would be a garbage value. In the tate!ent a = i++; the value of the variable i
would get aigned firt to a i.e. a"#$ and then the value of i would get incre!ented b% &. 'ince a i.e. a"&$ ha
not been initiali(ed, a will have a garbage value.
)h% doen*t the following code give the deired reult+
int , = -###, % = .### ;
long int ( = , / % ;
0ere the !ultiplication i carried out between two int , and %, and the reult that would overflow would be
truncated before being aigned to the variable ( of t%pe long int. 0owever, to get the correct output, we hould
ue an e,plicit cat to force long arith!etic a hown below1
long int ( = ( long int ) , / % ;
2ote that ( long int )( , / % ) would not give the deired effect.
)h% doen*t the following tate!ent wor3+
char tr" $ = "0ello" ;
trcat ( tr, *4* ) ;
The tring function trcat( ) concatenate tring and not a character. The baic difference between a tring
and a character i that a tring i a collection of character, repreented b% an arra% of character wherea a
character i a ingle character. To !a3e the above tate!ent wor3 write the tate!ent a hown below1
trcat ( tr, "4" ) ;
0ow do I 3now how !an% ele!ent an arra% can hold+
The a!ount of !e!or% an arra% can conu!e depend on the data t%pe of an arra%. In 56' environ!ent, the
a!ount of !e!or% an arra% can conu!e depend on the current !e!or% !odel (i.e. Tin%, '!all, 7arge,
0uge, etc.). In general an arra% cannot conu!e !ore than 89 3b. :onider following progra!, which how
the !a,i!u! nu!ber of ele!ent an arra% of t%pe int, float and char can have in cae of '!all !e!or% !odel.
!ain( )
int i"-.<8<$ ;
float f"&8-=-$ ;
char "8>>->$ ;
0ow do I write code that read data at !e!or% location pecified b% eg!ent and offet+
?e pee3b( ) function. Thi function return b%te() read fro! pecific eg!ent and offet location in !e!or%.
The following progra! illutrate ue of thi function. In thi progra! fro! @5? !e!or% we have read
character and it attribute of the firt row. The infor!ation tored in file i then further read and dipla%ed
uing pee3( ) function.
Ainclude Btdio.hC
Ainclude Bdo.hC
!ain( )
char far /cr = #,D=###### ;
EI7F /fp ;
int offet ;
char ch ;
if ( ( fp = fopen ( "cr.dat", "wb" ) ) == 2?77 )
printf ( "Gn?nable to open file" ) ;
e,it( ) ;
HH read and write to file
for ( offet = # ; offet B &8# ; offet++ )
fprintf ( fp, "%c", pee3b ( cr, offet ) ) ;
fcloe ( fp ) ;
if ( ( fp = fopen ( "cr.dat", "rb" ) ) == 2?77 )
printf ( "Gn?nable to open file" ) ;
e,it( ) ;
HH read and write to file
for ( offet = # ; offet B &8# ; offet++ )
fcanf ( fp, "%c", Ich ) ;
printf ( "%c", ch ) ;
fcloe ( fp ) ;