You are on page 1of 13

[1]

/*consider the following piece of C - code fragement */


main() {
int *p, *q;
int x[3], y;
x[0] = 5;
x[1] = 2;
x[2] = 3;

Ans: address of p 6558


address of p after inc 6

560
p = q = x;
printf("address of p %u\n",p);
y = (*p)++;
printf("address of p after inc %u\n",p);
printf("%d %u\n",y,&y);
printf("%d %u\n",*p,&(*p));

5 6556
2 6560

}
/*y is same as &x[0] y contains value of x[1] y is same as &x[1]
* y contains value of x[0]*/
-------------------------------------------------------------------[2]
/* What is the output of the following ? */
#include <stdio.h>
#define FLOAT 1
#define INTA 2
typedef union {
struct {
int tag;
}p1;
struct {
int tag;
int intelem;
}p2;
struct {
int tag;
float floatelem;
}p3;
}un;
main() {
Ans: 6
un u;
value of float element 3
.14
u.p1.tag=FLOAT;
u.p3.floatelem=3.14;
/*
printf("floatelem = %f",u.p3.floatelem);
printf("tag = %d",u.p2.tag);*/
printf ("%d\n",sizeof(u));
if(u.p2.tag == FLOAT) {
printf("Value of float element = %.2f\n",u.p3.floatelem);
}
else {
printf("Value of float element is not defined\n");
}
}

[3]

/*Consider the following piece of C - code fragement*/


main() {
int *p, *q;
int x[3], y;
x[0] = 5;
x[1] = 2;
x[2] = 3;
p = q = x;
printf("address of p %u\n",p);
y = *p++;
printf("address of p after inc %u\n",p);
printf("%d %u\n",y,&y);
printf("%d %u\n",*p,&(*p));
}
/*
*
y is same as &x[0]
* y contains value of x[1]
* y is same as &x[1]
* y contains value of x[0]
*/
------------------------------------------------------------------[4]
/*
*
What is the output of the programe ?
*/
#include <stdio.h>
void func();
main() {
char s[10];
strcpy(s,"hello");
func(s);
printf("%s\n",s);
}
void func(char *p) {
int i, j;
char t;
for(i=0, j=strlen(p)-1; i<j; i++, j--) {
t=p[i];
p[i]=p[j];
p[j]=t;
}
}
Ans: oll
eh
/*
*
a) lelho
b) lleoh
c) hello
d) none
*/
-------------------------------------------------------------------[5]
#include <stdio.h>
main() {
/*link("../blustar/test.cpp","vennn.c"); */
Ans: deletes the file if exists
unlink("vennn.c");
}
[6]
/*
*

On a machine on which int is stored as a 16 bit entity

*
what is the output of the varable a after the last step
*
the following piece of C - code fragement executed
*/
main() {
int a=6, b;
a = a & -0xfd;
b = a << 2;
printf("%d",b);
Ans: 8
}
--------------------------------------------------------------------[7]
/*
*
Declaring a register variable as in :
*
register int a;
Ans: c
*
achieve the following effects
*
a) Run time instance of variable a will be placed in register
*
b) Register storage for variable a will be guarenteed at
*
the compile time
*
c) Eligibility of variable a for actually being placed in
*
register is implimentation dependent
* d) None
-----------------------------------------------------------------------[8]
/*
*
What does the function func() delined below do ?
*/
Node fun(list)
Node * list;
{
Node * newlist=0;
while(list) {
Node temp=list;
list=list->next;
temp->next=newlist;
newlist=temp;
}
return(newlist);
}
/* Given that Node is structure containing of two componants.
* Node information in first and pointer to another Node structure.
* in the second (see below)
* struct_node {
*
int info;
*
Node * next;
And: d
* }Node;
*a)reverses the linked list pointed to by list
*b)destroys the linked list pointed to by list
*c)Right rotates the linked list pointed to by list,by one element.
*d)Left rotates the linked list pointed to by list by one element.
*/
[9]
/*
*
Assume the following programe is compiled and executable thus
*
produced is run with the following argument list:
* <executable_name> hello its me
*
What is the output generatred ?

*/
#include <stdio.h>
main(argc,argv) int argc; char *argv[]; {
while(--argc > 0) {
printf("%s %s",++argv[argc>1]?".":"");
}
printf("\n");
Ans: compilation error
return 0;
}
--------------------------------------------------------------------[10]
/*
*
Consider the following declarations:
*
*
A)
char *name[]= {"illegal month","jan","feb","mar"};
*
B)
char aname[][15]={"illegal month", "jan", "feb", "mar"}
*
Which of the following statement is true:
*
a)
The declaration B is invalid
*
b)
Both declaration A and B are invalid
*
c)
Both declaration are valid and exactly equivalent
*
d)
Declaration B takes less storage space than A
*/
main() {
char *name[]={"illegal month", "jan", "feb", "mar"};
char aname[][15]={"illegal month", "jan", "feb", "mar"};
printf("%s\n",aname);
printf("%s\n",*name);
printf("%s\n",aname[1]);
printf("%s\n",name[1]);
printf("%s\n",aname[2]);
Ans: c
printf("%s\n",name[2]);
ilegal month
printf("%s\n",aname[3]);
jan
printf("%s\n",name[3]);
feb
}
mar
-------------------------------------------------------------------[11]
/*
*
Consider a function f(a,b,c). The parameters a, b, c are
*
passed as:
*
*
a)
through global memory
*
b)
through registers
*
c)
through stack with a, b, c PUSHed in that order
*
d)
through stack with c, b, a PUSHed in that order
*/
Ans: d

----------------------------------------------------------------------[12]
/*
How in C would you declare an array 'a' of N pointers to
*
functions returning pointers to functions returning pointers to
*
characters ?
*
a)
char *(*(a[N]))();
*
b)
char *(*(*a[N])())();
*
c)
char *(*(*a[N])());
*
d)
None

*/
Ans: b
-----------------------------------------------------------------------[13]
/*What is the output of the following proframe ? */
#include <stdio.h>
main() {
int i;
i=6;
printf("%d%%",i++);
}
/*
*
a) 6
*
b) 6%
* c) 6%%
* d) None
Ans: 6%
*/
--------------------------------------------------------------------[14]
/*
*
How much space ( in bytes ) does the following structure
* my_str occupy ? Assume that structures are compact and byte aligned.
*
( Given that unsigned int on the machine occupies 4 bytes )
*/
#include <stdio.h>
struct {
unsigned tt :5;
unsigned te :8;
unsigned tg :6;
unsigned td :7;
unsigned ty :4;
unsigned other :3;
}my_str;
main()
{
printf("size of my_str is %d\n",sizeof(my_str));
printf("size of unsigned=%d\n",sizeof(unsigned));}
/*
a) 4
b) 8
c) 16 d) 32
Ans: 6 2
*/
---------------------------------------------------------------------[15]
/*What is the output on the screen when the following
*function fac() is called with argument 4 from main as show ? */
#include <stdio.h>
int fac();
main() {
int i=4;
printf("factorial(%d)=%d\n",i,fac(i));
}
fac(int x) {
int f=1,i;
for(i=x;i>=1;i--)
f*=i;
return(f);
Ans: foctorial (4) = 24
}
-----------------------------------------------------------------------[16]
/*
*
How much storage space (in bytes) does the following bl_str occupy ?
*
Given that the machine stores words only on 4 byte boundaries.

*
Assume size of int as 4 bytes,
*/
struct {
char p[5];
/*
int q;
char o[5];

char as 1 byte.
8 bytes */
/*
4 bytes */
/* 4 bytes*/

}bl_str;
main() {
printf("sizeof bl_str = %d\n",sizeof(bl_str));
}
/*
Ans: 6 2 6 == 14
*
a) 4 bytes
b) 8 bytes
c) 16 bytes
d) 13 bytes
*/
----------------------------------------------------------------------[17]
/*
*
How much storage space does the following structure a_str
*
occupy ?
*
Given that double on the machine occupies 8 bytes. int 4 bytes &
*
char 1 byte.
*/
union a_str {
int ival;
double dval;
char sval;
}a_str;
main() {
printf("sizeof a_str = %d\n",sizeof(a_str));
}
/*
Ans: b 8bytes
*
a) 4 bytes
b) 8 bytes
c) 12 bytes
d) 13 bytes
*/
---------------------------------------------------------------------[18]
/*
*
Determine the output of the following program.
*/
#include <stdio.h>
main() {
int i, a;
a = 4;
for(i=0; i<=3; i++) {
switch(i) {
case 0 :
case 1 : a+=4;
break;
case 2 : a+=5;
break;
default: a+=6;
break;
}
}
printf("Value of a = %d\n",a);
}
/*
*

a)

Value of a = 9

*
a)
Value of a = 23
Ans : 23
*
a)
Value of a = 15
*
a)
Value of a = 17
*/
-----------------------------------------------------------------------[19]
/*
*
What will be the output of the following programe ?
*/
#include <stdio.h>
#define max(a,b)
((a) > (b) ? (a) : (b))
main() {
int i, j;
i = j = 4;
if(max(i++, j++) == 5) {
printf("Value of i = %d\n",i);
}
else {
printf("Value of j = %d\n",j);
}
}
/*
Ans: value of i = 5
*
a) Value of a = 4
*
a) Value of a = 5
*
a) Value of j = 4
*
a) Value of j = 6
*/
----------------------------------------------------------------[20]
/*
*
Determine the output ofd the following program ?
*/
#include <stdio.h>
/*
*
Determine the output of the following program ?
*/
#include <stdio.h>
void func();
main() {
int a, b;
a = -3; b = 4;
func(&a, &b);
printf("Value of a : %d & b : %d\n",a, b);
}
void func(c,d) int *c; int *d; {
*d= (*c += *d)- *d;
*c-=*d;
}
/*
*
a)
Value of a: -7 & b: 7
*
b)
Value of a: 1 & b: 5
Ans: d
*
c)
Value of a: -4 & b: 3
*
d)
Value of a: 4 & b: -3
*/
--------------------------------------------------------------------[21]
/*
*
Determine the output of the following program ?

*/
#include <stdio.h>
void swap();
main() {
int a, b;
a = 3; b = 4;
swap(a,b);
printf("Value of a: %d & b: %d\n",a,b);
}
void swap(c,d) int c; int d; {
int temp;
temp = c;
c = d;
d = temp;
printf("Value of c: %d & d: %d\n",c,d);
}
/*
*
a) Value of a: 3 & b: 4
Ans : 1
*
b) Value of a: 3 & b: 3
*
c) Value of a: 4 & b: 3
* d) None of the above
*/
-----------------------------------------------------------------------[22]
main ()
{
int a=3,c=3,i=1;
printf("%d %d %d", i++,++i,++i);
Ans: 3 3
2
}
[23]
#include <stdio.h>
main() {
int j;
j=strlen("hello");
printf("%d",j);
}

Ans : 5

[24]
#include <math.h>
#define max 2
#define inr( r) r++
main() {
printf("%d\n",inr(max));
Ans: Lvalue required
}
2++ is invalid
-----------------------------------------------------------------------[25]
main(){
int p,i =143;
p = --i+printf("Venk--------> %d\n",printf("Dinu---> %d\n",i))-i--;
printf("%d\n",p);
Ans: Dinu--->142 venk --> 13
}
p is 17
-----------------------------------------------------------------------[26]
Ans: Venk---> Hello Yama
main(){
Dinu------->20
int p, j = 120;
p is 20

char *i="Hello Yama";


p = --j+printf("Dinu--------> %d\n",printf("Venk---> %s\n",i))-j--;
printf("%d\n",p);
}
--------------------------------------------------------------------[27]
Ans: Dinu---> 142
main(){
Venk------>13 p is 17
int p,i =143;
p = --i+printf("Venk--------> %d\n",printf("Dinu---> %d\n",i))-i--;
printf("%d\n",p);
}
-----------------------------------------------------------------------[28]
main() {
int i = 5;
Ans: 10
while(i-->3) {
10
int i = 10;
printf("%d\n",i);
}
}
--------------------------------------------------------------------[29]
main() {
int i =10;
5+5;
if(i++ ==11)
printf("%d",i);
else if(--i ==10)
printf("xxx%d",i);
Ans: xxx10
else printf("%d",i);
}
---------------------------------------------------------------------[30]
#define print(int) printf(#int " = %d\n",int)
main() {
int x = 3;
print(x);
Ans: x=3
}
----------------------------------------------------------------------[31]
#define print(int) printf("int = %d\n",int)
main() {
print(3);
Ans: int = 3
}
--------------------------------------------------------------------[32]
main() {
int i=3,y=2;
printf("%d %d \n");
Ans: 2 3
}
--------------------------------------------------------------------[33]
main() {
const int i = 10;
i=25;
printf("%d\n",i);
Ans: cannot change
}
constant
--------------------------------------------------------------------[34]
main(argc,argv)
int argc;

char *argv[]; {
int c;
c = strlen(argv[1]);
Ans: prints the entered string if it
if(c >= 80)
is more than 80 char
printf("%s\n",argv[1]);
}
----------------------------------------------------------------------[35]
main()
{
int a, r, i=0;
printf("enter any number\n");
scanf("%d", &a);
while (a > 0) {
r = a % 10;
if (r == 1)
Ans: counts the no of on
es
i++;
a = a / 10;
}
printf("no. of ones =%d\n", i);
}
----------------------------------------------------------------------[36]
#include<stdio.h>
main()
{
int a[5],i,j,temp;
printf("enter array elements");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
i=0;
while(i<5)
{
Ans:sorting in ascending
order
for(j=i+1;j<5;j++)
if(a[i] >= a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
i++;
}
for(i=0;i<5;i++)
printf("#output %d\n",a[i]);
}
------------------------------------------------------------------[37]
main() {
Ans: Chinna Thambi -->Di
nesh
printf("Calling from main --> %d\n",f(9));
Recursive call 25
}
Recursive call 1
8
f(int x) {
Recursive call 1
8
if(x<=6)
Calling from mai
n 18
return printf("Chinna Thambi --> %s\n","Dinesh");

else
return printf("Recursive call %d\n",f(--x));
}
---------------------------------------------------------------------[38]
#include <math.h>
#define max 2
#define inr( r) r++
main() {
Ans: Lvalue requ
ired
printf("%d\n",inr(max));
}
----------------------------------------------------------------------[39]
#include<stdio.h>
#include "tree.h"
int compare(char *oper1,char *oper2);
void print_leaves(td)
TREE *td;
{
void print_leaf(char *,TREE *);
navigate((void *) td,print_leaf)
;
}
void print_leaf(data,td)
char *data;
TREE *td;
{
printf("%d\n",*(int *)data);
fflush(stdout);
}
int compare(char *oper1,char *oper2)
{
return(*(int *)oper1 - *(int *)oper2);
}
------------------------------------------------------------------[40]
main()
{
TREE * td ;
int c,num;
char line[100];
char *p;
td = open_tree(sizeof(int),compare);
while(1)
{
if((fgets(line,100,stdin))==NULL)
exit(0);
p = line;
switch(*p )
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':

case '7':
case '8':
case '9':
num = atoi(p);
printf("%d\n",num);
fflush(stdout);
add_leaf(td,(char*)&num);
break;
case 'p':
case 'P':
print_leaves(td);
break;
}
}
}
--------------------------------------------------------------------[41]
# include <stdio.h>
# include <string.h>
main()
{
char *s = "hello word";
char *d ;
Ans:gjgjgj
d = "gjgjgj";
hello
printf("%s\n",d);
cmp(d,s);
printf("%s\n",d);
}
cmp(char *dc,char *sc)
{
while(*dc)
*dc++= *sc++;
}
-------------------------------------------------------------------[42]
main() {
int x=10 ;
10+10;
printf("%d\n",x);
Ans:10
}
------------------------------------------------------------------[43]
main() {
printf("%d\n",f(7));
}
f(int x) {
if(x<=4)

Ans: 4
return x;

else
return f(--x);
}
----------------------------------------------------------------[44]
main() {
union tt {
char s;
int a, *s2[4];

char *s1[16];
}pp;
printf("%d\n",sizeof(pp));
}
-----------------------------------------------------------------[45]
#include<stdio.h>
static char *s1 ;
static *s2 ;
main(){
char *p1 ;
s1 = p1 ;
*s1 = NULL ;
s2 = p1 ;
*s2 = NULL ;
}
-------------------------------------------------------------------[46]
main(){
const i = 9 ;
Ans: 9
printf("%d\n",i);
}
------------------------------------------------------------------[47]
main(){
union pp
Ans: 12
{
char *s1[6] ;
char s ;
int *s2[4] ;
}tt ;
printf("%d\n",sizeof(tt));
}

You might also like