Vous êtes sur la page 1sur 172

www.mallepoolu.

com
C is interviewers favorite language. If you have good command on C language
you can get through Technical Interview easily. Most of the interviewers ask basic
questions in C. Some times they may ask you to write small programs like reversal of a
string, determining whether the given number is a palindrome or not and finding
factorial of a given number using recursion etc. You should be able to write these
programs quickly.
Most favorite question for interviewers is interchanging two variables with out
using the third variable. Another favorite topic for interviewers is Pointers. Please go
through pointers once before going to the interview.
Many interviewers look for logic in your program rather than syntax. Once you
write the program you should be able to explain the logic to the interviewer with the help
of flow-chart or algorithm. If there are more than one logic for a single program, choose
the logic that is simple and easy to understand at the same time it should efficient one.
Below are around 100 FAQ in C language. Try to answer these questions on your
own. I believe that if you are able to answer these questions, you can easily get through
the technical interview.

1.
What will be the output of the following code?
void main ()
{ int i = 0 , a[3] ;
a[i] = i++;
printf (%d",a[i]) ;
}
Ans: The output for the above code would be a garbage value. 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.
------------------------------------------------------------------------------------------------2.
Why doesn't the following code give the desired result?
int x = 3000, y = 2000 ;
long int z = x * y ;
Ans: Here the multiplication is carried out between two ints x and y, and the result that would
overflow would be truncated before being assigned to the variable z of type long int.
However, to get the correct output, we should use an explicit cast to force long arithmetic as
shown below:
long int z = ( long int ) x * y ;
Note that ( long int )( x * y ) would not give the desired effect.

Interview Questions on C-Language

www.mallepoolu.com
------------------------------------------------------------------------------------------------3.
Why doesn't the following statement work?
char str[ ] = "Hello" ;
strcat ( str, '!' ) ;
Ans: The string function strcat( ) concatenates strings and not a character. The basic
difference between a string and a character is that a string is a collection of characters,
represented by an array of characters whereas a character is a single character. To make the
above statement work writes the statement as shown below:
strcat ( str, "!" ) ;
------------------------------------------------------------------------------------------------4.
How do I know how many elements an array can hold?
Ans: The amount of memory an array can consume depends on the data type of an array. In
DOS environment, the amount of memory an array can consume depends on the current
memory model (i.e. Tiny, Small, Large, Huge, etc.). In general an array cannot consume
more than 64 kb. Consider following program, which shows the maximum number of
elements an array of type int, float and char can have in case of Small memory model.
main( )
{
int i[32767] ;
float f[16383] ;
char s[65535] ;
}
------------------------------------------------------------------------------------------------5.
How do I write code that reads data at memory location specified by segment and offset?
Ans: Use peekb( ) function. This function returns byte(s) read from specific segment and
offset locations in memory. The following program illustrates use of this function. In this
program from VDU memory we have read characters and its attributes of the first row. The
information stored in file is then further read and displayed using peek( ) function.
#include <stdio.h>
#include <dos.h>
main( )
{
char far *scr = 0xB8000000 ;
FILE *fp ;
int offset ;
char ch ;

Interview Questions on C-Language

www.mallepoolu.com
if ( ( fp = fopen ( "scr.dat", "wb" ) ) == NULL )
{
printf ( "\nUnable to open file" ) ;
exit( ) ;
}
// reads and writes to file
for ( offset = 0 ; offset < 160 ; offset++ )
fprintf ( fp, "%c", peekb ( scr, offset ) ) ;
fclose ( fp ) ;
if ( ( fp = fopen ( "scr.dat", "rb" ) ) == NULL )
{
printf ( "\nUnable to open file" ) ;
exit( ) ;
}
// reads and writes to file
for ( offset = 0 ; offset < 160 ; offset++ )
{
fscanf ( fp, "%c", &ch ) ;
printf ( "%c", ch ) ;
}
fclose ( fp ) ;
}
------------------------------------------------------------------------------------------------6.
How do I compare character data stored at two different memory locations?
Ans: Sometimes in a program we require to compare memory ranges containing strings. In
such a situation we can use functions like memcmp( ) or memicmp( ). The basic difference
between two functions is that memcmp( ) does a case-sensitive comparison whereas
memicmp( ) ignores case of characters. Following program illustrates the use of both the
functions.
#include <mem.h>
main( )
{

Interview Questions on C-Language

www.mallepoolu.com
char *arr1 = "Kicit" ;
char *arr2 = "kicitNagpur" ;
int c ;
c = memcmp ( arr1, arr2, sizeof ( arr1 ) ) ;
if ( c == 0 )
printf ( "\nStrings arr1 and arr2 compared using memcmp are identical" ) ;
else
printf ( "\nStrings arr1 and arr2 compared using memcmp are not identical"
);
c = memicmp ( arr1, arr2, sizeof ( arr1 ) ) ;
if ( c == 0 )
printf ( "\nStrings arr1 and arr2 compared using memicmp are identical" )
;
else
printf ( "\nStrings arr1 and arr2 compared using memicmp are not
identical" ) ;
}
------------------------------------------------------------------------------------------------7.
Fixed-size objects are more appropriate as compared to variable size data objects. Using
variable-size data objects saves very little space. Variable size data objects usually have some
overhead. Manipulation of fixed-size data objects is usually faster and easier. Use fixed size
when maximum size is clearly bounded and close to average. And use variable-size data
objects when a few of the data items are bigger than the average size. For example,
char *num[10] = { "One", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight", "Nine", "Ten" } ;
Instead of using the above, use
char num[10][6] = { "One", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight", "Nine", "Ten" } ;
The first form uses variable-size data objects. It allocates 10 pointers, which are pointing to
10 string constants of variable size. Assuming each pointer is of 4 bytes, it requires 90 bytes.
On the other hand, the second form uses fixed size data objects. It allocates 10 arrays of 6
characters each. It requires only 60 bytes of space. So, the variable-size in this case does not
offer any advantage over fixed size.
------------------------------------------------------------------------------------------------8.
The Spawnl( ) function...

Interview Questions on C-Language

www.mallepoolu.com
DOS is a single tasking operating system, thus only one program runs at a time. The Spawnl(
) function provides us with the capability of starting the execution of one program from
within another program. The first program is called the parent process and the second
program that gets called from within the first program is called a child process. Once the
second program starts execution, the first is put on hold until the second program completes
execution. The first program is then restarted. The following program demonstrates use of
spawnl( ) function.
/* Mult.c */
int main ( int argc, char* argv[ ] )
{
int a[3], i, ret ;
if ( argc < 3 || argc > 3 )
{
printf ( "Too many or Too few arguments..." ) ;
exit ( 0 ) ;
}
for ( i = 1 ; i < argc ; i++ )
a[i] = atoi ( argv[i] ) ;
ret = a[1] * a[2] ;
return ret ;
}
/* Spawn.c */
#include <process.h>
#include <stdio.h>
main( )
{
int val ;
val = spawnl ( P_WAIT, "C:\\Mult.exe", "3", "10",
"20", NULL ) ;
printf ( "\nReturned value is: %d", val ) ;
}
Here, there are two programs. The program 'Mult.exe' works as a child process whereas
'Spawn.exe' works as a parent process. On execution of 'Spawn.exe' it invokes 'Mult.exe' and
passes the command-line arguments to it. 'Mult.exe' in turn on execution, calculates the
product of 10 and 20 and returns the value to val in 'Spawn.exe'. In our call to spawnl( )
function, we have passed 6 parameters, P_WAIT as the mode of execution, path of '.exe' file
to run as child process, total number of arguments to be passed to the child process, list of
command line arguments and NULL. P_WAIT will cause our application to freeze execution
until the child process has completed its execution. This parameter needs to be passed as the
default parameter if you are working under DOS. under other operating systems that support
multitasking, this parameter can be P_NOWAIT or P_OVERLAY. P_NOWAIT will cause
the parent process to execute along with the child process, P_OVERLAY will load the child
process on top of the parent process in the memory.

Interview Questions on C-Language

www.mallepoolu.com
------------------------------------------------------------------------------------------------9.
Are the following two statements identical?
char str[6] = "Kicit" ;
char *str = "Kicit" ;
Ans: No! Arrays are not pointers. An array is a single, pre-allocated chunk of contiguous
elements (all of the same type), fixed in size and location. A pointer on the other hand, is a
reference to any data element (of a particular type) located anywhere. A pointer must be
assigned to point to space allocated elsewhere, but it can be reassigned any time. The array
declaration char str[6] ; requests that space for 6 characters be set aside, to be known
by name str. In other words there is a location named str at which six characters are stored.
The pointer declaration char *str ; on the other hand, requests a place that holds a pointer, to
be known by the name str. This pointer can point almost anywhere to any char, to any
contiguous array of chars, or nowhere.
------------------------------------------------------------------------------------------------10.
Is the following code fragment correct?
const int x = 10 ;
int arr[x] ;
Ans: No! Here, the variable x is first declared as an int so memory is reserved for it. Then it is
qualified by a const qualifier. Hence, const qualified object is not a constant fully. It is an
object with read only attribute, and in C, an object associated with memory cannot be used in
array dimensions.
11.
How do I write code to retrieve current date and time from the system and display it as a
string?
Ans: Use time( ) function to get current date and time and then ctime( ) function to display it
as a string. This is shown in following code snippet.
#include <sys\types.h>
void main( )
{
time_t curtime ;
char ctm[50] ;
time ( &curtime ) ; //retrieves current time &
stores in curtime
printf ( "\nCurrent Date & Time: %s", ctime (
&curtime ) ) ;
}

Interview Questions on C-Language

www.mallepoolu.com
------------------------------------------------------------------------------------------------12.
How do I change the type of cursor and hide a cursor?
Ans: We can change the cursor type by using function _setcursortype( ). This function can
change the cursor type to solid cursor and can even hide a cursor. Following code shows how
to change the cursor type and hide cursor.
#include <conio.h>
main( )
{
/* Hide cursor */
_setcursortype ( _NOCURSOR ) ;
/* Change cursor to a solid cursor */
_setcursortype ( _SOLIDCURSOR ) ;
/* Change back to the normal cursor */
_setcursortype ( _NORMALCURSOR ) ;
}
------------------------------------------------------------------------------------------------13.
How do I write code that would get error number and display error message if any standard
error occurs?
Ans: Following code demonstrates this.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
main( )
{
char *errmsg ;
FILE *fp ;
fp = fopen ( "C:\file.txt", "r" ) ;
if ( fp == NULL )
{
errmsg = strerror ( errno ) ;
printf ( "\n%s", errmsg ) ;
}
}
Here, we are trying to open 'file.txt' file. However, if the file does not exist, then it would
cause an error. As a result, a value (in this case 2) related to the error generated would get set
in errno. errno is an external int variable declared in 'stdlib.h' and also in 'errno.h'. Next, we
have called sterror( ) function which takes an error number and returns a pointer to standard
error message related to the given error number.

Interview Questions on C-Language

www.mallepoolu.com
------------------------------------------------------------------------------------------------14.
How do I write code to get the current drive as well as set the current drive?
Ans: The function getdisk( ) returns the drive number of current drive. The drive number 0
indicates 'A' as the current drive, 1 as 'B' and so on. The Setdisk( ) function sets the current
drive. This function takes one argument which is an integer indicating the drive to be set.
Following program demonstrates use of both the functions.
#include <dir.h>
main( )
{
int dno, maxdr ;
dno = getdisk( ) ;
printf ( "\nThe current drive is: %c\n", 65 + dno
);
maxdr = setdisk ( 3 ) ;
dno = getdisk( ) ;
printf ( "\nNow the current drive is: %c\n", 65 +
dno ) ;
}
------------------------------------------------------------------------------------------------15.
The functions memcmp( ) and memicmp( )
The functions memcmp( ) and memicmp( ) compares first n bytes of given two blocks of
memory or strings. However, memcmp( ) performs comparison as unsigned chars whereas
memicmp( ) performs comparison as chars but ignores case (i.e. upper or lower case). Both
the functions return an integer value where 0 indicates that two memory buffers compared are
identical. If the value returned is greater than 0 then it indicates that the first buffer is bigger
than the second one. The value less than 0 indicate that the first buffer is less than the second
buffer. The following code snippet demonstrates use of both
#include <stdio.h>
#include <mem.h>
main( )
{
char str1[] = "This string contains some
characters" ;
char str2[] = "this string contains" ;
int result ;
result = memcmp ( str1, str2, strlen ( str2 ) ) ;
printf ( "\nResult after comapring buffer using

Interview Questions on C-Language

www.mallepoolu.com
memcmp( )" ) ;
show ( result ) ;
result = memicmp ( str1, str2, strlen ( str2 ) ) ;
printf ( "\nResult after comapring buffer using
memicmp( )" ) ;
show ( result ) ;
}
show ( int r )
{
if ( r == 0 )
printf ( "\nThe buffer str1 and str2 hold
identical data" ) ;
if ( r > 0 )
printf ( "\nThe buffer str1 is bigger than buffer
str2" ) ;
if ( r < 0 )
printf ( "\nThe buffer str1 is less than buffer
str2" ) ;
}
------------------------------------------------------------------------------------------------16.
How do I write code to find an amount of free disk space available on current drive?
Ans: Use getdfree( ) function as shown in follow code.
#include <stdio.h>
#include <stdlib.h>
#include <dir.h>
#include <dos.h>
main( )
{
int dr ; struct dfree disk ;
long freesp ;
dr = getdisk( ) ;
getdfree ( dr + 1 , &disk ) ;
if ( disk.df_sclus == 0xFFFF )
{
printf ( "\ngetdfree( ) function failed\n");
exit ( 1 ) ;
}
freesp = ( long ) disk.df_avail
* ( long ) disk.df_bsec

Interview Questions on C-Language

www.mallepoolu.com
* ( long ) disk.df_sclus ;
printf ( "\nThe current drive %c: has %ld bytes
available as free space\n", 'A' + dr, freesp ) ;
}
17.
Use of array indices...
If we wish to store a character in a char variable ch and the character to be stored depends on
the value of another variable say color (of type int), then the code would be as shown below:
switch ( color )
{
case 0 :
ch = 'R' ;
break ;
case 1 :
ch = 'G' ;
break ;
case 2 :
ch = 'B' ;
break ;
}
In place of switch-case we can make use of the value in color as an index for a character
array. How to do this is shown in following code snippet.
char *str = "RGB' ;
char ch ;
int color ;
// code
ch = str[ color ] ;
------------------------------------------------------------------------------------------------18.
Function atexit( ) recevies parameter as the address of function of the type void fun ( void ).
The function whose address is passed to atexit( ) gets called before the termination of
program. If atexit( ) is called for more than one function then the functions are called in "first
in last out" order. You can verify that from the output.
#include <stdio.h>
#include <stdlib.h>
void fun1( )
{
printf("Inside fun1\n");
}
void fun2( )
{
printf("Inside fun2\n");
}

Interview Questions on C-Language

www.mallepoolu.com
main( )
{
atexit ( fun1 ) ;
/* some code */
atexit ( fun2 ) ;
printf ( "This is the last statement of
program?\n" );
}
------------------------------------------------------------------------------------------------19.
How do I write a user-defined function, which deletes each character in a string str1, which
matches any character in string str2?
Ans: The function is as shown below:
Compress ( char str1[], char str2[] )
{
int i, j, k ;
for ( i = k = 0 ; str1[i] != \0 ; i++ )
{
for ( j = 0 ; str2[j] != \0 && str2[j] !=
str1[i] ; j++ )
;
if ( str2[j] == \0 )
str1[k++] = str1[I] ;
}
str1[k] = \0
}
------------------------------------------------------------------------------------------------20.
How does free( ) know how many bytes to free?
Ans: The malloc( ) / free( ) implementation remembers the size of each block allocated and
returned, so it is not necessary to remind it of the size when freeing.
------------------------------------------------------------------------------------------------21.
What is the use of randomize( ) and srand( ) function?
Ans: While generating random numbers in a program, sometimes we require to control the
series of numbers that random number generator creates. The process of assigning the
random number generators starting number is called seeding the generator. The randomize( )
and srand( ) functions are used to seed the random number generators. The randomize( )

Interview Questions on C-Language

www.mallepoolu.com
function uses PC's clock to produce a random seed, whereas the srand( ) function allows us to
specify the random number generator's starting value.
------------------------------------------------------------------------------------------------22.
How do I determine amount of memory currently available for allocating?
Ans: We can use function coreleft( ) to get the amount of memory available for allocation.
However, this function does not give an exact amount of unused memory. If, we are using a
small memory model, coreleft( ) returns the amount of unused memory between the top of the
heap and stack. If we are using a larger model, this function returns the amount of memory
between the highest allocated memory and the end of conventional memory. The function
returns amount of memory in terms of bytes.
------------------------------------------------------------------------------------------------23.
How does a C program come to know about command line arguments?
Ans: When we execute our C program, operating system loads the program into memory. In
case of DOS, it first loads 256 bytes into memory, called program segment prefix. This
contains file table, environment segment, and command line information. When we compile
the C program the compiler inserts additional code that parses the command, assigning it to
the argv array, making the arguments easily accessible within our C program.
24.
When we open a file, how does functions like fread( )/fwrite( ), etc. get to know from where
to read or to write the data?
Ans: When we open a file for read/write operation using function like fopen( ), it returns a
pointer to the structure of type FILE. This structure stores the file pointer called position
pointer, which keeps track of current location within the file. On opening file for read/write
operation, the file pointer is set to the start of the file. Each time we read/write a character,
the position pointer advances one character. If we read one line of text at a step from the file,
then file pointer advances to the start of the next line. If the file is opened in append mode,
the file pointer is placed at the very end of the file. Using fseek( ) function we can set the file
pointer to some other place within the file.
-------------------------------------------------------------------------------------------------

25.
The sizeof( ) function doesnt return the size of the block of memory pointed to by a pointer.
Why?
Ans: The sizeof( ) operator does not know that malloc( ) has been used to allocate a pointer.
sizeof( ) gives us the size of pointer itself. There is no handy way to find out the size of a

Interview Questions on C-Language

www.mallepoolu.com
block allocated by malloc( ).
------------------------------------------------------------------------------------------------26.
FP_SEG And FP_OFF
Sometimes while working with far pointers we need to break a far address into its segment
and offset. In such situations we can use FP_SEG and FP_OFF macros. Following program
illustrates the use of these two macros.
#include <dos.h>
main( )
{
unsigned s, o ;
char far *ptr = "Hello!" ;
s = FP_SEG ( ptr ) ;
o = FP_OFF ( ptr ) ;
printf ( "\n%u %u", s, o ) ;
}
------------------------------------------------------------------------------------------------27.
How do I write a program to convert a string containing number in a hexadecimal form to its
equivalent decimal?
Ans: The following program demonstrates this:
main( )
{
char str[] = "0AB" ;
int h, hex, i, n ;
n=0;h=1;
for ( i = 0 ; h == 1 ; i++ )
{
if ( str[i] >= '0' && str[i] <= '9' )
hex = str[i] - '0' ;
else
{
if ( str[i] >= 'a' && str[i] <= 'f' )
hex = str[i] - 'a' + 10 ;
else
if ( str[i] >= 'A' && str[i] <= 'F' )
hex = str[i] - 'A' + 10 ;
else
h=0;
}
if ( h == 1 )
n = 16 * n + hex ;
}
printf ( "\nThe decimal equivalent of %s is %d",
str, n ) ;
}

Interview Questions on C-Language

www.mallepoolu.com
The output of this program would be the decimal equivalent of 0AB is 171.
------------------------------------------------------------------------------------------------28.
How do I write code that reads the segment register settings?
Ans: We can use segread( ) function to read segment register settings. There are four segment
registerscode segment, data segment, stack segment and extra segment. Sometimes when
we use DOS and BIOS services in a program we need to know the segment register's value.
In such a situation we can use segread( ) function. The following program illustrates the use
of this function.
#include <dos.h>
main( )
{
struct SREGS s ;
segread ( &s ) ;
printf ( "\nCS: %X DS: %X SS: %X ES: %X",s.cs,
s.ds, s.ss, s.es ) ;
}
------------------------------------------------------------------------------------------------29.
What is environment and how do I get environment for a specific entry?
Ans: While working in DOS, it stores information in a memory region called environment. In
this region we can place configuration settings such as command path, system prompt, etc.
Sometimes in a program we need to access the information contained in environment. The
function getenv( ) can be used when we want to access environment for a specific entry.
Following program demonstrates the use of this function.
#include <stdio.h>
#include <stdlib.h>
main( )
{
char *path = NULL ;
path = getenv ( "PATH" ) ;
if ( *path != NULL )
printf ( "\nPath: %s", path ) ;
else
printf ( "\nPath is not set" ) ;
}
------------------------------------------------------------------------------------------------30.
How do I display current date in the format given below?
Saturday October 12, 2002
Ans: Following program illustrates how we can display date in above given format.

Interview Questions on C-Language

www.mallepoolu.com
#include <stdio.h>
#include <time.h>
main( )
{
struct tm *curtime ;
time_t dtime ;
char str[30] ;
time ( &dtime ) ;
curtime = localtime ( &dtime ) ;
strftime ( str, 30, "%A %B %d, %Y", curtime ) ;
printf ( "\n%s", str ) ;
}
Here we have called time( ) function which returns current time. This time is returned in
terms of seconds, elapsed since 00:00:00 GMT, January 1, 1970. To extract the week day,
day of month, etc. from this value we need to break down the value to a tm structure. This is
done by the function localtime( ). Then we have called strftime( ) function to format the time
and store it in a string str.
31.
If we have declared an array as global in one file and we are using it in another file then why
doesn't the sizeof operator works on an extern array?
Ans: An extern array is of incomplete type as it does not contain the size. Hence we cannot
use sizeof operator, as it cannot get the size of the array declared in another file. To resolve
this use any of one the following two solutions:
1. In the same file declare one more variable that holds the size of array. For example,
array.c
int arr[5] ;
int arrsz = sizeof ( arr ) ;
myprog.c
extern int arr[] ;
extern int arrsz ;
2. Define a macro which can be used in an array
declaration. For example,
myheader.h
#define SZ 5
array.c

Interview Questions on C-Language

www.mallepoolu.com
#include "myheader.h"
int arr[SZ] ;
myprog.c
#include "myheader.h"
extern int arr[SZ] ;
------------------------------------------------------------------------------------------------32.
How do I write printf( ) so that the width of a field can be specified at runtime?
Ans: This is shown in following code snippet.
main( )
{
int w, no ;
printf ( "Enter number and the width for the
number field:" ) ;
scanf ( "%d%d", &no, &w ) ;
printf ( "%*d", w, no ) ;
}
Here, an '*' in the format specifier in printf( ) indicates that an int value from the argument
list should be used for the field width.
------------------------------------------------------------------------------------------------33.
How to find the row and column dimension of a given 2-D array?
Ans: Whenever we initialize a 2-D array at the same place where it has been declared, it is
not necessary to mention the row dimension of an array. The row and column dimensions of
such an array can be determined programmatically as shown in following program.
void main( )
{
int a[][3] = { 0, 1, 2,
9,-6, 8,
7, 5, 44,
23, 11,15 } ;
int c = sizeof ( a[0] ) / sizeof ( int ) ;
int r = ( sizeof ( a ) / sizeof ( int ) ) / c ;
int i, j ;
printf ( "\nRow: %d\nCol: %d\n", r, c ) ;
for ( i = 0 ; i < r ; i++ )
{
for ( j = 0 ; j < c ; j++ )

Interview Questions on C-Language

www.mallepoolu.com
printf ( "%d ", a[i][j] ) ;
printf ( "\n" ) ;
}
}
------------------------------------------------------------------------------------------------34.
The access( ) function...
The access( ) function checks for the existence of a file and also determines whether it can be
read, written to or executed. This function takes two arguments the filename and an integer
indicating the access mode. The values 6, 4, 2, and 1 checks for read/write, read, write and
execute permission of a given file, whereas value 0 checks whether the file exists or not.
Following program demonstrates how we can use access( ) function to check if a given file
exists.
#include <io.h>
main( )
{
char fname[67] ;
printf ( "\nEnter name of file to open" ) ;
gets ( fname ) ;
if ( access ( fname, 0 ) != 0 )
{
printf ( "\nFile does not exist." ) ;
return ;
}
}
------------------------------------------------------------------------------------------------35.
How do I convert a floating-point number to a string?
Ans: Use function gcvt( ) to convert a floating-point number to a string. Following program
demonstrates the use of this function.
#include <stdlib.h>
main( )
{
char str[25] ;
float no ;
int dg = 5 ; /* significant digits */
no = 14.3216 ;
gcvt ( no, dg, str ) ;

Interview Questions on C-Language

www.mallepoolu.com
printf ( "String: %s\n", str ) ;
}
36.
What is a stack ?
Ans: The stack is a region of memory within which our programs temporarily store data as
they execute. For example, when a program passes parameters to functions, C places the
parameters on the stack. When the function completes, C removes the items from the stack.
Similarly, when a function declares local variables, C stores the variable's values on the stack
during the function's execution. Depending on the program's use of functions and parameters,
the amount of stack space that a program requires will differ.
------------------------------------------------------------------------------------------------37.
Allocating memory for a 3-D array
#include "alloc.h"
#define MAXX 3
#define MAXY 4
#define MAXZ 5
main( )
{
int ***p, i, j, k ;
p = ( int *** ) malloc ( MAXX * sizeof ( int ** ) ) ;
for ( i = 0 ; i < MAXX ; i++ )
{
p[i] = ( int ** ) malloc ( MAXY * sizeof ( int * ) ) ;
for ( j = 0 ; j < MAXY ; j++ )
p[i][j] = ( int * ) malloc ( MAXZ * sizeof ( int ) ) ;
}
for ( k = 0 ; k < MAXZ ; k++ )
{
for ( i = 0 ; i < MAXX ; i++ )
{
for ( j = 0 ; j < MAXY ; j++ )
{
p[i][j][k] = i + j + k ;
printf ( "%d ", p[i][j][k] ) ;
}
printf ( "\n" ) ;
}
printf ( "\n\n" ) ;
}
}
Data Structures
How to distinguish between a binary tree and a tree?
Ans: A node in a tree can have any number of branches. While a binary tree is a tree structure
in which any node can have at most two branches. For binary trees we distinguish between
the subtree on the left and subtree on the right, whereas for trees the order of the subtrees is

Interview Questions on C-Language

www.mallepoolu.com
irrelevant.
Consider the following figure...
This above figure shows two binary trees, but these binary trees are different. The first has an
empty right subtree while the second has an empty left subtree. If the above are regarded as
trees (not the binary trees), then they are same despite the fact that they are drawn differently.
Also, an empty binary tree can exist, but there is no tree having zero nodes.
------------------------------------------------------------------------------------------------38.
How do I use the function ldexp( ) in a program?
Ans: The math function ldexp( ) is used while solving the complex mathematical equations.
This function takes two arguments, a double value and an int respectively. The order in which
ldexp( ) function performs calculations is ( n * pow ( 2, exp ) ) where n is the double value
and exp is the integer. The following program demonstrates the use of this function.
#include <stdio.h>
#include <math.h>
void main( )
{
double ans ;
double n = 4 ;
ans = ldexp ( n, 2 ) ;
printf ( "\nThe ldexp value is : %lf\n", ans ) ;
}
Here, ldexp( ) function would get expanded as ( 4 * 2 * 2 ), and the output would be the ldexp
value is : 16.000000
------------------------------------------------------------------------------------------------39.
Can we get the mantissa and exponent form of a given number?
Ans: The function frexp( ) splits the given number into a mantissa and exponent form. The
function takes two arguments, the number to be converted as a double value and an int to
store the exponent form. The function returns the mantissa part as a double value. Following
example demonstrates the use of this function.
#include <math.h>
#include <stdio.h>
void main( )
{
double mantissa, number ;
int exponent ;
number = 8.0 ;
mantissa = frexp ( number, &exponent ) ;

Interview Questions on C-Language

www.mallepoolu.com
printf ( "The number %lf is ", number ) ;
printf ( "%lf times two to the ", mantissa ) ;
printf ( "power of %d\n", exponent ) ;
return 0 ;
}
------------------------------------------------------------------------------------------------40.
How do I write code that executes certain function only at program termination?
Ans: Use atexit( ) function as shown in following program.
#include <stdlib.h>
main( )
{
int ch ;
void fun ( void ) ;
atexit ( fun ) ;
// code
}
void fun( void )
{
printf ( "\nTerminate program......" ) ;
getch( ) ;
}
------------------------------------------------------------------------------------------------41.
What are memory models?
Ans: The compiler uses a memory model to determine how much memory is allocated to the
program. The PC divides memory into blocks called segments of size 64 KB. Usually,
program uses one segment for code and a second segment for data. A memory model defines
the number of segments the compiler can use for each. It is important to know which memory
model can be used for a program. If we use wrong memory model, the program might not
have enough memory to execute. The problem can be solved using larger memory model.
However, larger the memory model, slower is your program execution. So we must choose
the smallest memory model that satisfies our program needs. Most of the compilers support
memory models like tiny, small, medium, compact, large and huge.
42.
How does C compiler store elements in a multi-dimensional array?
Ans: The compiler maps multi-dimensional arrays in two waysRow major order and
Column order. When the compiler places elements in columns of an array first then it is
called column-major order. When the compiler places elements in rows of an array first then
it is called row-major order. C compilers store multidimensional arrays in row-major order.
For example, if there is a multi-dimensional array a[2][3], then according row-major order,
the elements would get stored in memory following order:
a[0][0], a[0][1], a[0][2], a[1][0], a[1][1], a[1][2]
------------------------------------------------------------------------------------------------43.

Interview Questions on C-Language

www.mallepoolu.com

If the result of an _expression has to be stored to one of two variables, depending on a


condition, can we use conditional operators as shown below?
( ( i < 10 ) ? j : k ) = l * 2 + p ;
Ans: No! The above statement is invalid. We cannot use the conditional operators in this
fashion. The conditional operators like most operators, yields a value, and we cannot assign
the value of an _expression to a value. However, we can use conditional operators as shown
in following code snippet.
main( )
{
int i, j, k, l ;
i = 5 ; j = 10 ; k = 12, l = 1 ;
* ( ( i < 10 ) ? &j : &k ) = l * 2 + 14 ;
printf ( "i = %d j = %d k = %d l = %d", i, j, k, l ) ;
}
The output of the above program would be as given below:
i = 5 j = 16 k = 12 l = 1
------------------------------------------------------------------------------------------------44.
How can I find the day of the week of a given date?
Ans: The following code snippet shows how to get the day of week from the given date.
dayofweek ( int yy, int mm, int dd )
{
/*Monday = 1 and Sunday = 0 */
/* month number >= 1 and <= 12, yy > 1752 or so */
static int arr[ ] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 } ;
yy = yy - mm < 3 ;
return ( yy + yy / 4 - yy / 100 + yy / 400 + arr[ mm - 1] + dd ) % 7 ;
}
void main( )
{
printf ( "\n\n\nDay of week : %d ", dayofweek ( 2002, 5, 18 ) ) ;
}
-------------------------------------------------------------------------------------------------

45.
What's the difference between these two declarations?

Interview Questions on C-Language

www.mallepoolu.com
struct str1 { ... } ;
typedef struct { ... } str2 ;
Ans : The first form declares a structure tag whereas the second declares a typedef. The main
difference is that the second declaration is of a slightly more abstract type -- its users don't
necessarily know that it is a structure, and the keyword struct is not used when declaring
instances of it.
------------------------------------------------------------------------------------------------46.
How do I print the contents of environment variables?
Ans:. The following program shows how to achieve this:
main( int argc, char *argv[ ], char *env[ ] )
{
int i = 0 ;
clrscr( ) ;
while ( env[ i ] )
printf ( "\n%s", env[ i++ ] ) ;
}
main( ) has the third command line argument env, which is an array of pointers to the strings.
Each pointer points to an environment variable from the list of environment variables.
------------------------------------------------------------------------------------------------47.
div( )...
The function div( ) divides two integers and returns the quotient and remainder. This function
takes two integer values as arguments; divides first integer with the second one and returns
the answer of division of type div_t. The data type div_t is a structure that contains two long
ints, namely quot and rem, which store quotient and remainder of division respectively. The
following example shows the use of div( ) function.
#include <stdlib.h>
void main( )
{
div_t res ;
res = div ( 32, 5 ) ;
printf ( "\nThe quotient = %d and remainder = %d ", res.quot, res.rem ) ;
}
48.
What would the second and the third printf( ) output the following program?
main( )
{
char *str[ ] = {
"Good Morning"
"Good Evening"
"Good Afternoon"

Interview Questions on C-Language

www.mallepoolu.com
};
printf ( "\nFirst string = %s", str[0] ) ;
printf ( "\nSecond string = %s", str[1] ) ;
printf ( "\nThird string = %s", str[2] ) ;
}
Ans: For the above given program, we expect the output as Good Evening and Good
Afternoon, for the second and third printf( ). However, the output would be as shown below.
First string = Good MorningGood EveningGood Afternoon
Second string = ( null )
Third string =
What is missing in the above given code snippet is a comma separator which should separate
the strings Good Morning, Good Evening and Good Afternoon. On adding comma, we would
get the output as shown below.
First string = Good Morning
Second string = Good Evening
Third string = Good Afternoon
------------------------------------------------------------------------------------------------49.
How do I use scanf( ) to read the date in the form 'dd-mm-yy' ?
Ans: There are two ways to read the date in the form of 'dd-mm-yy' one possible way is...
int dd, mm, yy ;
char ch ; /* for char '-' */
printf ( "\nEnter the date in the form of dd-mm-yy : " ) ;
scanf( "%d%c%d%c%d", &dd, &ch, &mm, &ch, &yy ) ;
And another best way is to use suppression character * as...
int dd, mm, yy ;
scanf( "%d%*c%d%*c%d", &dd, &mm, &yy ) ;
The suppression character * suppresses the input read from the standard input buffer for the
assigned control character.
------------------------------------------------------------------------------------------------50.
How do I print a floating-point number with higher precision say 23.34568734 with only
precision up to two decimal places?
Ans: This can be achieved through the use of suppression char '*' in the format string of
printf( ) as shown in the following program.
main( )
{
int i = 2 ;
float f = 23.34568734 ;
printf ( "%.*f", i, f ) ;

Interview Questions on C-Language

www.mallepoolu.com
}
The output of the above program would be 23.35.
------------------------------------------------------------------------------------------------51.
Are the expressions *ptr++ and ++*ptr same?
Ans: No. *ptr++ increments the pointer and not the value pointed by it, whereas ++*ptr
increments the value being pointed to by ptr.
------------------------------------------------------------------------------------------------52.
strpbrk( )
The function strpbrk( ) takes two strings as parameters. It scans the first string, to find, the
first occurrence of any character appearing in the second string. The function returns a
pointer to the first occurrence of the character it found in the first string. The following
program demonstrates the use of string function strpbrk( ).
#include <string.h>
main( )
{
char *str1 = "Hello!" ;
char *str2 = "Better" ;
char *p ;
p = strpbrk ( str1, str2 ) ;
if ( p )
printf ( "The first character found in str1 is %c", *p ) ;
else
printf ( "The character not found" ) ;
}
The output of the above program would be the first character found in str1 is e
53.
Can we convert an unsigned long integer value to a string?
Ans: The function ultoa( ) can be used to convert an unsigned long integer value to a string.
This function takes three arguments, first the value that is to be converted, second the base
address of the buffer in which the converted number has to be stored (with a string
terminating null character '\0') and the last argument specifies the base to be used in
converting the value. Following example demonstrates the use of this function.
#include <stdlib.h>
void main( )
{
unsigned long ul = 3234567231L ;
char str[25] ;

Interview Questions on C-Language

www.mallepoolu.com
ultoa ( ul, str, 10 ) ;
printf ( "str = %s unsigned long = %lu\n", str, ul ) ;
}
------------------------------------------------------------------------------------------------54.
ceil( ) and floor( )
The math function ceil( ) takes a double value as an argument. This function finds the
smallest possible integer to which the given number can be rounded up. Similarly, floor( )
being a math function, takes a double value as an argument and returns the largest possible
integer to which the given double value can be rounded down. The following program
demonstrates the use of both the functions.
#include <math.h>
void main( )
{
double no = 1437.23167 ;
double down, up ;
down = floor ( no ) ;
up = ceil ( no ) ;
printf ( "The original number %7.5lf\n", no ) ;
printf ( "The number rounded down %7.5lf\n", down ) ;
printf ( "The number rounded up %7.5lf\n", up ) ;
}
The output of this program would be,
The original number 1437.23167
The number rounded down 1437.00000
The number rounded up 1438.00000
------------------------------------------------------------------------------------------------55.
How do I use function ecvt( ) in a program?
Ans: The function ecvt( ) converts a floating-point value to a null terminated string. This
function takes four arguments, such as, the value to be converted to string, the number of
digits to be converted to string, and two integer pointers. The two-integer pointer stores the
position of the decimal point (relative to the string) and the sign of the number, respectively.
If the value in a variable, used to store sign is 0, then the number is positive and, if it is nonzero, then the number is negative. The function returns a pointer to the string containing
digits. Following program demonstrates the use of this function.
#include <stdlib.h>
main( )
{
char *str ;
double val ;

Interview Questions on C-Language

www.mallepoolu.com
int dec, sign ;
int ndig = 4 ;
val = 22 ;
str = ecvt ( val, ndig, &dec, &sign ) ;
printf ( "string = %s dec = %d sign = %d\n", str, dec, sign ) ;
val = -345.67 ;
ndig = 8 ;
str = ecvt ( val, ndig, &dec, &sign ) ;
printf ( "string = %s dec = %d sign = %d\n", str, dec, sign ) ;
// number with a scientific notation
val = 3.546712e5 ;
ndig = 5 ;
str = ecvt ( val, ndig, &dec, &sign ) ;
printf ( "string = %s dec = %d sign = %d\n", str, dec, sign ) ;
}
The output of this program would be
string = 2200 dec = 2 sign = 0
string = 34567000 dec = 3 sign = 1
string = 35467 dec = 6 sign = 0
------------------------------------------------------------------------------------------------56.
How to run DIR command programmatically?
Ans: We can use the system( ) function to execute the DIR command along with its options.
Following program shows how this can be achieved:
// mydir.c
main ( int argc, char *argv[ ] )
{
char str[30] ;
if ( argc < 2 )
exit ( 0 ) ;
sprintf ( str, "dir %s %s", argv[1], argv[2] ) ;
system ( str ) ;
}
If we run the executable file of this program at command prompt passing the command line
arguments as follows:

Interview Questions on C-Language

www.mallepoolu.com
> mydir abc.c /s
This will search the file 'abc.c' in the current directory.
------------------------------------------------------------------------------------------------57.
Suppose I have a structure having fields name, age, salary and have passed address of age to
a function fun( ). How I can access the other member of the structure using the address of
age?
Ans:
struct emp
{
char name[20] ;
int age ;
float salary ;
};
main( )
{
struct emp e ;
printf ( "\nEnter name: " ) ;
scanf ( "%s", e.name ) ;
printf ( "\nEnter age: " ) ;
scanf ( "%d", &e.age ) ;
printf ( "\nEnter salary: " ) ;
scanf ( "%f", &e.salary ) ;
fun ( &e.age ) ;
}
fun ( int *p )
{
struct emp *q ;
int offset ;
offset = ( char * ) ( & ( ( struct emp * ) 0 ) -> age ) - ( char * ) ( (
struct emp* ) 0 ) ;
q = ( struct emp * ) ( ( char * ) p - offset ) ;
printf ( "\nname: %s", q -> name ) ;
printf ( "\nage: %d", q -> age ) ;
printf ( "\nsalary: %f", q -> salary ) ;
}
58.
How to restrict the program's output to a specific screen region?
Ans: A C function window( ) can be used to restrict the screen output to a specific region.
The window( ) function defines a text-mode window. The parameters passed to this function
defines the upper-left and lower-right corner of the region within which you want the output.
In the following program, the string 'Hello!' gets printed within the specified region. To print
the string we must use cprintf( ) function which prints directly on the text-mode window.
#include <conio.h>

Interview Questions on C-Language

www.mallepoolu.com
main( )
{
int i, j ;
window ( 20, 8, 60, 17 ) ;
for ( i = 0 ; i < 8 ; i++ )
for ( j = 0 ; j < 10 ; j++ )
cprintf ( "Hello!" ) ;
}
------------------------------------------------------------------------------------------------59.
Sometimes you need to prompt the user for a password. When the user types in the password,
the characters the user enters should not appear on the screen. A standard library function
getpass( ) can be used to perform such function. Maximum number of characters that can be
entered as password is 8.
main( )
{
char *pwd ;
pwd = getpass ( "Enter Password" ) ;
if ( strcmp ( pwd, "orgcity" ) )
printf ( "\nPassword %s is incorrect", pwd ) ;
else
printf ( "\nCorrect Password" ) ;
}
------------------------------------------------------------------------------------------------60.
How to obtain the current drive through C ?
Ans: We can use the function _getdrive( ) to obtain the current drive. The _getdrive( )
function uses DOS function 0X19 to get the current drive number
#include <direct.h>
main( )
{
int disk ;
disk = _getdrive( ) + 'A' - 1 ;
printf ( "The current drive is: %c\n", disk ) ;
}
------------------------------------------------------------------------------------------------61.
How come the output for both the programs is different when the logic is same?
main( )
{
int i, j ;

Interview Questions on C-Language

www.mallepoolu.com

for ( i = 1, j = 1 ; i <= 5, j <= 100 ; i++, j++ )


{
gotoxy ( 1, 1, ) ;
printf ( "%d %d", i, j ) ;
}
}
main( )
{
int i, j ;
for ( i =1, j = 1; j <= 100, i <= 5; i++, j++ )
{
gotoxy ( 1, 1 ) ;
printf ( "%d %d", i, j ) ;
}
}
Output -> 5 5
Even if logic of both the programs is same the output of the first program comes out to be
100, 100, but of the second program it is 5, 5. The comma operator plays a vital role inside
the for loop. It always considers the value of the latest variable. So, at the time of testing the
condition in for loop, the value of j will be considered in the first program and value of i in
the second.
------------------------------------------------------------------------------------------------62.
Can we get the x and y coordinate of the current cursor position ?
Ans : The function wherex( ) and wherey( ) returns the x-coordinate and y-coordinate of the
current cursor position respectively. Both the functions return an integer value. The value
returned by wherex( ) is the horizontal position of cursor and the value returned by wherey( )
is the vertical position of the cursor. Following program shows how to use the wherex( ) and
wherey( ) functions.
#include <conio.h>
main( )
{
printf ( "Just\n To\n Test\n Where\n the cursor\n goes" ) ;
printf ( "Current location is X: %d Y: %d\n", wherex( ), wherey( ) ) ;
}
63.
How do I programmatically delete lines in the text window?
Ans: While writing programs that perform screen-based I/O, you may want to-delete the
current line's contents, moving one line up, all of the output that follows. In such cases a
function called delline( ) can be used. Following code snippet illustrates the use of function
delline( ).

Interview Questions on C-Language

www.mallepoolu.com

#include <conio.h>
main( )
{
int i ;
clrscr( ) ;
for ( i = 0; i <= 23; i++ )
printf ( "Line %d\r\n", i ) ;
printf ( "Press a key to continue : " ) ;
getch( ) ;
gotoxy ( 2, 6 ) ;
for ( i = 6; i <= 12; i++ )
delline( ) ;
getch( ) ;
}
------------------------------------------------------------------------------------------------64.
How do I get the time elapsed between two function calls ?
Ans: The function difftime( ) finds the difference between two times. It calculates the elapsed
time in seconds and returns the difference between two times as a double value.
#include <time.h>
#include <stdio.h>
#include <dos.h>
main( )
{
int a[] = { 2, -34, 56, 78, 112, 33, -7, 11, 45, 29, 6 } ;
int s ;
time_t t1, t2 ; // time_t defines the value used for time function
s = sizeof ( a ) / 2 ;
t1 = time ( NULL ) ;
sel_sort ( a, s ) ; // sort array by selection sort
bub_sort ( a, s ) ; // sort array by bubble sort method
t2 = time ( NULL ) ;
printf ( "\nThe difference between two function calls is %f", difftime (
t2, t1 ) ) ;
}
In the above program we have called difftime( ) function that returns the time elapsed from t1
to t2.

Interview Questions on C-Language

www.mallepoolu.com
------------------------------------------------------------------------------------------------65.
How do I use swab( ) in my program ?
Ans: The function swab( ) swaps the adjacent bytes of memory. It copies the bytes from
source string to the target string, provided that the number of characters in the source string is
even. While copying, it swaps the bytes which are then assigned to the target string.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
main ( )
{
char *str1 = "hS eesll snsiasl not eh es as oher " ;
char *str2 ;
clrscr( ) ;
swab ( str1, str2, strlen ( str1 ) ) ;
printf ( "The target string is : %s\n", str2 ) ; // output -- She sells
snails on the sea shore
getch( ) ;
}
------------------------------------------------------------------------------------------------66.
Turbo C provides various command line compiler options which we can use through TCC.
The compiler options include : displaying specific warning messages, generating 8087
hardware instructions, using a filename for generating assembly code, etc. Instead of
compiler options being executed at command line we can use these compiler options in our
program. This can be achieved using #pragma options. We can use various flags with
#pragma options to use the compiler options. All these flags are available in turbo C's online
help.
------------------------------------------------------------------------------------------------67.
I have an array declared in file 'F1.C' as,
int a[ ] = { 1, 2, 3, 4, 5, 6 } ;
and used in the file 'F2.C' as,
extern int a[ ] ;
In the file F2.C, why sizeof doesn't work on the array a[ ]?
Ans: An extern array of unspecified size is an incomplete type. You cannot apply sizeof to it,
because sizeof operates during compile time and it is unable to learn the size of an array that
is defined in another file. You have three ways to resolve this problem:
1. In file 'F1.C' define as,
int a[ ] = { 1, 2, 3, 4, 5, 6 } ;
int size_a = sizeof ( a ) ;

Interview Questions on C-Language

www.mallepoolu.com
and in file F2.C declare as,
extern int a[ ] ;
extern int size_a ;
2. In file 'F1.H' define,
#define ARR_SIZ 6
In file F1.C declare as,
#include "F1.H"
int a[ ARR_SIZ ] ;
and in file F2.C declare as,
#include "F1.H"
extern int a[ ARR_SIZ ] ;
3. In file 'F1.C' define as,
int a[ ] = { 1, 2, 3, 4, 5, 6, -1 } ;
and in file 'F2.C' declare as,
extern int a[ ] ;
Here the element -1 is used as a sentinel value, so the code can
understand the end without any explicit size.
68.
How to delete a line from text displayed on the screen?
Ans: Sometimes, specially when we are creating a text editor like program we may wish to
allow user to delete a line. We can do so by using two functions namely clreol( ) and delline(
). The clreol( ) function deletes the line from the current cursor position to the end of line.
The delline() function deletes the entire line at the current cursor position and
moves up the following line. Following program shows how to use these functions.
#include <conio.h>
main( )
{
int i ;
for ( i = 1 ; i <= 20 ; i++ )
printf ( "This is Line %d\n", i ) ;
getch( ) ;
gotoxy ( 1, 7 ) ;
clreol( ) ;
getch( ) ;
gotoxy ( 1, 12 ) ;
delline( ) ;
getch( ) ;

Interview Questions on C-Language

www.mallepoolu.com
}
------------------------------------------------------------------------------------------------69.
How do I programmatically insert lines in the text window?
Ans: We can insert a blank line in the text window using the insline( ) function. This function
inserts line at current cursor position. While doing so, it shifts down the lines that are below
the newly inserted line.
#include <conio.h>
void main( )
{
printf ( "The little snail was slowly moving up. She wanted\r\n" ) ;
printf ( "to reach the top of the tree. It was chilly\r\n" ) ;
printf ( "winter season. Most of the animals were resting in\r\n" ) ;
printf ( "their nests as there was a heavy snow fall.\r\n" ) ;
printf ( "\r\nPress any key to continue:" ) ;
gotoxy ( 10, 2 ) ;
getch( ) ;
insline( ) ;
getch( ) ;
}
------------------------------------------------------------------------------------------------70.
What will be the output of the following program?
main( )
{
unsigned int num ;
int i ;
printf ( "\nEnter any number" ) ;
scanf ( "%u", &num ) ;
for ( i = 0 ; i < 16 ; i++ )
printf ( "%d", ( num << i & 1 << 15 ) ? 1 : 0 ) ;
}
Ans: The output of this program is the binary equivalent of the given number. We have used
bitwise operators to get the binary number.
------------------------------------------------------------------------------------------------71.
Graphics
Building Mouse Cursors...

Interview Questions on C-Language

www.mallepoolu.com
In text mode the mouse cursor appears as a block, whereas in graphics mode it appears as an
arrow. If we wish we can change the graphics cursor to any other shape the way Windows
does. The mouse cursor in graphics mode occupies a 16 by 16 pixel box. By highlighting or
dehighlighting some of the pixels in this box we can get the desired shape. For example, the
following bit-pattern can be used to generate the cursor which looks like an hour-glass.
1111111111111111 0000000000000000
1000000000000001 0000000000000000
1111111111111111 0000000000000000
1000000000000001 0000000000000000
0100000000000010 1000000000000001
0010000000000100 1100000000000011
0000100000010000 1111000000001111
0000001001000000 1111110000111111
0000001001000000 1111110000111111
0000100000010000 1111000000001111
0010000000000100 1100000000000011
0100000000000010 1000000000000001
1000000000000001 0000000000000000
1111111111111111 0000000000000000
1000000000000001 0000000000000000
1111111111111111 0000000000000000
Mouse pointer bitmap Screen Mask the one's in the mouse pointer bitmap indicate that the
pixel would be drawn whereas the zeros indicate that the pixel would stand erased. It is
important to note that the mouse pointer bit pattern is 32 bytes long. However, while actually
writing a program to change the pointer shape we need a 64 byte bit-map. This provision is
made to ensure that when the cursor reaches a position on the screen where something is
already written or drawn only that portion should get overwritten which is to be occupied by
the mouse cursor. Of the 64 bytes the first 32 bytes contain a bit mask which is first ANDed
with the screen image, and then the second 32 bytes bit mask is XORed with the screen
image.
The following program changes the mouse cursor in graphics mode to resemble an hour
glass.
# include "graphics.h"
# include "dos.h"
union REGS i, o ;
struct SREGS s ;
int cursor[32] =
{
/* Hour-glass screen mask */
0x0000, 0x0000, 0x0000, 0x0000,
0x8001, 0xc003, 0xf00f, 0xfc3f,
0xfc3f, 0xf00f, 0xc003, 0x8001,
0x0000, 0x0000, 0x0000, 0x0000,
/* The mouse pointer bitmap */
0xffff, 0x8001, 0xffff, 0x8001,
0x4002, 0x2004, 0x1008, 0x0240,

Interview Questions on C-Language

www.mallepoolu.com
0x0240, 0x0810, 0x2004, 0x4002,
0x8001, 0xffff, 0x8001, 0xffff,
};
main( )
{
int gd = DETECT, gm ;
initgraph ( &gd, &gm, "c:\\tc\\bgi" ) ;
if ( initmouse( ) == -1 )
{
closegraph( ) ;
printf ( "\n Mouse not installed!" ) ;
exit( ) ;
}
gotoxy ( 10, 1 ) ; printf ( "Press any key to exit..." ) ;
changecursor ( cursor ) ; showmouseptr( ) ;
getch( ) ;
}
initmouse( )
{
i.x.ax = 0 ; int86 ( 0x33, &i, &o ) ;
return ( o.x.ax == 0 ? -1 : 0 ) ;
}
showmouseptr( )
{
i.x.ax = 1 ; int86 ( 0x33, &i, &o ) ;
}
changecursor ( int *shape )
{
i.x.ax = 9 ; /* service number */
i.x.bx = 0 ; /* actual cursor position from left */
i.x.cx = 0 ; /* actual cursor position from top */
i.x.dx = ( unsigned ) shape ; /* offset address of pointer image*/
segread ( &s ) ;
s.es = s.ds ; /* segment address of pointer */
int86x ( 0x33, &i, &i, &s ) ;
}
72.
Towers Of Hanoi
Suppose there are three pegs labeled A, B and C. Four disks are placed on peg A. The
bottom-most disk is largest, and disks go on decreasing in size with the topmost disk being
smallest. The objective of the game is to move the disks from peg A to peg C, using peg B as
an auxiliary peg. The rules of the game are as follows:
Only one disk may be moved at a time, and it must be the top disk on one of the pegs. A
larger disk should never be placed on the top of a smaller disk. Suppose we are to write a
program to print out the sequence in which the disks should be moved such that all disks on

Interview Questions on C-Language

www.mallepoolu.com
peg A are finally transferred to peg C. Here it is...
main( )
{
int n = 4 ;
move ( n, 'A', 'B', 'C' ) ;
}
move ( n, sp, ap, ep )
int n ;
char sp, ap, ep ;
{
if ( n == 1 )
printf ( "\n Move from %c to %c ", sp, ep ) ;
else
{
move ( n - 1, sp, ep, ap ) ;
move ( 1, sp, ' ', ep ) ;
move ( n - 1, ap, sp, ep ) ;
}
}
And here is the output...
Move from A to B
Move from A to C
Move from B to C
Move from A to B
Move from C to A
Move from C to B
Move from A to B
Move from A to C
Move from B to C
Move from B to A
Move from C to A
Move from B to C
Move from A to B
Move from A to C
Move from B to C
This problem is the famous Towers of Hanoi problem, wherein three pegs are to be employed
for transferring the disks with the given criteria. Here's how we go about it. We have three
pegs: the starting peg, sp, the auxiliary peg ap, and the ending peg, ep, where the disks must
finally be. First, using the ending peg as an auxiliary or supporting peg, we transfer all but the
last disk to ap. Next the last disk is moved from sp to ep. Now, using sp as the supporting
peg, all the disks are moved from ap to ep. A, B and C denote the three pegs. The recursive
function move( ) is called with different combinations of these pegs as starting, auxiliary and
ending pegs.
------------------------------------------------------------------------------------------------73.

Interview Questions on C-Language

www.mallepoolu.com
What would be the output of following program?
struct syntax
{
int i ;
float g ;
char c ;
}
main( )
{
printf ( "I won't give you any error" ) ;
}
Ans: The above program would get compiled successfully and on execution it would print the
message given in printf(). What strikes in the above code snippet is the structure syntax
which is declared but not terminated with the statement terminator, the semicolon. The
compiler would not give any error message for it, as it assumes that main( ) function have a
return type of struct syntax and hence would successfully compile and execute the program.
------------------------------------------------------------------------------------------------74.
How to get the memory size ?
Ans: Consider the following program
#include <stdio.h>
#include <bios.h>
main( )
{
int memsize;
memsize = biosmemory( ) ;
printf ( "RAM size = %dK\n",memsize ) ;
return 0 ;
}
The function biosmemory uses BIOS interrupt 0x12 to return the size of memory.
------------------------------------------------------------------------------------------------75.
Float Format
How does C compiler stores float values ?
Ans: In C, the float values are stored in a mantissa and exponent form. While writing a
number we specify the exponent part in the form of base 10. But, in case of C compiler, the
exponent for floats is stored in the form of base 2. Obviously, because, computer stores the
numbers in binary form. The C compiler follows an IEEE standard to store a float. The IEEE
format expresses a floating-point number in a binary form known as `normalized' form.
Normalization involves adjusting the exponent so that the "binary point" (the binary analog of
the decimal point) in the mantissa always lies to the right of most significant nonzero digit. In
binary representation, this means that the most significant digit of the mantissa is always a 1.

Interview Questions on C-Language

www.mallepoolu.com
This property of the normalized representation is exploited by the IEEE format when storing
the mantissa. Let us consider an example of generating the normalized form of a floating
point number. Suppose we want to represent the decimal number 5.375. Its binary equivalent
can be obtained as shown below:
2|5
.375 x 2 = 0.750 0
|-----.750 x 2 = 1.500 1
2|21
.500 x 2 = 1.000 1
|-----2|10
|-----|01
Writing remainders in reverse writing whole parts in the same order we get 101 order in
which they are obtained we get 011 thus the binary equivalent of 5.375 would be 101.011.
The normalized form of this binary number is obtained by adjusting the exponent until the
decimal point is to the right of most significant 1. In this case the result is 1.01011 x 22. The
IEEE format for floating point storage uses a sign bit, a mantissa and an exponent for
representing the power of 2. The sign bit denotes the sign of the number: a 0 represents a
positive value and a 1 denotes a negative value. The mantissa is represented in binary.
Converting the floating-point number to its normalized form results in a mantissa whose most
significant digit is always 1. The IEEE format takes advantage of this by not storing this bit at
all. The exponent is an integer stored in unsigned binary format after adding a positive integer
bias. This ensures that the stored exponent is always positive. The value of the bias is 127 for
floats and 1023 for doubles. Thus, 1.01011 x 22 is represented as shown below:
--- --------------- ---------------------------------------------| 0 | 100 0000 1 | 010 1100 0000 0000 0000 0000 |
--- ---------------- --------------------------------------------sign bit exponent- mantissa stored in normalized form obtained after adding a bias
127 to exponent 2
Data Structures
Which is the best sorting method?
Ans: There is no sorting method that is universally superior to all others. The programmer
must carefully examine the problem and the desired results before deciding the particular
sorting method. Some of the sorting methods are given below:
Bubble sort : When a file containing records is to be sorted then Bubble sort is the best
sorting method when sorting by address is used.
Bsort : It can be recommended if the input to the file is known to be nearly sorted.
Meansort : It can be recommended only for input known to be very nearly sorted.
Quick Sort : In the virtual memory environment, where pages of data are constantly being
swapped back and forth between external and internal storage. In practical situations, quick
sort is often the fastest available because of its low overhead and its average behavior.

Interview Questions on C-Language

www.mallepoolu.com
Heap sort : Generally used for sorting of complete binary tree. Simple insertion sort and
straight selection sort : Both are more efficient than bubble sort. Selection sort is
recommended for small files when records are large and for reverse situation insertion sort is
recommended. The heap sort and quick sort are both more efficient than insertion or selection
for large number of data.
Shell sort : It is recommended for moderately sized files of several hundred elements.
Radix sort : It is reasonably efficient if the number of digits in the keys is not too large.
76.
Calculating Wasted Bytes On Disk
When a file gets stored on the disk, at a time DOS allocates one cluster for it. A cluster is
nothing but a group of sectors. However, since all file sizes cannot be expected to be a
multiple of 512 bytes, when a file gets stored often part of the cluster remains unoccupied.
This space goes waste unless the file size grows to occupy these wasted bytes. The
following program finds out how much space is wasted for all files in all the directories of the
current drive.
#include <dir.h>
#include <dos.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
unsigned bytes_per_cluster ;
unsigned long wasted_bytes ;
unsigned long num_files = 0 ;
main( )
{
int ptr = 0, flag = 0, first = 0 ;
struct ffblk f[50] ;
struct dfree free ;
/* get cluster information and calculate bytes per cluster */
getdfree ( 0, &free ) ;
bytes_per_cluster = free.df_bsec * free.df_sclus ;
chdir ( "\\" ) ;
/* check out files in root directory first */
cal_waste( ) ;
/* loop until all directories scanned */
while ( ptr != -1 )
{
/* should I do a findfirst or a findnext? */
if ( first == 0 )
flag = findfirst ( "*.*", &f[ptr], FA_DIREC ) ;
else
flag = findnext ( &f[ptr] ) ;
while ( flag == 0 )
{

Interview Questions on C-Language

www.mallepoolu.com
/* make sure its a directory and skip over . & .. entries */
if ( f[ptr].ff_attrib == FA_DIREC && f[ptr].ff_name[0] != '.' )
{
flag = chdir ( f[ptr].ff_name ) ; /* try changing directories */
if ( flag == 0 ) /* did change dir work? */
{
cal_waste( ) ;
first = 0 ; /* set for findfirst on next pass */
break ;
}
}
flag = findnext ( &f[ptr] ) ; /* search for more dirs */
}
if ( flag != 0 || ptr == 49 ) /* didn't find any more dirs */
{
ptr-- ;
chdir ( ".." ) ; /* go back one level */
first = 1 ; /* set to findnext on next pass */
}
else
ptr++ ;
}
printf ( "There are %lu bytes wasted in %lu files.\n", wasted_bytes,
num_files ) ;
}
cal_waste( )
{
int flag = 0 ;
long full_cluster ;
struct ffblk ff ;
/* look for all file types */
flag = findfirst ( "*.*", &ff, FA_RDONLY | FA_HIDDEN | FA_SYSTEM | FA_ARCH
);
while ( flag == 0 )
{
num_files++ ;
full_cluster = ff.ff_fsize / bytes_per_cluster * bytes_per_cluster ;
wasted_bytes += bytes_per_cluster - ( ff.ff_fsize - full_cluster ) ;
flag = findnext ( &ff ) ;
}
}
Data Structures
Polish Notation
The method of writing all operators either before their operation, or after them, is called
Polish notation, in honor of its discoverer, the Polish mathematician Jan Lukasiewicz. When
the operators are written before their operands, it is called the prefix form. When the
operators come after their operands. It is called the postfix form, or, sometimes reverse Polish

Interview Questions on C-Language

www.mallepoolu.com
form or suffix form. In this context, it is customary to use the coined phrase infix form to
denote the usual custom of writing binary operators between their operands. For example, the
expression A + B becomes +AB in prefix form and AB+ in postfix form. In the expression A
+ B x C, the multiplication is done first, so we convert it first, obtaining first A + ( BCx ) and
then ABCx+ in postfix form. The prefix form of this expression is +A x BC. The prefix and
postfix forms are not related by taking mirror images or other such simple transformation.
Also all parentheses have been omitted in the Polish forms.
------------------------------------------------------------------------------------------------77.
The Longjmp And Setjmp
The C programming language does not let you nest functions. You cannot write a function
definition inside another function definition, as in:
int fun1( )
{
int fun2() /* such nesting of functions is not allowed */
{
.....
}
}
Because of this restriction it is not possible to hide function names inside a hierarchy. As a
result all the functions that you declare within a program are visible to each other. This of
course is not a major drawback since one can limit visibility by grouping functions within
separate C source files that belong to different logical units of the program. C does, however,
suffer in another way because of this design decision. It provides no easy way to transfer
control out of a function except by returning to the expression that called the function. For the
vast majority of function calls, that is a desirable limitation. You want the discipline of nested
function calls and returns to help you understand flow of control through a program.
Nevertheless, on some occasions that discipline is too restrictive. The program is sometimes
easier to write, and to understand, if you can jump out of one or more function invocations at
a single stroke. You want to bypass the normal function returns and transfer control to
somewhere in an earlier function invocation.
For example, you may want to return to execute some code for error recovery no matter
where an error is detected in your application. The setjmp and the longjmp functions provide
the tools to accomplish this. The setjmp function saves the "state" or the "context" of the
process and the longjmp uses the saved context to revert to a previous point in the program.
What is the context of the process? In general, the context of a process refers to information
that enables you to reconstruct exactly the way the process is at a particular point in its flow
of execution. In C program the relevant information includes quantities such as values of SP,
SS, FLAGS, CS, IP, BP, DI, ES, SI and DS registers.
To save this information Turbo C uses the following structure, which is defined, in the header
file 'setjmp.h'.
typedef struct
{
unsigned j_sp ;
unsigned j_ss ;

Interview Questions on C-Language

www.mallepoolu.com
unsigned j_flag ;
unsigned j_cs ;
unsigned j_ip ;
unsigned j_bp ;
unsigned j_di ;
unsigned j_es ;
unsigned j_si ;
unsigned j_ds ;
} jmp_buf[1] ;
This is a system-dependent data type because different systems might require different
amounts of information to capture the context of a process. In Turbo C, jmp_buf is simply an
array of ten 2-byte integers. To understand the mechanics of setjmp and longjmp, look at the
following code
fragment.
#include "setjmp.h"
jmp_buf buf ;
main( )
{
if ( setjmp ( buf ) == 0 )
process( ) ;
else
handle_error( ) ; /* executed when longjmp is called */
}
process( )
{
int flag = 0 ;
/* some processing is done here */
/* if an error occurs during processing flag is set up */
if ( flag )
longjmp ( buf, 1 ) ;
}
Upon entry to setjmp the stack contains the address of the buffer buf and the address of the if
statement in the main function, to which setjmp will return. The setjmp function copies this
return address as well as the current values of registers, SP, SS, FLAGS, BP, DI, ES, SI and
DS, into the buffer buf. Then setjmp returns with a zero. In this case, the if statement is
satisfied and the process( ) function is called. If something goes wrong in process( )
(indicated by the flag variable), we call longjmp with two arguments: the first is the buffer
that contains the context to which we will return. When the stack reverts back to this saved
state, and the return statement in longjmp is executed, it will be as if we were returning from
the call to setjmp, which originally saved the buffer buf. The second argument to longjmp
specifies the return value to be used during this return. It should be other than zero so that in
the if statement we can tell whether the return is induced by a longjmp.
The setjmp/longjmp combination enables you to jump unconditionally from one C function to
another without using the conventional return statements. Essentially, setjmp marks the
destination of the jump and longjmp is a non-local goto that executes the jump.
Data Structures
Comparison Trees...

Interview Questions on C-Language

www.mallepoolu.com
The comparison trees also called decision tree or search tree of an algorithm, is obtained by
tracing through the actions of the algorithm, representing each comparison of keys by a
vertex of the tree (which we draw as a circle). Inside the circle we put the index of the key
against which we are comparing the target key. Branches (lines) drawn down from the circle
represent the possible outcomes of the comparison and are labeled accordingly. When the
algorithm terminates, we put either F (for failure) or the location where the target is found at
the end of the appropriate branch, which we call a leaf, and draw as a square. Leaves are also
sometimes called end vertices or external vertices of the tree. The remaining vertices are
called the internal vertices of the tree. The comparison tree for sequential search is especially
simple.
------------------------------------------------------------------------------------------------78.
Suppose we have a floating-point number with higher precision say 12.126487687 and we
wish it to be printed with only precision up to two decimal places. How can I do this?
Ans. This can achieved through the use of suppression char '*' in the format string of printf( )
which is shown in the following program.
main( )
{
int p = 2 ;
float n = 12.126487687 ;
printf ( "%.*f",p, n ) ;
}
------------------------------------------------------------------------------------------------79.
Spawning
All programs that we execute from DOS prompt can be thought of as children of
COMMAND.COM. Thus, the program that we execute is a child process, whereas
COMMAND.COM running in memory is its parent. The process of a parent process giving
birth to a child process is known as 'spawning'. If the spawned program so desires, it may in
turn spawn children of its own, which then execute and return control to their parent. Who is
the parent of COMMAND.COM? COMMAND.COM itself. We can trace the ancestors of
our program using the field Parent Process ID (PID) present at offset 0x16 in the Program
Segment Prefix (PSP). To trace this ancestry our program should first locate its PSP, extract
the parent process ID from it and then use this to find PSP of the parent. This process can be
repeated till we reach COMMAND.COM (process ID of COMMAND.COM is its own PSP),
the father of all processes. Here is a program which achieves this...
/* SPAWN.C */
#include "dos.h"
unsigned oldpsp, newpsp, far *eb_seg, i ;
char far *eb_ptr ;
main( )
{
oldpsp = _psp ;

Interview Questions on C-Language

www.mallepoolu.com
while ( 1 )
{
printf ( "\n" ) ;
printname ( oldpsp ) ;
printf ( " spawned by " ) ;
newpsp = * ( ( unsigned far * ) MK_FP ( oldpsp, 0x16 ) ) ;
if ( * ( ( unsigned * ) MK_FP ( newpsp, 0x16 ) ) == newpsp )
break ;
else
oldpsp = newpsp ;
printname ( newpsp ) ;
}
printf ( "%-20s (%04X)", "COMMAND.COM", newpsp ) ;
}
printname ( unsigned lpsp )
{
char drive[5], dir[68], name[13], ext[5] ;
eb_seg = ( unsigned far * ) MK_FP ( lpsp, 0x2C ) ;
eb_ptr = MK_FP ( *eb_seg, 0 ) ;
i=0;
while ( 1 )
{
if ( eb_ptr[i] == 0 )
{
if ( eb_ptr[i + 1] == 0 && eb_ptr[i + 2] == 1 )
{
i += 4 ;
break ;
}
}
i++ ;
}
fnsplit ( eb_ptr + i, drive, dir, name, ext ) ;
strcat ( name, ext ) ;
printf ( "%-20s (%04X)", name, oldpsp ) ;
}
On running the program from within TC the output obtained is shown below. SPWAN.EXE
(58A9) spawned by TC.EXE (0672) TC.EXE (0672) spawned by COMMAND.COM (05B8).
The program simply copies its own process ID in the variable oldpsp and then uses it to
extract its own filename from its environment block. This is done by the function printname(
). The value in oldpsp is then used to retrieve the parent's PID in newpsp. From there the

Interview Questions on C-Language

www.mallepoolu.com
program loops reporting the values of oldpsp, newpsp and the corresponding file names until
the program reaches COMMAND.COM.
The printname( ) function first locates the environment block of the program and then
extracts the file name from the environment block. The fnsplit( ) function has been used to
eliminate the path present prior to the file name. Do not run the program from command line
since it would give you only one level of ancestry.
Data Structures
Choosing the data structures to be used for information retrieval. For problems of information
retrieval, consider the size, number, and location of the records along with the type and
structure of the keys while choosing the data structures to be used. For small records, highspeed internal memory will be used, and binary search trees will likely prove adequate. For
information retrieval from disk files, methods employing multiway branching, such as trees,
B-trees , and hash tables, will usually be superior. Tries are particularly suited to applications
where the keys are structured as a sequence of symbols and where the set of keys is relatively
dense in the set of all possible keys. For other applications, methods that treat the key as a
single unit will often prove superior. B-trees, together with various generalization and
extensions, can be usefully applied to many problems concerned with external information
retrieval.
80.
Variably Dimensioned Arrays
While dealing with Scientific or Engineering problems one is often required to make use of
multi-dimensioned array. However, when it comes to passing multidimensional arrays to a
function C is found wanting. This is because the C compiler wants to know the size of all but
the first dimension of any array passed to a function. For instance, we can define a function
compute ( int n, float x[] ), but not compute ( int n, x[][]).
Thus, C can deal with variably dimensioned 1-D arrays, but when an array has more than one
dimension, the C compiler has to know the size of the last dimensions expressed as a
constant. This problem has long been recognized, and some of the solutions that are often
used are:
Declare the arrays in the functions to be big enough to tackle all possible situations. This can
lead to a wastage of lot of precious memory in most cases. Another solution is to construct
multiple-dimension array as an array of pointers. For example, a matrix (2-D array) of floats
can be declared as a 1-D array of float pointers, with each element pointing to an array of
floats. The problem with this method is that the calling function has to define all arrays in this
fashion. This means that any other computations done on the arrays must take this special
structure into account.
Another easy solution, though seldom used, exists. This is based on the following method:
Pass the array to the function as though it is a pointer to an array of floats (or the appropriate
data type), no matter how many dimensions the array actually has, along with the dimensions
of the array. Reference individual array elements as offsets from this pointer.

Interview Questions on C-Language

www.mallepoolu.com
Write your algorithm so that array elements are accessed in storage order. The following
program for multiplying two matrices illustrates this
procedure.
# define M 3
# define N 2
# define P 4
float a[M][N], b[N][P], c[M][P] ;
void mulmat ( int, int, int, float*, float*, float* ) ;
main( )
{
int i, j ;
for ( i = 0 ; i < M ; i++ )
for ( j = 0 ; j < N ; j++ )
a[i][j] = i + j ;
for ( i = 0 ; i < N ; i++ )
for ( j = 0 ; j < P ; j++ )
b[i][j] = i + j ;
mulmat ( M, N, P, a, b, c ) ;
for ( i = 0 ; i < M ; i++ )
{
printf ( "\n" ) ;
for ( j = 0 ; j < P ; j++ )
printf ( "%f\t", c[i][j] ) ;
}
}
void mulmat ( int m, int n, int p, float *a, float *b, float *c )
{
float *ptrtob, *ptrtoc ;
int i, j, k, nc ;
/* set all elements of matrix c to 0 */
for ( i = 0 ; i < m * p ; i++ )
*( c + i ) = 0 ;
for ( i = 0 ; i < m ; i++ )
{
ptrtob = b ;
for ( k = 0 ; k < n ; k++ )
{
ptrtoc = c ;
for ( j = 0 ; j < p ; j++ )
*ptrtoc++ += *a * *ptrtob++ ;
a++ ;
}

Interview Questions on C-Language

www.mallepoolu.com
c += p ;
}
}
We know that C stores array elements in a row-major order. Hence to ensure that the
elements are accessed in the storage order the above program uses a variation of the normal
matrix-multiplication procedure. The pseudo code for this is given below:
for i = 1 to m
for j = 1 to p
c[i][j] = 0
end
for k = 1 to n
for j = 1 to p
c[i][j] = c[i][j] + a[i][k] * b[k][j]
end
end
end
------------------------------------------------------------------------------------------------81.
Why is it not possible to scan strings from keyboard in case of array of pointers to string?
Ans: When an array is declared, dimension of array should be specified so that compiler can
allocate memory for the array. When array of pointers to strings is declared its elements
would contain garbage addresses. These addresses would be passed to scanf( ). So strings can
be received but they would get stored at unkown locations. This is unsafe.
------------------------------------------------------------------------------------------------82.
Bit Arrays
If in a program a variable is to take only two values 1 and 0, we really need only a single bit
to store it. Similarly, if a variable is to take values from 0 to 3, then two bits are sufficient to
store these values. And if a variable is to take values from 0 through 7, then three bits will be
enough, and so on. Why waste an entire integer when one or two or three bits will do?
Because there aren't any one bit or two bit or three bit data types available in C. However,
when there are several variables whose maximum values are small enough to pack into a
single memory location, we can use `bit fields' to store several values in a single integer. Bit
fields are discussed in most standard C texts. They are usually used when we want to store
assorted information which can be accommodated in 1, 2, 3 bits etc.
For example, the following data about an employee can be easily stored using bit fields.
male or female
single, married, divorced or widowed
have one of the eight different hobbies
can choose from any of the fifteen different schemes proposed by the company to pursue
his/her hobby.
This means we need one bit to store gender, two to store marital status, three for hobby, and
four for scheme (with one value used for those who are not desirous of availing any of the

Interview Questions on C-Language

www.mallepoolu.com
schemes). We need ten bits altogether, which means we can pack all this information into a
single integer, since an integer is 16 bits long.
At times we may need to store several True or False statuses. In such cases instead of using
bit fields using an array of bits would be more sensible. On this array we may be required to
perform the following operations:
Set a bit (make it 1).
Clear a bit (make it 0).
Test the status of a bit in the array.
Reach the appropriate bit slot in the array.
Generate a bit mask for setting and clearing a bit.
We can implement these operations using macros given below:
#define CHARSIZE 8
#define MASK ( y ) ( 1 << y % CHARSIZE )
#define BITSLOT ( y ) ( y / CHARSIZE )
#define SET ( x, y ) ( x[BITSLOT( y )] |= MASK( y ) )
#define CLEAR ( x, y ) ( x[BITSLOT( y )] &= ~MASK( y ) )
#define TEST ( x, y ) ( x[BITSLOT( y )] & MASK( y ) )
#define NUMSLOTS ( n ) ( ( n + CHARSIZE - 1) / CHARSIZE )
Using these macros we can declare an array of 50 bits be saying,
char arr[NUMSLOTS(50)] ;
To set the 20th bit we can say,
SET(arr, 20 ) ;
And if we are to test the status of 40th bit we may say,
if ( TEST ( arr, 40 ) )
Using bit arrays often results into saving a lot of precious memory. For example, the
following program which implements the Sieve of Eratosthenes for generating prime
numbers smaller than 100 requires only 13 bytes. Had we implemented the same logic using
an array of integers we would have required an array of 100 integers, that is 200 bytes.
#include <stdio.h>
#include <string.h>
#define MAX 100
main( )
{
char arr[NUMSLOTS( MAX )] ;
int i, j ;
memset ( arr, 0, NUMSLOTS( MAX ) ) ;
for ( i = 2 ; i < MAX ; i++ )
{
if ( !TEST ( arr, i ) )
{
printf ( "\n%d", i ) ;

Interview Questions on C-Language

www.mallepoolu.com
for ( j = i + i ; j < MAX ; j += i )
SET ( arr, j ) ;
}
}
}
------------------------------------------------------------------------------------------------83.
Information Hiding in C
Though C language doesn't fully support encapsulation as C++ does, there is a simple
technique through which we can implement encapsulation in C. The technique that achieves
this is modular programming in C. Modular programming requires a little extra work from
the programmer, but pays for itself during maintenance. To understand this technique let us
take the example of the popular stack data structure. There are many methods of
implementing a stack (array, linked list, etc.). Information hiding teaches that users should be
able to push and pop the stack's elements without knowing about the stack's implementation.
A benefit of this sort of information hiding is that users don't have to change their code even
if the implementation details change.
Consider the following scenario:
To be able to appreciate the benefits of modular programming and thereby information
hiding, would first show a traditional implementation of the stack data structure using
pointers and a linked list of structures. The main( ) function calls the push( ) and pop( )
functions.
#include <alloc.h>
typedef int element ;
void initialize_stack ( struct node ** ) ;
void push ( struct node **, element ) ;
element pop ( struct node * ) ;
int isempty ( struct node * ) ;
struct node
{
element data ;
struct node *next ;
};
void main( )
{
struct node *top ;
element num ;
initialize_stack ( &top ) ;
push ( &top, 10 ) ;
push ( &top, 20 ) ;
push ( &top, 30 ) ;
if ( isempty ( top ) )
printf ( "\nStack is empty" ) ;
else
{
num = pop ( top ) ;

Interview Questions on C-Language

www.mallepoolu.com
printf ( "\n Popped %d", num ) ;
}
}
void initialize_stack ( struct node **p )
{
*p = NULL ;
}
void push ( struct node **p, element n )
{
struct node *r ;
r = ( struct node *) malloc ( sizeof ( struct node ) ) ;
r -> data = n ;
if ( *p == NULL )
r -> next = NULL ;
else
r -> next = *p ;
*p = r ;
}
element pop ( struct node *p )
{
element n ;
struct node *r ;
n = p -> data ;
r=p;
p = p -> next ;
free ( r ) ;
return ( n ) ;
}
int isempty ( struct node *p )
{
if ( p == NULL )
return ( -1 ) ;
else
return ( 0 ) ;
}
Notice how the specific implementation of the data structure is strewn throughout main( ).
main( ) must see the definition of the structure node to use the push( ), pop( ), and other stack
functions. Thus the implementation is not hidden, but is mixed with the abstract operations.
Data Structures
Radix Sort
This sorting technique is based on the values of the actual digits in the positional
representations of the numbers being sorted. Using the decimal base, for example, where the
radix is 10, the numbers can be partitioned into ten groups on the sorter. For example, to sort
a collection of numbers where each number is a four-digit number, then, All the numbers are
first sorted according to the the digit at unit's place.
In the second pass, the numbers are sorted according to the digit at tenth place. In the third
pass, the numbers are sorted according to the digit at hundredth place. In the forth and last

Interview Questions on C-Language

www.mallepoolu.com
pass, the numbers are sorted according to the digit at thousandth place.
During each pass, each number is taken in the order in which it appears in partitions from
unit's place onwards. When these actions have been performed for each digit, starting with
the least significant and ending with most significant, the numbers are sorted. This sorting
method is called the radix sort.
Let us take another example. Suppose we have a list of names. To sort these names using
radix sort method we will have to classify them into 26 groups The list is first sorted on the
first letter of each name, i.e. the names are arranged in 26 classes, where the first class
consists of those names that begin with alphabet 'A', the second class consists of those names
that begin with alphabet 'B' and so on. During the second pass each class is alphabetized
according to the second letter of the name, and so on.
84.
Exception Handling in C
Consider the following program:
#include <math.h>
void main( )
{
float i ;
i = pow ( -2, 3 ) ;
printf ( "%f", i ) ;
}
int matherr ( struct exception *a )
{
if ( a -> type == DOMAIN )
{
if ( !strcmp ( a -> name, "pow" ) )
{
a -> retval = pow ( - ( a -> arg1 ), a -> arg2 ) ;
return 1 ;
}
}
return 0 ;
}
If we pass a negative value in pow( ) function a run time error occurs. If we wish to get the
proper output even after passing a negative value in the pow( ) function we must handle the
run time error. For this, we can define a function matherr( ) which is declared in the 'math.h'
file. In this function we can detect the run-time error and write our code to correct the error.
The elements of the exception structure receives the function name and arguments of the
function causing the exception.
Data Structures

Interview Questions on C-Language

www.mallepoolu.com
AVL Trees
For ideal searching in a binary search tree, the heights of the left and right sub-trees of any
node should be equal. But, due to random insertions and deletions performed on a binary
search tree, it often turns out to be far from ideal. A close approximation to an ideal binary
search tree is achievable if it can be ensured that the difference between the heights of the left
and the right sub trees of any node in the tree is at most one. A binary search tree in which the
difference of heights of the right and left sub-trees of any node is less than or equal to one is
known as an AVL tree. AVL tree is also called as Balanced Tree. The name "AVL Tree" is
derived from the names of its inventors who are Adelson-Veilskii and Landi. A node in an
AVL tree have a new field to store the "balance factor" of a node which denotes the
difference of height between the left and the right sub-trees of the tree rooted at that node.
And it can assume one of the
three possible values {-1,0,1}.
------------------------------------------------------------------------------------------------85.
Unique combinations for a given number
How do I write a program which can generate all possible combinations of numbers from 1 to
one less than the given number ?
main( )
{
long steps, fval, bstp, cnt1 ;
int num, unit, box[2][13], cnt2, cnt3, cnt4 ;
printf ( "Enter Number " ) ;
scanf ( "%d", &num ) ;
num = num < 1 ? 1 : num > 12 ? 12 : num ;
for ( steps = 1, cnt1 = 2 ; cnt1 <= num ; steps *= cnt1++ ) ;
for ( cnt1 = 1 ; cnt1 <= steps ; cnt1++ )
{
for ( cnt2 = 1 ; cnt2 <= num ; cnt2++ )
box[0][cnt2] = cnt2 ;
for ( fval = steps, bstp = cnt1, cnt2 = 1 ; cnt2 <= num ; cnt2++ )
{
if ( bstp == 0 )
{
cnt4=num ;
while ( box[0][cnt4] == 0 )
cnt4-- ;
}
else
{
fval /= num - cnt2 + 1 ;
unit = ( bstp + fval - 1 ) / fval ;
bstp %= fval ;
for ( cnt4 = 0, cnt3 = 1 ; cnt3 <= unit ; cnt3++ )
while ( box[0][++cnt4] == 0 ) ;
}
box[1][cnt2] = box[0][cnt4] ;

Interview Questions on C-Language

www.mallepoolu.com
box[0][cnt4] = 0 ;
}
printf ( "\nSeq.No.%ld:", cnt1 ) ;
for ( cnt2 = 1 ; cnt2 <= num ; cnt2++ )
printf ( " %d", box[1][cnt2] ) ;
}
}
This program computes the total number of steps. But instead of entering into the loop of the
first and last combination to be generated it uses a loop of 1 to number of combinations. For
example, in case of input being 5 the number of possible combinations would be factorial 5,
i.e. 120. The program suffers from the limitation that it cannot generate combinations for
input beyond 12 since a long int cannot handle the resulting combinations.
Data Structures
Hashing...
Hashing or hash addressing is a searching technique. Usually, search of an element is carried
out via a sequence of comparisons. Hashing differs from this as it is independent of the
number of elements n in the collection of data. Here, the address or location of an element is
obtained by computing some arithmetic function. Hashing is usually used in file
management. The general idea is of using the key to determine the address of a record. For
this, a function fun( ) is applied to each key, called the hash function. Some of the popular
hash functions are: 'Division' method, 'Midsquare' method, and 'Folding' method. Two
records cannot occupy the same position. Such a situation is called a hash collision or a hash
clash. There are two basic methods of dealing with a hash clash. The first technique, called
rehashing, involves using secondary hash function on the hash key of the item. The rehash
function is applied successively until an empty position is found where the item can be
inserted. If the hash position of the item is found to be occupied during a search, the rehash
function is again used to locate the item. The second technique, called chaining, builds a
linked list of all items whose keys hash to the same values. During search, this short linked
list is traversed sequentially for the desired key. This technique involves adding an extra link
field to each table position.
------------------------------------------------------------------------------------------------86.
The following program demonstrates how to get input from the user in graphics mode,
echoed in the current colors and font size and font style.
#define ON 1
#define OFF 0
#include <graphics.h>
main( )
{
char nameString[80], ageString[80] ;
int age, gd = DETECT, gm ;
initgraph ( &gd, &gm, "c:\\tc\\bgi" ) ;
setbkcolor ( BLUE ) ;
setcolor ( YELLOW ) ;

Interview Questions on C-Language

www.mallepoolu.com
settextstyle ( GOTHIC_FONT, HORIZ_DIR, 0 ) ;
moveto ( 0, 0 ) ;
outtext ( "Enter your name: " ) ;
getGrString ( nameString ) ;
moveto ( 0, gety( ) + textheight ( "A" ) ) ;
outtext ( "Name: " ) ;
outtext ( nameString ) ;
moveto ( 0, gety( ) + textheight ( "A" ) ) ;
outtext ( "Press key to exit! " ) ;
getch( ) ;
closegraph( ) ;
restorecrtmode( ) ;
}
getGrString ( char *inputString )
{
int stringIndex = 0, oldColor ;
char ch, outString[2] ;
/* xVal will store the screen position for each char */
int xVal[255] ;
outString[1] = 0 ;
xVal[0] = getx( ) ;
do
{
cursor ( ON ) ;
ch = getch( ) ;
cursor ( OFF ) ;
if ( ch == 0 ) /* avoid dealing with all special keys */
getch( ) ;
else
{
if ( ch == 8 ) /* backspace */
{
oldColor = getcolor( ) ;
--stringIndex ;
if ( stringIndex < 0 )
stringIndex = 0 ;
/* move to ( old horz position, current vert position ) */
moveto ( xVal[stringIndex], gety( ) ) ;
setcolor ( getbkcolor( ) ) ;
outString[0] = inputString[stringIndex] ;
outtext ( outString ) ;
moveto ( xVal [stringIndex], gety( ) ) ;
setcolor ( oldColor ) ;
}
else
{
inputString[stringIndex] = ch ;
outString[0] = ch ;
outtext ( outString ) ;
++stringIndex ;

Interview Questions on C-Language

www.mallepoolu.com
xVal[stringIndex] = getx( ) ;
}
}
} while ( ch != 13 && ch != 10 ) ;
inputString[stringIndex] = 0 ;
}
cursor ( int on )
{
int curX, oldColor ;
/* we'll use an underscore as a cursor */
char uBarStr[2] = { '_', 0 } ;
if ( !on )
{
oldColor = getcolor( ) ;
setcolor ( getbkcolor( ) ) ;
}
/* save horizontal position before drawing cursor */
curX = getx( ) ;
outtext ( uBarStr ) ;
moveto ( curX, gety( ) ) ;
/* if we changed the color to erase cursor, change it back */
if ( !on )
setcolor ( oldColor ) ;
}
The function getGrString( ) echoes graphically the user input and stores it in a buffer, and the
function cursor( ) handles the cursor position.
System Utility
What is garbage collection?
Ans: Suppose some memory space becomes reusable because a node is released from a
linked list. Hence, we want the space to be available for future use. One way to bring this
about is to immediately reinsert the space into the free-storage list. However, this method
may be too time-consuming for the operating system. The operating system may periodically
collect all the deleted space onto the free-storage list. The technique that does this collection
is called Garbage Collection. Garbage Collection usually takes place in two steps: First the
Garbage Collector runs through all lists, tagging whose cells are currently in use, and then it
runs through the memory, collecting all untagged space onto the free-storage list. The
Garbage Collection may take place when there is only some minimum amount of space or no
space at all left in the free-storage list, or when the CPU is idle and has time to do the
collection. Generally speaking, the Garbage Collection is invisible to the programmer.
87.
How do I get the time elapsed between two function calls ?
Ans: The function difftime( ) finds the difference between two times. It calculates the elapsed
time in seconds and returns the difference between two times as a double value.

Interview Questions on C-Language

www.mallepoolu.com
#include <time.h>
#include <stdio.h>
#include <dos.h>
main( )
{
int a[] = { 2, -34, 56, 78, 112, 33, -7, 11, 45, 29, 6 } ;
int s ;
time_t t1, t2 ; // time_t defines the value used for time function
s = sizeof ( a ) / 2 ;
t1 = time ( NULL ) ;
sel_sort ( a, s ) ; // sort array by selection sort
bub_sort ( a, s ) ; // sort array by bubble sort method
t2 = time ( NULL ) ;
printf ( "\nThe difference between two function calls is %f", difftime (
t2, t1 ) ) ;
}
In the above program we have called difftime( ) function that returns the time elapsed from t1
to t2.
------------------------------------------------------------------------------------------------88.
General
main( )
{
char *s ;
s = fun ( 128, 2 ) ;
printf ( "\n%s", s ) ;
}
fun ( unsigned int num, int base )
{
static char buff[33] ;
char *ptr ;
ptr = &buff [ sizeof ( buff ) - 1 ] ;
*ptr = '\0' ;
do
{
*--ptr = "0123456789abcdef"[ num % base ] ;
num /= base ;
} while ( num != 0 ) ;
return ptr ;
}
The above program would convert the number 128 to the base 2. You can convert a number
to a hexadecimal or octal form by passing the number and the base, to the function fun( ).
Data Structures
What is a priority queue?

Interview Questions on C-Language

www.mallepoolu.com

Ans: As we know in a stack, the latest element is deleted and in a queue the oldest element is
deleted. It may be required to delete an element with the highest priority in the given set of
values and not only the oldest or the newest one. A data structure that supports efficient
insertions of a new element and deletions of elements with the highest priority is known as
priority queue. There are two types of priority queues: an ascending priority queue is a
collection of items into which items can be inserted arbitrarily and from which only the
smallest item can be removed. A descending order priority queue is similar but allows only
the largest item to be deleted.
------------------------------------------------------------------------------------------------89.
What is the difference between const char *p, char const *p, and char* const p ?
'const char *p' and 'char const *p' are the same, i.e. p points to a constant character. On the
other hand, 'char* const p' means p is a constant pointer pointing to a character which means
we cannot change the pointer p but we can change the character which p is pointing to.
------------------------------------------------------------------------------------------------90.
What's the difference between a null pointer, a NULL macro, the ASCII NUL character and a
null string?
Ans: A null pointer is a pointer which doesn't point anywhere. A NULL macro is used to
represent the null pointer in source code. It has a value 0 associated with it. The ASCII NUL
character has all its bits as 0 but doesn't have any relationship with the null pointer. The null
string is just another name for an empty string "".
System Utility
Sparse Matrix...
A sparse matrix is one where most of its elements are zero. There is no precise definition as
to know whether a matrix is sparsed or not, but it is a concept which we all can recognize
intuitively. The natural method of representing matrices in memory as two-dimensional
arrays may not be suitable for sparse matrices. That is one may save space by storing only
those entries which may be nonzero. If this is done, then the matrix may be thought of as an
ordered list of non-zero elements only. Information about a non-zero element has three parts:
an integer representing its row,
an integer representing its column and
the data associated with this element.
That is, each element of a matrix is uniquely characterized by its row and column position,
say i, j. We might store that matrix as a list of 3-tuples of the form (i, j, data), as shown
below,
Although the non-zero elements may be stored in the array in any order, keeping them
ordered in some fashion may be advantageous for further processing. Note that above array is
arranged in increasing order of the row number of non-zero elements. Moreover, for elements
in the same row number, the array is arranged in order of increasing column number.

Interview Questions on C-Language

www.mallepoolu.com
------------------------------------------------------------------------------------------------91.
Pointers
What does the error "Null Pointer Assignment" mean and what causes this error?
Ans: The Null Pointer Assignment error is generated only in small and medium memory
models. This error occurs in programs which attempt to change the bottom of the data
segment. In Borland's C or C++ compilers, Borland places four zero bytes at the bottom of
the data segment, followed by the Borland copyright notice "Borland C++ - Copyright 1991
Borland Intl.". In the small and medium memory models, a null pointer points to DS:0000.
Thus assigning a value to the memory referenced by this pointer will overwrite the first zero
byte in the data segment. At program termination, the four zeros and the copyright banner are
checked. If either has been modified, then the Null Pointer Assignment error is generated.
Note that the pointer may not truly be null, but may be a wild pointer that references these
key areas in the data segment.
Data Structures
How to build an expression trees ?
Ans: An expression tree is a binary tree which is built from simple operands and operators of
an (arithmetic or logical ) expression by placing simple operands as the leaves of a binary tree
and the operators as the interior nodes. If an operator is binary , then it has two nonempty
subtrees, that are its left and right operands (either simple operands or sub expressions). If an
operator is unary, then only one of its subtrees is nonempty, the one on the left or right
according as the operator is written on the right or left of its operand. We traditionally write
some unary operators to the left of their operands, such as "-" ( unary negation) or the
standard functions like log( ), sin( ) etc. Others are written on the right, such as the factorial
function ()!. If the operator is written on the left, then in the expression tree we take its left
subtree as empty. If it appears on the right, then its right subtree will be empty. An example
of an expression tree is shown below for the expression ( -a < b ) or ( c + d ) .
------------------------------------------------------------------------------------------------92.
Can we get the remainder of a floating point division ?
Ans : Yes. Although the % operator fails to work on float numbers we can still get the
remainder of floating point division by using a function fmod( ). The fmod( ) function divides
the two float numbers passed to it as parameters and returns the remainder as a floating-point
value. Following program shows fmod( ) function at work.
#include <math.h>
main( )
{
printf ( "%f", fmod ( 5.15, 3.0 ) ) ;
}

Interview Questions on C-Language

www.mallepoolu.com
The above code snippet would give the output as 2.150000.
------------------------------------------------------------------------------------------------93.
How to extract the integer part and a fractional part of a floating point number?
Ans: C function modf( ) can be used to get the integer and fractional part of a floating point.
#include "math.h"
main( )
{
double val, i, f ;
val = 5.15 ;
f = modf ( val, &i ) ;
printf ( "\nFor the value %f integer part = %f and fractional part = %f",
val, i, f ) ;
}
The output of the above program will be:
For the value 5.150000 integer part = 5.000000 and fractional part =
0.150000
94.
How do I define a pointer to a function which returns a char pointer?
Ans:
char * ( *p )( ) ;
or
typedef char * ( * ptrtofun )( ) ;
ptrtofun p ;
Here is a sample program which uses this definition.
main( )
{
typedef char * ( * ptrtofun ) ( ) ;
char * fun( ) ;
ptrtofun fptr ;
char *cptr ;
fptr = fun ;
cptr = (*fptr) ( ) ;
printf ( "\nReturned string is \"%s\"", cptr ) ;
}
char * fun( )
{
static char s[ ] = "Hello!" ;
printf ( "\n%s", s ) ;
return s ;
}
-------------------------------------------------------------------------------------------------

Interview Questions on C-Language

www.mallepoolu.com
95.
What's wrong with the following declaration: char* ptr1, ptr2 ; get errors when I try to use
ptr2 as a pointer.
Ans: char * applies only to ptr1 and not to ptr2. Hence ptr1 is getting declared as a char
pointer, whereas, ptr2 is being declared merely as a char. This can be rectified in two ways :
char *ptr1, *ptr2 ;
typedef char* CHARPTR ; CHARPTR ptr1, ptr2 ;
------------------------------------------------------------------------------------------------96.
How to use scanf( ) to read the date in the form of dd-mm-yy?
Ans: To read the date in the form of dd-mm-yy one possible way is,
int dd, mm, yy ;
char ch ; /* for char '-' */
printf ( "\nEnter the date in the form of dd-mm-yy : " ) ;
scanf( "%d%c%d%c%d", &dd, &ch, &mm, &ch, &yy ) ;
Another way is to use suppression character * is as follows:
int dd, mm, yy ;
scanf( "%d%*c%d%*c%d", &dd, &mm, &yy ) ;
The suppression character '*' suppresses the input read from the standard input buffer for the
assigned control character.
------------------------------------------------------------------------------------------------97.
Why the output of sizeof ( 'a' ) is 2 and not 1 ?
Ans: Character constants in C are of type int, hence sizeof ( 'a' ) is equivalent to sizeof ( int ),
i.e. 2. Hence the output comes out to be 2 bytes.
------------------------------------------------------------------------------------------------98.
Can we use scanf( ) function to scan a multiple words string through keyboard?
Ans: Yes. Although we usually use scanf( ) function to receive a single word string and gets(
) to receive a multi-word string from keyboard we can also use scanf( ) function for scanning
a multi-word string from keyboard. Following program shows how to achieve this.
main( )
{
char buff[15] ;
scanf ( "%[^\n]s", buff ) ;
puts ( buff ) ;
}
In the scanf( ) function we can specify the delimiter in brackets after the ^ character. We have
specified '\n' as the delimiter. Hence scanf( ) terminates only when the user hits Enter key.
------------------------------------------------------------------------------------------------99.

Interview Questions on C-Language

www.mallepoolu.com
How to set the system date through a C program ?
Ans: We can set the system date using the setdate( ) function as shown in the following
program. The function assigns the current time to a
structure date.
#include "stdio.h"
#include "dos.h"
main( )
{
struct date new_date ;
new_date.da_mon = 10 ;
new_date.da_day = 14 ;
new_date.da_year = 1993 ;
setdate ( &new_date ) ;
}
------------------------------------------------------------------------------------------------100.
How can I write a general-purpose swap without using templates?
Ans: Given below is the program which uses the stringizing preprocessor directive ## for
building a general purpose swap macro which can swap two integers, two floats, two chars,
etc.
#define swap( a, b, t ) ( g ## t = ( a ), ( a ) = ( b ), ( b ) = g ## t )
int gint;
char gchar;
float gfloat ;
main( )
{
int a = 10, b = 20 ;
char ch1 = 'a' , ch2 = 'b' ;
float f1 = 1.12, f2 = 3.14 ;
swap ( a, b, int ) ;
printf ( "\na = %d b = %d", a, b ) ;
swap ( ch1, ch2, char ) ;
printf ( "\nch1 = %c ch2 = %c", ch1, ch2 ) ;
swap ( f1, f2, float ) ;
printf ( "\nf1 = %4.2f f2 = %4.2f", f1, f2 ) ;
}
swap ( a, b, int ) would expand to,
( gint = ( a ), ( a ) = ( b ), ( b ) = gint )
------------------------------------------------------------------------------------------------101.
What is a heap ?

Interview Questions on C-Language

www.mallepoolu.com
Ans : Heap is a chunk of memory. When in a program memory is allocated dynamically, the
C run-time library gets the memory from a collection of unused memory called the heap. The
heap resides in a program's data segment. Therefore, the amount of heap space available to
the program is fixed, and can vary from one program to another.
102.
How to obtain a path of the given file?
Ans: The function searchpath( ) searches for the specified file in the subdirectories of the
current path. Following program shows how to make use of the searchpath( ) function.
#include "dir.h"
void main ( int argc, char *argv[] )
{
char *path ;
if ( path = searchpath ( argv[ 1 ] ) )
printf ( "Pathname : %s\n", path ) ;
else
printf ( "File not found\n" ) ;
}
------------------------------------------------------------------------------------------------103.
Can we get the process identification number of the current program?
Ans: Yes! The macro getpid( ) gives us the process identification number of the program
currently running. The process id. uniquely identifies a program. Under DOS, the getpid( )
returns the Program Segment Prefix as the process id. Following program illustrates the use
of this macro.
#include <stdio.h>
#include <process.h>
void main( )
{
printf ( "The process identification number of this program is %X\n",
getpid( ) ) ;
}
-------------------------------------------------------------------------------------------------

104.
How do I write a function that takes variable number of arguments?
Ans: The following program demonstrates this.

Interview Questions on C-Language

www.mallepoolu.com
#include <stdio.h>
#include <stdarg.h>
void main( )
{
int i = 10 ;
float f = 2.5 ;
char *str = "Hello!" ;
vfpf ( "%d %f %s\n", i, f, str ) ;
vfpf ( "%s %s", str, "Hi!" ) ;
}
void vfpf ( char *fmt, ... )
{
va_list argptr ;
va_start ( argptr, fmt ) ;
vfprintf ( stdout, fmt, argptr ) ;
va_end ( argptr ) ;
}
Here, the function vfpf( ) has called vfprintf( ) that take variable argument lists. va_list is an
array that holds information required for the macros va_start and va_end. The macros
va_start and va_end provide a portable way to access the variable argument lists. va_start
would set up a pointer argptr to point to the first of the variable arguments being passed to the
function. The macro va_end helps the called function to perform a normal return.
------------------------------------------------------------------------------------------------105.
Can we change the system date to some other date?
Ans: Yes, We can! The function stime( ) sets the system date to the specified date. It also sets
the system time. The time and date is measured in seconds from the 00:00:00 GMT, January
1, 1970. The following program shows how to use this function.
#include <stdio.h>
#include <time.h>
void main( )
{
time_t tm ;
int d ;
tm = time ( NULL ) ;
printf ( "The System Date : %s", ctime ( &tm ) ) ;
printf ( "\nHow many days ahead you want to set the date : " ) ;
scanf ( "%d", &d ) ;
tm += ( 24L * d ) * 60L * 60L ;
stime ( &tm ) ;
printf ( "\nNow the new date is : %s", ctime ( &tm ) ) ;

Interview Questions on C-Language

www.mallepoolu.com
}
In this program we have used function ctime( ) in addition to function stime( ). The ctime( )
function converts time value to a 26-character long string that contains date and time.
------------------------------------------------------------------------------------------------106.
How to use function strdup( ) in a program?
Ans : The string function strdup( ) copies the given string to a new location. The function
uses malloc( ) function to allocate space required for the duplicated string. It takes one
argument a pointer to the string to be duplicated. The total number of characters present in
the given string plus one bytes get allocated for the new string. As this function uses malloc( )
to allocate memory, it is the programmers responsibility to deallocate the memory using
free( ).
#include <stdio.h>
#include <string.h>
#include <alloc.h>
void main( )
{
char *str1, *str2 = "double";
str1 = strdup ( str2 ) ;
printf ( "%s\n", str1 ) ;
free ( str1 ) ;
}
------------------------------------------------------------------------------------------------107.
On including a file twice I get errors reporting redefinition of function.
How can I avoid duplicate inclusion?
Ans: Redefinition errors can be avoided by using the following macro definition. Include this
definition in the header file.
#if !defined filename_h
#define filename_h
/* function definitions */
#endif
Replace filename_h with the actual header file name. For example, if name of file to be
included is 'goto.h' then replace filename_h with 'goto_h'.
------------------------------------------------------------------------------------------------108.
How to write a swap( ) function which swaps the values of the variables using bitwise
operators.
Ans: Here is the swap( ) function.
swap ( int *x, int *y )
{
*x ^= *y ;
*y ^= *x ;

Interview Questions on C-Language

www.mallepoolu.com
*x ^= *y ;
}
The swap( ) function uses the bitwise XOR operator and does not require any temporary
variable for swapping.

Interview Questions on C-Language

www.mallepoolu.com
1.

Can we declare a static function as virtual?


Ans: No. The virtual function mechanism is used on the specific object that determines
which virtual function to call. Since the static functions are not any way related to
objects, they cannot be declared as virtual.
------------------------------------------------------------------------------------------------2.
Can user-defined object be declared as static data member of another class?
Ans: Yes. The following code shows how to initialize a user-defined object.
#include
class test
{
int i ;
public :
test ( int ii = 0 )
{
i = ii ;
}
};
class sample
{
static test s ;
};
test sample::s ( 26 ) ;
Here we have initialized the object s by calling the one-argument constructor. We can
use the same convention to initialize the object by calling multiple-argument constructor.
------------------------------------------------------------------------------------------------3.
What is forward referencing and when should it be used?
Ans: Consider the following program:
class test
{
public :
friend void fun ( sample, test ) ;
};
class sample
{
public :
friend void fun ( sample, test ) ;
};
void fun ( sample s, test t )
{
// code

Interview Questions on C++

www.mallepoolu.com
}
void main( )
{
sample s ;
test t ;
fun ( s, t ) ;
}
This program would not compile. It gives an error that sample is undeclared identifier in
the statement friend void fun ( sample, test ) ; of the class test. This is so because the
class sample is defined below the class
test and we are using it before
its definition. To overcome this error we need to give forward reference of
the class sample before the definition of class test. The following statement is the
forward reference of
class sample. Forward referencing is generally
required when we make a class or a function as a friend.
------------------------------------------------------------------------------------------------4.
The istream_withassign class has been derived from the istream class and overloaded
assignment
operator has been added to it. The _withassign classes
are much like their base classes except
that they include overloaded
assignment operators. Using these operators the objects of the
_withassign classes can be copied. The istream, ostream, and iostream classes are made
uncopyable
by making their overloaded copy constructor and
assignment operators private.
------------------------------------------------------------------------------------------------5.
How do I write my own zero-argument manipulator that should work same as hex?
Ans: This is shown in following program.
#include
ostream& myhex ( ostream &o )
{
o.setf ( ios::hex) ;
return o ;
}
void main( )
{
cout << endl << myhex << 2000 ;
}
------------------------------------------------------------------------------------------------6.
We all know that a const variable needs to be initialized at the time of declaration. Then
how come the program given below runs properly even when we have not initialized p?
#include
void main( )

Interview Questions on C++

www.mallepoolu.com
{
const char *p ;
p = "A const pointer" ;
cout << p ;
}
Ans: The output of the above program is 'A const pointer'. This is because in this
program p is declared as 'const char*' which means that value stored at p will be
constant and not p and so the program works properly
7.
How do I refer to a name of class or function that is defined within a namespace?
Ans: There are two ways in which we can refer to a name of class or function that is
defined within a
namespace: Using scope resolution operator
through the using keyword. This is shown in following example:
namespace name1
{
class sample1
{
// code
};
}
namespace name2
{
class sample2
{
// code
};
}
using namespace name2 ;
void main( )
{
name1::sample1 s1 ;
sample2 s2 ;
}
Here, class sample1 is referred using the scope resolution operator. On the other hand
we can
directly refer to class sample2 because of the statement
using namespace name2 ; the using keyword declares all the names in the namespace
to be in the current scope. So we can use the names without any qualifiers.
------------------------------------------------------------------------------------------------8.
While overloading a binary operator can we provide default values?
Ans: No!. This is because even if we provide the default arguments to the parameters of
the
overloaded operator function we would end up using the binary
operator incorrectly. This is explained
in the following example:
sample operator + ( sample a, sample b = sample (2, 3.5f ) )
{
}
void main( )
{
sample s1, s2, s3 ;

Interview Questions on C++

www.mallepoolu.com
s3 = s1 + ; // error
}
------------------------------------------------------------------------------------------------9.
How do I carry out conversion of one object of user-defined type to another?
Ans: To perform conversion from one user-defined type to another we need to provide
conversion
function. Following program demonstrates how to
provide such conversion function.
class circle
{
private :
int radius ;
public:
circle ( int r = 0 )
{
radius = r ;
}
};
class rectangle
{
private :
int length, breadth ;
public :
rectangle( int l, int b )
{
length = l ;
breadth = b ;
}
operator circle( )
{
return circle ( length ) ;
}
};
void main( )
{
rectangle r ( 20, 10 ) ;
circle c;
c=r;
}
Here, when the statement c = r ; is executed the compiler searches for an overloaded
assignment
operator in the class circle which accepts the object of
type rectangle. Since there is no such
overloaded assignment
operator, the conversion operator function that converts the rectangle
object to the circle object is searched in the rectangle class. We have provided such a
conversion function in the rectangle class. This conversion operator
function returns a circle
object. By default conversion operators
have the name and return type same as the object type to
which it
converts to. Here the type of the object is circle and hence the name of the operator
function as well as the return type is circle.
------------------------------------------------------------------------------------------------10.
How do I write code that allows to create only one instance of a class?
Ans: This is shown in following code snippet.

Interview Questions on C++

www.mallepoolu.com
#include
class sample
{
static sample *ptr ;
private:
sample( )
{
}
public:
static sample* create( )
{
if ( ptr == NULL )
ptr = new sample ;
return ptr ;
}
};
sample *sample::ptr = NULL ;
void main( )
{
sample *a = sample::create( ) ;
sample *b = sample::create( ) ;
}
Here, the class sample contains a static data member ptr, which is a pointer to the
object of
same class. The constructor is private which avoids us
from creating objects outside the class.
A static member function
called create( ) is used to create an object of the class. In this function
the condition is checked whether or not ptr is NULL, if it is then an object is created
dynamically and its address collected in ptr is returned. If ptr is not
NULL, then the same
address is returned. Thus, in main( ) on
execution of the first statement one object of sample gets
created
whereas on execution of second statement, b holds the address of the first object. Thus,
whatever number of times you call create( ) function, only one object of sample class will
be available.
11.

How do I write code to add functions, which would work as get and put properties of a
class?
Ans: This is shown in following code.
#include
class sample
{
int data ;
public:
__declspec ( property ( put = fun1, get = fun2 ) )
int x ;
void fun1 ( int i )
{
if ( i < 0 )
data = 0 ;
else
data = i ;
}
int fun2( )

Interview Questions on C++

www.mallepoolu.com
{
return data ;
}
};
void main( )
{
sample a ;
a.x = -99 ;
cout << a.x ;
}
Here, the function fun1( ) of class sample is used to set the given integer value into
data, whereas
fun2( ) returns the current value of data. To set
these functions as properties of a class we have
given the
statement as shown below:
__declspec ( property ( put = fun1, get = fun2 )) int x ;
As a result, the statement a.x = -99 ; would cause fun1( ) to get called to set the value
in data. On
the other hand, the last statement would cause fun2( )
to get called to return the value of data.
------------------------------------------------------------------------------------------------12.
How do I write code to make an object work like a 2-D array?
Ans: Take a look at the following program.
#include
class emp
{
public :
int a[3][3] ;
emp( )
{
int c = 1 ;
for ( int i = 0 ; i <= 2 ; i++ )
{
for ( int j = 0 ; j <= 2 ; j++ )
{
a[i][j] = c ;
c++ ;
}
}
}
int* operator[] ( int i )
{
return a[i] ;
}
};
void main( )
{
emp e ;
cout << e[0][1] ;
}
The class emp has an overloaded operator [ ] function. It takes one argument an integer
representing an array index and returns an int pointer. The

Interview Questions on C++

www.mallepoolu.com
statement cout << e[0][1] ; would get
converted into a call to the
overloaded [ ] function as e.operator[ ] ( 0 ). 0 would get
collected
in i. The function would return a[i] that represents the base address of the zeroeth
row. Next the statement would get expanded as base address of zeroeth
row[1] that can be further
expanded as *( base address + 1 ). This
gives us a value in zeroth row and first column.
13.
What are formatting flags in ios class?
Ans: The ios class contains formatting flags that help users to format the stream data.
Formatting
flags are a set of enum definitions. There are two types
of formatting flags:
On/Off flags
Flags that work in-group
The On/Off flags are turned on using the setf( ) function and are turned off using the
unsetf( )
function. To set the On/Off flags, the one argument setf( )
function is used. The flags
working in groups are set through the
two-argument setf( ) function. For example, to left justify a
string
we can set the flag as,
cout.setf ( ios::left ) ;
cout << "KICIT Nagpur" ;
To remove the left justification for subsequent output we can say,
cout.unsetf ( ios::left ) ;
The flags that can be set/unset include skipws, showbase, showpoint, uppercase,
showpos, unitbuf
and stdio. The flags that work in a group can have
only one of these flags set at a time.
------------------------------------------------------------------------------------------------14.
What is the purpose of ios::basefield in the following statement?
cout.setf ( ios::hex, ios::basefield ) ;
Ans: This is an example of formatting flags that work in a group. There is a flag for each
numbering system (base) like decimal, octal and hexadecimal.
Collectively, these flags are
referred to as basefield and are
specified by ios::basefield flag. We can have only one of these
flags
on at a time. If we set the hex flag as setf ( ios::hex ) then we will set the hex bit but
we
won't clear the dec bit resulting in undefined behavior. The
solution is to call setf( ) as setf
( ios::hex, ios::basefield ). This call
first clears all the bits and then sets the hex bit.
------------------------------------------------------------------------------------------------15.
Can we get the value of ios format flags?
Ans: Yes! The ios::flags( ) member function gives the value format flags. This function
takes no
arguments and returns a long ( typedefed to fmtflags) that
contains the current format flags.
------------------------------------------------------------------------------------------------16.
Is there any function that can skip certain number of characters present in the input
stream?
Ans: Yes! This can be done using cin::ignore( ) function. The prototype of this function is
as
shown below:

Interview Questions on C++

www.mallepoolu.com
istream& ignore ( int n = 1, int d =EOF ) ;
Sometimes it happens that some extra characters are left in the input stream while
taking the
input such as, the '\n' (Enter) character. This extra
character is then passed to the next input
and may pose problem.
To get rid of such extra characters the cin::ignore( ) function is used. This is equivalent
to fflush ( stdin ) used in C language. This function ignores the first n characters (if
present) in the input stream, stops if delimiter d is encountered.
------------------------------------------------------------------------------------------------17.
Write a program that implements a date class containing day, month and year as data
members.
Implement assignment operator and copy constructor in
this class.
Ans: This is shown in following program:
#include
class date
{
private :
int day ;
int month ;
int year ;
public :
date ( int d = 0, int m = 0, int y = 0 )
{
day = d ;
month = m ;
year = y ;
}
// copy constructor
date ( date &d )
{
day = d.day ;
month = d.month ;
year = d.year ;
}
// an overloaded assignment operator
date operator = ( date d )
{
day = d.day ;
month = d.month ;
year = d.year ;
return d ;
}
void display( )
{
cout << day << "/" << month << "/" << year ;
}
};
void main( )
{

Interview Questions on C++

www.mallepoolu.com
date d1 ( 25, 9, 1979 ) ;
date d2 = d1 ;
date d3 ;
d3 = d2 ;
d3.display( ) ;
}
18.
When should I use unitbuf flag?
Ans: The unit buffering (unitbuf) flag should be turned on when we want to ensure that
each
character is output as soon as it is inserted into an output
stream. The same can be done using
unbuffered output but unit
buffering provides a better performance than the unbuffered output.
------------------------------------------------------------------------------------------------19.
What are manipulators?
Ans: Manipulators are the instructions to the output stream to modify the output in
various
ways. The manipulators provide a clean and easy way for
formatted output in comparison to the
formatting flags of the ios
class. When manipulators are used, the formatting instructions
are
inserted directly into the stream. Manipulators are of two types, those that take an
argument and those that don't.
------------------------------------------------------------------------------------------------20.
What is the difference between the manipulator and setf( ) function?
Ans: The difference between the manipulator and setf( ) function are as follows:
The setf( ) function is used to set the flags of the ios but manipulators directly insert the
formatting instructions into the stream. We can create userdefined manipulators but
setf( ) function uses data members of ios
class only. The flags put on through the setf( ) function
can be put
off through unsetf( ) function. Such flexibility is not available with manipulators.
------------------------------------------------------------------------------------------------21.
How do I get the current position of the file pointer?
Ans: We can get the current position of the file pointer by using the tellp( ) member
function of ostream class or tellg( ) member function of istream class. These functions
return (in bytes)
positions of put pointer and get pointer
respectively.
------------------------------------------------------------------------------------------------22.
What are put and get pointers?
Ans: These are the long integers associated with the streams. The value present in the
put pointer
specifies the byte number in the file from where next
write would take place in the file. The get
pointer specifies the byte
number in the file from where the next reading should take place.
------------------------------------------------------------------------------------------------23.
What do the nocreate and noreplace flag ensure when they are used for opening a file?
Ans: nocreate and noreplace are file-opening modes. A bit in the ios class defines these
modes.
The flag nocreate ensures that the file must exist before
opening it. On the other hand the flag
noreplace ensures that while
opening a file for output it does not get overwritten with new one

Interview Questions on C++

www.mallepoolu.com
unless ate or app is set. When the app flag is set then whatever we write gets appended
to the
existing file. When ate flag is set we can start reading or
writing at the end of existing file.
------------------------------------------------------------------------------------------------24.
What is the limitation of cin while taking input for character array?
Ans: To understand this consider following statements,
char str[5] ;
cin >> str ;
While entering the value for str if we enter more than 5 characters then there is no
provision in
cin to check the array bounds. If the array overflows, it
may be dangerous. This can be
avoided by using get( ) function.
For example, consider following statement,
cin.get ( str, 5 ) ;
On executing this statement if we enter more than 5 characters, then get( ) takes only
first five
characters and ignores rest of the characters. Some more
variations of get( ) are available, such
as shown below:
get ( ch ) - Extracts one character only
get ( str, n ) - Extracts up to n characters into str

delimiter (such as

get ( str, DELIM ) - Extracts characters into array str until specified
'\n'). Leaves delimiting character in stream.

get ( str, n, DELIM ) - Extracts characters into array str until n


characters or DELIM character, leaving delimiting character in stream.
------------------------------------------------------------------------------------------------25.
What is the purpose of istream class?
Ans: The istream class performs activities specific to input. It is derived from the ios
class. The most commonly used member function of this class is the
overloaded >> operator which can
extract values of all basic types.
We can extract even a string using this operator.
------------------------------------------------------------------------------------------------26.
Would the following code work?
#include
void main( )
{
ostream o ;
o << "Dream. Then make it happen!" ;
}
Ans: No! This is because we cannot create an object of the ostream class since its
constructor
and copy constructor are declared private.
27.
Can we use this pointer inside static member function?
Ans: No! The this pointer cannot be used inside a static member function. This is
because a static
member function is never called through an object.
------------------------------------------------------------------------------------------------28.

Interview Questions on C++

www.mallepoolu.com
What is strstream?
Ans: strstream is a type of input/output stream that works with the memory. It allows
using
section of the memory as a stream object. These streams
provide the classes that can be used for
storing the stream of bytes
into memory. For example, we can store integers, floats and strings
as a stream of bytes. There are several classes that implement this in-memory
formatting. The class ostrstream derived from ostream is used when output is to be sent
to memory, the class istrstream derived from istream is used when input is taken from
memory and strstream class derived from iostream is used for
memory objects that do both input and output.
------------------------------------------------------------------------------------------------29.
Ans: When we want to retrieve the streams of bytes from memory we can use
istrestream. The following
example shows the use of istrstream
class.
#include
void main( )
{
int age ;
float salary ;
char name[50] ;
char str[] = "22 12004.50 K. Vishwanatth" ;
istrstream s ( str ) ;
s >> age >> salary >> name ;
cout << age << endl << salary << endl << name ;
cout << endl << s.rdbuf( ) ;
}
Here, s is the object of the class istrstream. When we are creating the object s, the
constructor
of istrstream gets called that receives a pointer to the
zero terminated character array str. The
statement s >> age >>
salary >> name ; extracts the age, salary and the name from the istrstream
object s. However, while extracting the name, only the first word of name gets
extracted. The balance
is extracted using rdbuf( ).
------------------------------------------------------------------------------------------------30.
When the constructor of a base class calls a virtual function, why doesn't the override
function of the derived class gets called?
Ans: While building an object of a derived class first the constructor of the base class
and then
the constructor of the derived class gets called. The object
is said an immature object at the stage
when the constructor of
base class is called. This object will be called a matured object after the
execution of the constructor of the derived class. Thus, if we call a virtual function
when an object
is still immature, obviously, the virtual function of
the base class would get called. This is
illustrated in the following
example.
#include
class base
{

Interview Questions on C++

www.mallepoolu.com
protected :
int i ;
public :
base ( int ii = 0 )
{
i = ii ;
show( ) ;
}
virtual void show( )
{
cout << "base's show( )" << endl ;
}
};
class derived : public base
{
private :
int j ;
public :
derived ( int ii, int jj = 0 ) : base ( ii )
{
j = jj ;
show( ) ;
}
void show( )
{
cout << "derived's show( )" << endl ;
}
};
void main( )
{
derived dobj ( 20, 5 ) ;
}
The output of this program would be:
base's show( )
derived's show( )
31.
Can I have a reference as a data member of a class? If yes, then how do I initialise it?
Ans: Yes, we can have a reference as a data member of a class. A reference as a data
member of a
class is initialised in the initialisation list of the
constructor. This is shown in following
program.
#include
class sample
{
private :
int& i ;
public :
sample ( int& ii ) : i ( ii )
{
}
void show( )

Interview Questions on C++

www.mallepoolu.com
{
cout << i << endl ;
}
};
void main( )
{
int j = 10 ;
sample s ( j ) ;
s.show( ) ;
}
Here, i refers to a variable j allocated on the stack. A point to note here is that we
cannot bind
a reference to an object passed to the constructor as a
value. If we do so, then the reference i
would refer to the function
parameter (i.e. parameter ii in the constructor), which would
disappear as soon as the function returns, thereby creating a situation of dangling
reference.
------------------------------------------------------------------------------------------------32.
Why does the following code fail?
#include
class sample
{
private :
char *str ;
public :
sample ( char *s )
{
strcpy ( str, s ) ;
}
~sample( )
{
delete str ;
}
};
void main( )
{
sample s1 ( "abc" ) ;
}
Ans: Here, through the destructor we are trying to deal locate memory, which has been
allocated
statically. To remove an exception, add following
statement to the constructor.
sample ( char *s )
{
str = new char[strlen(s) + 1] ;
strcpy ( str, s ) ;
}
Here, first we have allocated memory of required size, which then would get deal located
through the
destructor.
------------------------------------------------------------------------------------------------33.
assert( ) macro...

Interview Questions on C++

www.mallepoolu.com
We can use a macro called assert( ) to test for conditions that should not occur in a
code. This
macro expands to an if statement. If test evaluates to 0,
assert prints an error message and
calls abort to abort the program.
#include
#include
void main( )
{
int i ;
cout << "\nEnter an integer: " ;
cin >> i ;
assert ( i >= 0 ) ;
cout << i << endl ;
}
34.
Why it is unsafe to deal locate the memory using free( ) if it has been allocated using
new?
Ans: This can be explained with the following example:
#include
class sample
{
int *p ;
public :
sample( )
{
p = new int ;
}
~sample( )
{
delete p ;
}
};
void main( )
{
sample *s1 = new sample ;
free ( s1 ) ;
sample *s2 = ( sample * ) malloc ( sizeof ( sample
));
delete s2 ;
}
The new operator allocates memory and calls the constructor. In the constructor we
have allocated
memory on heap, which is pointed to by p. If we
release the object using the free( ) function the object would die but the memory
allocated in the constructor would leak. This is because free( ) being a C library function
does not call the destructor where we have deal located the memory.
As against this, if we allocate memory by calling malloc( ) the constructor would not get
called. Hence p holds a garbage address. Now if the memory is deal located using delete,
the destructor would
get called where we have tried to release the
memory pointed to by p. Since p contains garbage
this may result
in a runtime error.

Interview Questions on C++

www.mallepoolu.com
-------------------------------------------------------------------------------------------------

35.
Can we distribute function templates and class templates in object libraries?
Ans: No! We can compile a function template or a class template into object code (.obj
file). The code that contains a call to the function template or the code that creates an
object from a class template can get compiled. This is because the compiler merely
checks whether the call matches the declaration (in case of function template) and
whether the object definition matches class declaration (in case of class template). Since
the function template and the class template definitions are not found, the compiler
leaves it to the linker to restore this. However, during linking, linker doesn't find the
matching definitions for the function call or a matching definition for object creation. In
short the expanded versions of templates are not found in
the object library. Hence the linker reports error.
------------------------------------------------------------------------------------------------36.
What is the difference between an inspector and a mutator ?
Ans: An inspector is a member function that returns information about an object's state
(information stored in object's data members) without changing the object's state. A
mutator is a member function that changes the state of an object. In the class Stack
given below we have defined a mutator and an inspector.
class Stack
{
public :
int pop( ) ;
int getcount( ) ;
}
In the above example, the function pop( ) removes top element of stack thereby
changing the state of an object. So, the function pop( ) is a mutator. The function
getcount( ) is an inspector because it simply counts the number of elements in the stack
without changing the stack.
37.
Namespaces:
The C++ language provides a single global namespace. This can cause problems with
global name clashes. For instance, consider these two C++ header files:
// file1.h
float f ( float, int ) ;
class sample { ... } ;
// file2.h
class sample { ... } ;
With these definitions, it is impossible to use both header files in a single program; the
sample classes will clash.A namespace is a declarative region that attaches an additional
identifier to any names declared inside it. The additional identifier thus avoids the
possibility that a name will conflict with names declared elsewhere in the program. It is
possible to use the same name in separate namespaces without conflict even if the
names appear in the same translation unit. As long as they appear in separate

Interview Questions on C++

www.mallepoolu.com
namespaces, each name will be unique because of the addition of the namespace
identifier. For example:
// file1.h
namespace file1
{
float f ( float, int ) ;
class sample { ... } ;
}
// file2.h
namespace file2
{
class sample { ... } ;
}
Now the class names will not clash because they become file1::sample and
file2::sample, respectively.
------------------------------------------------------------------------------------------------38.
What would be the output of the following program?
#include
class user
{
int i ;
float f ;
char c ;
public :
void displaydata( )
{
cout << endl << i << endl << f << endl << c ;
}
};
void main( )
{
cout << sizeof ( user ) ;
user u1 ;
cout << endl << sizeof ( u1 ) ;
u1.displaydata( ) ;
}
Ans: The output of this program would be,
9 or 7
9 or 7
Garbage
Garbage
Garbage
Since the user class contains three elements, int, float and char its size would be 9 bytes
(int-4, float-4, char-1) under Windows and 7 bytes (int-2, float-4, char-1) under DOS.
Second output is again the same because u1 is an object of the class user. Finally three
garbage values are printed out because i, f and c are not initialized anywhere in the
program.

Interview Questions on C++

www.mallepoolu.com
Note that if you run this program you may not get the answer shown here. This is
because packing is done for an object in memory to increase the access efficiency. For
example, under DOS, the object would be aligned on a 2-byte boundary. As a result, the
size of the object would be reported as 6 bytes. Unlike this, Windows being a 32-bit OS
the object would be aligned on a 4-byte boundary. Hence the size of the object would be
reported as 12 bytes. To force the alignment on a 1-byte boundary, write the following
statement before the class declaration.
#pragma pack ( 1 )
------------------------------------------------------------------------------------------------39.
Write a program that will convert an integer pointer to an integer and vice-versa.
Ans: The following program demonstrates this.
#include
void main( )
{
int i = 65000 ;
int *iptr = reinterpret_cast ( i ) ;
cout << endl << iptr ;
iptr++ ;
cout << endl << iptr ;
i = reinterpret_cast ( iptr ) ;
cout << endl << i ;
i++ ;
cout << endl << i ;
}
40.
What is a const_cast?
Ans. The const_cast is used to convert a const to a non-const. This is shown in the
following
program:
#include
void main( )
{
const int a = 0 ;
int *ptr = ( int * ) &a ; //one way
ptr = const_cast_ ( &a ) ; //better way
}
Here, the address of the const variable a is assigned to the pointer to a non-const
variable. The const_cast is also used when we want to change the data members of a
class inside the const member functions. The following code snippet shows this:
class sample
{
private:
int data;
public:
void func( ) const
{
(const_cast (this))->data = 70 ;
}
};

Interview Questions on C++

www.mallepoolu.com
------------------------------------------------------------------------------------------------41.
What is forward referencing and when should it be used?
Ans: Forward referencing is generally required when we make a class or a function as a
friend.
Consider following program:
class test
{
public:
friend void fun ( sample, test ) ;
};
class sample
{
public:
friend void fun ( sample, test ) ;
};
void fun ( sample s, test t )
{
// code
}
void main( )
{
sample s ;
test t ;
fun ( s, t ) ;
}
On compiling this program it gives error on the following statement of test class. It gives
an error that sample is undeclared identifier. friend void fun ( sample, test ) ;
This is so because the class sample is defined below the class test and we are using it
before its definition. To overcome this error we need to give forward reference of the
class sample before the definition of class test. The following statement is the forward
reference of class sample.
class sample ;
------------------------------------------------------------------------------------------------42.
How would you give an alternate name to a namespace?
Ans: An alternate name given to namespace is called a namespace-alias. namespacealias is generally used to save the typing effort when the names of namespaces are very
long or complex. The following syntax is used to give an alias to a namespace.
namespace myname = my_old_very_long_name ;
------------------------------------------------------------------------------------------------43.
Using a smart pointer can we iterate through a container?
Ans: Yes. A container is a collection of elements or objects. It helps to properly organize
and store the data. Stacks, linked lists, arrays are examples of containers. Following
program shows how to iterate through a container using a smart pointer.
#include
class smartpointer
{
private :

Interview Questions on C++

www.mallepoolu.com
int *p ; // ordinary pointer
public :
smartpointer ( int n )
{
p = new int [ n ] ;
int *t = p ;
for ( int i = 0 ; i <= 9 ; i++ )
*t++ = i * i ;
}
int* operator ++ ( int )
{
return p++ ;
}
int operator * ( )
{
return *p ;
}
};
void main( )
{
smartpointer sp ( 10 ) ;
for ( int i = 0 ; i <= 9 ; i++ )
cout << *sp++ << endl ;
}
Here, sp is a smart pointer. When we say *sp, the operator * ( ) function gets called. It
returns the integer being pointed to by p. When we say sp++ the operator ++ ( )
function gets called. It increments p to point to
the next element in the array and then returns the address of this new location.
44.
Can objects read and write themselves?
Ans: Yes! This can be explained with the help of following example:
#include
#include
class employee
{
private :
char name [ 20 ] ;
int age ;
float salary ;
public :
void getdata( )
{
cout << "Enter name, age and salary of employee : " ;
cin >> name >> age >> salary ;
}
void store( )
{
ofstream file ;
file.open ( "EMPLOYEE.DAT", ios::app | ios::binary ) ;

Interview Questions on C++

www.mallepoolu.com
file.write ( ( char * ) this, sizeof ( *this ) ) ;
file.close( ) ;
}
void retrieve ( int n )
{
ifstream file ;
file.open ( "EMPLOYEE.DAT", ios::binary ) ;
file.seekg ( n * sizeof ( employee ) ) ;
file.read ( ( char * ) this, sizeof ( *this ) ) ;
file.close( ) ;
}
void show( )
{
cout << "Name : " << name
<< endl << "Age : " << age
<< endl << "Salary :" << salary << endl ;
}
};
void main( )
{
employee e [ 5 ] ;
for ( int i = 0 ; i <= 4 ; i++ )
{
e [ i ].getdata( ) ;
e [ i ].store( ) ;
}
for ( i = 0 ; i <= 4 ; i++ )
{
e [ i ].retrieve ( i ) ;
e [ i ].show( ) ;
}
}
Here, employee is the class whose objects can write and read themselves. The getdata(
) function has been used to get the data of employee and store it in the data members
name, age and salary. The store( ) function is used to write an object to the file. In this
function a file has been opened in append mode and each time data of current object has
been stored after the last record (if any) in the file.Function retrieve( ) is used to get the
data of a particular employee from the file. This retrieved data has been stored in the
data members name, age and salary. Here this has been used to store data since it
contains the address of the current object. The function show( ) has been used to display
the data of employee.
------------------------------------------------------------------------------------------------45.
Why is it necessary to use a reference in the argument to the copy constructor?
Ans : If we pass the copy constructor the argument by value, its copy would get
constructed using the copy constructor. This means the copy constructor would call itself
to make this copy. This process would go on and on until the compiler runs out of
memory. This can be explained with the help of following example:
class sample
{
int i ;

Interview Questions on C++

www.mallepoolu.com
public :
sample ( sample p )
{
i = p.i ;
}
};
void main( )
{
sample s ;
sample s1 ( s ) ;
}
While executing the statement sample s1 ( s ), the copy constructor would get called. As
the copy construct here accepts a value, the value of s would be passed which would get
collected in p. We can think of this statement as sample p = s. Here p is getting created
and initialized. Means again the copy constructor would get called. This would result into
recursive calls. Hence we must use a reference as an argument in a copy constructor.
46.
Virtual Multiple Inheritance:
A class b is defined having member variable i. Suppose two classes d1 and d2 are
derived from class b and a class multiple is derived from both d1 and d2. If variable i is
accessed from a member function of multiple then it gives error as 'member is
ambiguous'. To avoid this error derive classes d1 and d2 with modifier virtual as shown
in the following program.
#include
class b
{
public :
int i ;
public :
fun( )
{
i=0;
}
};
class d1 : virtual public b
{
public :
fun( )
{
i=1;
}
};
class d2 : virtual public b
{
public :
fun( )
{
i=2;
}
};
class multiple : public d1, public d2
{
public :
fun( )
{
i = 10 ;

Interview Questions on C++

www.mallepoolu.com
}
};
void main( )
{
multiple d ;
d.fun( ) ;
cout << d.i ;
}
------------------------------------------------------------------------------------------------47.
Can we use this pointer in a class specific, operator-overloading function for new
operator?
Ans: No! The this pointer is never passed to the overloaded operator new() member
function because this function gets called before the object is created. Hence there is no
question of the this pointer getting passed to operator new( ).
------------------------------------------------------------------------------------------------48.
Can we allocate memory dynamically for a reference?
Ans: No! It is not possible to allocate memory dynamically for a reference. This is
because, when we create a reference, it gets tied with some variable of its type. Now, if
we try to allocate memory dynamically for a reference, it is not possible to mention that
to which variable the reference would get tied.
49.
When should I overload new operator on a global basis or a class basis?
Ans: We overload operator new in our program, when we want to initialize a data item or
a class object at the same place where it has been allocated memory. The following
example shows how to overload new operator on global basis.
#include
#include
void * operator new ( size_t s )
{
void *q = malloc ( s ) ;
return q ;
}
void main( )
{
int *p = new int ;
*p = 25 ;
cout << *p ;
}
When the operator new is overloaded on global basis it becomes impossible to initialize
the data members of a class as different classes may have different types of data
members. The following example shows how to
overload new operator on class-by-class basis.
#include
#include
class sample
{
int i ;

Interview Questions on C++

www.mallepoolu.com
public :
void* operator new ( size_t s, int ii )
{
sample *q = ( sample * ) malloc ( s ) ;
q -> i = ii ;
return q ;
}
};
class sample1
{
float f ;
public :
void* operator new ( size_t s, float ff )
{
sample1 *q = ( sample1 * ) malloc ( s ) ;
q -> f = ff ;
return q ;
}
};
void main( )
{
sample *s = new ( 7 ) sample ;
sample1 *s1 = new ( 5.6f ) sample1 ;
}
Overloading the operator new on class-by-class basis makes it possible to allocate
memory for an object and initialize its data members at the same place.
------------------------------------------------------------------------------------------------50.
How would you define a pointer to a data member of the type pointer to pointer?
Ans: The following program demonstrates this...
#include
class sample
{
public :
sample ( int **pp )
{
p = pp ;
}
int **p ;
};
int **sample::*ptr = &sample::p ;
void main( )
{
int i = 9 ;
int *pi = &i ;
sample s ( ) ;
cout << ** ( s.*ptr ) ;
}
Here, ptr is the pointer to data member p of class sample, which in turn is a pointer
pointing to an int.
-------------------------------------------------------------------------------------------------

Interview Questions on C++

www.mallepoolu.com
51.
How do I write a code to catch multiple types of exceptions in one single catch block?
Ans: The following program demonstrates the use of a single catch block to catch
multiple exceptions.
#include
class test
{
};
class sample
{
public :
void fun1( )
{
throw 99 ;
}
void fun2( )
{
throw 3.14f ;
}
void fun3( )
{
throw "error" ;
}
void fun4( )
{
throw test( ) ;
}
};
void main( )
{
try
{
sample s ;
s.fun4( ) ;
s.fun1( ) ;
s.fun2( ) ;
s.fun3( ) ;
}
catch ( ... )
{
cout << "strange" ;
}
}
Here, different types of exceptions are thrown by the member functions of the class
sample. While catching the exception instead of four different catch blocks we can as
well define one single catch block. Note the syntax for defining the catch block, where
we have used three dots () in the formal parameter list. This indicates that any thrown
exception should get caught in the same catch block. When the exception is thrown from
the fun4( ) control reaches the catch block, ignoring the rest of the calls.
52.
Can we return an error value from the constructor of a class?

Interview Questions on C++

www.mallepoolu.com
Ans: No. We cannot return any error value from the constructor, as the constructor
doesn't have any return type. However, by throwing an exception we can pass value to
catch block. This is shown in the following example:
#include
class sample
{
public :
sample ( int i )
{
if ( i == 0 )
throw "error" ;
}
};
void main( )
{
try
{
sample s ( 0 ) ;
}
catch ( char * str )
{
cout << str ;
}
}
In this program, the statement throw "error" ; would throw an exception when an object
s of the class sample would get created. The catch block would collect the string error.
------------------------------------------------------------------------------------------------53.
How do I define the member function of a template class, which has to be defined
outside the template class. The function receives an object of its own class as a
parameter and returns the value of the same type.
Ans: The following example shows how we can define such a function.
sample sample::fun ( sample s )
{
// code
}
Here, the first sample indicates the return type of the function and the next sample is
used for the scope of function.
------------------------------------------------------------------------------------------------54.
How name mangling can be prevented?
Ans: To avoid name mangling the function should be declared with an extern "C"
attribute. Functions declared as extern "C" are treated as C-style functions. Hence the
compiler does not mangle them. The following code snippet shows how to declare such a
function.
#include
extern "C" void display( )
{
cout << "See the effect of C in C++ " ;

Interview Questions on C++

www.mallepoolu.com
}
void main( )
{
display( ) ;
}
------------------------------------------------------------------------------------------------55.
Can we allocate memory dynamically for a reference?
Ans: No, it is not possible to allocate memory dynamically for a reference. A reference is
initialized at the time of creation. Trying to allocate memory dynamically for a reference
creates a problem in initializing it. Thus, the compiler does not allow us to dynamically
allocate the memory for references.
------------------------------------------------------------------------------------------------56.
What is RTTI?
Ans: RTTI stands for 'Run Time Type Information'. We use virtual function mechanism
where we can call derived class's member functions using base class's pointer. However,
many times we wish to know the exact type of the object. We can know the type of the
object using RTTI. A function that returns the type of the object is known as RTTI
functions. C++ supports two ways to obtain information about the object's class at run
time, they are typeid( ) operator and dynamic_cast operator.
57.
What is Data Conversion?
Ans: Assignments between types whether they are basic or user-defined, are handled by
the compiler. If the variables are of different basic types compiler calls a special routine
to convert the value. But if we want to convert between user-defined data type and
basic types we have to write conversion routine ourselves. A conversion routine to
convert user-defined data type string to integer is shown below:
class string
{
private :
char str[20] ;
public :
string( )
{
}
string ( char *s )
{
strcpy ( str, s ) ;
}
operator int( )
{
return 123 ; // Write logic to convert string to integer
}
};
main( )
{
string s2 = "123" ;
int i1 = int ( s2 ) ;
cout << endl << i1 ;
}
-------------------------------------------------------------------------------------------------

Interview Questions on C++

www.mallepoolu.com

58.
How to obtain type information using typeid( ) operator?
Ans: typeid( ) operator takes an object, a reference or a pointer and returns its type.
Following program shows how to use the typeid( ) operator.
#include
#include
class Base
{
public :
virtual void show( )
{
}
};
class Der1 : public Base
{
};
void main( )
{
Base *b1 ;
cout << endl << typeid ( b1 ).name( ) ;
Der1 d1 ;
b1 = &d1 ;
cout << endl << typeid ( *b1 ).name( ) ;
cout << endl << typeid ( 12 ).name( ) << endl << typeid ( 12.5 ).name( ) ;
}
The output of this program will be
Base*
Der1
int
double
RTTI operators must be used for polymorphic class (class having virtual function) only.
For non-polymorphic class static type information is returned.
------------------------------------------------------------------------------------------------59.
How to use RTTI with class templates?
Ans: Templates can generate different classes. We may wish to get the type of class,
which we are working in. The following program shows how to use RTTI operator typeid(
) with class template.
#include
#include
template

Interview Questions on C++

www.mallepoolu.com
class base
{
public :
base( )
{
cout << typeid ( *this ).name( ) << "Constructor" << endl ;
}
T add ( T a, T b )
{
return a + b ;
}
~base( )
{
cout << typeid ( *this ).name( ) << "Destructor" << endl ;
}
};
void main( )
{
base b1 ;
cout << b1.add ( 10, 20 ) << endl ;
base b2 ;
cout << b2.add ( 5.5, 10.5 ) << endl ;
}
60.
We can use following C++ operators for typecasting.static_cast is used for castless
conversions, narrowing conversions, conversion from void* and implicit type
conversions. const_cast is used to convert a const to a non-const. reinterpret_cast is
used to assign one kind of pointer to another.
------------------------------------------------------------------------------------------------61.
What will be the output of the following program?
#include
class A
{
public :
A( )
{
cout << "Reached in Constructor\n" ;
}
};
void main( )
{
A a( ) ;
Ab;
}
Output : Reached in Constructor
Constructor gets called only once when the object b is created. When the statement A a(
) ; gets executed constructor does not get called. This is because compiler takes this
statement as a prototype declaration of function a( ) that returns an object of class A.

Interview Questions on C++

www.mallepoolu.com
However, if we pass arguments like
A a ( 10 ) ;
Compiler would search for one argument constructor and if not found would flash an
error.
------------------------------------------------------------------------------------------------62.
What is a container?
Ans: A container is an object that holds other objects. Various collection classes like List,
Hash Table, AbstractArray, etc. are the examples of containers. We can use the classes
to hold objects of any derived classes. The containers provide various methods using
which we can get the number of objects stored in the container and iterate through the
objects stored in it.
------------------------------------------------------------------------------------------------63.
Function template overloading
One can declare several function templates with the same name and even declare a
combination of function templates and ordinary functions with the same name. When an
overloaded function is called, overload resolution is necessary to find the right function
or template function to invoke.
For example:
template < class T > T sqrt ( T ) ;
template < class T > complex < T > sqrt ( complex < T > ) ;double sqrt (
double ) ;
void f ( complex < double > z )
{
sqrt ( 2 ) ;
// sqrt < int > ( int )
sqrt ( 2.0 ) ; // sqrt ( double )
sqrt ( z ) ;
// sqrt < complex < double > ( complex < double > )
}
In the same way that a template function is a generalization of the notion of a function,
the rules for resolution in the presence of function templates are generalizations of the
function overload resolution rules. Basically, for each template we find the specialization
that is best for the set of function arguments. Then we apply the usual function overload
resolution rules to these specializations and all ordinary functions.
64.
Exception Handling in C++
In C++ we can handle run-time errors generated by c++ classes by using three new
keywords: throw, catch, and try. We also have to create an exception class. If during the
course of execution of a member function of
this class a run-time error occurs, then this member function informs the application that
an error has occurred. This process of informing is called 'throwing' an exception. The
following code shows how to deal with exception handling.
class sample
{
public :
class errorclass
{
};
void fun( )

Interview Questions on C++

www.mallepoolu.com
{
if ( some error occurs )
throw errorclass( ) // throws exception
}
};
//application
void main( )
{
try
{
sample s ;
s.fun( ) ;
}
catch ( sample::errorclass )
{
// do something about the error
}
}
------------------------------------------------------------------------------------------------65.
Consider the following code:
#include
class base
{
public :
int data ;
};
class d1 : public base
{
};
class d2 : public base
{
};
class der : public d1, public d2
{
public :
void showdata( )
{
cout << data ;
}
};
void main( )
{
der d ;
d.showdata( ) ;
}
If you run this program it is bound to give you errors. This is because of the rules of
inheritance:
1. Each base class not specified virtual will have its own sub-object representing it. In
the above program, if we create object of d1 it will have a sub-object of class base

Interview Questions on C++

www.mallepoolu.com
containing a data member data. If we create an object of class der it will have subobjects of classes d1 and d2 and both the sub-objects will refer to a separate copy of
data. Hence, to access data from class der we will have to mention the class name. For
example, d1::data or d2::data.
2. If we want that only one sub-object should exist we must use the concept of virtual
base class. The single object of this will represent every base class of given name that is
specified to be virtual
class. After making d1 and d2 as virtual base class if we create an object of der only one
sub-object would exist and so accessing data would no longer give us errors.
------------------------------------------------------------------------------------------------66.
How to declare a pointer to a member function?
Ans: Suppose, I wish to declare a pointer to a member function that receives an int and
returns an int. I will have to declare it as int (A::* ) ( int ). Following is an example.
#include
class A
{
public :
int fun ( int f )
{
cout << "in fun\n" ;
return f * f ;
}
};
typedef int ( A:: *pfun ) ( int ) ;
void main( )
{
pfun p = A::fun ;
Aa;
int s = ( a.*p ) ( 6 ) ;
cout << s ;
}
67.
What is the disadvantage of a template function?
Ans: A template function cannot be distributed in the obj form. This is because, with
which parameters the template function is going to be called is decided at the run time
only. Therefore an obj form of a template function cannot be made by merely compiling
it.
------------------------------------------------------------------------------------------------68.
How to declare a pointer to the data members of a class?
Ans: Following program shows how to declare a pointer to non-function members of a
class.
#include

Interview Questions on C++

www.mallepoolu.com
class A
{
public :
int a ;
void print( )
{
cout << a ;
}
};
void main( )
{
int A::*pa = &A::a ;
A obj ;
obj.*pa = 20 ;
obj.print( ) ;
}
Here, we have initialised the data member a using the pointer pa.
-------------------------------------------------------------------------------------------------

69.
How to allocate memory for a multidimensional array dynamically?
Ans: Many times we need to allocate memory for a multidimensional array dynamically.
Because of complexity of pointers many find this difficult. Following program allocates
memory for a 3 x 3 array dynamically, copies contents of a 3 x 3 array in it and prints
the contents using the pointer.
#include
#include
int a[ ][3] = {
1, 2, 3,
4, 5, 6,
7, 8, 9
};
void main( )
{
int **p ;
p = new int *[3] ;
for ( int i = 0 ; i < 3 ; i++ )
p[i] = new int[3] ;
for ( i = 0 ; i < 3 ; i++ )
for ( int j = 0 ; j < 3 ; j++ )
p[i][j] = a[i][j] ;
for ( i = 0 ; i < 3 ; i++ )
{
for ( j = 0 ; j < 3 ; j++ )
cout << p[i][j] ;

Interview Questions on C++

www.mallepoolu.com
cout << "\n" ;
}
}
70.
When should we use the :: ( scope resolution ) operator to invoke the virtual functions?
Ans: Generally, :: operator is used to call a virtual function from constructor or
destructor. This is because, if we call a virtual function from base class constructor or
destructor the virtual function of the base class would get called even if the object being
constructed or destroyed would be the object of the derived class. Thus, whenever we
want to bypass the dynamic binding mechanism we must use the :: operator to call a
virtual function.
------------------------------------------------------------------------------------------------71.
How do I use operators .* and ->* in a program?
Ans: The following code snippet demonstrates the use of .* and ->* operators.
#include
class sample
{
public :
int i ;
void fun( )
{
cout << "fun" << endl ;
}
};
void ( sample::*pf )( ) = &sample::fun ;
int sample::*pdm = &sample::i ;
void main( )
{
sample s ;
sample *p = new sample ;

( s .* pf )( ) ;
( p ->* pf )( ) ;

s .* pdm = 1 ;
p ->* pdm = 2 ;
cout << s .* pdm << endl ;
cout << p ->* pdm << endl ;
}
In the above program pf is a pointer to a function fun( ) of class sample, and pdm is a
pointer to a data member i of the same class sample. The object s of the class sample is
created statically. Next, p is a pointer to an object created dynamically. The using the
operator .* and ->* the member functions are called and also the public data member is
accessed.
------------------------------------------------------------------------------------------------72.

Interview Questions on C++

www.mallepoolu.com
What happens when we add an int value to a user defined type of object?
Ans: Whenever an int value is added to an object of user defined type, the object would
search for an overloaded operator int( ). This operator must be defined in such a way
that it always returns an int value. However, we need not specify the return type as on
doing so the compiler flashes an error.
#include
class sample
{
int i ;
public :
sample ( )
{
i = 10 ;
}
operator int( )
{
return this -> i ;
}
};
void main( )
{
sample s ;
int i ;
i = s + 10 ;
cout << i ;
}
In the above program on adding 10 to an object s, the value of i would become 20.
73.
Can we have a reference to an array?
Ans: Yes, we can have a reference to an array.
int a[ ] = { 8, 2, 12, 9 } ;
int ( &r ) [ 4 ] = a ; // reference to an array
Here, r is a reference to an array of four elements. We can even print the elements of
array with the help of reference. This is shown in the following code segment:
for ( int i = 0 ; i < 4 ; i++ )
cout << r [i] << endl ;
------------------------------------------------------------------------------------------------74.
When friend function becomes indispensable...
Ans: Consider the following program.
#include
class distance
{
private :
int feet ;
public :
distance( )

Interview Questions on C++

www.mallepoolu.com
{
feet = 0 ;
}
distance ( int f )
{
feet = f ;
}
distance operator + ( distance x )
{
int f = feet + x.feet ;
return distance ( f ) ;
}
};
void main( )
{
distance d1 ( 20 ), d2, d3 ;
d2 = d1 + 10 ;
d3 = 10 + d2 ;
}
If you run this program it is bound to give errors. The error lies in the statement d3 = 10
+ d2 ; We may think that since we have overloaded + operator this statement would
add 10 to d2. But this does not happen. This is because the specified statement will get
converted as d3 = 10.operator+ ( d2 ) ; This means that this statement should call the
operator+( ) function that takes an object of distance class as parameter written in
the float class, which is not possible. The solution is to write operator+( ) as a 'friend'
function. Declare operator+ function in distance class as given below:
friend distance operator + ( distance x1, distance x2 ) ;
and define it outside the class as shown below:
distance operator + ( distance x1, distance x2 )
{
int f = x1.feet + x2.feet ;
return distance ( f ) ;
}
When compiler would see that the 'friend' operator+( ) function is available it would
convert the statement d3 = 10 + d2 as operator+ (10, d2 ). Now since 10 is passed as a
parameter not as a calling object there would be no error. Thus in such cases 'friend'
function becomes indispensable.
------------------------------------------------------------------------------------------------75.
How to use a memory as a stream?
Ans: Suppose, details of an employee such as name, designation, age, etc. are stored in
different types of variables. Now, if we wish to concatenate these details in a character
array we will have to use various string manipulation functions like strcpy( ) and strcat(
). Instead of using these functions we can use more easy and clean way to gather the
details in the char array in the form of streams. We can declare the memory allocated
for the array as stream and use the << operator to store variables having different types
in this memory. Following program shows how to achieve this.
#include
void main( )
{
char buff [50] ;

Interview Questions on C++

www.mallepoolu.com
char str[ ] = "Sanjay" ;
char desig[ ] = "Manager" ;
char jd[ ] = "27/12/1995" ;
int age = 35 ;
ostrstream o ( buff, sizeof ( buff ) ) ;
o << str << endl << desig << endl << jd << endl << age << ends ;
cout << buff ;
}
As shown in the program we can also use the manipulators and formatting flags. The
output of this program will be:
Sanjay
Manager
27/12/1995
35
------------------------------------------------------------------------------------------------76.
How would you declare and initialize reference to a data member?
Ans: Sometimes we may need to declare a data member, which is a reference to another
data member of the class as shown below:
class A
{
public :
char *p ;
char *&rp ;
};
We can't initialize a reference to a data member at the time of declaration. It should be
initialized using 'member wise initialization as shown below.
#include
class A
{
public :
char *p ;
char *&rp ;
A( ) : rp ( p )
{
p = "" ;
}
A ( char *s ) : rp ( p )
{
p=s;
}
};
void main( )
{
A a ( "abcd" ) ;
cout << a.rp ;
}
77.
iostream library has made it easy to read data from various input devices and write data
to the output devices. The following program shows how to print a disk file 'data.dat' on

Interview Questions on C++

www.mallepoolu.com
the printer using stream classes. Every hardware device has a familiar name given by
the operating system. The printer is generally connected to the first parallel port. So, the
file name for the printer should be PRN or lpt1.
#include
void main( )
{
ifstream i ( "data.dat" ) ;
ofstream o ;
o.open ( "PRN" ) ;
char ch ;
while ( 1 )
{
i.get ( ch ) ;
if ( i.eof( ) )
break ;
o.put ( ch ) ;
}
o.put ( '\x0C' ) ;
}
------------------------------------------------------------------------------------------------78.
We know that a destructor is automatically called when an object of a class goes out of
scope. There is another case where destructor is called automatically. If an object is
created in a try block and an exception is thrown after the object is created, then the
destructor is called automatically.
------------------------------------------------------------------------------------------------79.
Can a function call be at the left hand side of the assignment operator?
Ans: Yes. Following program shows how it is possible.
#include
class ref
{
private :
struct data
{
int a ; char *p ;
} d1, d2 ;
public :
data &set ( )
{
return d1 ;
}
data &get ( )
{
cin >> d2.a >> d2.p ;
return d2 ;
}
};
void main( )
{
ref r ;
r.set( ) = r.get( ) ;
r.print( ) ;
}
In the above program the functions get( ) and set( ) both return a reference to the

Interview Questions on C++

www.mallepoolu.com
object of the structure data. We have assigned the reference returned by get( ) to the
reference returned by set( ) function. That is, we are assigning d2 to d1. So, the values
of d2 would get assigned to d1. You can check this out by printing the values of d1.
------------------------------------------------------------------------------------------------80.
If a class contains a virtual function a pointer called VPTR is created. This VPTR becomes
a part of every object of that class. The first two bytes (in DOS) are occupied by VPTR.
We can prove this by displaying the first two bytes of memory allocated for the objects.
Following program shows how this can be achieved.
#include
class vir
{
public :
virtual void f( )
{
}
};
void main( )
{
vir v, v1 ;
int *p1 = ( int* ) &v ;
int *p2 = ( int* ) &v1 ;
cout << endl << *p1 << " " << *p2 ;
}
81.
Exception Handling in C++
In C++ we can handle run-time errors generated by c++ classes by using three new
keywords: throw, catch, and try. We also have to create an exception class. If during the
course of execution of a member function of
this class a run-time error occurs, then this member function informs the application that
an error has occurred. This process of informing is called 'throwing' an exception. The
following code shows how to deal with exception handling.
class sample
{
public :
class errorclass
{
};
void fun( )
{
if ( some error occurs )
throw errorclass( ) // throws exception
}
};
//application
void main( )
{
try

Interview Questions on C++

www.mallepoolu.com
{
sample s ;
s.fun( ) ;
}
catch ( sample::errorclass )
{
// do something about the error
}
}
------------------------------------------------------------------------------------------------82.
Accessing a private data member from a different Object...
Different objects of the same class can access each other's members, even if these
members are private. For example:
#include < iostream.h >
class sample
{
float f ;
public :
sample ( float ff )
{
f = ff ;
}
void fun ( sample* objptr )
{
objptr -> n = 0 ;
cout << "Value of this objects f is : " << f << endl ;
cout << "Value of other objects f" << objptr -> n << endl ;
} // another object's private member!
};
void main( )
{
sample s1 ( 6.5f ) , s2 ( 2.5f ) ;
s1.f ( &s2 ) ; // s1 changes s2's n
}
Typically, this coding style should be avoided. However, you should be aware that
private members of an object can be changed by another object of the same type.
Therefore, in certain special conditions, this coding style may be useful.
------------------------------------------------------------------------------------------------83.
Can you access private data members of a class from out side the class?
Ans: Yes. This program shows how.
#include
class emp
private :
int i ;
public :
emp( )
{
i = 10 ;
}
};
void main( )
emp *p = new emp ;
int *pi = (int*) p ;

Interview Questions on C++

www.mallepoolu.com
cout << *pi ;
*pi = 20 ;
cout << *pi ;
}
The pointer to the class is typecasted in an integer pointer. With the help of this pointer
private data member 'i' is accessed in main( ).
------------------------------------------------------------------------------------------------84.
Why creating array of references is not possible?
Ans: The array name always refers or points to the zeroeth element. If array is of
references then the array name would point to the zeroeth element which happens to be
a reference. Creating pointer to a reference is not valid. So, creating array of references
too is not possible.
85.
How do I call a virtual function of a class using a pointer to a function ?
Ans :
#include
class Cvirtual
{
public :
virtual float vfun( )
{
cout << "from vfun" << endl ;
return 7.03f ;
}
};
void main( )
{
Cvirtual obj ;
int **p = ( int ** ) &obj ;
float ( *pf1 ) ( ) ;
pf1 = ( float ( * ) ( ) ) **p ;
float f = ( *pf1 ) ( ) ;
cout << "return val = " << f << endl ;
}
In the above program class Cvirtual consists of a virtual function vfun(). In variable p we
have stored the address of an object of class Cvirtual. While doing so, we have type
casted the address of obj to int **, because obj holds a hidden data member called vptr,
which in turn holds the address of virtual function vfun( ). In pf1, a pointer to a function,
we are collecting the address of the virtual function vfun( ). Thus the value returned by
vfun( ) would then get collected in f.
-------------------------------------------------------------------------------------------------

86.
Why an overloaded new operator defined in a class is static?
Ans: An overloaded new function is by default static even if it is not declared so. This is
because non-static member functions can be called through an object only. But when
an overloaded new operator function gets called the object doesn't stand created. Since
new operator function itself is responsible for creating the object. Hence to be able to
call a function without an object, the function must be static.
-------------------------------------------------------------------------------------------------

Interview Questions on C++

www.mallepoolu.com
87.
What is a pure virtual destructor?
Ans: Like a pure virtual function we can also have a pure virtual destructor. If a base
class contains a pure virtual destructor it becomes necessary for the derived classes to
implement the destructor. An ordinary pure virtual function does not have a body but
pure virtual destructor must have a body. This is because all the destructors in the
hierarchy of inheritance are always called as a part of destruction.
------------------------------------------------------------------------------------------------88.
When we are required to find offset of an element within a structure? or, how do we call
the function of an outer class from a function in the inner class? (The inner class is
nested in the outer class)
Ans:
#include
class outer
{
int i ;
float f ;
public :
class inner
{
public :
infunc( )
{
outer *pout ;
pout = (outer*) this - ( size_t ) &( ( ( outer* ) 0 ) -> in ) ;
pout -> outfunc( ) ;
}
};
inner in ;
outfunc( )
{
cout << "in outer class's function" ;
}
};
void main( )
{
outer out ;
out.in.infunc( )
}
In the above example we are calling outer::outfunc( ) from inner::infunc(). To call
outfunc( ) we need a pointer to the outer class. To get the pointer we have subtracted
offset of the inner class's object (base
address of outer class's object - address of
inner class's object) from address of inner class's object.
89.
void f ( float n, int i = 10 ) ;
void f ( float a ) ;
void main( )
{
f ( 12.6 ) ;
}
void f ( float n, int i )
{

Interview Questions on C++

www.mallepoolu.com
}
void f ( float n )
{
}
The above program results in an error (ambiguous call) since without the default
argument the two functions have arguments that are matching in number, order and
type.
------------------------------------------------------------------------------------------------90.
Some programs need to exercise precise control over the memory areas where data is
placed. For example, suppose we wish to read the contents of the boot sector into a
structure. For this the byte arrangement of the
structure elements must match the arrangement of various fields in the boot sector of
the disk.
The #pragma pack directives offer a way to fulfill this requirement. The #pragma pack
directive specifies packing alignment for structure and union members. The #pragma
takes effect at the first structure or union
declaration after the #pragma is seen.
Consider the following structure:
#pragma pack (1)
struct emp
{
int a ;
float s ;
char ch ;
};
#pragma pack( )
Here, #pragma pack ( 1 ) lets each structure element to begin on a 1-byte boundary.
Hence the size of the structure will be 9. (int - 4, float - 4, char - 1). If we use #pragma
pack ( 2 ) each structure element can begin
on a 2-byte boundary. Hence the size of
the structure will be 10. (int - 4, float - 4, char - 2).
------------------------------------------------------------------------------------------------91.
How to restrict a friend class's access to the private data members?
Ans: If we declare a class as a friend of our class the friend class can access the private
data members of our class. However, if we want we can restrict this access to some
selective functions of the class. Following program shows how to achieve this:
#include
class X
{
public :
void print ( class Z &z ) ;
};
class Z
{
private :
int i ;
public :
Z ( int ii )
{
i = ii ;

Interview Questions on C++

www.mallepoolu.com
}
friend X::print ( class Z &z ) ;
};
void X::print ( Z &z1 )
{
cout << z1.i ;
}
main( )
{
Z z ( 10 ) ;
Xx;
x.print ( 10 ) ;
}
In the above program only the X::print( ) function can access the private data members
of class Z.
------------------------------------------------------------------------------------------------92.
What is name mangling?
Ans: C++ enables you to assign the same function name to more than one functions but
with different parameter types. This feature is called function overloading. But when we
give several functions the same name, how does the compiler decide which particular
function is to be called? C++ solves this problem by applying a process called name
mangling. Name mangling applies a decorated name to the function. The mangled name
includes tokens that identify the functions' return type and the types of its arguments.
class test
{
public :
void fun ( int a, char b ) ;
void fun ( char *c, float y ) ;
};
void main( )
{
test s1 ;
s1.fun ( 65, 'A' ) ;
s1.fun ( "Anil", 5.5f ) ;
}
At the time of resolving the calls to fun( ) function the linker would not be able to find
the definition of the overloaded function fun( ) and it would report an error. If you look
at these errors you will see the mangled names like, (?fun@test@@QAEXJJ@Z) and
(?fun@test@@QAEXMM@Z). Note that different compilers may use different name
mangling schemes.
93.
How would you call a C function from C++ code?
Ans: Using extern "C".
The function prototype must be preceded by extern "C". More than one C
be grouped inside braces as shown below:
extern "C"
{
void f( ) ;
void f1( ) ;
}

Interview Questions on C++

functions can

www.mallepoolu.com
// In cfunc.c
#include
void f( )
{
printf ( "in f( )" ) ;
}
// In func.cpp
#include
extern "C" void f( ) ;
void main( )
{
f( ) ;
}
Ensure that both .c and .cpp files are in the same project.
------------------------------------------------------------------------------------------------94.
How to restrict the number of floating-point digits displayed ?
Ans: When we display floating-point values, we can use the setprecision manipulator to
specify the desired number of digits to the right of the decimal point.
For example,
cout << setprecision ( 3 ) << 12.34678 ;
This statement would give the output as 12.347.
------------------------------------------------------------------------------------------------95.
What is a wild pointer ?
Ans: A wild pointer is the one that points to a garbage value. For example, an
uninitialized pointer that contains garbage value or a pointer that refers to something
that no longer exists.
-------------------------------------------------------------------------------------------------

96.
How friend function helps to increase the versatility of overloaded operators?
Ans: Consider the following statement,
s2 = s1 * 2 ;
where, s1 and s2 are objects of sample class. This statement would work if the
overloaded operator * ( sample s ) or conversion function is provided in the class.
Internally this statement would get converted to,
s2 = s1.operator * ( 2 ) ;
The function materializes because it is called with an object s1. The this pointer of s1
would get passed implicitly. To collect 2 in s, first the compiler would call the oneargument constructor, then it would build a
nameless object, which then would get collected in s. However, if we write the above
statement as,
s2 = 2 * s1 ;
then it won't compile. This is because the call now would get treated as,
s2 = 2.operator * ( s1 ) ;
and 2 is not an object. The friend function helps to get rid of such a situation. This is
shown in the following program.

Interview Questions on C++

www.mallepoolu.com
#include
class sample
{
private :
int i ;
public :
sample ( int ii = 0 )
{
i = ii ;
}
void showdata( )
{
cout << i << endl ;
}
friend sample operator * ( sample, sample ) ;
};
sample operator * ( sample s1, sample s2 )
{
sample temp ;
temp.i = s1.i * s2.i ;
return ( temp ) ;
}
void main( )
{
sample s1 ( 10 ), s2 ;
s2 = s1 * 2 ;
s2.showdata( ) ;
s1 = 2 * s2 ;
s1.showdata( ) ;
}
Here the operator *( ) function takes two parameters. This is because the operator
function is no longer a member function of the class. It is a friend of the class sample.
Thus the statement s2 = s1 * 2 ; would not take the form s2.operator * ( 2 ). This
example shows that using friend permits the overloaded operators to be more versatile
97.
What is a const_cast?
Ans: The const_cast is used to convert a const to a non-const. This is shown in the
following program.
#include
void main( )
{
const int a = 0 ;
int *ptr = ( int * ) &a ; // one way
ptr = const_cast ( &a ) ; // better way

Interview Questions on C++

www.mallepoolu.com
}
Here, the address of the const variable a is assigned to the pointer to a non-const
variable. The const_cast is also used when we want to change the data members of a
class inside the const member functions. The following code snippet shows how to do
this.
class sample
{
private :
int data ;
public :
void fun( ) const
{
( const_cast ( this ) ) -> data = 70 ;
}
};
------------------------------------------------------------------------------------------------98.
Using a smart pointer we can make an object appear like a pointer.
If a class overloads the operator -> then any object of that class can appear like a
pointer when the operator -> ( ) is called. The following program illustrates this.
#include
class test
{
public :
void fun( )
{
cout << "fun of smart pointer" ;
}
};
class smartpointer
{
test t ;
public :
test* operator ->( )
{
return &t ;
}
};
void main( )
{
smartpointer sp ;
sp -> fun( ) ;
}
The beauty of overloading operator -> is that even though sp is an object we can make
it work like a pointer. The operator -> ( ) returns the address of the object of the type
test. Using this address of the test object the function fun( ) of the class test gets called.
Thus even though fun( ) is not a member of smartpointer class we can still call it using
sp.
------------------------------------------------------------------------------------------------99.
Can we apply delete on this pointer inside a member function?

Interview Questions on C++

www.mallepoolu.com
Ans : Yes! If the member function of a class is called using a pointer to an object, which
is allocated dynamically, the object would get deleted. But if the member function is
called using the object, which is allocated statically, then a runtime error would occur.
This is because we cannot call delete on statically allocated objects. This is illustrated in
the following example.
class sample
{
private :
int i ;
public :
void fun( )
{
delete this ;
}
};
void main( )
{
sample *s = new sample ;
s -> fun( ) ; // no error
sample s1 ;
s1.fun( ) ; // would throw a runtime error
}
100.
Why can't data members of a class be initialized at the time of declaration as given in
the following code?
class emp
{
private :
int j = 10 ;
};
Ans: Memory for data members of a class is allocated only when object of that class is
created. One cannot store data in a memory location, which does not exist at all.
Therefore initialization at the time of declaration
is not possible.
------------------------------------------------------------------------------------------------101.
Why in a copy constructor an object is collected in a reference to object as shown below?
#include
class emp
{
public :
emp( )
{
}
emp ( emp& )
{
cout << "copy" ;
}
};
void main( )
{
emp e ;

Interview Questions on C++

www.mallepoolu.com
emp e1 = e ;
}
Ans: A copy constructor is called when an object is created and initialised at the same
time. It is also called when object is passed to a function. So, If we pass the object to
copy constructor copy constructor would get called recursively. Thus it will stuck up in an
infinite loop.
------------------------------------------------------------------------------------------------102.
What is Early Binding and Dynamic Binding?
Ans: The term binding refers to the connection between a function call and the actual
code executed as a result of the call. Early Binding: If which function is to be called is
known at the compile-time it is known as static or early binding. Dynamic Binding: If
which function is to be called is decided at run time it is called as late or dynamic
binding. Dynamic binding is so called because the actual function called at run-time
depends on the contents of the pointer. For example, call to virtual functions, call to
functions to be linked from dlls use late binding.
------------------------------------------------------------------------------------------------103.
When can we use the function ostrstream::freeze( )?
Ans: While outputting data to memory in the in-memory formatting we need to create an
object of the class ostrstream. The constructor of ostrstream receives the address of the
buffer but if we want that the ostrstream
object should do its own memory management then we need to create an ostrstream
object with no constructor arguments as:
ostrstream s ;
Now s will do its own memory management. We can stuff as many bytes into it as we
want. If it falls short of memory, it will allocate more memory. If it cannot, it may even
move the block of memory. When the object goes out of scope, the heap storage is
automatically released. This is a more flexible approach if we do not know how much
space we are going to need. If we want the physical address of the memory used by s
we can obtain it by calling the str( ) member function:
char* p = s.str( ) ;
Once str( ) has been called then the block of memory allocated by ostrstream cannot be
moved. This is logical. It can't move the block since we are now expecting it to be at a
particular location. In such a case we
say that ostrstream has freezed itself. Once frozen we can't add any more characters to
it. Adding characters to a frozen ostrstream results in undefined behavior. In addition,
the ostrstream is no longer responsible for cleaning up the storage. You took over that
responsibility when you asked for the char * with str( ). We can clean the storage in two
ways: Using the delete operator as shown below:
ostrstream s ;
char *p ;
p = s.str( ) ;
delete p ;
By unfreezing the ostrstream. You do this by calling freeze( ), with an argument 1.
During freezing it is called with the default argument of 0.

Interview Questions on C++

www.mallepoolu.com
1.what is a transient variable?
A transient variable is a variable that may not be serialized.

2.which containers use a border Layout as their default layout?


The window, Frame and Dialog classes use a border layout as their default layout.

3.Why do threads block on I/O?


Threads block on i/o (that is enters the waiting state) so that other threads may
execute while the i/o Operation is performed.

4. How are Observer and Observable used?


Objects that subclass the Observable class maintain a list of observers. When an
Observable object is updated it invokes the update() method of each of its observers
to notify the observers that it has changed state. The Observer interface is
implemented by objects that observe Observable objects.

5. What is synchronization and why is it important?


With respect to multithreading, synchronization is the capability to control the access
of multiple threads to shared resources. Without synchronization, it is possible for
one thread to modify a shared object while another thread is in the process of using
or updating that object's value. This often leads to significant errors.

6. Can a lock be acquired on a class?


Yes, a lock can be acquired on a class. This lock is acquired on the class's Class
object..

7. What's new with the stop(), suspend() and resume() methods in JDK 1.2?
The stop(), suspend() and resume() methods have been deprecated in JDK 1.2.

8. Is null a keyword?
The null value is not a keyword.

9. What is the preferred size of a component?


The preferred size of a component is the minimum component size that will allow the
component to display normally.

Interview Questions on Core Java

www.mallepoolu.com

10. What method is used to specify a container's layout?


The setLayout() method is used to specify a container's layout.

11. Which containers use a FlowLayout as their default layout?


The Panel and Applet classes use the FlowLayout as their default layout.

12. What state does a thread enter when it terminates its processing?
When a thread terminates its processing, it enters the dead state.

13. What is the Collections API?


The Collections API is a set of classes and interfaces that support operations on
collections of objects.

14. Which characters may be used as the second character of an identifier,


but not as the first character of an identifier?
The digits 0 through 9 may not be used as the first character of an identifier but they
may be used after the first character of an identifier.

15. What is the List interface?


The List interface provides support for ordered collections of objects.

16. How does Java handle integer overflows and underflows?


It uses those low order bytes of the result that can fit into the size of the type
allowed by the operation.

17. What is the Vector class?


The Vector class provides the capability to implement a growable array of objects

18. What modifiers may be used with an inner class that is a member of an outer
class?
A (non-local) inner class may be declared as public, protected, private, static, final,
or abstract.

Interview Questions on Core Java

www.mallepoolu.com
19. What is an Iterator interface?
The Iterator interface is used to step through the elements of a Collection.

20. What is the difference between the >> and >>> operators?
The >> operator carries the sign bit when shifting right. The >>> zero-fills bits that
have been shifted out.

21. Which method of the Component class is used to set the position and
size of a component?
setBounds()

22. How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8
characters?
Unicode requires 16 bits and ASCII require 7 bits. Although the ASCII character set
uses only 7 bits, it is usually represented as 8 bits. UTF-8 represents characters
using 8, 16, and 18 bit patterns. UTF-16 uses 16-bit and larger bit patterns.

23What is the difference between yielding and sleeping?


When a task invokes its yield() method, it returns to the ready state. When a task
invokes its sleep() method, it returns to the waiting state.

24. Which java.util classes and interfaces support event handling?


The EventObject class and the EventListener interface support event processing.

25. Is sizeof a keyword?


The sizeof operator is not a keyword.

26. What are wrapped classes?


Wrapped classes are classes that allow primitive types to be accessed as objects.

27. Does garbage collection guarantee that a program will not run out of memory?
Garbage collection does not guarantee that a program will not run out of memory. It
is possible for programs to use up memory resources faster than they are garbage
collected. It is also possible for programs to create objects that are not subject to
garbage collection

Interview Questions on Core Java

www.mallepoolu.com

28. What restrictions are placed on the location of a package statement


within a source code file?
A package statement must appear as the first line in a source code file (excluding
blank lines and comments).

29. Can an object's finalize() method be invoked while it is reachable?


An object's finalize() method cannot be invoked by the garbage collector while the
object is still reachable. However, an object's finalize() method may be invoked by
other objects.

30. What is the immediate superclass of the Applet class?


Panel

31. What is the difference between preemptive scheduling and time slicing?
Under preemptive scheduling, the highest priority task executes until it enters the
waiting or dead states or a higher priority task comes into existence. Under time
slicing, a task executes for a predefined slice of time and then reenters the pool of
ready tasks. The scheduler then determines which task should execute next, based
on priority and
other factors.

32. Name three Component subclasses that support painting.


The Canvas, Frame, Panel, and Applet classes support painting.

33. What value does readLine() return when it has reached the end of a file?
The readLine() method returns null when it has reached the end of a file.

34. What is the immediate superclass of the Dialog class?


Window

35. What is clipping?


Clipping is the process of confining paint operations to a limited area or shape.

36. What is a native method?

Interview Questions on Core Java

www.mallepoolu.com
A native method is a method that is implemented in a language other than Java.

37. Can a for statement loop indefinitely?


Yes, a for statement can loop indefinitely. For example, consider the following:
for(;;) ;

38. What are order of precedence and associativity, and how are they used?
Order of precedence determines the order in which operators are evaluated in
expressions. Associatity determines whether an expression is evaluated left-to-right
or right-to-left

39. When a thread blocks on I/O, what state does it enter?


A thread enters the waiting state when it blocks on I/O.

40. To what value is a variable of the String type automatically initialized?


The default value of an String type is null.

41. What is the catch or declare rule for method declarations?


If a checked exception may be thrown within the body of a method, the method
must either catch the exception or declare it in its throws clause.
42. What is the difference between a MenuItem and a CheckboxMenuItem?
The CheckboxMenuItem class extends the MenuItem class to support a menu item
that may be checked or unchecked.
43. What is a task's priority and how is it used in scheduling?
A task's priority is an integer value that identifies the relative order in which it should
be executed with respect to other tasks. The scheduler attempts to schedule higher
priority tasks before lower priority tasks.
44. What class is the top of the AWT event hierarchy?
The java.awt.AWTEvent class is the highest-level class in the AWT event-class
hierarchy.
45. When a thread is created and started, what is its initial state?
A thread is in the ready state after it has been created and started.
46. Can an anonymous class be declared as implementing an interface and extending
a class?
An anonymous class may implement an interface or extend a superclass, but may
not be declared to do both.
47. What is the range of the short type?
The range of the short type is -(2^15) to 2^15 - 1.

Interview Questions on Core Java

www.mallepoolu.com
48. What is the range of the char type?
The range of the char type is 0 to 2^16 - 1.
49. In which package are most of the AWT events that support the event-delegation
model defined?
Most of the AWT-related events of the event-delegation model are defined in the
java.awt.event package. The AWTEvent class is defined in the java.awt package.
50. What is the immediate superclass of Menu?
MenuItem
51. What is the purpose of finalization?
The purpose of finalization is to give an unreachable object the opportunity to
perform any cleanup processing before the object is garbage collected.
52. Which class is the immediate superclass of the MenuComponent class.
Object
53. What invokes a thread's run() method?
After a thread is started, via its start() method or that of the Thread class, the JVM
invokes the thread's run() method when the thread is initially executed.
54. What is the difference between the Boolean & operator and the && operator?
If an expression involving the Boolean & operator is evaluated, both operands are
evaluated. Then the & operator is applied to the operand. When an expression
involving the && operator is evaluated, the first operand is evaluated. If the first
operand returns a value of true then the second operand is evaluated. The &&
operator is then applied to the first and second operands. If the first operand
evaluates to false, the evaluation of the second operand is skipped.
55. Name three subclasses of the Component class.
Box.Filler, Button, Canvas, Checkbox, Choice, Container, Label, List, Scrollbar, or
TextComponent
56. What is the GregorianCalendar class?
The GregorianCalendar provides support for traditional Western calendars.
57. Which Container method is used to cause a container to be laid out and
redisplayed?
validate()
58. What is the purpose of the Runtime class?
The purpose of the Runtime class is to provide access to the Java runtime system.
59. How many times may an object's finalize() method be invoked by the
garbage collector?
An object's finalize() method may only be invoked once by the garbage collector.
60. What is the purpose of the finally clause of a try-catch-finally statement?
The finally clause is used to provide the capability to execute code no matter
whether or not an exception is thrown or caught.
61. What is the argument type of a program's main() method?

Interview Questions on Core Java

www.mallepoolu.com
A program's main() method takes an argument of the String[] type.
62. Which Java operator is right associative?
The = operator is right associative.
63. What is the Locale class?
The Locale class is used to tailor program output to the conventions of a particular
geographic, political, or cultural region.
64. Can a double value be cast to a byte?
Yes, a double value can be cast to a byte.
65. What is the difference between a break statement and a continue statement?
A break statement results in the termination of the statement to which it applies
(switch, for, do, or while). A continue statement is used to end the current loop
iteration and return control to the loop statement.
66. What must a class do to implement an interface?
It must provide all of the methods in the interface and identify the interface in its
implements clause.
67. What method is invoked to cause an object to begin executing as a separate
thread?
The start() method of the Thread class is invoked to cause an object to begin
executing as a separate thread.
68. Name two subclasses of the TextComponent class.
TextField and TextArea
69. What is the advantage of the event-delegation model over the earlier eventinheritance model?
The event-delegation model has two advantages over the event-inheritance model.
First, it enables event handling to be handled by objects other than the ones that
generate the events (or their containers). This allows a clean separation between a
component's design and its use. The other advantage of the event-delegation model
is that it performs much better in applications where many events are generated.
This performance improvement is due to the fact that the event-delegation model
does not have to repeatedly process unhandled events, as is the case of the eventinheritance model.
70. Which containers may have a MenuBar?
Frame
71. How are commas used in the intialization and iteration parts of a for statement?
Commas are used to separate multiple statements within the initialization and
iteration parts of a for statement.
72. What is the purpose of the wait(), notify(), and notifyAll() methods?
The wait(),notify(), and notifyAll() methods are used to provide an efficient way for
threads to wait for a shared resource. When a thread executes an object's wait()
method, it enters the waiting state. It only enters the ready state after another
thread invokes the object's notify() or notifyAll() methods..
73. What is an abstract method?

Interview Questions on Core Java

www.mallepoolu.com
An abstract method is a method whose implementation is deferred to a subclass.
74. How are Java source code files named?
A Java source code file takes the name of a public class or interface that is defined
within the file. A source code file may contain at most one public class or interface. If
a public class or interface is defined within a source code file, then the source code
file must take the name of the public class or interface. If no public class or interface
is defined within a source code file, then the file must take on a name that is
different than its classes and interfaces. Source code files use the .java extension.
75. What is the relationship between the Canvas class and the Graphics class?
A Canvas object provides access to a Graphics object via its paint() method.
76. What are the high-level thread states?
The high-level thread states are ready, running, waiting, and dead.
77. What value does read() return when it has reached the end of a file?
The read() method returns -1 when it has reached the end of a file.
78. Can a Byte object be cast to a double value?
No, an object cannot be cast to a primitive value.
79. What is the difference between a static and a non-static inner class?
A non-static inner class may have object instances that are associated with instances
of the class's outer class. A static inner class does not have any object instances.
80. What is the difference between the String and StringBuffer classes?
String objects are constants. StringBuffer objects are not.
81. If a variable is declared as private, where may the variable be accessed?
A private variable may only be accessed within the class in which it is declared.
82. What is an object's lock and which object's have locks?
An object's lock is a mechanism that is used by multiple threads to obtain
synchronized access to the object. A thread may execute a synchronized method of
an object only after it has acquired the object's lock. All objects and classes have
locks. A class's lock is acquired on the class's Class object.
83. What is the Dictionary class?
The Dictionary class provides the capability to store key-value pairs.
84. How are the elements of a BorderLayout organized?
The elements of a BorderLayout are organized at the borders (North, South, East,
and West) and the center of a container.
85. What is the % operator?
It is referred to as the modulo or remainder operator. It returns the remainder of
dividing the first operand by the second operand.
86. When can an object reference be cast to an interface reference?
An object reference be cast to an interface reference when the object implements
the referenced interface.
87. What is the difference between a Window and a Frame?

Interview Questions on Core Java

www.mallepoolu.com
The Frame class extends Window to define a main application window that can have
a menu bar.
88. Which class is extended by all other classes?
The Object class is extended by all other classes.
89. Can an object be garbage collected while it is still reachable?
A reachable object cannot be garbage collected. Only unreachable objects may be
garbage collected..
90. Is the ternary operator written x : y ? z or x ? y : z ?
It is written x ? y : z.
92. How is rounding performed under integer division?
The fractional part of the result is truncated. This is known as rounding toward zero.
93. What happens when a thread cannot acquire a lock on an object?
If a thread attempts to execute a synchronized method or synchronized statement
and is unable to acquire an object's lock, it enters the waiting state until the lock
becomes available.
94. What is the difference between the Reader/Writer class hierarchy and the
InputStream/OutputStream class hierarchy?
The Reader/Writer class hierarchy is character-oriented, and the
InputStream/OutputStream class hierarchy is byte-oriented.
95. What classes of exceptions may be caught by a catch clause?
A catch clause can catch any exception that may be assigned to the Throwable type.
This includes the Error and Exception types.
96. If a class is declared without any access modifiers, where may the class be
accessed?
A class that is declared without any access modifiers is said to have package access.
This means that the class can only be accessed by other classes and interfaces that
are defined within the same package.
97. What is the SimpleTimeZone class?
The SimpleTimeZone class provides support for a Gregorian calendar.
98. What is the Map interface?
The Map interface replaces the JDK 1.1 Dictionary class and is used associate keys
with values.
99. Does a class inherit the constructors of its superclass?
A class does not inherit constructors from any of its superclasses.
100. For which statements does it make sense to use a label?
The only statements for which it makes sense to use a label are those statements
that can enclose a break or continue statement.
101. What is the purpose of the System class?
The purpose of the System class is to provide access to system resources.
102. Which TextComponent method is used to set a TextComponent to the read-only

Interview Questions on Core Java

www.mallepoolu.com
state?
setEditable()
103. How are the elements of a CardLayout organized?
The elements of a CardLayout are stacked, one on top of the other, like a deck of
cards.
104. Is &&= a valid Java operator?
No, it is not.
105. Name the eight primitive Java types.
The eight primitive types are byte, char, short, int, long, float, double, and boolean.
106. Which class should you use to obtain design information about an object?
The Class class is used to obtain information about an object's design.
107. What is the relationship between clipping and repainting?
When a window is repainted by the AWT painting thread, it sets the clipping regions
to the area of the window that requires repainting.
108. Is "abc" a primitive value?
The String literal "abc" is not a primitive value. It is a String object.
109. What is the relationship between an event-listener interface and an
event-adapter class?
An event-listener interface defines the methods that must be implemented by an
event handler for a particular kind of event. An event adapter provides a default
implementation of an event-listener interface.
110. What restrictions are placed on the values of each case of a switch statement?
During compilation, the values of each case of a switch statement must evaluate to a
value that can be promoted to an int value.
111. What modifiers may be used with an interface declaration?
An interface may be declared as public or abstract.
112. Is a class a subclass of itself?
A class is a subclass of itself.
113. What is the highest-level event class of the event-delegation model?
The java.util.EventObject class is the highest-level class in the event-delegation class
hierarchy.
114. What event results from the clicking of a button?
The ActionEvent event is generated as the result of the clicking of a button.
115. How can a GUI component handle its own events?
A component can handle its own events by implementing the required event-listener
interface and adding itself as its own event listener.
116. What is the difference between a while statement and a do statement?
A while statement checks at the beginning of a loop to see whether the next loop
iteration should occur. A do statement checks at the end of a loop to see whether the
next iteration of a loop should occur. The do statement will always execute the body
of a loop at least once.

Interview Questions on Core Java

www.mallepoolu.com
117. How are the elements of a GridBagLayout organized?
The elements of a GridBagLayout are organized according to a grid. However, the
elements are of different sizes and may occupy more than one row or column of the
grid. In addition, the rows and columns may have different sizes.
118. What advantage do Java's layout managers provide over traditional windowing
systems?
Java uses layout managers to lay out components in a consistent manner across all
windowing platforms. Since Java's layout managers aren't tied to absolute sizing and
positioning, they are able to accomodate platform-specific differences among
windowing systems.
119. What is the Collection interface?
The Collection interface provides support for the implementation of a mathematical
bag - an unordered collection of objects that may contain duplicates.
120. What modifiers can be used with a local inner class?
A local inner class may be final or abstract.
121. What is the difference between static and non-static variables?
A static variable is associated with the class as a whole rather than with specific
instances of a class. Non-static variables take on unique values with each object
instance.
122. What is the difference between the paint() and repaint() methods?
The paint() method supports painting via a Graphics object. The repaint() method is
used to cause paint() to be invoked by the AWT painting thread.
123. What is the purpose of the File class?
The File class is used to create objects that provide access to the files and directories
of a local file system.
124. Can an exception be rethrown?
Yes, an exception can be rethrown.
125. Which Math method is used to calculate the absolute value of a number?
The abs() method is used to calculate absolute values.
126. How does multithreading take place on a computer with a single CPU?
The operating system's task scheduler allocates execution time to multiple tasks. By
quickly switching between executing tasks, it creates the impression that tasks
execute sequentially.
127. When does the compiler supply a default constructor for a class?
The compiler supplies a default constructor for a class if no other constructors are
provided.
128. When is the finally clause of a try-catch-finally statement executed?
The finally clause of the try-catch-finally statement is always executed unless the
thread of execution terminates or an exception occurs within the execution of the
finally clause.
129. Which class is the immediate superclass of the Container class?
Component

Interview Questions on Core Java

www.mallepoolu.com
130. If a method is declared as protected, where may the method be accessed?
A protected method may only be accessed by classes or interfaces of the same
package or by subclasses of the class in which it is declared.
131. How can the Checkbox class be used to create a radio button?
By associating Checkbox objects with a CheckboxGroup.
132. Which non-Unicode letter characters may be used as the first character
of an identifier?
The non-Unicode letter characters $ and _ may appear as the first character of an
identifier
133. What restrictions are placed on method overloading?
Two methods may not have the same name and argument list but different return
types.
134. What happens when you invoke a thread's interrupt method while it is
sleeping or waiting?
When a task's interrupt() method is executed, the task enters the ready state. The
next time the task enters the running state, an InterruptedException is thrown.
135. What is casting?
There are two types of casting, casting between primitive numeric types and casting
between object references. Casting between numeric types is used to convert larger
values, such as double values, to smaller values, such as byte values. Casting
between object references is used to refer to an object by a compatible class,
interface, or array type reference.
136. What is the return type of a program's main() method?
A program's main() method has a void return type.
137. Name four Container classes.
Window, Frame, Dialog, FileDialog, Panel, Applet, or ScrollPane
138. What is the difference between a Choice and a List?
A Choice is displayed in a compact form that requires you to pull it down to see the
list of available choices. Only one item may be selected from a Choice. A List may be
displayed in such a way that several List items are visible. A List supports the
selection of one or more List items.
139. What class of exceptions are generated by the Java run-time system?
The Java runtime system generates RuntimeException and Error exceptions.
140. What class allows you to read objects directly from a stream?
The ObjectInputStream class supports the reading of objects from input streams.
141. What is the difference between a field variable and a local variable?
A field variable is a variable that is declared as a member of a class. A local variable
is a variable that is declared local to a method.
142. Under what conditions is an object's finalize() method invoked by the garbage
collector?
The garbage collector invokes an object's finalize() method when it detects that the

Interview Questions on Core Java

www.mallepoolu.com
object has become unreachable.
143. How are this() and super() used with constructors?
this() is used to invoke a constructor of the same class. super() is used to invoke a
superclass constructor.
144. What is the relationship between a method's throws clause and the exceptions
that can be thrown during the method's execution?
A method's throws clause must declare any checked exceptions that are not caught
within the body of the method.
145. What is the difference between the JDK 1.02 event model and the eventdelegation
model introduced with JDK 1.1?
The JDK 1.02 event model uses an event inheritance or bubbling approach. In this
model, components are required to handle their own events. If they do not handle a
particular event, the event is inherited by (or bubbled up to) the component's
container. The container then either handles the event or it is bubbled up to its
container and so on, until the highest-level container has been tried..
In the event-delegation model, specific objects are designated as event handlers for
GUI components. These objects implement event-listener interfaces. The eventdelegation model is more efficient than the event-inheritance model because it
eliminates the processing required to support the bubbling of unhandled events.
146. How is it possible for two String objects with identical values not to be equal
under the == operator?
The == operator compares two objects to determine if they are the same object in
memory. It is possible for two String objects to have the same value, but located
indifferent areas of memory.
147. Why are the methods of the Math class static?
So they can be invoked as if they are a mathematical code library.
148. What Checkbox method allows you to tell if a Checkbox is checked?
getState()
149. What state is a thread in when it is executing?
An executing thread is in the running state.
150. What are the legal operands of the instanceof operator?
The left operand is an object reference or null value and the right operand is a class,
interface, or array type.
151. How are the elements of a GridLayout organized?
The elements of a GridBad layout are of equal size and are laid out using the squares
of a grid.
152. What an I/O filter?
An I/O filter is an object that reads from one stream and writes to another, usually
altering the data in some way as it is passed from one stream to another.
153. If an object is garbage collected, can it become reachable again?
Once an object is garbage collected, it ceases to exist. It can no longer become
reachable again.

Interview Questions on Core Java

www.mallepoolu.com
154. What is the Set interface?
The Set interface provides methods for accessing the elements of a finite
mathematical set. Sets do not allow duplicate elements.
155. What classes of exceptions may be thrown by a throw statement?
A throw statement may throw any expression that may be assigned to the Throwable
type.
156. What are E and PI?
E is the base of the natural logarithm and PI is mathematical value pi.
157. Are true and false keywords?
The values true and false are not keywords.
158. What is a void return type?
A void return type indicates that a method does not return a value.
159. What is the purpose of the enableEvents() method?
The enableEvents() method is used to enable an event for a particular object.
Normally, an event is enabled when a listener is added to an object for a particular
event. The enableEvents() method is used by objects that handle events by
overriding their event-dispatch methods.
160. What is the difference between the File and RandomAccessFile classes?
The File class encapsulates the files and directories of the local file system. The
RandomAccessFile class provides the methods needed to directly access data
contained in any part of a file.
161. What happens when you add a double value to a String?
The result is a String object.
162. What is your platform's default character encoding?
If you are running Java on English Windows platforms, it is probably Cp1252. If you
are running Java on English Solaris platforms, it is most likely 8859_1..
163. Which package is always imported by default?
The java.lang package is always imported by default.
164. What interface must an object implement before it can be written to a
stream as an object?
An object must implement the Serializable or Externalizable interface before it can be
written to a stream as an object.
165. How are this and super used?
this is used to refer to the current object instance. super is used to refer to the
variables and methods of the superclass of the current object instance.
166. What is the purpose of garbage collection?
The purpose of garbage collection is to identify and discard objects that are no longer
needed by a program so that their resources may be reclaimed and reused.
167. What is a compilation unit?
A compilation unit is a Java source code file.

Interview Questions on Core Java

www.mallepoolu.com
168. What interface is extended by AWT event listeners?
All AWT event listeners extend the java.util.EventListener interface.
169. What restrictions are placed on method overriding?
Overridden methods must have the same name, argument list, and return type.
The overriding method may not limit the access of the method it overrides.
The overriding method may not throw any exceptions that may not be thrown
by the overridden method.
170. How can a dead thread be restarted?
A dead thread cannot be restarted.
171. What happens if an exception is not caught?
An uncaught exception results in the uncaughtException() method of the thread's
ThreadGroup being invoked, which eventually results in the termination of the
program in which it is thrown.
172. What is a layout manager?
A layout manager is an object that is used to organize components in a container.
173. Which arithmetic operations can result in the throwing of an
ArithmeticException?
Integer / and % can result in the throwing of an ArithmeticException.
174. What are three ways in which a thread can enter the waiting state?
A thread can enter the waiting state by invoking its sleep() method, by blocking on
I/O, by unsuccessfully attempting to acquire an object's lock, or by invoking an
object's wait() method. It can also enter the waiting state by invoking its
(deprecated) suspend() method.
175. Can an abstract class be final?
An abstract class may not be declared as final.
176. What is the ResourceBundle class?
The ResourceBundle class is used to store locale-specific resources that can be
loaded by a program to tailor the program's appearance to the particular locale in
which it is being run.
177. What happens if a try-catch-finally statement does not have a catch clause to
handle an exception that is thrown within the body of the try statement?
The exception propagates up to the next higher level try-catch statement (if any) or
results in the program's termination.
178. What is numeric promotion?
Numeric promotion is the conversion of a smaller numeric type to a larger numeric
type, so that integer and floating-point operations may take place. In numerical
promotion, byte, char, and short values are converted to int values. The int values
are also converted to long values, if necessary. The long and float values are
converted to double values, as required.
179. What is the difference between a Scrollbar and a ScrollPane?
A Scrollbar is a Component, but not a Container. A ScrollPane is a Container. A
ScrollPane handles its own events and performs its own scrolling.

Interview Questions on Core Java

www.mallepoolu.com
180. What is the difference between a public and a non-public class?
A public class may be accessed outside of its package. A non-public class may not be
accessed outside of its package.
181. To what value is a variable of the boolean type automatically initialized?
The default value of the boolean type is false.
182. Can try statements be nested?
Try statements may be tested.
183. What is the difference between the prefix and postfix forms of the ++ operator?
The prefix form performs the increment operation and returns the value of the
increment operation. The postfix form returns the current value all of the expression
and then performs the increment operation on that value.
184. What is the purpose of a statement block?
A statement block is used to organize a sequence of statements as a single
statement group.
185. What is a Java package and how is it used?
A Java package is a naming context for classes and interfaces. A package is used to
create a separate name space for groups of classes and interfaces. Packages are also
used to organize related classes and interfaces into a single API unit and to control
accessibility to these classes and interfaces.
186. What modifiers may be used with a top-level class?
A top-level class may be public, abstract, or final.
187. What are the Object and Class classes used for?
The Object class is the highest-level class in the Java class hierarchy. The Class class
is used to represent the classes and interfaces that are loaded by a Java program..
188. How does a try statement determine which catch clause should be used to
handle an exception?
When an exception is thrown within the body of a try statement, the catch clauses of
the try statement are examined in the order in which they appear. The first catch
clause that is capable of handling the exception is executed. The remaining catch
clauses are ignored.
189. Can an unreachable object become reachable again?
An unreachable object may become reachable again. This can happen when the
object's finalize() method is invoked and the object performs an operation which
causes it to become accessible to reachable objects.
190. When is an object subject to garbage collection?
An object is subject to garbage collection when it becomes unreachable to the
program in which it is used.
191. What method must be implemented by all threads?
All tasks must implement the run() method, whether they are a subclass of Thread
or implement the Runnable interface.
192. What methods are used to get and set the text label displayed by a Button
object?

Interview Questions on Core Java

www.mallepoolu.com
getLabel() and setLabel()
193. Which Component subclass is used for drawing and painting?
Canvas
194. What are synchronized methods and synchronized statements?
Synchronized methods are methods that are used to control access to an object. A
thread only executes a synchronized method after it has acquired the lock for the
method's object or class. Synchronized statements are similar to synchronized
methods. A synchronized statement can only be executed after a thread has
acquired the lock for the object or class referenced in the synchronized statement.
195. What are the two basic ways in which classes that can be run as threads may
be defined?
A thread class may be declared as a subclass of Thread, or it may implement the
Runnable interface.
196. What are the problems faced by Java programmers who don't use layout
managers?
Without layout managers, Java programmers are faced with determining how their
GUI will be displayed across multiple windowing systems and finding a common
sizing and positioning that will work within the constraints imposed by each
windowing system.
197. What is the difference between an if statement and a switch statement?
The if statement is used to select among two alternatives. It uses a boolean
expression to decide which alternative should be executed. The switch statement is
used to select among multiple alternatives. It uses an int expression to determine
which alternative should be executed.
198. What happens when you add a double value to a String?
The result is a String object.
199. What is the List interface?
The List interface provides support for ordered collections of objects.

Interview Questions on Core Java

www.mallepoolu.com
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 command-line 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 64K?
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")"?
What is the output of printf("%d")
What will happen if I say delete this
Difference between "C structure" and "C++ structure".
Diffrence between a "assignment operator" and a "copy constructor"
What is the difference between "overloading" and "overridding"?
Explain the need for "Virtual Destructor".
Can we have "Virtual Constructors"?
What are the different types of polymorphism?
What are Virtual Functions? How to implement virtual functions in "C"
What are the different types of Storage classes?
What is Namespace?
What are the types of STL containers?.
Difference 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?
Talk sometiming about profiling?
How many lines of code you have written for a single program?
What is "strstream" ?
How to write Multithreaded applications using C++?
Explain "passing by value", "passing by pointer" and "passing by reference"
Write any small program that will compile in "C" but not in "C++"
Have you heard of "mutable" keyword?
Is there something that I can do in C and not in C++?
Why preincrement operator is faster than postincrement?
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"?
What is Memory Alignment?
Explain working of printf.
Difference between "printf" and "sprintf".
What is "map" in STL?
When shall I use Multiple Inheritance?
What are the techniques you use for debugging?
How to reduce a final size of executable?
Give 2 examples of a code optimization.
What is inheritance?
Difference between Composition and Aggregation.

Interview Questions on OOPS

www.mallepoolu.com
Difference: Sequence Diagrams, Collaboration Diagrams.
Difference: 'uses', 'extends', 'includes'
What shall I go for Package Diagram?
What is Polymorphism?
Is class an Object? Is object a class?
Comment: C++ "includes" behavior and java "imports"
What do you mean by "Realization"?
What is a Presistent, Transient Object?
What is the use of Operator Overloading?
Does UML guarantee project success?
Difference: Activity Diagram and Sequence Diagram.
What is association?
How to resolve many to many relationship?
How do you represent static members and abstract classes in Class Diagram?
What does static variable mean?
What is a pointer?
What is a structure?
What are the differences between structures and arrays?
In header files whether functions are declared or defined?
What are the differences between malloc() and calloc()?
What are macros? what are its advantages and disadvantages?
Difference between pass by reference and pass by value?
What is static identifier?
Where are the auto variables stored?
Where does global, static, local, register variables, free memory and C Program
instructions get stored?
Difference between arrays and linked list?
What are enumerations?
Describe about storage allocation and scope of global, extern, static, local and
register variables?
What are register variables? What are the advantage of using register variables?
What is the use of typedef?
Can we specify variable field width in a scanf() format string? If possible how?
Out of fgets() and gets() which function is safe to use and why?
Difference between strdup and strcpy?
What is recursion?
Differentiate between a for loop and a while loop? What are it uses?
What are the different storage classes in C?
Write down the equivalent pointer expression for referring the same element
a[i][j][k][l]?
What is difference between Structure and Unions?
What the advantages of using Unions?
What are the advantages of using pointers in a program?
What is the difference between Strings and Arrays?
In a header file whether functions are declared or defined?
What is a far pointer? where we use it?
How will you declare an array of three function pointers where each function
receives two ints and returns a float?
what is a NULL Pointer? Whether it is same as an uninitialized pointer?
What is a NULL Macro? What is the difference between a NULL Pointer and a NULL
Macro?
What does the error 'Null Pointer Assignment' mean and what causes this error?
What is near, far and huge pointers? How many bytes are occupied by them?

Interview Questions on OOPS

www.mallepoolu.com
How would you obtain segment and offset addresses from a far address of a
memory location?
Are the expressions arr and &arr same for an array of integers?
Does mentioning the array name gives the base address in all the contexts?
Explain one method to process an entire string as one unit?
What is the similarity between a Structure, Union and enumeration?
Can a Structure contain a Pointer to itself?
How can we check whether the contents of two structure variables are same or
not?
How are Structure passing and returning implemented by the complier?
How can we read/write Structures from/to data files?
What is the difference between an enumeration and a set of pre-processor #
defines?
what do the 'c' and 'v' in argc and argv stand for?
Are the variables argc and argv are local to main?
What is the maximum combined length of command line arguments including the
space between adjacent arguments?
If we want that any wildcard characters in the command line arguments should be
appropriately expanded, are we required to make any special provision? If yes,
which?
Does there exist any way to make the command line arguments available to other
functions without passing them as arguments to the function?
What are bit fields? What is the use of bit fields in a Structure declaration?
To which numbering system can the binary number 1101100100111100 be easily
converted to?
Which bit wise operator is suitable for checking whether a particular bit is on or off?
Which bit wise operator is suitable for turning off a particular bit in a number?
Which bit wise operator is suitable for putting on a particular bit in a number?
Which bit wise operator is suitable for checking whether a particular bit is on or off?
which one is equivalent to multiplying by 2:Left shifting a number by 1 or Left
shifting an unsigned int or char by 1?
Write a program to compare two strings without using the strcmp() function.
Write a program to concatenate two strings.
Write a program to interchange 2 variables without using the third one.
Write programs for String Reversal & Palindrome check
Write a program to find the Factorial of a number
Write a program to generate the Fibinocci Series
Write a program which employs Recursion
Write a program which uses Command Line Arguments
Write a program which uses functions like strcmp(), strcpy()? etc
What are the advantages of using typedef in a program?
How would you dynamically allocate a one-dimensional and two-dimensional array
of integers?
How can you increase the size of a dynamically allocated array?
How can you increase the size of a statically allocated array?
When reallocating memory if any other pointers point into the same piece of
memory do you have to readjust these other pointers or do they get readjusted
automatically?
Which function should be used to free the memory allocated by calloc()?
How much maximum can you allocate in a single call to malloc()?
Can you dynamically allocate arrays in expanded memory?
What is object file? How can you access object file?

Interview Questions on OOPS

www.mallepoolu.com
Which header file should you include if you are to develop a function which can
accept variable number of arguments?
Can you write a function similar to printf()?
How can a called function determine the number of arguments that have been
passed to it?
Can there be at least some solution to determine the number of arguments passed
to a variable argument list function?
How do you declare the following:
An array of three pointers to chars
An array of three char pointers
A pointer to array of three chars
A pointer to function which receives an int pointer and returns a float pointer
A pointer to a function which receives nothing and returns nothing
What do the functions atoi(), itoa() and gcvt() do?
Does there exist any other function which can be used to convert an integer or a
float to a string?
How would you use qsort() function to sort an array of structures?
How would you use qsort() function to sort the name stored in an array of pointers
to string?
How would you use bsearch() function to search a name stored in array of pointers
to string?
How would you use the functions sin(), pow(), sqrt()?
How would you use the functions memcpy(), memset(), memmove()?
How would you use the functions fseek(), freed(), fwrite() and ftell()?
How would you obtain the current time and difference between two times?
How would you use the functions randomize() and random()?
How would you implement a substr() function that extracts a sub string from a
given string?
What is the difference between the functions rand(), random(), srand() and
randomize()?
What is the difference between the functions memmove() and memcpy()?
How do you print a string on the printer?
Can you use the function fprintf() to display the output on the screen?
What is an object?
What is the difference between an object and a class?
What is the difference between class and structure?
What is public, protected, private?
What is friend function?
What is a scope resolution operator?
What do you mean by inheritance?
What is abstraction?
What is polymorphism? Explain with an example.
What is encapsulation?
What do you mean by binding of data and functions?
What is function overloading and operator overloading?
What is virtual class and friend class?
What do you mean by inline function?
What do you mean by public, private, protected and friendly?
When is an object created and what is its lifetime?
What do you mean by multiple inheritance and multilevel inheritance? Differentiate
between them.
Difference between realloc() and free?
What is a template?

Interview Questions on OOPS

www.mallepoolu.com
What are the main differences between procedure oriented languages and object
oriented languages?
What is R T T I ?
What are generic functions and generic classes?
What is namespace?
What is the difference between pass by reference and pass by value?
Why do we use virtual functions?
What do you mean by pure virtual functions?
What are virtual classes?
Does c++ support multilevel and multiple inheritance?
What are the advantages of inheritance?
When is a memory allocated to a class?
What is the difference between declaration and definition?
What is virtual constructors/destructors?
In c++ there is only virtual destructors, no constructors. Why?
What is late bound function call and early bound function call? Differentiate.
How is exception handling carried out in c++?
When will a constructor executed?
What is Dynamic Polymorphism?
Write a macro for swapping integers

Interview Questions on OOPS

www.mallepoolu.com
Following are a few basic questions that cover the essentials of OS:
1. When is a system in safe state?
The set of dispatchable processes is in a safe state if there exists at least one temporal
order in which all processes can be run to completion without resulting in a deadlock.
2. What is cycle stealing?
We encounter cycle stealing in the context of Direct Memory Access (DMA). Either the
DMA controller can use the data bus when the CPU does not need it, or it may force the
CPU to temporarily suspend operation. The latter technique is called cycle stealing. Note
that cycle stealing can be done only at specific break points in an instruction cycle.
3. What is meant by arm-stickiness?
If one or a few processes have a high access rate to data on one track of a storage disk,
then they may monopolize the device by repeated requests to that track. This generally
happens with most common device scheduling algorithms (LIFO, SSTF, C-SCAN, etc).
High-density multisurface disks are more likely to be affected by this than low density
ones.
4. What are the stipulations of C2 level security?
C2 level security provides for:
> Discretionary Access Control
> Identification and Authentication
> Auditing
> Resource reuse
5. What is busy waiting?
The repeated execution of a loop of code while waiting for an event to occur is called
busy-waiting. The CPU is not engaged in any real productive activity during this period,
and the process does not progress toward completion.
6. What are short-, long- and medium-term scheduling?
Long term scheduler determines which programs are admitted to the system for
processing. It controls the degree of multiprogramming. Once admitted, a job becomes a
process.
Medium term scheduling is part of the swapping function. This relates to processes that
are in a blocked or suspended state. They are swapped out of real-memory until they are
ready to execute. The swapping-in decision is based on memory-management criteria.
Short term scheduler, also know as a dispatcher executes most frequently, and makes the
finest-grained decision of which process should execute next. This scheduler is invoked
whenever an event occurs. It may lead to interruption of one process by preemption.
6. What are short-, long- and medium-term scheduling?
Long term scheduler determines which programs are admitted to the system for
processing. It controls the degree of multiprogramming. Once admitted, a job becomes a
process.

Interview Questions on Operating Systems

www.mallepoolu.com
Medium term scheduling is part of the swapping function. This relates to processes that
are in a blocked or suspended state. They are swapped out of real-memory until they are
ready to execute. The swapping-in decision is based on memory-management criteria.
Short term scheduler, also know as a dispatcher executes most frequently, and makes the
finest-grained decision of which process should execute next. This scheduler is invoked
whenever an event occurs. It may lead to interruption of one process by preemption.
7. What are turnaround time and response time?
Turnaround time is the interval between the submission of a job and its completion.
Response time is the interval between submission of a request, and the first response to
that request.
8. What are the typical elements of a process image?
> User data: Modifiable part of user space. May include program data, user stack area,
and programs that may be modified.
> User program: The instructions to be executed.
> System Stack: Each process has one or more LIFO stacks associated with it. Used to
store parameters and calling addresses for procedure and system calls.
> Process control Block (PCB): Info needed by the OS to control processes.
9. What is the Translation Lookaside Buffer (TLB)?
In a cached system, the base addresses of the last few referenced pages is maintained in
registers called the TLB that aids in faster lookup. TLB contains those page-table entries
that have been most recently used. Normally, each virtual memory reference causes 2
physical memory accesses-- one to fetch appropriate page-table entry, and one to fetch
the desired data. Using TLB in-between, this is reduced to just one physical memory
access in cases of TLB-hit.
10. What is the resident set and working set of a process?
Resident set is that portion of the process image that is actually in real-memory at a
particular instant. Working set is that subset of resident set that is actually needed for
execution. (Relate this to the variable-window size method for swapping techniques.)
11. Explain the concept of Reentrancy.
It is a useful, memory-saving technique for multiprogrammed timesharing systems. A
Reentrant Procedure is one in which multiple users can share a single copy of a program
during the same period. Reentrancy has 2 key aspects: The program code cannot modify
itself, and the local data for each user process must be stored separately. Thus, the
permanent part is the code, and the temporary part is the pointer back to the calling
program and local variables used by that program. Each execution instance is called
activation. It executes the code in the permanent part, but has its own copy of local
variables/parameters. The temporary part associated with each activation is the activation
record. Generally, the activation record is kept on the stack.
Note: A reentrant procedure can be interrupted and called by an interrupting program,
and still execute correctly on returning to the procedure.

Interview Questions on Operating Systems

www.mallepoolu.com
12. Explain Belady's Anomaly.
Also called FIFO anomaly. Usually, on increasing the number of frames allocated to a
process' virtual memory, the process execution is faster, because fewer page faults occur.
Sometimes, the reverse happens, i.e., the execution time increases even when more
frames are allocated to the process. This is Belady's Anomaly. This is true for certain
page reference patterns.
13. What is a binary semaphore? What is its use?
A binary semaphore is one, which takes only 0 and 1 as values. They are used to
implement mutual exclusion and synchronize concurrent processes.
14. What is thrashing?
It is a phenomenon in virtual memory schemes when the processor spends most of its
time swapping pages, rather than executing instructions. This is due to an inordinate
number of page faults.
15. List the Coffman's conditions that lead to a deadlock.
> Mutual Exclusion: Only one process may use a critical resource at a time.
> Hold & Wait: A process may be allocated some resources while waiting for others.
> No Pre-emption: No resource can be forcible removed from a process holding it.
> Circular Wait: A closed chain of processes exist such that each process holds at least
one resource needed by another process in the chain.
16. Explain the popular multiprocessor thread-scheduling strategies.
> Load Sharing: Processes are not assigned to a particular processor. A global queue of
threads is maintained. Each processor, when idle, selects a thread from this queue. Note
that load balancing refers to a scheme where work is allocated to processors on a more
permanent basis.
> Gang Scheduling: A set of related threads is scheduled to run on a set of processors at
the same time, on a 1-to-1 basis. Closely related threads / processes may be scheduled
this way to reduce synchronization blocking, and minimize process switching. Group
scheduling predated this strategy.
> Dedicated processor assignment: Provides implicit scheduling defined by assignment
of threads to processors. For the duration of program execution, each program is
allocated a set of processors equal in number to the number of threads in the program.
Processors are chosen from the available pool.
> Dynamic scheduling: The number of thread in a program can be altered during the
course of execution.
17. When does the condition 'rendezvous' arise?
In message passing, it is the condition in which, both, the sender and receiver are blocked
until the message is delivered.
18. What is a trap and trapdoor?
Trapdoor is a secret undocumented entry point into a program used to grant access

Interview Questions on Operating Systems

www.mallepoolu.com
without normal methods of access authentication. A trap is a software interrupt, usually
the result of an error condition.
19. What are local and global page replacements?
Local replacement means that an incoming page is brought in only to the relevant
process' address space. Global replacement policy allows any page frame from any
process to be replaced. The latter is applicable to variable partitions model only.
20. Define latency, transfer and seek time with respect to disk I/O.
Seek time is the time required to move the disk arm to the required track. Rotational
delay or latency is the time it takes for the beginning of the required sector to reach the
head. Sum of seek time (if any) and latency is the access time. Time taken to actually
transfer a span of data is transfer time.
21. Describe the Buddy system of memory allocation.
Free memory is maintained in linked lists, each of equal sized blocks. Any such block is
of size 2^k. When some memory is required by a process, the block size of next higher
order is chosen, and broken into two. Note that the two such pieces differ in address only
in their kth bit. Such pieces are called buddies. When any used block is freed, the OS
checks to see if its buddy is also free. If so, it is rejoined, and put into the original freeblock linked-list.
22. What is time-stamping?
It is a technique proposed by Lamport, used to order events in a distributed system
without the use of clocks. This scheme is intended to order events consisting of the
transmission of messages. Each system 'i' in the network maintains a counter Ci. Every
time a system transmits a message, it increments its counter by 1 and attaches the timestamp Ti to the message. When a message is received, the receiving system 'j' sets its
counter Cj to 1 more than the maximum of its current value and the incoming time-stamp
Ti. At each site, the ordering of messages is determined by the following rules: For
messages x from site i and y from site j, x precedes y if one of the following conditions
holds....(a) if Ti<Tj or (b) if Ti=Tj and i<j.
23. How are the wait/signal operations for monitor different from those for semaphores?
If a process in a monitor signal and no task is waiting on the condition variable, the signal
is lost. So this allows easier program design. Whereas in semaphores, every operation
affects the value of the semaphore, so the wait and signal operations should be perfectly
balanced in the program.
24. In the context of memory management, what are placement and replacement
algorithms?
Placement algorithms determine where in available real-memory to load a program.
Common methods are first-fit, next-fit, best-fit. Replacement algorithms are used when
memory is full, and one process (or part of a process) needs to be swapped out to
accommodate a new program. The replacement algorithm determines which are the
partitions to be swapped out.

Interview Questions on Operating Systems

www.mallepoolu.com
25. In loading programs into memory, what is the difference between load-time dynamic
linking and run-time dynamic linking?
For load-time dynamic linking: Load module to be loaded is read into memory. Any
reference to a target external module causes that module to be loaded and the references
are updated to a relative address from the start base address of the application module.
With run-time dynamic loading: Some of the linking is postponed until actual reference
during execution. Then the correct module is loaded and linked.
25. In loading programs into memory, what is the difference between load-time dynamic
linking and run-time dynamic linking?
For load-time dynamic linking: Load module to be loaded is read into memory. Any
reference to a target external module causes that module to be loaded and the references
are updated to a relative address from the start base address of the application module.
With run-time dynamic loading: Some of the linking is postponed until actual reference
during execution. Then the correct module is loaded and linked.
26. What are demand- and pre-paging?
With demand paging, a page is brought into memory only when a location on that page is
actually referenced during execution. With pre-paging, pages other than the one
demanded by a page fault are brought in. The selection of such pages is done based on
common access patterns, especially for secondary memory devices.
27. Paging a memory management function, while multiprogramming a processor
management function, are the two interdependent?
Yes.
28. What is page cannibalizing?
Page swapping or page replacements are called page cannibalizing.
29. What has triggered the need for multitasking in PCs?
> Increased speed and memory capacity of microprocessors together with the support fir
virtual memory and
> Growth of client server computing
30. What are the four layers that Windows NT have in order to achieve independence?
> Hardware abstraction layer
> Kernel
> Subsystems
> System Services.
31. What is SMP?
To achieve maximum efficiency and reliability a mode of operation known as symmetric
multiprocessing is used. In essence, with SMP any process or threads can be assigned to
any processor.

Interview Questions on Operating Systems

www.mallepoolu.com
32. What are the key object oriented concepts used by Windows NT?
> Encapsulation
> Object class and instance
33. Is Windows NT a full blown object oriented operating system? Give reasons.
No Windows NT is not so, because its not implemented in object oriented language and
the data structures reside within one executive component and are not represented as
objects and it does not support object oriented capabilities .
34. What is a drawback of MVT?
It does not have the features like
> ability to support multiple processors
> virtual storage
> source level debugging
35. What is process spawning?
When the OS at the explicit request of another process creates a process, this action is
called process spawning.
36. How many jobs can be run concurrently on MVT?
15 jobs
37. List out some reasons for process termination.
> Normal completion
> Time limit exceeded
> Memory unavailable
> Bounds violation
> Protection error
> Arithmetic error
> Time overrun
> I/O failure
> Invalid instruction
> Privileged instruction
> Data misuse
> Operator or OS intervention
> Parent termination.
38. What are the reasons for process suspension?
> swapping
> interactive user request
> timing
> parent process request
39. What is process migration?
It is the transfer of sufficient amount of the state of process from one machine to the

Interview Questions on Operating Systems

www.mallepoolu.com
target machine
40. What is mutant?
In Windows NT a mutant provides kernel mode or user mode mutual exclusion with the
notion of ownership.
41. What is an idle thread?
The special thread a dispatcher will execute when no ready thread is found.
42. What is FtDisk?
It is a fault tolerance disk driver for Windows NT.
43. What are the possible threads a thread can have?
> Ready
> Standby
> Running
> Waiting
> Transition
> Terminated.

Interview Questions on Operating Systems

www.mallepoolu.com
1. What is an ABAP data dictionary?
ABAP 4 data dictionary describes the logical structures of the objects used in application
development and shows how they are mapped to the underlying relational database in
tables/views.
2. What are domains and data element?
Domains:Domain is the central object for describing the technical characteristics of an
attribute of an business objects. It describes the value range of the field. Data Element: It
is used to describe the semantic definition of the table fields like description the field.
Data element describes how a field can be displayed to end-user.
3. What is foreign key relationship?
A relationship which can be defined between tables and must be explicitly defined at
field level. Foreign keys are used to ensure the consistency of data. Data entered should
be checked against existing data to ensure that there are now contradiction. While
defining foreign key relationship cardinality has to be specified. Cardinality mentions
how many dependent records or how referenced records are possible.
4. Describe data classes.
Master data: It is the data which is seldomly changed. Transaction data: It is the data
which is often changed. Organization data: It is a customizing data which is entered in the
system when the system is configured and is then rarely changed. System data:It is the
data which R/3 system needs for itself.
5. What are indexes?
Indexes are described as a copy of a database table reduced to specific fields. This data
exists in sorted form. This sorting form ease fast access to the field of the tables. In order
that other fields are also read, a pointer to the associated record of the actual table are
included in the index. Yhe indexes are activated along with the table and are created
automatically with it in the database.
6. Difference between transparent tables and pooled tables
Transparent tables: Transparent tables in the dictionary has a one-to-one relation with the
table in database. Its structure corresponds to single database field. Table in the database
has the same name as in the dictionary. Transparent table holds application data. Pooled
tables. Pooled tables in the dictionary has a many-to-one relation with the table in
database. Table in the database has the different name as in the dictionary. Pooled table
are stored in table pool at the database level.
7. What is an ABAP/4 Query?

Interview Questions on SAP

www.mallepoolu.com
ABAP/4 Query is a powerful tool to generate simple reports without any coding. ABAP/4
Query can generate the following 3 simple reports: Basic List: It is the simple reports.
Statistics: Reports with statistical functions like Average, Percentages. Ranked Lists: For
analytical reports. - For creating a ABAP/4 Query, programmer has to create user group
and a functional group. Functional group can be created using with or without logical
database table. Finally, assign user group to functional group. Finally, create a query on
the functional group generated.
8. What is BDC programming?
Transferring of large/external/legacy data into SAP system using Batch Input
programming. Batch input is a automatic procedure referred to as BDC(Batch Data
Communications).The central component of the transfer is a queue file which receives
the data vie a batch input programs and groups associated data into sessions.
9. What are the functional modules used in sequence in BDC?
These are the 3 functional modules which are used in a sequence to perform a data
transfer successfully using BDC programming: BDC_OPEN_GROUP - Parameters like
Name of the client, sessions and user name are specified in this functional modules.
BDC_INSERT - It is used to insert the data for one transaction into a session.
BDC_CLOSE_GROUP - This is used to close the batch input session.
10. What are internal tables?
Internal tables are a standard data type object which exists only during the runtime of the
program. They are used to perform table calculations on subsets of database tables and
for re-organising the contents of database tables according to users need.
11. What is ITS? What are the merits of ITS?
ITS is a Internet Transaction Server. ITS forms an interface between HTTP server and
R/3 system, which converts screen provided data by the R/3 system into HTML
documents and vice-versa. Merits of ITS: A complete web transaction can be developed
and tested in R/3 system. All transaction components, including those used by the ITS
outside the R/3 system at runtime, can be stored in the R/3 system. The advantage of
automatic language processing in the R/3 system can be utilized to language-dependent
HTML documents at runtime.
12. What is DynPro?
DynPro is a Dynamic Programming which is a combination of screen and the associated
flow logic Screen is also called as DynPro.
13. What are screen painter and menu painter?

Interview Questions on SAP

www.mallepoolu.com
Screen painter: Screen painter is a tool to design and maintain screen and its elements. It
allows user to create GUI screens for the transactions. Attributes, layout, filed attributes
and flow logic are the elements of Screen painter. Menu painter: Menu painter is a tool to
design the interface components. Status, menu bars, menu lists, F-key settings, functions
and titles are the components of Menu painters. Screen painter and menu painter both are
the graphical interface of an ABAP/4 applications.
14. What are the components of SAP scripts?
SAP scripts is a word processing tool of SAP which has the following components:
Standard text. It is like a standard normal documents. Layout sets. - Layout set consists of
the following components: Windows and pages, Paragraph formats, Character formats.
Creating forms in the R/3 system. Every layout set consists of Header, paragraph, and
character string. ABAP/4 program.
15. What is ALV programming in ABAP?
When is this grid used in ABAP?- ALV is Application List viewer. Sap provides a set of
ALV (ABAP LIST VIEWER) function modules which can be put into use to embellish
the output of a report. This set of ALV functions is used to enhance the readability and
functionality of any report output. Cases arise in sap when the output of a report contains
columns extending more than 255 characters in length. In such cases, this set of ALV
functions can help choose selected columns and arrange the different columns from a
report output and also save different variants for report display. This is a very efficient
tool for dynamically sorting and arranging the columns from a report output. The report
output can contain up to 90 columns in the display with the wide array of display options.
16. What are the events in ABAP/4 language?
Initialization, At selection-screen, Start-of-selection, end-of-selection, top-of-page, endof-page, At line-selection, At user-command, At PF, Get, At New, At LAST, AT END,
AT FIRST.
17. What is CTS and what do you know about it?
The Change and Transport System (CTS) is a tool that helps you to organize
development projects in the ABAP Workbench and in Customizing, and then transport
the changes between the SAP Systems and clients in your system landscape. This
documentation provides you with an overview of how to manage changes with the CTS
and essential information on setting up your system and client landscape and deciding on
a transport strategy. Read and follow this documentation when planning your
development project.
18. What are logical databases?

Interview Questions on SAP

www.mallepoolu.com
What are the advantages/ dis-advantages of logical databases?- To read data from a
database tables we use logical database. A logical database provides read-only access to a
group of related tables to an ABAP/4 program. Advantages: i)check functions which
check that user input is complete, correct,and plausible. ii)Meaningful data selection.
iii)central authorization checks for database accesses. iv)good read access performance
while retaining the hierarchical data view determined by the application logic. dis
advantages: i)If you donot specify a logical database in the program attributes,the GET
events never occur. ii)There is no ENDGET command,so the code block associated with
an event ends with the next event statement (such as another GET or an END-OFSELECTION).
19. What is a batch input session?
BATCH INPUT SESSION is an intermediate step between internal table and database
table. Data along with the action is stored in session ie data for screen fields, to which
screen it is passed, program name behind it, and how next screen is processed.
20. How to upload data using CATT ?
These are the steps to be followed to Upload data through CATT: Creation of the CATT
test case & recording the sample data input. Download of the source file template.
Modification of the source file. Upload of the data from the source file.
21. What is Smart Forms?
Smart Forms allows you to create forms using a graphical design tool with robust
functionality, color, and more. Additionally, all new forms developed at SAP will be
created with the new Smart Form solution.
22. How can I make a differentiation between dependent and independent data?
Client dependent or independent transfer requirements include client specific or cross
client objects in the change requests. Workbench objects like SAPscripts are client
specific, some entries in customizing are client independent. If you display the object list
for one change request, and then for each object the object attributes, you will find the
flag client specific. If one object in the task list has this flag on, then that transport will be
client dependent.
23. What is the difference between macro and subroutine?
Macros can only be used in the program the are defined in and only after the definition
are expanded at compilation / generation. Subroutines (FORM) can be called from both
the program the are defined in and other programs . A MACRO is more or less an
abbreviation for some lines of code that are used more than once or twice. A FORM is a
local subroutine (which can be called external). A FUNCTION is (more or less) a
subroutine that is called external. Since debugging a MACRO is not really possible,

Interview Questions on SAP

www.mallepoolu.com
prevent the use of them (Ive never used them, but seen them in action). If the subroutine
is used only local (called internal) use a FORM. If the subroutine is called external (used
by more than one program) use a FUNCTION.
ABAP Technical Interview Questions:
1. What is the typical structure of an ABAP program?
2. What are field symbols and field groups.? Have you used "component idx of structure"
clause with field groups?
3. What should be the approach for writing a BDC program?
4. What is a batch input session?
5. What is the alternative to batch input session?
6. A situation: An ABAP program creates a batch input session. We need to submit the
program and the batch session in background. How to do it?
7. What is the difference between a pool table and a transparent table and how they are
stored at the database level?
8. What are the problems in processing batch input sessions? How is batch input process
different from processing on line?
9. What do you define in the domain and data element?
10. What are the different types of data dictionary objects?
11. How many types of tables exist and what are they in data dictionary?
12. What is the step-by-step process to create a table in data dictionary?
13. Can a transparent table exist in data dictionary but not in the database physically?
14. What are the domains and data elements?
15. Can you create a table with fields not referring to data elements?
16. What is the advantage of structures? How do you use them in the ABAP programs?
17. What does an extract statement do in the ABAP program?
18. What is a collect statement? How is it different from append?
19. What is open sql vs native sql?
20. What does an EXEC SQL stmt do in ABAP? What is the disadvantage of using it?
21. What is the meaning of ABAP editor integrated with ABAP data dictionary?
22. What are the events in ABAP language?
23. What is an interactive report? What is the obvious diff of such report compared with
classical type reports?
24. What is a drill down report?
25. How do you write a function module in SAP? Describe.
26. What are the exceptions in function module?
27. What is a function group?
28. How are the date abd time field values stored in SAP?
29. What are the fields in a BDC_Tab Table?
30. Name a few data dictionary objects?
31. What happens when a table is activated in DD?
32. What is a check table and what is a value table?
33. What are match codes? Describe?
34. What transactions do you use for data analysis?

Interview Questions on SAP

www.mallepoolu.com
35. What is table maintenance generator?
36. What are ranges? What are number ranges?
37. What are select options and what is the diff from parameters?
38. How do you validate the selection criteria of a report? And how do you display initial
values in a selection screen?
39. What are selection texts?
40. What is CTS and what do you know about it?
41. When a program is created and need to be transported to prodn does selection texts
always go with it? if not how do you make sure? Can you change the CTS entries? How
do you do it?
42. What is the client concept in SAP? What is the meaning of client independent?
43. Are programs client dependent?
44. Name a few system global variables you can use in ABAP programs?
45. What are internal tables? How do you get the number of lines in an internal table?
How to use a specific number occurs statement?
46. How do you take care of performance issues in your ABAP programs?
47. What are datasets?
48. How to find the return code of a stmt in ABAP programs?
49. What are interface/conversion programs in SAP?
50. Have you used SAP supplied programs to load master data?
51. What are the techniques involved in using SAP supplied programs? Do you prefer to
write your own programs to load master data? Why?
52. What are logical databases? What are the advantages/disadvantages of logical
databases?

FAQ in SAP Business Information Warehouse (a part SAP Netweaver)


1. What is table partition?
2. What are the options available in transfer rule and when ABAP code is recquired
during the transfer rule what important variables you can use?
3. How would you optimize the dimensions?
4. What are the conversion routines for units and currencies in the update rule?
5. Can you make an infoobject as info provider and why?
6. What are the steps to unload non cumulative cubes?
7. Give step to step approach to archiving cubex.
8. What are the load process and post processing.
9. What are the data target administration task
10. What are the parallel process that could have locking problems
11. How would you convert a info package group into a process chain?
12. How do you transoform Open Hub Data?
13. What are the data loading tuning one can do?
14. What is ODS?
15. What is the use of BW Statistics?
16. What are the options when definging aggregates?
17. How will you debug errors with SAP GUI (like Active X error etc)

Interview Questions on SAP

www.mallepoolu.com
18. When you write user exit for variables what does I_Step do?
19. How do you replace a query result from a master query to a child query?
20. How do you define exception reporting in the background?
21. What kind of tools are available to monitor the overall Query Performance?
22. What can I do if the database proportion is high for all queries?
23. How to call a BW Query from an ABAP program?
24. What are the extractor types?
25. What are the steps to extract data from R/3?
26. What are the steps to configure third party (BAPI) tools?
27. What are the delta options available when you load from flat file?
28. What are the table filled when you select ABAP?
29. What are the steps to create classes in SAP-BW
30. How do you convert from LIS to LO extraction?
31. How do you keep your self-updated on major SAP Develppments, Give an analytical
example.

Interview Questions on SAP

www.mallepoolu.com
Software QA and Testing Frequently-Asked-Questions
1.What is 'Software Quality Assurance'?
Software QA involves the entire software development PROCESS - monitoring and
improving the process, making sure that any agreed- upon standards and procedures are
followed, and ensuring that problems are found and dealt with. It is oriented to
'prevention'.
2.What is 'Software Testing'?
Testing involves operation of a system or application under controlled conditions and
evaluating the results (eg, 'if the user is in interface A of the application while using
hardware B, and does C, then D should happen'). The controlled conditions should
include both normal and abnormal conditions. Testing should intentionally attempt to
make things go wrong to determine if things happen when they shouldn't or things don't
happen when they should. It is oriented to 'detection'.
3.What are some recent major computer system failures caused by software bugs?
In April of 2003 it was announced that the largest student loan company in the U.S. made
a software error in calculating the monthly payments on 800,000 loans. Although
borrowers were to be notified of an increase in their required payments, the company will
still reportedly lose $8 million in interest. The error was uncovered when borrowers
began reporting inconsistencies in their bills.
News reports in February of 2003 revealed that the U.S. Treasury Department mailed
50,000 Social Security checks without any beneficiary names. A spokesperson indicated
that the missing names were due to an error in a software change. Replacement checks
were subsequently mailed out with the problem corrected, and recipients were then able
to cash their Social Security checks.
In March of 2002 it was reported that software bugs in Britain's national tax system
resulted in more than 100,000
erroneous tax overcharges. The problem was partly attibuted to the difficulty of testing
the integration of multiple systems.
A newspaper columnist reported in July 2001 that a serious flaw was found in off-theshelf software that had long been used in systems for tracking certain U.S. nuclear
materials. The same software had been recently donated to another country to be used in
tracking their own nuclear materials, and it was not until scientists in that country
discovered the problem, and shared the information, that U.S. officials became aware of
the problems. According to newspaper stories in mid-2001, a major systems development
contractor was fired and sued over problems with a large retirement plan management
system. According to the reports, the client claimed that system deliveries were late, the
software had excessive defects, and it caused other systems to crash.

Interview Questions on Testing Tools

www.mallepoolu.com
In January of 2001 newspapers reported that a major European railroad was hit by the
aftereffects of the Y2K bug. The company found that many of their newer trains would
not run due to their inability to recognize the date '31/12/2000'; the trains were started by
altering the control system's date settings.News reports in September of 2000 told of a
software vendor settling a lawsuit with a large mortgage lender; the vendor had
reportedly delivered an online mortgage processing system that did not meet
specifications, was delivered late, and didn't work.
In early 2000, major problems were reported with a new computer system in a large
suburban U.S. public school district with 100,000+ students; problems included 10,000
erroneous report cards and students left stranded by failed class registration systems; the
district's CIO was fired. The school district decided to reinstate it's original 25-year old
system for at least a year until the bugs were worked out of the new system by the
software vendors.
In October of 1999 the $125 million NASA Mars Climate Orbiter spacecraft was
believed to be lost in space due to a simple data conversion error. It was determined that
spacecraft software used certain data in English units that should have been in metric
units. Among other tasks, the orbiter was to serve as a communications relay for the Mars
Polar Lander mission, which failed for unknown reasons in December 1999. Several
investigating panels were convened to determine the process failures that allowed the
error to go undetected.
Bugs in software supporting a large commercial high-speed data network affected 70,000
business customers over a period of 8 days in August of 1999. Among those affected was
the electronic trading system of the largest U.S. futures exchange, which was shut down
for most of a week as a result of the outages.
In April of 1999 a software bug caused the failure of a $1.2 billion military satellite
launch, the costliest unmanned accident in the history of Cape Canaveral launches. The
failure was the latest in a string of launch failures, triggering a complete military and
industry review of U.S. space launch programs, including software integration and testing
processes. Congressional oversight hearings were requested.
A small town in Illinois received an unusually large monthly electric bill of $7 million in
March of 1999. This was about
700 times larger than its normal bill. It turned out to be due to bugs in new software that
had been purchased by the local power company to deal with Y2K software issues.
4.Why is it often hard for management to get serious about quality assurance?
Solving problems is a high-visibility process; preventing problems is low-visibility. This
is illustrated by an old parable:
In ancient China there was a family of healers, one of whom was known throughout the
land and employed as a physician to a great lord. The physician was asked which of his
family was the most skillful healer. He replied,"I tend to the sick and dying with drastic

Interview Questions on Testing Tools

www.mallepoolu.com
and dramatic treatments, and on occasion someone is cured and my name gets out among
the lords." "My elder brother cures sickness when it just begins to take root, and his skills
are known among the local peasants and neighbors." "My eldest brother is able to sense
the spirit of sickness and eradicate it before it takes form. His name is unknown outside
our home."
5.Why does software have bugs?
Miscommunication or no communication - as to specifics of what an application
should or shouldn't do (the application's requirements).software complexity - the
complexity of current software applications can be difficult to comprehend for anyone
without experience in modern-day software development. Windows-type interfaces,
client-server and distributed applications, data communications, enormous relational
databases, and sheer size of
applications have all contributed to the exponential growth in software/system
complexity. And the use of object-oriented techniques can complicate instead of simplify
a project unless it is well-engineered.
Programming errors - programmers, like anyone else, can make mistakes.
Changing requirements - the customer may not understand the effects of changes, or
may understand and request them anyway - redesign, rescheduling of engineers, effects
on other projects, work already completed that may have to be redone or thrown out,
hardware requirements that may be affected, etc. If there are many minor changes or any
major changes, known and unknown dependencies among parts of the project are likely
to interact and cause
problems, and the complexity of keeping track of changes may result in errors.
Enthusiasm of engineering staff may
be affected. In some fast-changing business environments, continuously modified
requirements may be a fact of life. In this case, management must understand the
resulting risks, and QA and test engineers must adapt and plan for continuous extensive
testing to keep the inevitable bugs from running out of control
Time pressures - scheduling of software projects is difficult at best, often requiring a lot
of guesswork. When deadlines loom and the crunch comes, mistakes will be made.
Egos - people prefer to say things like:
'no problem'
'piece of cake'
'I can whip that out in a few hours'
'it should be easy to update that old code'
Instead of:

Interview Questions on Testing Tools

www.mallepoolu.com
'that adds a lot of complexity and we could end up making a lot of mistakes'
'we have no idea if we can do that; we'll wing it'
'I can't estimate how long it will take, until I
take a close look at it'
'we can't figure out what that old spaghetti code did in the first place'
If there are too many unrealistic 'no problem's', the result is bugs.
Poorly documented code - it's tough to maintain and modify code that is badly written
or poorly documented; the result is bugs. In many organizations management provides no
incentive for programmers to document their code or write clear, understandable code. In
fact, it's usually the opposite: they get points mostly for quickly turning out code, and
there's job security if nobody else can understand it ('if it was hard to write, it should be
hard to read').
Software development tools - visual tools, class libraries, compilers, scripting tools, etc.
often introduce their own bugs or are poorly documented, resulting in added bugs .
6.How can new Software QA processes be introduced in an existing organization?
A lot depends on the size of the organization and the risks involved. For large
organizations with high-risk (in terms of lives or property) projects, serious management
buy-in is required and a formalized QA process is necessary.
Where the risk is lower, management and organizational buy-in and QA implementation
may be a slower, step-at-a-time process. QA processes should be balanced with
productivity so as to keep bureaucracy from getting out of hand.
For small groups or projects, a more ad-hoc process may be appropriate, depending on
the type of customers and projects. A lot will depend on team leads or managers,
feedback to developers, and ensuring adequate communications among customers,
managers, developers, and testers.
In all cases the most value for effort will be in requirements management processes, with
a goal of clear, complete, testable requirement specifications or expectations .
7.What is verification? validation?
Verification typically involves reviews and meetings to evaluate documents, plans, code,
requirements, and specifications. This can be done with checklists, issues lists,
walkthroughs, and inspection meetings. Validation typically involves actual testing and
takes place after verifications are completed. The term 'IV & V' refers
to Independent Verification and Validation.
8.What is a 'walkthrough'?

Interview Questions on Testing Tools

www.mallepoolu.com
A 'walkthrough' is an informal meeting for evaluation or informational purposes. Little or
no preparation is usually
required .
9.What's an 'inspection'?
An inspection is more formalized than a 'walkthrough', typically with 3-8 people
including a moderator, reader, and a recorder to take notes. The subject of the inspection
is typically a document such as a requirements spec or a test plan, and the purpose is to
find problems and see what's missing, not to fix anything. Attendees should prepare for
this type of meeting by reading thru The document; most problems will be found during
this preparation. The result of the inspection meeting should be a written report.
Thorough preparation for inspections is difficult, painstaking work,
but is one of the most cost effective methods of ensuring quality. Employees who are
most skilled at inspections are like the 'eldest brother' in the parable in 'Why is it often
hard for management to get serious about quality assurance?'. Their skill may have low
visibility but they are extremely valuable to any software development organization,
since bug prevention is far more cost- effective than bug detection .
10. What kinds of testing should be considered?
Black box testing - not based on any knowledge of internal design or code. Tests are
based on requirements and functionality.
White box testing - based on knowledge of the internal logic of an application's code.
Tests are based on coverage of code statements, branches, paths, conditions.
Unit testing - the most 'micro' scale of testing; to test particular functions or code
modules. Typically done by the
programmer and not by testers, as it requires detailed knowledge of the internal program
design and code. Not always easily done unless the application has a well-designed
architecture with tight code; may require developing test driver modules or test harnesses.
Incremental integration testing - continuous testing of an application as new
functionality is added; requires that various aspects of an application's functionality be
independent enough to work separately before all parts of the program are completed, or
that test drivers be developed as needed; done by programmers or by testers.
Integration testing - testing of combined parts of an application to determine if they
function together correctly. The
'parts' can be code modules, individual applications, client and server applications on a
network, etc. This type of testing is especially relevant to client/server and distributed
systems.
Functional testing - black-box type testing geared to functional requirements of an
application; this type of testing should be done by testers. This doesn't mean that the

Interview Questions on Testing Tools

www.mallepoolu.com
programmers shouldn't check that their code works before releasing it (which of course
applies to any stage of testing.)
System testing - black-box type testing that is based on overall requirements
specifications; covers all combined parts of a system.
End-to-end testing - similar to system testing; the 'macro' end of the test scale; involves
testing of a complete application environment in a situation that mimics real-world use,
such as interacting with a database, using network communications, or interacting with
other hardware, applications, or systems if appropriate.
Sanity testing - typically an initial testing effort to determine if a new software version is
performing well enough to
accept it for a major testing effort. For example, if the new software is crashing systems
every 5 minutes, bogging down systems to a crawl, or destroying databases, the software
may not be in a 'sane' enough condition to warrant further testing in its current state.
Regression testing - re-testing after fixes or modifications of the software or its
environment. It can be difficult to determine how much re-testing is needed, especially
near the end of the development cycle. Automated testing tools can be especially useful
for this type of testing.
Acceptance testing - final testing based on specifications of the end-user or customer, or
based on use by end-users/customers over some limited period of time.Load testing testing an application under heavy loads, such
as testing of a web site under a range of loads to determine at what point the system's
response time degrades or fails.
Stress testing - term often used interchangeably with 'load' and 'performance' testing.
Also used to describe such tests as system functional testing while under unusually heavy
loads, heavy repetition of certain actions or inputs, input of large numerical values, large
complex queries to a database system, etc.
Performance testing - term often used interchangeably with 'stress' and 'load' testing.
Ideally 'performance' testing (and any other 'type' of testing) is defined in requirements
documentation or QA or Test Plans.
Usability testing - testing for 'user-friendliness'. Clearly this is subjective, and will
depend on the targeted end-user or customer. User interviews, surveys, video recording
of user sessions, and other techniques can be used. Programmers and testers are usually
not appropriate as usability testers.
Install/uninstall testing - testing of full, partial, or upgrade install/uninstall processes.
Recovery testing - testing how well a system recovers from crashes, hardware failures,
or other catastrophic problems.

Interview Questions on Testing Tools

www.mallepoolu.com
Security testing - testing how well the system protects against unauthorized internal or
external access, willful damage, etc; may require sophisticated testing techniques.
Compatability testing - testing how well software performs in a particular
hardware/software/operating system/network/etc. environment.
Exploratory testing - often taken to mean a creative, informal software test that is not
based on formal test plans or test cases; testers may be learning the software as they test
it.
Ad-hoc testing - similar to exploratory testing, but often taken to mean that the testers
have significant understanding of the software before testing it.
User acceptance testing - determining if software is satisfactory to an end-user or
customer.
Comparison testing - comparing software weaknesses and strengths to competing
products.
Alpha testing - testing of an application when development is nearing completion; minor
design changes may still be made as a result of such testing. Typically done by end-users
or others, not by programmers or testers.
Beta testing - testing when development and testing are essentially completed and final
bugs and problems need to be found before final release. Typically done by end-users or
others, not by programmers or testers.
Mutation testing - a method for determining if a set of test data or test cases is useful, by
deliberately introducing various code changes ('bugs') and retesting with the original test
data/cases to determine if the 'bugs' are detected. Proper implementation requires large
computational resources .
11.What are 5 common problems in the software development process?
Poor requirements - if requirements are unclear, incomplete, too general, or not testable,
there will be problems.
Unrealistic schedule - if too much work is crammed in too little time, problems are
inevitable.
Inadequate testing - no one will know whether or not the program is any good until the
customer complains or systems crash.
Featuritis - requests to pile on new features after development is underway; extremely
common.

Interview Questions on Testing Tools

www.mallepoolu.com
Miscommunication - if developers don't know what's needed or customer's have
erroneous expectations, problems are guaranteed .
12.What are 5 common solutions to software development problems?
Solid requirements - clear, complete, detailed, cohesive, attainable, testable
requirements that are agreed to by all
players. Use prototypes to help nail down requirements.Realistic schedules - allow
adequate time for planning, design,
testing, bug fixing, re-testing, changes, and documentation; personnel should be able to
complete the project without burning out.
Adequate testing - start testing early on, re-test after fixes or changes, plan for adequate
time for testing and bug-fixing.
Stick to initial requirements as much as possible - be prepared to defend against
changes and additions once development has begun, and be prepared to explain
consequences. If changes are necessary, they should be adequately reflected in related
schedule changes. If possible, use rapid prototyping during the design phase so that
customers can see what to expect. This will provide them a higher comfort level with
their requirements decisions and minimize changes later on .
Communication - require walkthroughs and inspections when appropriate; make
extensive use of group communication tools - e-mail, groupware, networked bug-tracking
tools and change management tools, intranet capabilities, etc.; insure that documentation
is available and up-to- date - preferably electronic, not paper; promote teamwork and
cooperation; use protoypes early on so that customers' expectations are clarified .
13.What is software 'quality'?
Quality software is reasonably bug-free, delivered on time and within budget, meets
requirements and/or expectations, and is maintainable. However, quality is obviously a
subjective term. It will depend on who the 'customer' is and their overall influence in the
scheme of things. A wide-angle view of the 'customers' of a software development
project might include end-users, customer acceptance testers, customer contract officers,
customer management, the development organization's
management/accountants/testers/salespeople, future software maintenance engineers,
stockholders, magazine columnists, etc. Each type of 'customer' will have their own slant
on 'quality' - the accounting department might define quality in terms of profits while an
end-user might define quality as user-friendly and bug-free. (See the Bookstore section's
'Software QA' category for useful books with more information.)
14.What is 'good code'?
'Good code' is code that works, is bug free, and is readable and maintainable. Some
organizations have coding 'standards' that all developers are supposed to adhere to, but

Interview Questions on Testing Tools

www.mallepoolu.com
everyone has different ideas about what's best, or what is too many or too few rules.
There are also various theories and metrics, such as McCabe Complexity metrics. It
should be kept in mind that excessive use of standards and rules can stifle productivity
and creativity. 'Peer reviews', 'buddy checks' code analysis tools, etc. can be used to
check for problems and enforce standards. For C and C++ coding, here are some typical
ideas to consider in setting rules/standards; these may or may not apply to a particular
situation:
Minimize or eliminate use of global variables.
Use descriptive function and method names - use both upper and lower case, avoid
abbreviations, use as many characters as necessary to be adequately descriptive (use of
more than 20 characters is not out of line); be consistent in naming conventions.
Use descriptive variable names - use both upper and lower case, avoid abbreviations, use
as many characters as necessary to be adequately descriptive (use of more than 20
characters is not out of line); be consistent in naming conventions.
Function and method sizes should be minimized; less than 100 lines of code is good, less
than 50 lines is preferable.
Function descriptions should be clearly spelled out in comments preceding a function's
code.
Organize code for readability.
Use whitespace generously - vertically and horizontally
Each line of code should contain 70 characters max.
One code statement per line.
Coding style should be consistent throught a program (eg, use of brackets, indentations,
naming conventions, etc.)
In adding comments, err on the side of too many rather than too few comments; a
common rule of thumb is that there should be at least as many lines of comments
(including header blocks) as lines of code.
No matter how small, an application should include documentaion of the overall program
function and flow (even a few paragraphs is better than nothing); or if possible a separate
flow chart and detailed program documentation.
Make extensive use of error handling procedures and status and error logging.

Interview Questions on Testing Tools

www.mallepoolu.com
For C++, to minimize complexity and increase maintainability, avoid too many levels of
inheritance in class heirarchies
(relative to the size and complexity of the application). Minimize use of multiple
inheritance, and minimize use of operator overloading (note that the Java programming
language eliminates multiple inheritance and operator overloading.)
Cor C++, keep class methods small, less than 50 lines of code per method is
preferable.For C++, make liberal use of exception handlers
15. What is 'good design'?
'Design' could refer to many things, but often refers to 'functional design' or 'internal
design'. Good internal design is
indicated by software code whose overall structure is clear, understandable, easily
modifiable, and maintainable; is robust with sufficient error-handling and status logging
capability; and works correctly when implemented. Good functional design is indicated
by an application whose functionality can be traced back to customer and end-user
requirements. (See further discussion of functional and internal design in 'What's the big
deal about requirements?' in FAQ #2.) For programs that have a user interface, it's often a
good idea to assume that the end user will have little computer knowledge and may not
read a user manual or even the on-line help; some common rules-of-thumb
include:
The program should act in a way that least surprises the user
It should always be evident to the user what can be done next and how to exit
The program shouldn't let the users do something stupid without warning them.
16. What is SEI? CMM? ISO? IEEE? ANSI? Will it help?
SEI = 'Software Engineering Institute' at Carnegie-Mellon University; initiated by the
U.S. Defense Department to help improve software development processes.
CMM = 'Capability Maturity Model', developed by the SEI. It's a model of 5 levels of
organizational 'maturity' that determine effectiveness in delivering quality software. It is
geared to large organizations such as large U.S. Defense Department contractors.
However, many of the QA processes involved are appropriate to any organization, and if
reasonably applied can be helpful. Organizations can receive CMM ratings by
undergoing assessments by qualified auditors.
Level 1 - characterized by chaos, periodic panics, and eroic efforts required by
individuals to successfully complete projects. Few if any processes in place; successes
may not be repeatable.

Interview Questions on Testing Tools

www.mallepoolu.com
Level 2 - software project tracking, requirements management, realistic planning, and
configuration management processes are in place; successful practices can be repeated.
Level 3 - standard software development and maintenance rocesses are integrated
throughout an organization; a Software Engineering Process Group is is in place to
oversee software processes, and training programs are used to ensure understanding and
compliance.
Level 4 - metrics are used to track productivity, processes, and products. Project
performance is predictable, and quality is consistently high.
Level 5 - the focus is on continouous process improvement. The impact of new processes
and technologies can be predicted and effectively implemented when required.
Perspective on CMM ratings: During 1997-2001, 1018 organizations were assessed. Of
those, 27% were rated at Level 1, 39% at 2, 23% at 3, 6% at 4, and 5% at 5. (For ratings
during the period 1992-96, 62% were at Level 1, 23% at 2, 13% at 3, 2% at 4, and 0.4%
at 5.) The median size of organizations was 100 software engineering/maintenance
personnel; 32% of organizations were U.S. federal contractors or agencies. For those
rated at Level 1, the most problematical key process area was in Software Quality
Assurance.ISO = 'International Organisation for Standardization' - The ISO 9001:2000
standard (which replaces the previous standard of 1994)
concerns quality systems that are assessed by outside auditors, and it applies to many
kinds of production and manufacturing organizations, not just software. It covers
documentation, design, development, production, testing, installation, servicing, and
other processes. The full set of standards consists of: (a)Q9001-2000 - Quality
Management Systems: Requirements; (b)Q9000-2000 - Quality Management Systems:
Fundamentals and
Vocabulary; (c)Q9004-2000 - Quality Management Systems: Guidelines for Performance
Improvements. To be ISO 9001 certified, a third-party auditor assesses an organization,
and certification is typically good for about 3 years, after which a complete reassessment
is required. Note that ISO certification does not necessarily indicate quality products - it
indicates only that documented processes are followed. Also see http://www.iso.ch/ for
the latest information. In the U.S. the standards can be purchased via the ASQ web site at
http://e- standards.asq.org/
IEEE = 'Institute of Electrical and Electronics Engineers' - among other things, creates
standards such as 'IEEE Standard for Software Test Documentation' (IEEE/ANSI
Standard 829), 'IEEE Standard of Software Unit Testing (IEEE/ANSI Standard 1008),
'IEEE Standard for Software Quality Assurance Plans' (IEEE/ANSI Standard 730), and
others.
ANSI = 'American National Standards Institute', the primary industrial standards body in
the U.S.; publishes some software- related standards in conjunction with the IEEE and
ASQ (American Society for Quality).

Interview Questions on Testing Tools

www.mallepoolu.com
Other software development process assessment methods besides CMM and ISO 9000
include SPICE, Trillium, TickIT. and Bootstrap. See the 'Other Resources' section for
further information available on the web.
16.What is the 'software life cycle'?
The life cycle begins when an application is first conceived and ends when it is no longer
in use. It includes aspects such as initial concept, requirements analysis, functional
design, internal design, documentation planning, test planning, coding, document
preparation, integration, testing, maintenance, updates, retesting, phase-out, and other
aspects. (See the Bookstore section's 'Software QA', 'Software Engineering', and 'Project
Management' categories for
useful books with more information.)
17. Will automated testing tools make testing easier?
Possibly. For small projects, the time needed to learn and implement them may not be
worth it. For larger projects, or on- going long-term projects they can be valuable.
A common type of automated tool is the 'record/playback' type. For example, a tester
could click through all combinations of menu choices, dialog box choices, buttons, etc. in
an application GUI and have them 'recorded' and the results logged by a tool. The
'recording' is typically in the form of text based on a scripting language that is
interpretable by the testing tool. If new buttons are added, or some underlying code in the
application is changed, etc. the application can then be retested by just 'playing back' the
'recorded' actions, and comparing the logging
results to check effects of the changes. The problem with such tools is that if there are
continual changes to the system being tested, the 'recordings' may have to be changed so
much that it becomes very time-consuming to continuously update the scripts.
Additionally, interpretation of results (screens, data, logs, etc.) can be a difficult task.
Note that there are record/playback tools for text-based interfaces also, and for all types
of platforms.
Other automated tools can include :
Code analyzers - monitor code complexity, adherence to standards, etc .
Coverage analyzers - these tools check which parts of the code have been exercised by a
test, and may be oriented to code statement coverage,condition coverage, path coverage,
etc.Memory analyzers - such as bounds-checkers and leak detectors .
Load/performance test tools - for testing client/server and web applications under
various load levels.
Web test tools - to check that links are valid, HTML code usage is correct, client-side
and server-side programs work, a web site's interactions are secure.

Interview Questions on Testing Tools

www.mallepoolu.com
Other tools - for test case management, documentation management, bug reporting, and
configuration management.
18. What makes a good test engineer?
A good test engineer has a 'test to break' attitude, an ability to take the point of view of
the customer, a strong desire for quality, and an attention to detail. Tact and diplomacy
are useful in maintaining a cooperative relationship with developers, and an ability to
communicate with both technical (developers) and non-technical (customers,
management) people is useful. Previous software development experience can be helpful
as it provides a deeper understanding of the software development process, gives the
tester an appreciation for the developers' point of view, and reduce the learning curve in
automated test tool programming. Judgement skills are needed to assess high-risk areas
of an application on which to focus testing efforts when time is limited.
19. What makes a good Software QA engineer?
The same qualities a good tester has are useful for a QA engineer. Additionally, they
must be able to understand the entire software development process and how it can fit
into the business approach and goals of the organization. Communication skills and the
ability to understand various sides of issues are important. In organizations in the early
stages of implementing QA processes, patience and diplomacy are especially needed. An
ability to find problems as well as to see 'what's missing' is important for inspections and
reviews.
20. What makes a good QA or Test manager?
A good QA, test, or QA/Test(combined) manager should:
> Be familiar with the software development process
>Be able to maintain enthusiasm of their team and promote a positive atmosphere,
despite what is a somewhat 'negative' process (e.g., looking for or preventing problems)
> Be able to promote teamwork to increase productivity
>Be able to promote cooperation between software, test, and QA engineers
>Have the diplomatic skills needed to promote improvements in QA processes
> Have the ability to withstand pressures and say 'no' to other managers when quality is
insufficient or QA processes are not being adhered to
>Have people judgement skills for hiring and keeping skilled personnel

Interview Questions on Testing Tools

www.mallepoolu.com
>Be able to communicate with technical and non-technical people, engineers, managers,
and customers.
>Be able to run meetings and keep them focused
21. What's the role of documentation in QA?
Critical. (Note that documentation can be electronic, not necessarily paper.) QA practices
should be documented such that they are repeatable. Specifications, designs, business
rules, inspection reports, configurations, code changes, test plans, test cases, bug reports,
user manuals, etc. should all be documented. There should ideally be a system for easily
finding and obtaining documents and determining what documentation will have a
particular piece of information. Change management for documentation should be used if
possible.
22. What's the big deal about 'requirements'?
One of the most reliable methods of insuring problems, or failure, in a complex software
project is to have poorly
documented requirements specifications. Requirements are the details describing an
application's externally-perceived functionality and properties. Requirements should be
clear, complete, reasonably detailed, cohesive, attainable, and testable. A non-testable
requirement would be, for example, 'user-friendly' (too subjective). A testable
requirement would be something like 'the user must enter their previously-assigned
password to access the application'. etermining
and organizing requirements details in a useful and efficient way can be a difficult effort;
different methods are available
depending on the particular project. Many books are available that describe various
approaches to
Care should be taken to involve ALL of a project's significant 'customers' in the
requirements process. 'Customers' could be in-house personnel or out, and could include
end-users, customer acceptance testers, customer contract officers, customer
management, future software maintenance engineers, salespeople, etc. Anyone who could
later derail the project if their expectations aren't met should be included if possible.
Organizations vary considerably in their handling of requirements specifications. Ideally,
the requirements are spelled out in a document with statements such as 'The product
shall.....'. 'Design' specifications should not be confused with 'requirements'; design
specifications should be traceable back to the requirements.
In some organizations requirements may end up in high level project plans, functional
specification documents, in design documents, or in other documents at various levels of
detail. No matter what they are called, some type of documentation with detailed
requirements will be needed by testers in order to properly plan and execute tests.

Interview Questions on Testing Tools

www.mallepoolu.com
Without such documentation, there will be no clear-cut way to determine if a software
application is performing correctly.
23.What steps are needed to develop and run software tests?
The following are some of the steps to consider:
Obtain requirements, functional design, and internal design specifications and other
necessary documents
Obtain budget and schedule requirements
Determine project-related personnel and their responsibilities, reporting requirements,
required standards and processes (such as release processes, change processes, etc.)
Identify application's higher-risk aspects, set priorities, and determine scope and
limitations of tests
Determine test approaches and methods - unit, integration, functional, system, load,
usability tests, etc.
Determine test environment requirements (hardware, software communications, etc.)
Determine testware requirements (record/playback tools, coverage analyzers, test
tracking, problem/bug tracking, etc.)
Determine test input data requirements
Identify tasks, those responsible for tasks, and labor requirements
Set schedule estimates, timelines, milestones
Determine input equivalence classes, boundary value analyses, error classes
Prepare test plan document and have needed reviews/approvals
Write test cases
Have needed reviews/inspections/approvals of test cases
Prepare test environment and testware, obtain needed user manuals/reference
documents/configuration guides/installation guides, set up test tracking processes, set up
logging and archiving processes, set up or obtain test input data Obtain and install
software releases
Perform tests

Interview Questions on Testing Tools

www.mallepoolu.com
Evaluate and report results
Track problems/bugs and fixes
Retest as needed
Maintain and update test plans, test cases, test environment, and testware through life
cycle
23. What's a 'test plan'?
A software project test plan is a document that describes the objectives, scope, approach,
and focus of a software testing effort. The process of preparing a test plan is a useful way
to think through the efforts needed to validate the acceptability of a software product. The
completed document will help people outside the test group understand the 'why' and
'how' of product validation. It should be thorough enough to be useful but not so thorough
that no one outside the test group will read it. The following are some of the items that
might be included in a test plan, depending on the particular project:
Title
Identification of software including version/release numbers
Revision history of document including authors, dates, approvals
Table of Contents
Purpose of document, intended audience
Objective of testing effort
Software product overview
Relevant related document list, such as requirements, design documents, other test plans,
etc.
Relevant standards or legal requirements
Traceability requirements
Relevant naming conventions and identifier conventions
Overall software project organization and personnel/contact- info/responsibilties
Test organization and personnel/contact-info/responsibilities

Interview Questions on Testing Tools

www.mallepoolu.com
Assumptions and dependencies
Project risk analysis
Testing priorities and focus
Scope and limitations of testing
Test outline - a decomposition of the test approach by test type, feature, functionality,
process, system, module, etc. as applicable
Outline of data input equivalence classes, boundary value analysis, error classes
Test environment - hardware, operating systems, other required software, data
configurations, interfaces to other systems
Test environment validity analysis - differences between the test and production systems
and their impact on test validity.
Test environment setup and configuration issues Software migration processes Software
CM processes
Test data setup requirements
Database setup requirements
Outline of system-logging/error-logging/other capabilities, and tools such as screen
capture software, that will be used to help describe and report bugs
Discussion of any specialized software or hardware tools that will be used by testers to
help track the cause or source of bugs
Test automation - justification and overview
Test tools to be used, including versions, patches, etc.
Test script/test code maintenance processes and version control
Problem tracking and resolution - tools and processes
Project test metrics to be used
Reporting requirements and testing deliverables
Software entrance and exit criteria
Initial sanity testing period and criteria
Test suspension and restart criteria
Personnel allocation
Personnel pre-training needs
Test site/location
Outside test organizations to be utilized and their purpose, responsibilties, deliverables,
contact persons, and coordination issues

Interview Questions on Testing Tools

www.mallepoolu.com
Relevant proprietary, classified, security, and licensing issues. Open issues
Appendix - glossary, acronyms, etc
24. What's a 'test case'?
A test case is a document that describes an input, action, or event and an expected
response, to determine if a feature of an application is working correctly. A test case
should contain particulars such as test case identifier, test case name, objective, test
conditions/setup, input data requirements, steps, and expected results.
Note that the process of developing test cases can help find problems in the requirements
or design of an application, since it requires completely thinking through the operation of
the application. For this reason, it's useful to prepare test cases early in the development
cycle if possible.
25. What should be done after a bug is found?
The bug needs to be communicated and assigned to developers that can fix it. After the
problem is resolved, fixes should be re-tested, and determinations made regarding
requirements for regression testing to check that fixes didn't create problems elsewhere. If
a problem- tracking system is in place, it should encapsulate these processes. A variety of
commercial problem-tracking/management software tools are available (see the 'Tools'
section for web resources with listings of such tools). The following are items to consider
in the tracking process:
Complete information such that developers can understand the bug, get an idea of it's
severity, and reproduce it if necessary. Bug identifier (number, ID, etc.) Current bug
status (e.g., 'Released for Retest', 'New', etc.) The application name or identifier and
version The function, module, feature, object, screen, etc. where the bug occurred
Environment specifics, system, platform, relevant hardware specifics
Test case name/number/identifier
One-line bug description
Full bug description
Description of steps needed to reproduce the bug if not covered by a test case or if the
developer doesn't have easy access to the test case/test script/test tool
Names and/or descriptions of file/data/messages/etc. used in test
File excerpts/error messages/log file excerpts/screen shots/test tool logs that would be
helpful in finding the cause of the problem Severity estimate (a 5-level range such as 1-5
or 'critical'-to- 'low' is common)
26. Was the bug reproducible?
Tester name
Test date
Bug reporting date

Interview Questions on Testing Tools

www.mallepoolu.com
Name of developer/group/organization the problem is assigned to
Description of problem cause
Description of fix
Code section/file/module/class/method that was fixed
Date of fix
Application version that contains the fix
Tester responsible for retest
Retest date
Retest results
Regression testing requirements
Tester responsible for regression tests
Regression testing results
A reporting or tracking process should enable notification of appropriate personnel at
various stages. For instance, testers need to know when retesting is needed, developers
need to know when bugs are found and how to get the needed information, and
reporting/summary capabilities are needed for managers.
27. What is 'configuration management'?
Configuration management covers the processes used to control, coordinate, and track:
code, requirements, documentation, problems, change requests, designs,
tools/compilers/libraries/patches, changes made to them, and who makes the changes.
(See the 'Tools' section for web resources with listings of configuration management
tools. Also see the Bookstore section's 'Configuration Management' category for useful
books with more information.)
27. What if the software is so buggy it can't really be tested at all?
The best bet in this situation is for the testers to go through the process of reporting
whatever bugs or blocking-type problems initially show up, with the focus being on
critical bugs. Since this type of problem can severely affect schedules, and indicates
deeper problems in the software development process (such as insufficient unit testing or
insufficient integration testing, poor design, improper build or release procedures, etc.)
managers should be notified, and provided with some documentation as evidence of the
problem.
28. How can it be known when to stop testing?
This can be difficult to determine. Many modern software applications are so complex,
and run in such an interdependent environment, that complete testing can never be done.
Common factors in deciding when to stop are:
Deadlines (release deadlines, testing deadlines, etc.) Test cases completed with certain
percentage passed

Interview Questions on Testing Tools

www.mallepoolu.com
Test budget depleted Coverage of code/functionality/requirements reaches a specified
point Bug rate falls below a certain level Beta or alpha testing period ends
29. What if there isn't enough time for thorough testing?
Use risk analysis to determine where testing should be focused. Since it's rarely possible
to test every possible aspect of an application, every possible combination of events,
every dependency, or everything that could go wrong, risk analysis is appropriate to most
software development projects. This requires judgement skills, common sense, and
experience. (If warranted, formal methods are also available.) Considerations can
include:
Which functionality is most important to the project's intended purpose?
Which functionality is most visible to the user?
Which functionality has the largest safety impact?
Which functionality has the largest financial impact on users?
Which aspects of the application are most important to the customer?
Which aspects of the application can be tested early in the development cycle?
Which parts of the code are most complex, and thus most subject to errors?
Which parts of the application were developed in rush or panic mode?
Which aspects of similar/related previous projects caused problems?
Which aspects of similar/related previous projects had large maintenance expenses?
Which parts of the requirements and design are unclear or poorly thought out?
What do the developers think are the highest-risk aspects of the application?
What kinds of problems would cause the worst publicity?
What kinds of problems would cause the most customer service complaints?
What kinds of tests could easily cover multiple functionalities?
Which tests will have the best high-risk-coverage to time- required ratio?
What if the project isn't big enough to justify extensive testing?
Consider the impact of project errors, not the size of the project. However, if extensive
testing is still not justified,
risk analysis is again needed and the same considerations as described previously in
'What if there isn't enough time for thorough testing?' apply.

The tester might then do ad hoc testing, or write up a limited test plan based on the risk
analysis.
30. What can be done if requirements are changing continuously?
A common problem and a major headache.

Interview Questions on Testing Tools

www.mallepoolu.com
Work with the project's stakeholders early on to understand how requirements might
change so that alternate test plans and strategies can be worked out in advance, if
possible.
It's helpful if the application's initial design allows for some adaptability so that later
changes do not require redoing the
application from scratch.
If the code is well-commented and well-documented this makes changes easier for the
developers.
Use rapid prototyping whenever possible to help customers feel sure of their
requirements and minimize changes.
The project's initial schedule should allow for some extra time commensurate with the
possibility of changes.
Try to move new requirements to a 'Phase 2' version of an application, while using the
original requirements for the 'Phase 1' version.
Negotiate to allow only easily-implemented new requirements into the project, while
moving more difficult new requirements into future versions of the application.
Be sure that customers and management understand the scheduling impacts, inherent
risks, and costs of significant requirements changes. Then let management or the
customers (not the developers or testers) decide if the changes are warranted - after all,
that's their job.
Balance the effort put into setting up automated testing with the expected effort required
to re-do them to deal with changes.
Try to design some flexibility into automated test scripts.
Focus initial automated testing on application aspects that are most likely to remain
unchanged.
Devote appropriate effort to risk analysis of changes to minimize regression testing
needs.
Design some flexibility into test cases (this is not easily done; the best bet might be to
minimize the detail in the test cases, or set up only higher-level generic-type test plans)
Focus less on detailed test plans and test cases and more on ad hoc testing (with an
understanding of the added risk that this entails).
31. What if the application has functionality that wasn't in the requirements?

Interview Questions on Testing Tools

www.mallepoolu.com
It may take serious effort to determine if an application has significant unexpected or
hidden functionality, and it would
indicate deeper problems in the software development process. If the functionality isn't
necessary to the purpose of the application, it should be removed, as it may have
unknown impacts or dependencies that were not taken into account by the designer or the
customer. If not removed, design information will be needed to determine added testing
needs or regression testing needs. Management should be made aware of any significant
added risks as a result of the unexpected functionality. If the functionality only effects
areas such as minor improvements in the user interface,
for example, it may not be a significant risk.
32. How can Software QA processes be implemented without stifling productivity?
By implementing QA processes slowly over time, using consensus to reach agreement on
processes, and adjusting and experimenting as an organization grows and matures,
productivity will be improved instead of stifled. Problem prevention will lessen the need
for problem detection, panics and burn-out will decrease, and there will be improved
focus and less wasted effort. At the same time, attempts should be made to keep
processes simple and efficient, minimize paperwork, promote computer-based processes
and automated tracking and reporting, minimize time required in meetings, and promote
training as part of the QA process. However, no one - especially talented technical types likes rules or bureacracy, and in the short run things may slow down a bit. A typical
scenario would be that
more days of planning and development will be needed, but less time will be required for
late- night bug-fixing and calming of irate customers. (See the Bookstore section's
'Software QA', 'Software Engineering',
And 'Project Management' categories for useful books with more information.)
33. What if an organization is growing so fast that fixed QA processes are impossible?
This is a common problem in the software industry, especially in new technology areas.
There is no easy solution in this situation, other than:
Hire good people
Management should 'ruthlessly prioritize' quality issues and maintain focus on the
customer
Everyone in the organization should be clear on what 'quality' means to the customer
34. How does a client/server environment affect testing?
Client/server applications can be quite complex due to the multiple dependencies among
clients, data communications, hardware, and servers. Thus testing requirements can be
extensive. When time is limited (as it usually is) the focus should be on integration and
system testing. Additionally, load/stress/performance testing may be useful in
determining client/server application limitations and capabilities. There are commercial

Interview Questions on Testing Tools

www.mallepoolu.com
tools to assist with such testing. (See the 'Tools' section for web resources with listings
that include these kinds of test tools.)
35. How can World Wide Web sites be tested?
Web sites are essentially client/server applications - with web servers and 'browser'
clients. Consideration should be given to the interactions between html pages, TCP/IP
communications, Internet connections, firewalls, applications that run in web pages (such
as applets, javascript, plug-in applications), and applications that run on the server side
(such as cgi scripts, database interfaces, logging applications, dynamic page generators,
asp, etc.). Additionally, there are a wide variety of servers and browsers, various versions
of each, small but sometimes significant differences between them, variations in
connection speeds, rapidly changing technologies, and multiple standards and protocols.
The end result is that testing for web sites can become a major ongoing effort. Other
considerations might include:
What are the expected loads on the server (e.g., number of hits per unit time?), and what
kind of performance is required under such loads (such as web server response time,
database query response times). What kinds of tools will be needed for performance
testing (such as web load testing tools, other tools already in house that can be adapted,
web robot downloading tools, etc.)?
Who is the target audience? What kind of browsers will they be using? What kind of
connection speeds will they by using? Are they intra- organization (thus with likely high
connection speeds and similar browsers) or Internet-wide (thus with a wide variety of
connection speeds and browser types)?
What kind of performance is expected on the client side (e.g., how fast should pages
appear, how fast should animations, applets, etc. load and run)?
Will down time for server and content maintenance/upgrades be allowed? how much?
What kinds of security (firewalls, encryptions, passwords, etc.) will be required and what
is it expected to do? How can it be tested?

Interview Questions on Testing Tools

Vous aimerez peut-être aussi