Friday, February 24, 2012

string

(1)

Without using any semicolon (;) in program write a c program which output is: HELLO WORLD?

Answer:
void main()
{
if(printf("HELLO WORLD"))
{
}
}

(2)

What will be output of following code?
void main()
{
char a[5];
a[0]='q';
a[1]='u';
a[2]='e';
clrscr();
printf("%s",a);
getch();
}
Output: garbage
Explanation: %s is used for string but a is not a string it is only array of character since its last character is not null character.
If you will write a[3]=’\0’; then output will be que.

(3)

Write a scanf statement which can store one line of string which includes white space.

Answer:
void main()
{
char a[30];
clrscr();
scanf("%[^\n]",a);
printf("%s",a);
getch();
}

(4)

Write a c program in which a scanf function can store a paragraph at time?

Answer:
void main()
{
char a[30];
clrscr();
scanf("%[^\t]",a);
printf("%s",a);
getch();
}
Note: Paragraph will end when you will press tab key.

(5)

In the following program
void main()
{
extern char a[30];
}
How much array a is reserving memory space?
Answer:
It is not reserving any memory space because array a has only declared it has not initialized .Any not initialized variable doesn’t reserve any memory space.

data types


1.
What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    printf("%d\t",sizeof(6.5));
    printf("%d\t",sizeof(90000));
    printf("%d",sizeof('A'));
}

Choose all that apply:
(A) 4 2 1
(B) 8 2 1
(C) 4 4 1
(D) 8 4 1
(E) 8 4 2

Explanation: D

2.
Consider on following declaring of enum.
(i)        enum  cricket {Gambhir,Smith,Sehwag}c;
(ii)      enum  cricket {Gambhir,Smith,Sehwag};
(iii)    enum   {Gambhir,Smith=-5,Sehwag}c;
(iv)      enum  c {Gambhir,Smith,Sehwag};
Choose correct one:
(A)Only (i) is correct declaration
(B) Only (i) and (ii) is correct declaration
(C) Only (i) and (iii) are correct declaration
(D) Only (i),(ii) and are correct declaration
(E) All four are correct declaration

Explanation: E

3.
What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    signed x;
    unsigned y;
    x = 10 +- 10u + 10u +- 10;
    y = x;
    if(x==y)
         printf("%d %d",x,y);
    else if(x!=y)
         printf("%u  %u",x,y);
}
Choose all that apply:
(A) 0 0
(B) 65536 -10
(C) 0 65536
(D) 65536 0
(E) Compilation error

Explanation:A

4.
Which of the following is not modifier of data type in c?

(A) extern
(B) interrupt
(C) huge
(D) register
(E) All of these are modifiers of data type

Explanation:E

5.
What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    double num=5.2;
    int  var=5;
    printf("%d\t",sizeof(!num));
    printf("%d\t",sizeof(var=15/2));
    printf("%d",var);
}

Choose all that apply:
(A) 4 2 7
(B) 4 4 5
(C) 2 2 5
(D) 2 4 7
(E) 8 2 7

Explanation: C

6.
What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    const int *p;
    int a=10;
    p=&a;
    printf("%d",*p);
}

Choose all that apply:
(A) 0
(B) 10
(C) Garbage value
(D) Any memory address
(E) Error: Cannot modify const object

Explanation: B

7.
Consider on following declaration:
(i)        short i=10;
(ii)      static i=10;
(iii)    unsigned i=10;
(iv)      const i=10;

Choose correct one:
(A)Only (iv) is incorrect
(B) Only (ii) and (iv) are incorrect
(C) Only (ii),(iii) and (iv) are correct
(D) Only (iii) is correct
(E) All are correct declaration

Explanation: E

8.
What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    int a= sizeof(signed) +sizeof(unsigned);
    int b=sizeof(const)+sizeof(volatile);
    printf("%d",a+++b);
}

Choose all that apply:
(A) 10
(B) 9
(C) 8
(D) Error: Cannot find size of modifiers
(E) Error: Undefined operator +++

Explanation: C

9.
What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    signed x,a;
    unsigned y,b;
    a=(signed)10u;
    b=(unsigned)-10;
    y = (signed)10u + (unsigned)-10;
    x = y;
    printf("%d  %u\t",a,b);
    if(x==y)
         printf("%d %d",x,y);
    else if(x!=y)
         printf("%u  %u",x,y);
}

Choose all that apply:
(A) 10 -10   0 0
(B) 10 -10   65516 -10
(C) 10 -10   10 -10
(D) 10 65526      0 0
(E) Compilation error

Explanation: D

10.
Which of the following is integral data type?

(A) void
(B) char
(C) float
(D) double
(E) None of these

Explanation: B

11.
What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    volatile int a=11;
    printf("%d",a);
}

Choose all that apply:
(A) 11
(B) Garbage
(C) -2
(D) We cannot predict
(E) Compilation error

Explanation: D

12.
What is the range of signed int data type in that compiler in which size of int is two byte?

(A) -255 to 255
(B) -32767 to 32767
(C) -32768 to 32768
(D) -32767 to 32768
(E) -32768 to 32767

Explanation: E

13.
What will be output when you will execute following c code?

#include<stdio.h>
const enum Alpha{
      X,
      Y=5,
      Z
}p=10;
void main(){
    enum Alpha a,b;
    a= X;
    b= Z;
 
    printf("%d",a+b-p);
 
}

Choose all that apply:
(A) -4
(B) -5
(C) 10
(D) 11
(E) Error: Cannot modify constant object

Explanation: A

14.
What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    char a=250;
    int expr;
    expr= a+ !a + ~a + ++a;
    printf("%d",expr);
}

Choose all that apply:
(A) 249
(B) 250
(C) 0
(D) -6
(E) Compilation error

Explanation: D

15.
Consider on order of modifiers in following declaration:

(i)char volatile register unsigned c;
(ii)volatile register unsigned char c;
(iii)register volatile unsigned char c;
(iv)unsigned char volatile register c;

Choose correct one:
(A) Only (ii) is correct declaration
(B) Only (i) is correction declaration
(C) All are incorrect
(D) All are correct but they are different
(E) All are correct and same

Explanation: E

16.
What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    int a=-5;
    unsigned int b=-5u;
    if(a==b)
         printf("Avatar");
    else
         printf("Alien");
}

Choose all that apply:
(A) Avatar
(B) Alien
(C) Run time error
(D) Error: Illegal assignment
(E)   Error: Don’t compare signed no. with unsigned no.

Explanation: A

17.
What will be output when you will execute following c code?

#include<stdio.h>
extern enum cricket x;
void main(){
    printf("%d",x);
}
const enum cricket{
    Taylor,
    Kallis=17,
    Chanderpaul
}x=Taylor|Kallis&Chanderpaul;

Choose all that apply:
(A) 0
(B) 15
(C) 16
(D) 17
(E) Compilation error

Explanation: C

18.
Which of the following is not derived data type in c?

(A) Function
(B) Pointer
(C) Enumeration
(D) Array
(E) All are derived data type

Explanation: C

19.
What will be output when you will execute following c code?

#include<stdio.h>
enum A{
    x,y=5,
    enum B{
         p=10,q
    }varp;
}varx;

void main(){
    printf("%d %d",x,varp.q);
}

Choose all that apply:
(A) 0 11
(B) 5 10
(C) 4 11
(D)   0 10
(E) Compilation error

Explanation: E

20.
Consider on following declaration in c:

(i)short const register i=10;
(ii)static volatile const int i=10;
(iii)unsigned auto long register i=10;
(iv)signed extern float i=10.0;

Choose correct one:
(A) Only (iv)is correct
(B) Only (ii) and (iv) is correct
(C) Only (i) and (ii) is correct
(D) Only (iii) correct
(E) All are correct declaration

Explanation: C

Thursday, February 23, 2012

array


1.
What will be output if you will execute following c code?

#include<stdio.h>
void main(){
    char arr[7]="Network";
    printf("%s",arr);
   }

(A) Network
(B) N
(C) network
(D) Garbage value
(E) Compilation error
Explanation: **d  ,array size overflow....
2.
What will be output if you will execute following c code?

#include<stdio.h>
void main(){
    char arr[11]="The African Queen";
    printf("%s",arr);
}
(A) The African Queen
(B) The
(C) Queen
(D) null
(E) Compilation error
Explanation: **d
3.
What will be output if you will execute following c code?
#include<stdio.h>
void main(){
    char arr[20]="MysticRiver";
    printf("%d",sizeof(arr));  
}
(A) 20
(B) 11
(C) 12
(D) 22
(E) 24
Explanation: **a

4.
What will be output if you will execute following c code?
#include<stdio.h>
void main(){
    int const SIZE=5;
    int expr;
    double value[SIZE]={2.0,4.0,6.0,8.0,10.0};
    expr=1|2;
    printf("%f",value[expr]);
}
(A) 2.000000
(B) 4.000000
(C) 6.000000
(D) 8.000000
(E) Compilation error
Explanation: **d ,as precedence of bitwise inclusive OR ( | ) is more than  assignment operator so                               1|2=3  expr=3;

5.
What will be output if you will execute following c code?

#include<stdio.h>
enum power{
    Dalai,
    Vladimir=3,
    Barack,
    Hillary
};
void main(){
    float leader[Dalai+Hillary]={1.f,2.f,3.f,4.f,5.f};
    enum power p=Barack;
    printf("%0.f",leader[p>>1+1]);
}
(A) 1
(B) 2
(C) 3
(D) 5
(E) Compilation error
Explanation: **b  , initial value of p is 4 right shift by 2 make p=1 leader[1]=2

6.
What will be output if you will execute following c code?

#include<stdio.h>
#define var 3
void main(){
    char *cricket[var+~0]={"clarke","kallis"};
    char *ptr=cricket[1+~0];
    printf("%c",*++ptr);
}
(A) k
(B) a
(C) l
(D) i
(E) Compilation error
Explanation: **c

7.
What will be output if you will execute following c code?

#include<stdio.h>
#define var 3
void main(){
    char data[2][3][2]={0,1,2,3,4,5,6,7,8,9,10,11};
    printf("%o",data[0][2][1]);
}
(A) 5
(B) 6
(C) 7
(D) 8
(E) Compilation error
Explanation: **a

8.
What will be output if you will execute following c code?

#include<stdio.h>
#define var 3
void main(){
    short num[3][2]={3,6,9,12,15,18};
    printf("%d   %d",*(num+1)[1],**(num+2));
}
(A) 12  15
(B) 12  12
(C) 15  15
(D) 15  12
(E) Compilation error
Explanation: **c

9.
What will be output if you will execute following c code?

#include<stdio.h>
#define var 3
void main(){
    char *ptr="cquestionbank";
    printf("%d",-3[ptr]);
}
(A) 100
(B) -100
(C) 101
(D) -101
(E) Compilation error
Explanation: **d

10.
What will be output if you will execute following c code?

#include<stdio.h>
void main(){
    long  myarr[2][4]={0l,1l,2l,3l,4l,5l,6l,7l};
    printf("%ld\t",myarr[1][2]);
    printf("%ld%ld\t",*(myarr[1]+3),3[myarr[1]]);
    printf("%ld%ld%ld\t" ,*(*(myarr+1)+2),*(1[myarr]+2),3[1[myarr]]);
   }
(A) 6   66   777
(B) 6   77   667
(C) 5   66   777
(D) 7   77   666
(E) 6   67   667
Explanation: **b

11.
What will be output if you will execute following c code?

#include<stdio.h>
void main(){
    int array[2][3]={5,10,15,20,25,30};
    int (*ptr)[2][3]=&array;
    printf("%d\t",***ptr);
    printf("%d\t",***(ptr+1));
    printf("%d\t",**(*ptr+1));
    printf("%d\t",*(*(*ptr+1)+2));
}
(A) 5   Garbage value   20   30
(B)   5    15    20    25
(C)  10   20    30    30
(D) 5    15    20    30
(E)  Compilation error

Explanation: **a


12.
What will be output if you will execute following c code?

#include<stdio.h>
void main(){
    static int a=2,b=4,c=8;
    static int *arr1[2]={&a,&b};
    static int *arr2[2]={&b,&c};
    int* (*arr[2])[2]={&arr1,&arr2};
    printf("%d %d\t",*(*arr[0])[1],  *(*(**(arr+1)+1)));
}
(A) 2  4
(B) 4  2
(C) 4  8
(D) 8  4
(E) 8  2
Explanation: **c


13.
What will be output if you will execute following c code?

#include<stdio.h>
#include<math.h>
double myfun(double);
void main(){
    double(*array[3])(double);
    array[0]=exp;
    array[1]=sqrt;
    array[2]=myfun;
    printf("%.1f\t",(*array)((*array[2])((**(array+1))(4))));
}
double myfun(double d){
       d-=1;
       return d;
}
(A) 1.4
(B) 2.8
(C) 4.2
(D) 3.0
(E) 2.7
Explanation: **e


14.
What will be output if you will execute following c code?

#include<stdio.h>
#include<math.h>
typedef struct{
    char *name;
    double salary;
}job;
void main(){
    static job a={"TCS",15000.0};
    static job b={"IBM",25000.0};
    static job c={"Google",35000.0};
    int x=5;
    job * arr[3]={&a,&b,&c};
    printf("%s  %f\t",(3,x>>5-4)[*arr]);
}
double myfun(double d){
       d-=1;
       return d;
}
(A) Google 35000.000000
(B) TCS  15000.000000
(C) IBM  25000.000000
(D) null   15000.000000
(E) Google   null
Explanation: **a


15.
What will be output if you will execute following c code?

#include<stdio.h>
union group{
    char xarr[2][2];
    char yarr[4];
};
void main(){
    union group x={'A','B','C','D'};
    printf("%c",x.xarr[x.yarr[2]-67][x.yarr[3]-67]);
}
(A) A
(B) B
(C) C
(D) D
(E) null
Explanation: **b


16.
What will be output if you will execute following c code?

#include<stdio.h>
void main(){
    int a=5,b=10,c=15;
    int *arr[3]={&a,&b,&c};
    printf("%d",*arr[*arr[1]-8]);
}
(A) 5
(B) 10
(C) 18
(D) Garbage value
(E)   Compilation error

Explanation: **e


17.
What will be output if you will execute following c code?

#include<stdio.h>
void main(){
    int arr[][3]={{1,2},{3,4,5},{5}};
    printf("%d %d %d",sizeof(arr),arr[0][2],arr[1][2]);
}
(A)   6  0 4
(B) 6  1 5
(C) 18 0 5
(D) 18 1 5
(E) Compilation error
Explanation: **c


18.
What will be output if you will execute following c code?

#include<stdio.h>
void main(){
    int xxx[10]={5};
    printf("%d %d",xxx[1],xxx[9]);
}
(A) 0  5
(B) 5  5
(C) 5  0
(D) 0  0
(E) Compilation error
Explanation: **d
19.
What will be output if you will execute following c code?

#include<stdio.h>
#define WWW -1
enum {cat,rat};
void main(){
    int Dhoni[]={2,'b',0x3,01001,'\x1d','\111',rat,WWW};
    int i;
    for(i=0;i<8;i++)
         printf(" %d",Dhoni[i]);
}
(A) 2 98 3 513 29 73 0 -1
(B) 2 98 3 513 30 73 1 -1
(C) 2 99 3 513 29 73 1 -1
(D) 2 98 3 513 29 73 1 -1
(E) Compilation error
Explanation: **d

20.
What will be output if you will execute following c code?

#include<stdio.h>
void main(){
    long double a;
    signed char b;
    int arr[sizeof(!a+b)];
    printf("%d",sizeof(arr))
}
(A) 8
(B) 9
(C) 1
(D) 4
(E) Compilation error
Explanation: **d

Wednesday, February 22, 2012

pointers


1. 

What will be output of following program?
#include<stdio.h>
void main(){
   int a = 320;
   char *ptr;
   ptr =( char *)&a;
   printf("%d ",*ptr);
   getch();
}

(A) 2
(B) 320
(C) 64
(D) Compilation error
(E) None of above

Explanation: c

As we know int is two byte data byte while char is one byte data byte. char pointer can keep the address one byte at time.
Binary value of 320 is 00000001 01000000 (In 16 bit)
Memory representation of int a = 320 is:
So ptr is pointing only first 8 bit which color is green and Decimal value is 64.

2. 

What will be output of following program?

#include<stdio.h>
#include<conio.h>
void main(){
   void (*p)();
   int (*q)();
   int (*r)();
   p = clrscr;
   q = getch;
   r = puts;
  (*p)();
  (*r)("cquestionbank.blogspot.com");
  (*q)();
}

(A) NULL
(B) cquestionbank.blogspot.com
(C) c
(D) Compilation error
(E) None of above

Explanation: b
p is pointer to function whose parameter is void and return type is also void. r and q is pointer to function whose parameter is void and return type is int . So they can hold the address of such function.

3. 

What will be output of following program?

#include<stdio.h>
void main(){
   int i = 3;
   int *j;
   int **k;
   j=&i;
   k=&j;
   printf(“%u %u %d ”,k,*k,**k);
}

(A) Address, Address, 3
(B) Address, 3, 3
(C) 3, 3, 3
(D) Compilation error
(E) None of above

Explanation: a
Memory representation
Here 6024, 8085, 9091 is any arbitrary address, it may be different.
Value of k is content of k in memory which is 8085
Value of *k means content of memory location which address k keeps.
k keeps address 8085 .
Content of at memory location 8085 is 6024
In the same way **k will equal to 3.
Short cut way to calculate:
Rule: * and & always cancel to each other
i.e. *&a = a
So *k = *(&j) since k = &j
*&j = j = 6024
And
**k = **(&j) = *(*&j) = *j = *(&i) = *&i = i = 3

4. 

What will be output of following program?

#include<stdio.h>
void main(){
   char far *p =(char far *)0x55550005;
   char far *q =(char far *)0x53332225;
   *p = 80;
   (*p)++;
   printf("%d",*q);
   getch();
}

(A) 80
(B) 81
(C) 82
(D) Compilation error
(E) None of above

Explanation:b
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>
void main(){
char *ptr1 = NULL;
char *ptr2 = 0;
strcpy(ptr1," c");
strcpy(ptr2,"questions");
printf("\n%s %s",ptr1,ptr2);
getch();
}

(A) c questions
(B) c (null)
(C) (null) (null)
(D) Compilation error
(E) None of above

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

6. 

What will be output of following program?

#include<stdio.h>
void 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");
getch();
}

(A) power of pointer
(B) power of c
(C) power of c power of c
(D) Compilation error
(E) None of above

Explanation: a
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>
void main(){
register a = 25;
int far *p;
p=&a;
printf("%d ",*p);
getch();
}

(A) 25
(B) 4
(C) Address
(D) Compilation error
(E) None of above

Explanation: d
Register data type stores in CPU. So it has not any memory address. Hence we cannot write &a.

8. 

What will be output of following program?

#include<stdio.h>
#include<string.h>
void main(){
char far *p,*q;
printf("%d %d",sizeof(p),sizeof(q));
getch();
}

(A) 2 2
(B) 4 4
(C) 4 2
(D) 2 4
(E) None of above

Explanation: c
p is far pointer which size is 4 byte.
By default q is near pointer which size is 2 byte.

9. 

What will be output of following program?

#include<stdio.h>
void main(){
int a = 10;
void *p = &a;
int *ptr = p;
printf("%u",*ptr);
getch();
}

(A) 10
(B) Address
(C) 2
(D) Compilation error
(E) None of above

Explanation: a
Void pointer can hold address of any data type without type casting. Any pointer can hold void pointer without type casting.

10.

What will be output of following program?

#include<stdio.h>
#include<string.h>
void main(){
int register a;
scanf("%d",&a);
printf("%d",a);
getch();
}

//if a=25

(A) 25
(B) Address
(C) 0
(D) Compilation error
(E) None of above

Explanation: d
Register data type stores in CPU. So it has not any memory address. Hence we cannot write &a.

11. 

What will be output of following program?

#include<stdio.h>
void main(){
char arr[10];
arr = "world";
printf("%s",arr);
getch();
}

(A) world
(B) w
(C) Null
(D) Compilation error
(E) None of above

Explanation: d
Compilation error Lvalue required
Array name is constant pointer and we cannot assign any value in constant data type after declaration.

12.

What will be output of following program?

#include<stdio.h>
#include<string.h>
void 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);
}

(A) 2 2 2 2
(B) 1 2 4 8
(C) 1 2 2 4
(D) Compilation error
(E) None of above

Explanation: b
Address = next address
Since initial address of all data type is zero. So its
next address will be size of data type.

13. 

What will be output of following program?

#include<stdio.h>
#include<string.h>
void main(){
int a = 5,b = 10,c;
int *p = &a,*q = &b;
c = p - q;
printf("%d" , c);
getch();
}

(A) 1
(B) 5
(C) -5
(D) Compilation error
(E) None of above

Explanation: a
Difference of two same type of pointer is always one.

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;
}
void main(){
unsigned long int (*ptr)[3];
ptr = avg();
printf("%d" , *(*ptr+2));
getch();
}

(A) 1
(B) 2
(C) 3
(D) Compilation error
(E) None of above

Explanation:c

15. 

What will be output of following program?

#include<stdio.h>
void main(){
int * p , b;
b = sizeof(p);
printf(“%d” , b);
}

(A) 2
(B) 4
(C) 8
(D) Compilation error
(E) None of above

Explanation: e

Output: 2 or 4
since in this question it has not written p is which type of pointer. So its output will depend upon which memory model has selected. Default memory model is small.


16. 

What will be output of following program?

#include<stdio.h>
void main(){
int i = 5 , j;
int *p , *q;
p = &i;
q = &j;
j = 5;
printf("value of i : %d value of j : %d",*p,*q);
getch();
}

(A) 5 5
(B) Address Address
(C) 5 Address
(D) Compilation error
(E) None of above

Explanation: a

17. 

What will be output of following program?

#include<stdio.h>
void main(){
int i = 5;
int *p;
p = &i;
printf(" %u %u", *&p , &*p);
getch();
}

(A) 5 Address
(B) Address Address
(C) Address 5
(D) Compilation error
(E) None of above

Explanation: b
Since * and & always cancel to each other.
i.e. *&a = a
so *&p = p which store address of integer i
&*p = &*(&i) //since p = &i
= &(*&i)
= &i
So second output is also address of i

18.

What will be output of following program?

#include<stdio.h>
void 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);
getch();
}

(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: a
Within the scope of any variable, value of variable may change but its address will never change in any modification of variable.

19. 

What will be output of following program?

#include<stdio.h>
void main(){
char far *p =(char far *)0x55550005;
char far *q =(char far *)0x53332225;
*p = 25;
(*p)++;
printf("%d",*q);
getch();
}

(A) 25
(B) Address
(C) Garbage
(D) Compilation error
(E)None of above

Explanation: e
Far address of p and q are representing same physical address. Physical address of
0x55550005 = 0x5555 * ox10 + ox0005 = 0x55555
Physical address of
0x53332225 = 0x5333 * 0x10 + ox2225 = 0x55555
*p = 25, means content at memory location 0x55555 is assigning value 25
(*p)++ means to increase the content by one at memory the location 0x5555 so now content of memory location at 0x55555 is 26
*q also means content at memory location 0x55555 which is 26

20.

What will be output of following program?

#include<stdio.h>
void main(){
int I = 3;
int *j;
int **k;
j = &i;
k = &j;
printf(“%u %u %u”,i,j,k);
}

(A) 3 Address 3
(B) 3 Address Address
(C) 3 3 3
(D) Compilation error
(E) None of above

Explanation: b
Here 6024, 8085, 9091 is any arbitrary address, it may be different.


C question set 6


1=

main()
{
int i = 3;
for (;i++=0;) printf("%d",i);
}
Answer:

Compiler Error: Lvalue required.
Explanation:

As we know that increment operators return rvalues and hence it cannot appear on the left hand side of an
assignment operation.

2=

main()
{
int i=10,j=20;
j = i, j?(i,j)?i:j:j;
printf("%d %d",i,j);
}
Answer:

10 10
Explanation:

The Ternary operator ( ? : ) is equivalent for if-then-else statement. So the question can be written as:
if(i,j)
      {
if(i,j)
    j = i;
else
   j = j;
}
  else
j = j;

3=

const char *a;
char* const a;
char const *a;

-Differentiate the above declarations.
Answer:

'const' applies to char * rather than 'a' ( pointer to a constant char ) 
*a='F' : illegal 
a="Hi" : legal
'const' applies to 'a' rather than to the value of a (constant pointer to char ) 
*a='F' : legal 
a="Hi" : illegal
Same as 1.

4=

main()
{
int i=4,j=7;
j = j || i++ && printf("YOU CAN");
printf("%d %d", i, j);
}
Answer:  4 1


Explanation:

The boolean expression needs to be evaluated only till the truth value of the expression is not known.
 j is not equal to zero itself means that the expression's truth value is 1. Because it is followed by || and
true || (anything) => true where (anything) will not be evaluated. So the remaining expression is not
evaluated and so the value of i remains the same. Similarly when && operator is involved in an expression,
 when any of the operands become false, the whole expression's truth value becomes false and hence the
 remaining expression will not be evaluated.
false && (anything) => false where (anything) will not be evaluated.

5=

main()
{
register int a=2;
printf("Address of a = %d",&a);
printf("Value of a   = %d",a);
}
Answer:

Compier Error: '&' on register variable 
Rule to Remember:
& (address of ) operator cannot be applied on register variables.

C question set 5


1===========

Is the following statement a declaration/definition. Find what does it mean?
int (*x)[10];
Answer:

Definition.
x is a pointer to array of(size 10) integers.
Apply clock-wise rule to find the meaning of this definition.


2===========

What is the output for the program given below
typedef enum errorType{warning, error, exception,}error;
     main()
    {
        error g1;
        g1=1;
        printf("%d",g1);
     }
Answer:

Compiler error: Multiple declaration for error
Explanation:

The name error is used in the two meanings. One means that it is a enumerator constant with value 1.
 The another use is that it is a type name (due to typedef) for enum errorType. Given a situation the
 compiler cannot distinguish the meaning of error to know in what sense the error is used:
error g1;
g1=error;
// which error it refers in each case? When the compiler can distinguish between usages then it will not
 issue error (in pure technical terms, names can only be overloaded in different namespaces).
Note:

the extra comma in the declaration,
enum errorType{warning, error, exception,}
is not an error. An extra comma is valid and is provided just for programmer's convenience.



3============


#ifdef something
int some=0;
#endif

main()
{
int thing = 0;
printf("%d %d\n", some ,thing);
}
Answer:

Compiler error : undefined symbol some
Explanation:

This is a very simple example for conditional compilation. The name something is not already known to the
compiler making the declaration int some = 0; effectively removed from the source code.


4============


#if something == 0
int some=0;
#endif

main()
{
int thing = 0;
printf("%d %d\n", some ,thing);
}
Answer:

0 0
Explanation:

This code is to show that preprocessor expressions are not the same as the ordinary expressions. If a name
 is not known the preprocessor treats it to be equal to zero.

5============

main()
{
char *p = "ayqm";
printf("%c",++*(p++));
}

Answer: z

6============

main()
{
char *p = "ayqm";
char c;
c = ++*p++;
printf("%c",c);
}
Answer:

b
Explanation:

There is no difference between the expression ++*(p++) and ++*p++. Parenthesis just works as a visual
 clue for the reader to see which expression is first evaluated.

7============

#include<stdio.h>




main()
{
char *p = "ayqm";
printf("%c...%c....%c",*p,++*(p++),*p);
}

Answer:y...z....a

8============

int aaa() {printf("Hi");}
int bbb(){printf("hello");}
iny ccc(){printf("bye");}

main()
{
int ( * ptr[3]) ();
ptr[0] = aaa;
ptr[1] = bbb;
ptr[2] =ccc;
ptr[2]();
}
Answer:

bye
Explanation:

int (* ptr[3])() says that ptr is an array of pointers to functions that takes no arguments and returns the
type int. By the assignment ptr[0] = aaa; it means that the first function pointer in the array is initialized
 with the address of the function aaa. Similarly, the other two array elements also get initialized with the
addresses of the functions bbb and ccc. Since ptr[2] contains the address of the function ccc, the call to
 the function ptr[2]() is same as calling ccc(). So it results in printing "bye".

9============
main()
{
while (strcmp("some","some\0"))
printf("Strings are not equal\n");
}
Answer:

No output
Explanation:

Ending the string constant with \0 explicitly makes no difference. So "some" and "some\0" are equivalent.
So, strcmp returns 0 (false) hence breaking out of the while loop.

10===========

main()
{
char str1[] = {'s','o','m','e'};
char str2[] = {'s','o','m','e','\0'};
while (strcmp(str1,str2))
printf("Strings are not equal\n");
}
Answer:

"Strings are not equal"
"Strings are not equal" ...
Explanation:

If a string constant is initialized explicitly with characters, '\0' is not appended automatically to the string.
 Since str1 doesn't have null termination, it treats whatever the values that are in the following positions as
 part of the string until it randomly reaches a '\0'. So str1 and str2 are not the same, hence the result.

C question set 4


1-----------------------------------------------------------------------------------------

What are the following notations of defining functions known as?
int abc(int a,float b)
{
/* some code */
}
int abc(a,b)
int a; float b;
{
/* some code*/
}
Answer:


i. ANSI C notation 
ii. Kernighan & Ritche notation (K&R Notation)


2-----------------------------------------------------------------------------------------


main()
{
char *p;
p="%d\n";
            p++;
            p++;
            printf(p-2,300);
}
Answer:

300
Explanation:
The pointer points to % since it is incremented twice and again decremented by 2, it points to '%d\n' and 300 is
 printed.


3-----------------------------------------------------------------------------------------

main()
{
 int i;
 i = abc();
 printf("%d",i);
}
abc()
{
 _AX = 1000;
}
Answer: 1000
Explanation:< blockquote>Normally the return value from the function is through the information from the
accumulator. Here _AH is the pseudo global variable denoting the accumulator. Hence, the value of the
accumulator is set 1000 so the function returns value 1000.

4-----------------------------------------------------------------------------------------

int i;
        main(){
int t;
for ( t=4;scanf("%d",&i)-t;printf("%d\n",i))
              printf("%d--",t--);
            }
// If the inputs are 0,1,2,3 find the o/p
Answer:

4--0 
3--1 
2--2
Explanation:

Let us assume some x= scanf("%d",&i)-t the values during execution will be,
t i x
4 0 -4
3 1 -2
2 2 0



5--------------------------------------------------------------------------------------------

main(){
  int a= 0;int b = 20;char x =1;char y =10;
  if(a,b,x,y)
        printf("hello");
 }
Answer:

hello
Explanation:

The comma operator has associativity from left to right. Only the rightmost value is returned and the
 other values are evaluated and ignored. Thus the value of last variable y is returned to check in if.
 Since it is a non zero value if becomes true so, "hello" will be printed.


6----------------------------------------------------------------------------------------------

main(){
 unsigned int i;
 for(i=1;i>-2;i--)
 printf("c aptitude");
}
Explanation:

i is an unsigned integer. It is compared with a signed value. Since the both types doesn't match, signed
is promoted to unsigned value. The unsigned equivalent of -2 is a huge value so condition becomes false and
control comes out of the loop.



7------------------------------------------------------------------------------------------------

func(a,b)
int a,b;
{
 return( a= (a==b) );
}
main()
{
int process(),func();
printf("The value of process is %d !\n ",process(func,3,6));
}
process(pf,val1,val2)
int (*pf) ();
int val1,val2;
{
return((*pf) (val1,val2));
 }


Answer: The value if process is 0 !
Explanation:
The function 'process' has 3 parameters - 1, a pointer to another function 2 and 3, integers. When this
function is invoked from main, the following substitutions for formal parameters take place: func for pf, 3 for
 val1 and 6 for val2. This function returns the result of the operation performed by the function 'func'.
 The function func has two integer parameters. The formal parameters are substituted as 3 for a and 6 for b.
since 3 is not equal to 6, a==b returns 0. therefore the function returns 0 which in turn is returned by the
function 'process'.


8-----------------------------------------------------------------------------------------------

void main()
{
void *v;
int integer=2;
int *i=&integer;
v=i;
printf("%d",(int*)*v);
}
Answer:

Compiler Error. We cannot apply indirection on type void*.
Explanation:

Void pointer is a generic pointer type. No pointer arithmetic can be done on it. Void pointers are normally used for,
Passing generic pointers to functions and returning such pointers.
As a intermediate pointer type.
Used when the exact pointer type will be known at a later point of time.

9--------------------------------------------------------------------------------------------------

void main()
{
int i=i++,j=j++,k=k++;
printf("%d%d%d",i,j,k);
}
Answer:

Garbage values.
Explanation:

An identifier is available to use in program code from the point of its declaration. So expressions such
 as i = i++ are valid statements. The i, j and k are automatic variables and so they contain some garbage
 value. Garbage in is garbage out (GIGO).


10---------------------------------------------------------------------------------------------------

void main()
{
static int i=i++, j=j++, k=k++;
printf("i = %d j = %d k = %d", i, j, k);
}
Answer:

i = 1 j = 1 k = 1
Explanation:

Since static variables are initialized to zero by default.

C question set 3


(Q-1)

main()
{
printf("%d", out);
}
int out=100;
Answer:

Compiler error: undefined symbol out in function main.
Explanation:

The rule is that a variable is available for use from the point of declaration. Even though a is a global
variable, it is not available for main. Hence an error.


(Q-2)

main()
{
 extern out;
 printf("%d", out);
}
 int out=100;
Answer:

100
Explanation:

This is the correct way of writing the previous program.

(Q-3)

main( )
{
  int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}};
  printf("%u %u %u %d \n",a,*a,**a,***a);
 printf("%u %u %u %d \n",a+1,*a+1,**a+1,***a+1);
  }
Answer:
100, 100, 100, 2 
114, 104, 102, 3

Explanation:

The given array is a 3-D one. It can also be viewed as a 1-D array.
2 4 7 8 3 4 2 2 2 3 3 4
100 102 104 106 108 110 112 114 116 118 120 122
thus, for the first printf statement a, *a, **a give address of first element . since the indirection ***a gives
the value. Hence, the first line of the output.

for the second printf a+1 increases in the third dimension thus points to value at 114, *a+1 increments in second
dimension thus points to 104, **a +1 increments the first dimension thus points to 102 and ***a+1 first gets the
value at first location and then increments it by 1. Hence, the output.

(Q-4)

main( )
{
 static int  a[ ]   = {0,1,2,3,4};
 int  *p[ ] = {a,a+1,a+2,a+3,a+4};
 int  **ptr =  p;
 ptr++;
 printf("\n %d  %d  %d", ptr-p, *ptr-a, **ptr);
 *ptr++;
 printf("\n %d  %d  %d", ptr-p, *ptr-a, **ptr);
 *++ptr;
 printf("\n %d  %d  %d", ptr-p, *ptr-a, **ptr);
 ++*ptr;
printf("\n %d  %d  %d", ptr-p, *ptr-a, **ptr);
}



Answer:
111 
222 
333 
344
Explanation:

Let us consider the array and the two pointers with some address
a
0 1 2 3 4
100 102 104 106 108
p
100 102 104 106 108
1000 1002 1004 1006 1008
ptr
1000
2000
After execution of the instruction ptr++ value in ptr becomes 1002, if scaling factor for integer is 2 bytes.
 Now ptr - p is value in ptr - starting location of array p, (1002 - 1000) / (scaling factor) = 1,
 *ptr - a = value at address pointed by ptr - starting value of array a, 1002 has a value 102 so the value
is (102 - 100)/(scaling factor) = 1, **ptr is the value stored in the location pointed by the pointer of
ptr = value pointed by value pointed by 1002 = value pointed by 102 = 1. Hence the output of the firs printf is 1, 1, 1.

After execution of *ptr++ increments value of the value in ptr by scaling factor, so it becomes1004. Hence,
 the outputs for the second printf are ptr - p = 2, *ptr - a = 2, **ptr = 2.

After execution of *++ptr increments value of the value in ptr by scaling factor, so it becomes1004. Hence,
 the outputs for the third printf are ptr - p = 3, *ptr - a = 3, **ptr = 3.

After execution of ++*ptr value in ptr remains the same, the value pointed by the value is incremented by the
 scaling factor. So the value in array p at location 1006 changes from 106 10 108,. Hence, the outputs for the
 fourth printf are ptr - p = 1006 - 1000 = 3, *ptr - a = 108 - 100 = 4, **ptr = 4.

(Q-5)

main( )
{
 void *vp;
 char ch = 'g', *cp = "goofy";
 int j = 20;
 vp = &ch;
 printf("%c", *(char *)vp);
 vp = &j;
 printf("%d",*(int *)vp);
 vp = cp;
 printf("%s",(char *)vp + 3);
}
Answer:

g20fy
Explanation:

Since a void pointer is used it can be type casted to any other type pointer. vp = &ch stores address of char ch
 and the next statement prints the value stored in vp after type casting it to the proper data type pointer.
 the output is 'g'. Similarly the output from second printf is '20'. The third printf statement type casts it
 to print the string from the 4th value hence the output is 'fy'.


(Q-6)

main ( )
{
 static char *s[ ]  = {"black", "white", "yellow", "violet"};
 char **ptr[ ] = {s+3, s+2, s+1, s}, ***p;
 p = ptr;
 **++p;
 printf("%s",*--*++p + 3);
}
Answer:

ck
Explanation:

In this problem we have an array of char pointers pointing to start of 4 strings. Then we have ptr which is a pointer to a pointer of type char and
 a variable p which is a pointer to a pointer to a pointer of type char. p hold the initial value of ptr, i.e. p = s+3. The next statement increment
value in p by 1 , thus now value of p = s+2. In the printf statement the expression is evaluated *++p causes gets value s+1 then the pre decrement is
executed and we get s+1-1 = s . the indirection operator now gets the value from the array of s and adds 3 to the starting address. The string is
 printed starting from this position. Thus, the output is 'ck'.


(Q-7)

main()
{
 int  i, n;
 char *x = "girl";
 n = strlen(x);
 *x = x[n];
 for(i=0; i‹n; ++i)
   {
      printf("%s\n",x);
      x++;
   }
 }

Answer:

(blank space) 
irl 
rl 
l
Explanation:

Here a string (a pointer to char) is initialized with a value "girl". The strlen function returns the length
 of the string, thus n has a value 4. The next statement assigns value at the nth location ('\0') to the first
location. Now the string becomes "\0irl" . Now the printf statement prints the string after each iteration it
increments it starting position. Loop starts from 0 to 4. The first time x[0] = '\0' hence it prints nothing
 and pointer value is incremented. The second time it prints from x[1] i.e "irl" and the third time it
 prints "rl" and the last time it prints "l" and the loop terminates.

(Q-8)

main()
{
int i=-1;
+i;
printf("i = %d, +i = %d \n",i,+i);
}
Answer:

i = -1, +i = -1
Explanation:

Unary + is the only dummy operator in C. Where-ever it comes you can just ignore it just because it has no
 effect in the expressions (hence the name dummy operator).

(Q-9)

What are the files which are automatically opened when a C file is executed?
Answer: stdin, stdout, stderr (standard input,standard output,standard error).


(Q-10)

main(){
  int * j;
  void fun(int **);
  fun(&j);
 }
 void fun(int **k) {
  int a =0;
  /* add a stmt here*/
 }
Answer:   *k = &a
Explanation:

The argument of the function is a pointer to a pointer.

C question set 2

(Q-6)

main()
{
   static char names[5][20]={"pascal","ada","cobol","fortran","perl"};
    int i;
    char *t;
    t=names[3];
    names[3]=names[4];
    names[4]=t;
    for (i=0;i<=4;i++)
    printf("%s",names[i]);
}
Answer: Compiler error: Lvalue required in function main
Explanation:

Array names are pointer constants. So it cannot be modified.

(Q-7)

void main()
{
int i=5;
printf("%d",i++ + ++i);
}
Answer: Output Cannot be predicted exactly.
Explanation:

Side effects are involved in the evaluation of i

(Q-8)

main()
{
int i;
printf("%d",scanf("%d",&i));  // value 10 is given as input here
}

Answer:1

Explanation:  Scanf returns number of items successfully read and not 1/0. Here 10 is given as input which should
have been scanned successfully. So number of items read is 1

(Q-9)

#define f(g,g2) g##g2
main()
{
int var12=100;
printf("%d",f(var,12));
}
Answer: 100


(Q-10)

main()
{
int i=0;

for(;i++;printf("%d",i)) ;
printf("%d",i);
}
Answer:  1
Explanation:  before entering into the for loop the checking condition is "evaluated". Here it evaluates to 0
(false) and comes out of the loop, and i is incremented (note the semicolon after the for loop).