Practice C

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 17

Simple Program

1. main( )
{
int a=35;
printf(“ %d %d %d\n”, a == 35,a=50,a>40);
}

a. 1 50 1
b. 1 50 0
c. 0 50 0
d. 0 50 1

2. void main()
{
int i=1,j=2;
switch(i)
{
case 1: printf("One"); break;
case j: printf("Two"); break;
default: printf(“Default”); break;
}
}
a. One
b. Two
c. Default
d. Compiler Error

3. void main()
{
switch('a')
{
case 'A': printf("Zero"); break;
case 97: printf("One"); break;
default: printf("Error"); break;
}
}

a. Zero
b. One
c. Error
d. Compiler Error

4. void main()
{
int p=1,sum=0;
clrscr();
while(p<3)
{
p++;
sum+=p++;
}
printf("\nsum=%d",sum);
}

a. 120
b. 100
c. 110
d. Error

5.
void main()
{
int a=1,b=2,c=3;
c = (--a , b++)-c;
printf("%d %d %d",a,b,c);
}
(a)0 3 -3 (b)Compile-Time Error (c)0 3 -1 (d)0 3 0

6.
void main()
{
int i=5;
printf("%d %d %d %d %d",++i,i++,i++,i++,++i);
}
(a)Compile-Time Error (b)10 9 8 7 6 (c)9 8 7
66 (d)10 8 7 6 10

7.
void main()
{
int i, n =10;
for (i=1; i<n--; i+=2)
printf("%d", n-i);
}
(a)84 (b)840 (c)852
(d)864
5.

8. What is the output of the program?

    #include <stdio.h>

    int main( )


    {
        printf(" %d", printf("Hello Genesis"));
        return 0;
    }

A. Hello Genesis
B. 13 Hello Genesis
C. Hello Genesis 13
D. None of the above

9.

  #include <stdio.h>
   
    main()
    {
        switch (5)
        {
            case 5: printf(" 5 ");
            default: printf(" 10 ");
            case 6: printf(" 6 ");
        }
    }

A. 5
B. 5 10 6
C. 5 10
D. 5 6

10. What is the output of the program if the input is 103?

    main()
    {
        int p = 234;
        printf(" %d ", printf("%d", p), scanf("%d", &p));
    }

A. 3 103
B. 103
C. 103 3
D. 103 2

11. .
Write one statement equivalent to the following two
statements:  
x=sqr(a);  return(x);

 Choose from one of the alternatives


(a) return(sqr(a));                                   
(b) printf("sqr(a)");
(c) return(a*a);                                   
(d) printf("%d",sqr(a));

12. #include<stdio.h>
main()
{
const int i=4;
float j;
j = ++i;
printf("%d %f", i,++j);
}

a. 5 6.000000 b. 5 5.000000 c. 6 6.000000 d.


compiler error
function:

1)
int i=5;
fun( )
{
printf("%d\n", i * 3);
}
main( )
{
int i= 2;
{
int i = 3;
printf(" %d", i);
fun();
}
}
a. 3, 15
b. 3, 6
c. 3
d. 0
2) int x;
int modifyvalue()
{
return(x+=10);
}
int changevalue(int x)
{
return(x+=1);
}
void main()
{
int x=10;
x++;
changevalue(x);
x++;
modifyvalue();
printf("First output : %d",x);
changevalue(x);
printf("\nSecond output : %d",x);
}

a. 12 12
b. 11 12
c. 12 13
d. 13 13

3. int i=5;

int abc(int z)
{
return i/2;
}
main()
{
int i=4;
printf("%d",abc(i=i/4));
}
a) error
b) 5
c) 2
d) 0

Macros:

1)
#define xsq(x) x*x
main( )
{
int i, j;
i = 5;
j = xsq(i-2);
printf(“%d\n”, j);
}
b. –7
c. 9
d. 13
e. 29

2.
#define square(x) x*x
void main()
{
int i=7;
clrscr();
i=64/square(4);
printf("%d",i);
}

a. 7
b. 16
c. 64
d. 4
3.
#define man(x,y) (x)>(y)?(printf("%d",x)):(y)

void main()
{
int i=15,j=10,k=0;
k=man(i++,++j);
printf(" %d %d %d",i,j,k);
}

a. 16 17 11 2
b. 17 17 11 2
c. 16 16 11 2
d. 16 17 12 2

4.
#define swap(a,b) temp=a; a=b; b=temp;
void main()
{
static int a=5,b=6,temp;
if (a > b)
swap(a,b);
printf("a=%d b=%d",a,b);
}
(a) a=5 b=6 (b)a=6 b=5 (c)a=6 b=0
(d)None of these

5. #include <stdio.h>
    #define sq(a) a * a      

    void main()    


    {
        printf("%d", sq(3 + 2));
    }        

A. 25
B. 11
C. 10
D. Compilation error 

String:

1. void main()
{
char a[]="123abcd";
clrscr();
printf("%d",strlen(a));
getch();
}

a. 6
b. 7
c. 8
d. 5

Static variable:

1.
main()
{
static int var=5;
if(var--)
{
printf("%d",var);
main();
}
}

a. 4 3 2 1 0
b. 4 3 2 1
c. 5 4 3 2 1
d. 5 4 3 2 1 0

2. main()
{
static int i=3;
printf("%d",i--);
return i>0?main():0;
}

a. 3 2 1 0
b. 3 2 1
c. 2 1 0
d. 2 1

3. main()
{
static int var = 5;
printf("%d ",var--);
if(var)
main();
}

a. 4 3 2 1 b. 4 3 2 1 0 c. 5 4 3 2 1 d. 0 0 0 0 0

Pointers:
1. void main()
{
char *ptr="Hello World";
*ptr++;
printf("%s",ptr);
ptr++;
printf("%s",ptr);
}

a. Iello World Iello World


b. Hello World ello World
c. ello World ello World
d. ello World llo World

2. int const *p;


*p=5;
printf("%d",*p++);

a. 5
b. 6
c. Compiler Error
d. Garbage Value

3. void main()
{
char *str1="abcd";
char str2[]="abcd";
printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("ab"));
}

a. 5 5 3
b. 4 5 3
c. 2 5 3
d. 1 5 3

4. void main()
{
char *s[]={"dharma","hewlet-packard","siemens","ibm"};
char **p;
p=s;
printf("%s",++*p);
printf("\n%s",*p++);
printf("\n%s",++*p);
}

a. harma
harma
ewlet-packard
b. dharma
harma
ewlet-packard
c. harma
hewlet-packard
siemens
d. harma
harma
hewlet-packard

5. void main()
{
char *ptr="Ramco Systems";
(*ptr)++;
printf("%s\n",ptr);
ptr++;
printf("%s",ptr);
}

a. Samco Systems Samco Systems


b. Samco Systems amco Systems
c. amco Systems amco Systems
d. amco Systems mco Systems
Sizeof() operator:

1. printf("%d %d %d",sizeof(25.75),sizeof(123),sizeof(‘p’))
a. 2 2 2
b. 4 2 2
c. 8 4 1
d. 8 2 2

2. P is a character pointer variable then, what will be the output


of the following statement.assume size of int is 2.
printf("%d %d",sizeof(p),sizeof(*p));

a. 1 2
b. 2 1
c. 2 2
d. 1 1

3. void main()
{
printf("%d",sizeof(int));
return(0);
}
(a)Data types not allowed (b)Compile-Time Error(c)3 (d)2

Structure and union:

1. struct one
{
int no:1;
int pl:2;
int x:5;
};

void main()
{
struct one a;
a.no=0;
a.pl=1;
a.x=3;
printf("%d %d",a.no,sizeof(a));
printf("\n%d %u",a.pl,a.pl);
printf("\n%d %u",a.x,a.x);
}

a. 0 4
1 1
3 3
b. 0 0
2 2
3 3
c. 1 1
2 2
3 3
d. 1 1
2 2
2 2

2. void main()
{
struct emp
{
struct e
{
int *a;
}e1;
int a;
};

struct emp emp1;


printf("%d %d",sizeof(emp1),sizeof(emp1.e1));
}

a. 8 4
b. 2 2
c. 4 4
d. 4 2

3.

struct emp emp1;


struct emp
{
int a;
};
main()
{
printf("Enter 1 values:");
scanf("%d%d",&emp1.a,&emp1.a);
printf("a=%d a=%d",emp1.a,emp1.a);
}

a. 10 25
b. 25 25
c. 10 10
d. Compiler Error

4. #include<stdio.h>
main()
{
struct xx
{
int x=3;
char name[]="hello";
};
struct xx *s;
printf("%d",s->x);
printf("%s",s->name);
}

a. 3 hello b. Compiler Error c. Run time error d. use


dot (.) operator

5.
union U
{
int x;
float y;
char s[2];
};
union U ob;

what is the size of ob in bytes,

a) 4
b) 2
c) 8
d) 7

You might also like