C Quiz Part-II



1. What will be the output of the following c code?

#include<stdio.h>
extern int x;
int main(){
    do{
        do{
             printf("The value of x is: %o",x);
         }
         while(!-2);
    }
    while(0);
    return 0;
}
int x=8;

Output: The value of x is 10

  Explanation    What is extern Type ?   What is %o ?




(2)
What will be output of following c code?
        
#include<stdio.h>
int main(){
    int i=2,j=2;
    while(i+1?--i:j++)
         printf("%d",i);
    return 0;
}


Explanation



(3)
What will be output of following c code?

#include<stdio.h>
int main(){
    int x=011,i;
    for(i=0;i<x;i+=3){
         printf("Start ");
         continue;
         printf("End");
    }
    return 0;
}


Explanation




(4)What will be output of following c code?

#include<stdio.h>
int main(){
    int i,j;
    i=j=2,3;
    while(--i&&j++)
         printf("%d %d",i,j);
    return 0;
}



Explanation




(5)What will be output of following c code?


#include<stdio.h>
int main(){
    static int i;
    for(++i;++i;++i) {
         printf("%d ",i);
         if(i==4) break;
    }
    return 0;
}


Explanation



(6)What will be output of following c code?

#include<stdio.h>
int main(){
    int i=1;
    for(i=0;i=-1;i=1) {
         printf("%d ",i);
         if(i!=1) break;
    }
    return 0;
}


Explanation



(7)What will be output of following c code?

#include<stdio.h>
int main(){
    for(;;) {
         printf("%d ",10);
    }
    return 0;
}


Explanation



(8)What will be output of following c code?
        
#include<stdio.h>
int r();
int main(){
    for(r();r();r()) {
         printf("%d ",r());
    }
    return 0;
}
int r(){
    int static num=7;
    return num--;
}


Explanation



(9)What will be output of following c code?
        
#include<stdio.h>
#define p(a,b) a##b
#define call(x) #x
int main(){
    do{
         int i=15,j=3;
         printf("%d",p(i-+,+j));
    }
    while(*(call(625)+3));
    return 0;
}


Explanation



(10)

#include<stdio.h>
int main(){
    int i;
    for(i=0;i<=5;i++);
    printf("%d",i)
    return 0;
}


Explanation



(11)What will be output of following c code?
#include<stdio.h>
int i=40;
extern int i;
int main(){
    do{
         printf("%d",i++);
    }
    while(5,4,3,2,1,0);
    return 0;
}



Explanation



(12)What will be output of following c code?
#include<stdio.h>
char _x_(int,...);
int main(){
    char (*p)(int,...)=&_x_;
    for(;(*p)(0,1,2,3,4); )
         printf("%d",!+2);
    return 0;
}
char _x_(int a,...){
    static i=-1;
    return i+++a;
}



Explanation



(13)What will be output of following c code?
#include<stdio.h>
int main(){
    int i;
    for(i=10;i<=15;i++){
         while(i){
             do{
                 printf("%d ",1);
                 if(i>>1)
                      continue;
             }while(0);
             break;
         }
    }
    return 0;
}



Explanation



(14)How many times this loop will execute?
#include<stdio.h>
int main(){
    char c=125;
    do
         printf("%d ",c);
    while(c++);
    return 0;
}



Explanation



(15)What will be output of following c code?
        
#include<stdio.h>
int main(){
    int x=123;
    int i={
         printf("c" "++")
    };
    for(x=0;x<=i;x++){
         printf("%x ",x);
    }
    return 0;
}


Explanation

C pointers interview questions and answers


1.
What will be output of following program?

#include<stdio.h>
int main(){
   int a = 320;
   char *ptr;
   ptr =( char *)&a;
   printf("%d ",*ptr);
   return 0;
}
(A) 2
(B) 320
(C) 64

(D) Compilation error
(E) None of above


Explanation:



2.
What will be output of following program?
#include<stdio.h>
#include<conio.h>
int main(){
   void (*p)();
   int (*q)();
   int (*r)();
   p = clrscr;
   q = getch;
   r = puts;
  (*p)();
  (*r)("cquestionbank.blogspot.com");
  (*q)();
  return 0;
}

(A) NULL
(B) cquestionbank.blogspot.com

(C) c
(D) Compilation error

(E) None of above





4.
What will be output of following program?
#include<stdio.h>
int main(){
   char far *p =(char far *)0x55550005;
   char far *q =(char far *)0x53332225;
   *p = 80;
   (*p)++;
   printf("%d",*q);
   return 0;
}
(A) 80
(B) 81

(C) 82
(D) Compilation error

(E) None of above


Explanation:

Turbo C++ 3.0: 81

Turbo C ++4.5: Compilation error

Linux GCC: Compilation error

Visual C++: Compilation error



Far address of p and q are representing same physical address.

Physical address of 0x55550005 = (0x5555) * (0x10) + (0x0005) = 0x55555

Physical address of 0x53332225 = (0x5333 * 0x10) + (0x2225) = 0x55555

*p = 80, means content at memory location 0x55555 is assigning value 25

(*p)++ means increase the content by one at memory location 0x5555 so now content at memory location 0x55555 is 81

*q also means content at memory location 0x55555 which is 26

5.
What will be output of following program?
#include<stdio.h>
#include<string.h>
int main(){
char *ptr1 = NULL;
char *ptr2 = 0;
strcpy(ptr1," c");
strcpy(ptr2,"questions");
printf("\n%s %s",ptr1,ptr2);
return 0;
}
(A) c questions
(B) c (null)
(C) (null) (null)
(D) Compilation error
(E) None of above



Explanation:

Turbo C++ 3.0: (null) (null)

Turbo C ++4.5: Run time error

Linux GCC: Run time error

Visual C++: Run time error


We cannot assign any string constant in null pointer by strcpy function.

6.
What will be output of following program?
#include<stdio.h>
int main(){
int huge *a =(int huge *)0x59990005;
int huge *b =(int huge *)0x59980015;
if(a == b)
printf("power of pointer");
else
printf("power of c");
return 0;
}
(A) power of pointer

(B) power of c
(C) power of cpower of c
(D) Compilation error

(E) None of above


Explanation:

Turbo C++ 3.0: power of pointer

Turbo C ++4.5: power of c

Linux GCC: Compilation error

Visual C++: Compilation error


Here we are performing relational operation between two huge addresses. So first of all both a and b will normalize as:

a= (0x5999)* (0x10) + (0x0005) =0x9990+0x0005=0x9995

b= (0x5998)* (0x10) + (0x0015) =0x9980+0x0015=0x9995

Here both huge addresses are representing same physical address. So a==b is true.

7.
What will be output of following program?
#include<stdio.h>
#include<string.h>
int main(){
register a = 25;
int far *p;
p=&a;
printf("%d ",*p);
return 0;
}
(A) 25
(B) 4
(C) Address
(D) Compilation error
(E) None of above



Explanation:


8.
What will be output of following program?
#include<stdio.h>
#include<string.h>
int main(){
char far *p,*q;
printf("%d %d",sizeof(p),sizeof(q));
return 0;
}
(A) 2 2
(B) 4 4

(C) 4 2

(D) 2 4

(E) None of above


Explanation:

9.
What will be output of following program?
#include<stdio.h>
int main(){
int a = 10;
void *p = &a;
int *ptr = p;
printf("%u",*ptr);
return 0;
}
(A) 10
(B) Address

(C) 2
(D) Compilation error
(E) None of above



Explanation:

10.
What will be output of following program?
#include<stdio.h>
#include<string.h>
int main(){
int register a;
scanf("%d",&a);
printf("%d",a);
return 0;
}
//if a=25

(A) 25
(B) Address

(C) 0
(D) Compilation error
(E) None of above


Explanation:


11.
What will be output of following program?
#include<stdio.h>
int main(){
char arr[10];
arr = "world";
printf("%s",arr);
return 0;
}
(A) world
(B) w

(C) Null
(D) Compilation error

(E) None of above


Explanation:

12.
What will be output of following program?
#include<stdio.h>
#include<string.h>
int main(){
int a,b,c,d;
char *p = ( char *)0;
int *q = ( int *q)0;
float *r = ( float *)0;
double *s = 0;
a = (int)(p+1);
b = (int)(q+1);
c = (int)(r+1);
d = (int)(s+1);
printf("%d %d %d %d",a,b,c,d);
return 0;
}
(A) 2 2 2 2
(B) 1 2 4 8
(C) 1 2 2 4
(D) Compilation error
(E) None of above


Explanation:



13.
What will be output of following program?
#include<stdio.h>
#include<string.h>
int main(){
int a = 5,b = 10,c;
int *p = &a,*q = &b;
c = p - q;
printf("%d" , c);
return 0;
}
(A) 1
(B) 5
(C) -5

(D) Compilation error

(E) None of above


Explanation:


14.
What will be output of following program?
#include<stdio.h>
unsigned long int (* avg())[3]{
static unsigned long int arr[3] = {1,2,3};
return &arr;
}
int main(){
unsigned long int (*ptr)[3];
ptr = avg();
printf("%d" , *(*ptr+2));
return 0;
}
(A) 1
(B) 2
(C) 3
(D) Compilation error
(E) None of above


Explanation:

15.
What will be output of following program?
#include<stdio.h>
int main(){
int * p , b;
b = sizeof(p);
printf("%d" , b);
return 0;
}
(A) 2
(B) 4

(C) 8
(D) Compilation error
(E) None of above


Explanation:

16.
What will be output of following program?
#include<stdio.h>
int main(){
int i = 5 , j;
int *p , *q;
p = &i;
q = &j;
j = 5;
printf("%d %d",*p,*q);
return 0;
}
(A) 5 5
(B) Address Address
(C) 5 Address
(D) Compilation error
(E) None of above


Explanation:

17.
What will be output of following program?

#include<stdio.h>
int main(){
int i = 5;
int *p;
p = &i;
printf(" %u %u", *&p , &*p);
return 0;
}
(A) 5 Address
(B) Address Address
(C) Address 5
(D) Compilation error
(E) None of above


Explanation:

18.
What will be output of following program?
#include<stdio.h>
int main(){
int i = 100;
printf("value of i : %d addresss of i : %u",i,&i);
i++;
printf("\nvalue of i : %d addresss of i : %u",i,&i);
return 0;
}

(A)
value of i : 100 addresss of i : Address
value of i : 101 addresss of i : Address

(B)
value of i : 100 addresss of i : Address
value of i : 100 addresss of i : Address

(C)
value of i : 101 addresss of i : Address
value of i : 101 addresss of i : Address

(D) Compilation error

(E) None of above


Explanation:

19.
What will be output of following program?
#include<stdio.h>  
int main(){
char far *p =(char far *)0x55550005;
char far *q =(char far *)0x53332225;
*p = 25;
(*p)++;
printf("%d",*q);
return 0;
}
(A) 25
(B) Address

(C) Garbage
(D) Compilation error
(E)None of above



Explanation:

20.
What will be output of following program?
#include<stdio.h>  
int main(){
int i = 3;
int *j;
int **k;
j = &i;
k = &j;
printf("%u %u %u",i,j,k);
return 0;
}
(A) 3 Address 3
(B) 3 Address Address
(C) 3 3 3
(D) Compilation error
(E) None of above


Explanation:

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন