Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
WP PLUGINS
WORDPRESS
LINUX
ACADEMIC
INTERNET
PROJECTS
DOWNLOADS
SEARCH
INTECHGRITY
AMALGAMATING LIFE & TECHNOLOGY
#7: Enter and print details of n employees using structures and dynamic memory allocation
If you havent opened Turbo C++ for more than a year and the campus
placements are not very far away then you are at the correct place. The interviewers have a thing for
C programs and theres a great chance you might be bombarded with them on the interview day. Ive
compiled 10 programs that are very common and has been asked numerous times in the interviews.
Most importantly if you practice these programs you get to revise the whole syllabus. I tried to
include as many concepts as possible keeping in mind the difficulty level matched the level of the
interview questions. If you dont get any of these questions in the interview feel free to sue this
websites admin..
01
02
#include
03
04
05
#include
int main(){
int a,b;
printf("Before swapping:\n");
06
07
08
09
scanf("%d",&b);
10
//Sorting using
11
12
13
14
printf("After swapping:\n");
15
16
17
getch();
}
18
Follow up questions:
[learn_more caption= 1. What is XOR gate?]Dont try to give them any other answers because the
answer they are looking for is XOR gate outputs 1 when there are odd number of 1s. [/learn_more]
[learn_more caption= 2. Show some other ways to do the swapping. ] You can add the two
numbers and store it in the first number and then subtract the other number from it to swap the
value. Or you can multiplyand then divide. [/learn_more]
A B C D E D C B A
A B C D C B A
A B C B A
A B A
01
//Printing pattern
02
#include
03
04
05
#include
int main(){
int n,i,j;
printf("Enter no of lines: ");
06
07
08
scanf("%d",&n);
for(i=0;i<n;i++){
for(j=0;j<i;j++){ //for printing spaces
09
printf(" ");
10
11
12
13
}
for(j=n-i-2;j>=0;j--){ //for printing the right side
14
printf("%c ",'A'+j);
15
}
16
printf("\n");
17
18
19
20
getch();
}
21
Follow up questions:
[learn_more caption=1. What happens if we use %d instead of %c?] Try it out yourself.
[/learn_more]
[learn_more caption=2. Cant we use ASCII values to print this pattern?] We are indirectly using
ASCII values only. If you are still having problems you can visit my post on patterns.[/learn_more]
01
#include
02
#include
03
04
int y;
fibonacci(int x){
05
if(x==1 || x==0) //terminating step
06
return x;
07
08
return y;
09
10
int main(){
11
int a,r;
12
13
scanf("%d",&a);
r=fibonacci(a);
14
15
getch();
16
return 0;
17
18
Follow up questions:
[learn_more caption=1. Can we use this function to print the whole Fibonacci series?] We need to
write a for loop from 1 to n and call fibonacci() for each value of i. Its a bad technique anyway so
dont use it.[/learn_more]
[learn_more caption=2. What if we pass a negative number to the fibonacci() ?] Try it yourself to
find out and rewrite the program to print 0 in that case.[/learn_more]
01
#include
02
#include
03
04
05
noOfDigits++;
06
temp=temp/10;
07
08
temp=num;
09
10
11
12
temp = temp/10;
13
}
if (num == sum) //checks if the number is an armstrong no. or not
14
15
else
16
printf("%d is not an armstrong number.",num);
17
18
19
20
21
int main(){
int n;
22
23
scanf("%d",&n);
24
checkArmstrong(n);
25
getch();
26
27
return 0;
}
28
Follow up Questions:
[learn_more caption=1. Is 0 an Armstrong number?] No O^1=1 and not 0 thats why.[/learn_more]
[learn_more caption=2. How many 2 digit numbers are Armstrong numbers?] 0[/learn_more]
[learn_more caption=3. Why arent there any follow up question on the program?] I couldnt think of
any thats why ;-)[/learn_more]
01
#include
02
#include
03
04
#include
05
06
int i=0,j=0;
07
08
c[i++]=a[i];
09
10
c[i++]=b[j++];
11
12
printf("After concatenation:\n");
13
printf("Value = %s",c);
14
15
int main(){
char a[30], b[30];
16
17
gets(a);
18
19
gets(b);
20
concatenate(a,b);
21
getch();
22
23
24
return 0;
}
Follow up Questions:
[learn_more caption=1. What if we use scanf instead of gets?] scanf can take in only one word
whereas gets can take many words. Use scanf instead of gets to find the difference.[/learn_more]
[learn_more caption=2. What if we dont use \ at the end of the string?] It will show junk values after
the string.[/learn_more]
01
#include
02
#include
03
04
05
int main()
{
int arr[20], n, i, j, pos, temp;
printf("Enter number of elements (max 20): ");
06
07
08
scanf("%d", &n);
for(i=0;i<n;i++){
printf("Enter no %d: ", i+1);
09
10
11
12
13
pos=i;
14
for (j=i+1;j<n;j++ ){
15
pos=j;
16
17
temp= *(arr+i);
18
*(arr+i)= *(arr+pos);
19
*(arr+pos)= temp;
20
}
21
22
23
for(i=0;i<n;i++)
24
25
getch();
26
return 0;
27
28
Follow up Questions:
[learn_more caption=1. What is the bubble sort technique?] To find about bubble sort check out the
post [/learn_more]
[learn_more caption=2. What are the other sorting techniques that are asked during the
interviews?] Its unlikely that they will ask you to write the program but logic of heap sort, merge
sort, bucket sort, quick sort and radix sort can be asked.[/learn_more]
01
#include
02
#include
03
04
05
06
07
08
}emp;
int main(){
int n,i;
09
emp *employee;
10
11
scanf("%d",&n);
12
13
for(i=0;i<n;i++){
14
15
16
scanf("%s",employee[i].name);
17
18
19
scanf("%d",&employee[i].age);
20
21
scanf("%f",&employee[i].salary);
22
}
printf("\nPrinting details of all the employees:\n");
23
for(i=0;i<n;i++){
24
25
printf("\nName: %s",employee[i].name);
26
printf("\nAge: %d",employee[i].age);
27
printf("\nSalary: %.2f",employee[i].salary);
28
29
getch();
30
return 0;
31
32
Follow up Questions:
[learn_more caption=1. What is malloc?] malloc() is a built-in standard library function for dynamic
memory allocation. It allocates a block of size bytes from the free data area (heap). It allows a
program to allocate an exact amount of memory explicitly, as and when needed.
Syntax: void *malloc(size_t size);[/learn_more]
[learn_more caption=2. What are the other functions used in dynamic memory allocation/deallocation?] calloc, free and realloc. calloc() does the same thing as malloc but has a different
syntax. free() is used for de-allocating a memory block previously allocated by malloc() or calloc().
realloc() is used to adjust the amount of memory allocated earlier.
syntax: void *calloc(size_t nitems, size_t size);
01
#include
02
#include
03
04
05
06
07
08
09
return;
}
// Move top n-1 disks from A to B, using C as auxiliary
towers(n-1,frompeg,auxpeg,topeg);
10
11
12
13
14
towers(n-1,auxpeg,topeg,frompeg);
}
15
int main(){
16
int n;
17
printf("Enter the number of disks : ");
18
scanf("%d",&n);
19
20
towers(n,'A','C','B');
21
getch();
22
return 0;
}
23
24
Follow up Question:
[learn_more caption=1. Can we write of Hanoi without using recursion?] Yes we can write it without
using recursion but then we have to alternate between the smallest and the next-smallest disks.
Follow these steps:
For an even number of disks:
01
#include
02
#include
03
#include
04
05
06
07
08
09
10
11
12
getch();
13
return 0;
14
15
16
17
18
19
sum+=atoi(argv[i]);
}
20
21
22
printf("Ans=%d",sum);
23
getch();
24
25
Follow up Questions:
[learn_more caption=1. How can i run this program?] First you have to compile the program. Then
you have to run the program in command prompt and also provide the command line arguments
properly to get the answer.[/learn_more]
[learn_more caption=2. What if i run it in the usual way?] It will print Insufficient number of
arguments because no arguments were passed.[/learn_more]
This is a very interesting program. Well print the source file (programName.c) without taking any
input from the user. Well be using command line arguments. As we know the first argument is the
file name well be using this concept to make programName.c by adding .c to the file name. Then
we will open the file in read mode and print the characters from the file. The source code is given
below:
?
01
02
03
#include
#include
int main(int argc,char *argv[])
04
05
{
FILE *fp;
06
07
08
char ch;
09
10
11
12
13
fscanf(fp,"%c",&ch);
printf("%c",ch);
}//end while
getch();
14
15
return 0;
}//end main
16
Follow up Questions:
[learn_more caption=1. What are the modes used in fopen?] There are 6 modes:
w Create an empty file for writing. If a file with the same name already exists its content is
erased and the file is treated as a new empty file.
a Append to a file. Writing operations append data at the end of the file. The file is created if it
does not exist.
r+ Open a file for update both reading and writing. The file must exist.
w+ Create an empty file for both reading and writing. If a file with the same name already exists
its content is erased and the file is treated as a new empty file.
INTERVIEW Q
the
In header
defined?
files
What
are
calloc()?
the
differences
whether
between
functions
differences
structures
are
between
declared
malloc()
and
or
and
between
pass
by
reference
and
pass
by
for
the
advantages
of
using
pointers
in
declared
or
file
whether
functions
are
use
of
bit
fields
in
Structure
for
number
checking
for
checking
the
size
of
dynamically
be
used
to
free
the
memory
dynamically
allocate
arrays
in
expanded
the
number
of
use the
functions memcpy(),
functions
fseek(),
memset(),
freed(),
you
use
the
functions
randomize()
and
that
fprintf()
to
display
the
mean
by
public,
private,
protected
and
Differentiate
between
realloc() and free?
them.
Difference
between
What is a template?
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 inheritances?
What are the advantages of inheritance?
When is a memory allocated to a class?
What
is
the
definition?
difference
between
declaration
and
only
What is late
function call?
function
bound
virtual
call
destructor,
and
early
no
bound
STRUCTURE
number
of
comparisons
in
to
infix
convert
added
or
needed
in
prefix
or
postfix
know
about
drivers
and
database
What is serialization?
Can you load the server object dynamically?
If so what are the 3 major steps involved in it?
What is the layout for toolbar?
What is the difference between Grid and Gridbaglayout?
How will you add panel to a frame?
Where is the card layouts used?
What is the corresponding layout for card in swing?
What is light weight component?
Can you run the product development on all operating
systems? What are the benefits if Swing over AWT? How
can two threads be made to communicate with each
other?
What are the files generated after using IDL to java
compiler?
What is the protocol used by server and client?
What are the functionability stubs and skeletons?
What is the mapping mechanism used by java to identify
IDL language?
What is serializable interface?
What is the use of interface?
store
more
than
10
objects
in
remote
can
we
do
pointers,
What do
codes?
are
you
mean
by
multithreading?
What
byte
communicate
in
between
Applets
and
that
is
you
call
an
Applet
using
Java
Script
the
difference
between
RMI
registry
and
OS
on
RMI
and
JDBC
using
Stored
between Generic
Servlet and
HTTP
different
functions
of
Syntax
phase,
between
Primary
storage
and
What is multi
threading?
tasking,
multi
programming,
multi
Go
Up
which
type
addressed
by
of
a
counter
and
stack
pointer
16-bit
device
is
used
as
voltage
ranges
of
signals
transmission?
are
used
for
terrestrial
Which
type
of
to
to