Find Interview Questions for Top Companies
Ques:- which of the following statements is incorrect a.typedef struct new{ int n1; char n2; } DATA; b.typedef struct { int n3; char *n4; }ICE; c.typedef union { int n5; float n6; } UDT; d.#typedef union { int n7; float n8; } TUDAT;
Asked In :-
Right Answer:
d. #typedef union { int n7; float n8; } TUDAT;
Comments
Admin May 17, 2020

choice d.
Reason:
The macro sign # is not applicable here.
Syntactical error!

Admin May 17, 2020

d is the right answer
# should not be present at starting.

Ques:- main() { char *p1=”Name”; char *p2; p2=(char *)malloc(20); while(*p2++=*p1++); printf(“%sn”,p2); } what is the output?
Asked In :-
Comments
DK BOSS Jul 17, 2021

it will print p2=Name

Admin May 17, 2020

ANS: NULL
It is a bit tricky question. If u observe carefully then we
are incrementing the pointers p1,p2. When it reached the end
of the string, *p2 points to NULL. We have lost the address
of the starting position.

Admin May 17, 2020

Hi all,
#1 Mannucse's ans is wrong. cos as mentioned "Name" will
not be the output.
#2 Sanath's ans is wrong. Cos at the end of the while loop,
p2 will not point to NULL. It will point to the next byte
to the NULL termination ie., 6th byte.
#3 Shruti's ans is wrong. cos i think she got confused
between assignment(=) and comparisonal(==) operators. And
the statement given as "we cannot copy the value of p1 in
p2, the way its mentioned here" is absolutely wrong.
So,
Lets Analyse the program and how to get the required output.
hav a look on th program again.
main()
{
char *p1="Name";
char *p2;
p2=(char *)malloc(20);
while(*p2++ = *p1++)
{
printf("TEST n");
}
printf("%sn",p2);
getch();
}
Here, in every iteration of while loop, we are assigning
*p1 to *p2, and incrementing both pointers p1 and p2, After
completion(when *p1 value would be '')of the while loop,
first 5 bytes of p2 holds the
characters 'N','a','m','e' ''. At the end of while loop
p2 points to the 6th byte in the memory.
So, now printf("%sn",p2); shall start print the values
from the 6th byte to 20th bytes of the memory which was
allocated dynamically.
----------------------------------
To get the desired output change the printf statement to
printf("%sn",p2-5);
Now (p2-5) points to the starting address of p2 and will
print the values in the memory till it encounters ''
termination. ie., The output would be -> Name

Ques:- int *a[5] refers to
Asked In :-
Right Answer:
`int *a[5]` refers to an array of 5 pointers to integers.
Comments
Admin May 17, 2020

int *a[5] means that there are 5 integer pointers[which
store adresess of only integers] whose adresses are in
continous locations and each pointer name is accesed by *a
[0],*a[1]....*a[4].for example
a[0]=n means a[0] points to n, a[1]=p means a[1] points to
p.[where n and p are integers]Let the adress of n and p be
some 625 and 254 respectively. then a[0] will hold 625
whose adress is(assume)1000 and a[1]will hold 254 whose
adress is 1002.similarly the a[2] will hold some adress(if
u make it to point) with its adress 1004 and a[3] will hold
some others adress with its adress 1006 and a[4] will hold
some others adress with its adress 1008.

Admin May 17, 2020

array of pointers

Ques:- find the output of the following program main() { int x=5, *p; p=&x; printf(“%d”,++*p); }
Asked In :-
Comments
Admin May 17, 2020

6
becoz it(++*p) will evaluate like this-
1)
*(p)- gives value at this address that is 5
2)
now increment ++5
that is 6.

Ques:- consider the following C code main() { int i=3,x; while(i>0) { x=func(i); i–; } int func(int n) { static sum=0; sum=sum+n; return(sum); } the final value of x is
Asked In :-
Comments
Admin May 17, 2020

Answer is 6;
Sum being the static variale will retain its value state
between he function calls.

Ques:- # define prod(a,b)=a*b main() { int x=2; int y=3; printf(“%d”,prod(x+2,y-10)); } the output of the program is a.8 b.6 c.7 d.none
Asked In :-
Comments
Admin May 17, 2020

It will lead to compilation error..
Note: # define prod(a,b)=a*b
'=' is not allowed with #define
regards,
Arun Raj

Ques:- consider the following program sigment int n,sum=1; switch(n) { case 2:sum=sum+2; case 3:sum*=2; break; default:sum=0;} if n=2, what is the value of sum a.0 b.6 c.3 d.none
Asked In :-
Right Answer:
c. 3
Comments
Admin May 17, 2020

6

Ques:- the format specified for hexa decimal is a.%d b.%o c.%x d.%u
Asked In :-
Right Answer:
c.%x
Comments
Admin May 17, 2020

it is x for hexadecimal

Admin May 17, 2020

c

Ques:- which of the following go out of the loopo if expn 2 becoming false a.while(expn 1){…if(expn 2)continue;} b.while(!expn 1){if(expn 2)continue;…} c.do{..if(expn 1)continue;..}while(expn 2); d.while(!expn 2){if(expn 1)continue;..}
Asked In :-
Comments
Admin May 17, 2020

c) Since expn2 becoming false will terminate do-while loop.

Admin May 17, 2020

answer: c
here in do-while:
fist the statements inside the block gets executed.then
when it checks the condition ,it will be come out of loop
because conditions goes wrong

Ques:- pick out the odd one out of the following a.malloc() b.calloc() c.free() d.realloc()
Asked In :-
Right Answer:
c.free()
Comments
Admin May 17, 2020

c.free()

Admin May 17, 2020

here malloc(),calloc(),realloc)() are the memory allocation
function after allocation of memory free is used to freed
the memory which is allocated by the above function.
so the free is odd term.

Ques:- the operator for exponencation is a.** b.^ c.% d.not available
Asked In :-
Right Answer:
d. not available
Comments
Admin May 17, 2020

d.not available is d right ans.

Admin May 17, 2020

Hi All,
Upto my knowledge exponentiation operator is not available
in C. Because ^ operator is used for XOR operation. So we
cannot use that. But its possible by using Bitwise
operator to perform Exponentiation.
Thanks & Regards
Sathish Kumar

Ques:- what is y value of the code if input x=10 y=5; if (x==10) else if(x==9) elae y=8; a.9 b.8 c.6 d.7
Asked In :-
Right Answer:
9
Comments
Admin May 17, 2020

I'm getting "error C2181: illegal else without matching if".
If that is corrected then the output will be y = 5.

Admin May 17, 2020

the value of y is not changing so i think y wll reamin 5.

Ques:- what does the following code do? fn(int n,int p,int r) { static int a=p; switch(n){ case 4:a+=a*r; case 3:a+=a*r; case 2:a+=a*r; case 1:a+=a*r; } } a.computes simple interest for one year b.computes amount on compound interest for 1 to 4 years c.computes simple interest for four year d.computes compound interst for 1 year
Asked In :-
Right Answer:
b. computes amount on compound interest for 1 to 4 years
Comments
DK BOSS Jul 15, 2021

if this function is called from within a loop with n=0 to n<4
then cases 1,2,3,4 in switch will be called one by one
as int a is static , first time it will be initialized with the value p but afterwords
its memory location will be updated every time the fun is called with the latest value of a calculated within the case
but r should be a float value
example:
main()
int p=100;
float r=0.10;
{
for(n=1;na=100+100*.1
a=110

second iteration
a=110;
n=2
case(2)
a=a+a*r=>a=110+110*.1
a=121

third iteration
a=121
n=3
case(3)
a=a+a*r=> a=121+121*.1
a=133.1

fourth iteration
a=133
n=4
case(4)
a=133+133*.1
a=146

in this way, we can see this is the amount for compound interest for 1-4 years

Admin May 17, 2020

this code depends upon the value of 'n' actually....... if
the value is 4 the operation is differewnt... if 3 its
different..... so give the value of 'n'!

Admin May 17, 2020

Here a is declared as static,so it can't be re-initialized..

Ques:- a=0; while(a
Right Answer:
The code snippet is incomplete. A correct version could be:

```c
int a = 0;
while(a < 10) {
printf("%dn", a);
a++;
}
```

This will print numbers from 0 to 9.
Comments
Admin May 17, 2020

Hello,
The answer is b.
a=0+1=1
a=1+1=2
a=2+1=3
a=3+1=4
a=4+1=5
While a variable is storing the 5 result a is nither less
then are equal to 5 hence the application will come out of
the loop.So while loop had run 5 times.

Ques:- how many times does the loop iterated ? for (i=0;i=10;i+=2) printf(“Hin”);
Asked In :-
Comments
Admin May 17, 2020

infinite coz there is no condition to stop it

Admin May 17, 2020

infinite loop, bcoz there is no condition to break the loop.

Ques:- What will be the result of the following program? main() { char p[]=”String”; int x=0; if(p==”String”) { printf(“Pass 1”); if(p[sizeof(p)-2]==’g’) printf(“Pass 2”); else printf(“Fail 2”); } else { printf(“Fail 1”); if(p[sizeof(p)-2]==’g’) printf(“Pass 2”); else printf(“Fail 2”); } } a) Pass 1, Pass 2 b) Fail 1, Fail 2 c) Pass 1, Fail 2 d) Fail 1, Pass 2 e) syntax error during compilation
Asked In :- Centillion Networks,
Comments
Admin May 17, 2020

d) Fail 1, Pass 2

Admin May 17, 2020

Fail 1 , Pass 2.
Some explanation,
1. Fail 1
first of all, to compare strings in C, you use this strcmp
function, so this WOULD give PASS 1 :
if(strcmp(p,"String") == 0)
but
if(p=="String")
will fail because this line means :
if address of p is the same as address of some temporary
storage for literals, where literal "String" is stored,
which is very rarely true, because storing literals is
compiler specific and is very hard to estimate at runtime.
2. Pass 2
sizeof(p) gives 7, because sizeof(char) is 1 byte, and we
have 7 chars in array storing "String", which are :
[0]S
[1]t
[2]r
[3]i
[4]n
[5]g
[6] (EOS)
now, clearly sizeof(p) - 2 is [5] which is "g"
thats why
if(p[sizeof(p)-2]=='g')
is true.

Ques:- Struct(s) { int a; long b; } Union (u) {int a; long b; } Print sizeof(s)and sizeof(u) if sizeof(int)=4 and sizeof(long)=4
Asked In :-
Right Answer:
sizeof(s) = 8
sizeof(u) = 4
Comments
Admin May 17, 2020

Size fo strucure wil be the total bytes of the datatypes
inside it..
so,4+4=8;
For unions the size wi be the size of the datatype whose
memory is high,.
so,its 4,.

Admin May 17, 2020

I assume above code to be correctly written as below:
struct s
{
int a;
long b;
}
Union u
{int a;
long b;
}
sizeof(s)= 8
sizeof(u) = 4

Ques:- In the following code segment what will be the result of the function, value of x , value of y { unsigned int x=-1; int y; y = ~0; if(x == y) printf(“same”); else printf(“not same”); } a) same, MAXINT, -1 b) not same, MAXINT, -MAXINT c) same , MAXUNIT, -1 d) same, MAXUNIT, MAXUNIT e) not same, MAXINT, MAXUNIT
Asked In :- BCOM Computer Centre,
Ques:- what will be the result of the following program ? char *gxxx() { static char xxx[1024]; return xxx; } main() { char *g=”string”; strcpy(gxxx(),g); g = gxxx(); strcpy(g,”oldstring”); printf(“The string is : %s”,gxxx()); } a) The string is : string b) The string is :Oldstring c) Run time error/Core dump d) Syntax error during compilation e) None of these
Asked In :-
Comments
DK BOSS Jul 15, 2021

b) The string is :Oldstring
as per the question static variable memory is created for one time so it will return Oldstring

Admin May 17, 2020

oldstring

Admin May 17, 2020

b) The String is :OldString
Because ,for Static var memory is only one time created.
Eventhough the fn is multiple times called,
so
consider addr of xxx is 4444,
first strcpy copy the string "string" to addr 4444,
then g=4444;
then oldstring overwrites to location 4444.

Ques:- What will be result of the following program? void myalloc(char *x, int n) { x= (char *)malloc(n*sizeof(char)); memset(x,,n*sizeof(char)); } main() { char *g=”String”; myalloc(g,20); strcpy(g,”Oldstring”); printf(“The string is %s”,g); } a) The string is : String b) Run time error/Core dump c) The string is : Oldstring d) Syntax error during compilation e) None of these
Asked In :-
Comments
Admin May 17, 2020

a)the string is string

Admin May 17, 2020

Run time error/Core dump.



C programming is a powerful and enduring language that has significantly shaped the landscape of modern computing. Developed in the early 1970s by Dennis Ritchie at Bell Labs, its primary goal was to create a language capable of rewriting the Unix operating system. This origin story highlights C’s fundamental strengths: it is a “middle-level” language, bridging the gap between low-level assembly and high-level, human-readable languages. This unique position allows C programmers to exert fine-grained control over computer hardware, memory management, and system resources, making it an ideal choice for systems programming.

Its syntax is relatively minimal and straightforward, built around functions, variables, and control structures like loops and conditional statements. This simplicity, however, belies its incredible power. A key feature of C is its extensive use of pointers, which are variables that store memory addresses. While challenging for newcomers, mastering pointers unlocks the ability to manipulate data structures efficiently, manage memory dynamically, and interact directly with hardware.

C’s influence is profound and widespread. It’s the language of choice for developing operating systems like Windows and Linux kernels, embedded systems in cars and appliances, and device drivers. Furthermore, many modern languages, including C++, C#, Java, and Python, draw heavily from C’s syntax and core concepts, making it an essential stepping stone for any aspiring programmer. Despite its age, C remains a cornerstone of computer science education and a vital tool for creating high-performance, low-level applications where speed and efficiency are paramount. Its legacy is not just in the code it has written, but in the countless other languages and technologies it has inspired.

AmbitionBox Logo

What makes Takluu valuable for interview preparation?

1 Lakh+
Companies
6 Lakh+
Interview Questions
50K+
Job Profiles
20K+
Users