Practice c
-
Upload
sumeet-patel -
Category
Documents
-
view
112 -
download
0
Transcript of Practice c
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 0c. 0 50 0d. 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. Oneb. Twoc. Defaultd. Compiler Error
3. void main() {
switch('a'){
case 'A': printf("Zero"); break;case 97: printf("One"); break;default: printf("Error"); break;
}
}
a. Zerob. Onec. Errord. Compiler Error
4. void main() {
int p=1,sum=0;clrscr();while(p<20){
p++;sum+=p++;
}printf("\nsum=%d",sum);
}
a. 120b. 100c. 110d. 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
6 6 (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 GenesisB. 13 Hello GenesisC. Hello Genesis 13D. None of the above
9.
#include <stdio.h> main() { switch (5) { case 5: printf(" 5 "); default: printf(" 10 "); case 6: printf(" 6 "); } }
A. 5B. 5 10 6C. 5 10D. 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 103B. 103C. 103 3D. 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, 6c. 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 12b. 11 12c. 12 13d. 13 13
3. int i=5;
int abc(int z) { return i/2; } main() { int i=4; printf("%d",abc(i=i/4)); }
a) errorb) 5c) 2d) 0
Macros:
1)#define xsq(x) x*xmain( ){
int i, j;i = 5;j = xsq(i-2);printf(“%d\n”, j);
}b. –7 c. 9
d. 13e. 29
2. #define square(x) x*xvoid main(){int i=7;clrscr();i=64/square(4);printf("%d",i);}
a. 7b. 16c. 64d. 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 2b. 17 17 11 2c. 16 16 11 2d. 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. 25B. 11C. 10D. Compilation error
String:
1. void main(){char a[]="123abcd";clrscr();printf("%d",strlen(a));
getch();}
a. 6b. 7c. 8d. 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 1c. 5 4 3 2 1d. 5 4 3 2 1 0
2. main() {
static int i=3;printf("%d",i--);return i>0?main():0;
}
a. 3 2 1 0b. 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 Worldb. Hello World ello Worldc. ello World ello Worldd. ello World llo World
2. int const *p; *p=5; printf("%d",*p++);
a. 5b. 6c. 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 3b. 4 5 3c. 2 5 3d. 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-packardb. dharma harma ewlet-packardc. harma hewlet-packard siemensd. harma harma hewlet-packard
5. void main(){
char *ptr="Ramco Systems"; (*ptr)++;printf("%s\n",ptr);ptr++;printf("%s",ptr);
}
a. Samco Systems Samco Systemsb. Samco Systems amco Systemsc. amco Systems amco Systemsd. 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 2b. 2 1c. 2 2d. 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 3b. 0 0 2 2 3 3c. 1 1 2 2 3 3d. 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 4b. 2 2c. 4 4d. 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 25b. 25 25c. 10 10d. 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) 4b) 2
c) 8d) 7