Give the output of the programs in each case unless mentioned otherwise

  1. void main()
    {
    int d=5;
    printf("%f",d);
    }Ans: Undefined
  2. void main()
    {
    int i;
    for(i=1;i<4,i++)
    switch(i)
    case 1: printf("%d",i);break;
    {
    case 2:printf("%d",i);break;
    case 3:printf("%d",i);break;
    }
    switch(i) case 4:printf("%d",i);
    }Ans: 1,2,3,4
  3. void main()
    {
    char *s="\12345s\n";
    printf("%d",sizeof(s));
    }Ans: 6
  4. void main()
    {
    unsigned i=1; /* unsigned char k= -1 => k=255; */
    signed j=-1; /* char k= -1 => k=65535 */
    /* unsigned or signed int k= -1 =>k=65535 */
    if(i<j)
    printf("less");
    else
    if(i>j)
    printf("greater");
    else
    if(i==j)
    printf("equal");
    }Ans: less
  5. void main()
    {
    float j;
    j=1000*1000;
    printf("%f",j);
    }
    1. 1000000
    2. Overflow
    3. Error
    4. None
    Ans: 4
  6. How do you declare an array of N pointers to functions returning pointers to functions returning pointers to characters?Ans: The first part of this question can be answered in at least three ways:
  7. Build the declaration up incrementally, using typedefs:
    typedef char *pc; /* pointer to char */
    typedef pc fpc(); /* function returning pointer to char */
    typedef fpc *pfpc; /* pointer to above */
    typedef pfpc fpfpc(); /* function returning... */
    typedef fpfpc *pfpfpc; /* pointer to... */
    pfpfpc a[N]; /* array of... */
  8. Use the cdecl program, which turns English into C and vice versa:
    cdecl> declare a as array of pointer to function returning pointer to function returning pointer to char char *(*(*a[])())()
    cdecl can also explain complicated declarations, help with casts, and indicate which set of parentheses the arguments go in (for complicated function definitions, like the one above). Any good book on C should explain how to read these complicated C declarations "inside out" to understand them ("declaration mimics use"). The pointer-to-function declarations in the examples above have not included parameter type information. When the parameters have complicated types, declarations can *really* get messy. (Modern versions of cdecl can help here, too.)
  9. A structure pointer is defined of the type time . With 3 fields min,sec hours having pointers to intergers.
    Write the way to initialize the 2nd element to 10.
  10. In the above question an array of pointers is declared. Write the statement to initialize the 3rd element of the 2 element to 10
  11. int f()
    void main()
    {
    f(1);
    f(1,2);
    f(1,2,3);
    }
    f(int i,int j,int k)
    {
    printf("%d %d %d",i,j,k);
    }What are the number of syntax errors in the above?
    Ans: None.
  12. void main()
    {
    int i=7;
    printf("%d",i++*i++);
    }Ans: 56
  13. #define one 0
    #ifdef one
    printf("one is defined ");
    #ifndef one
    printf("one is not defined ");
    Ans: "one is defined"
  14. void main()
    {
    intcount=10,*temp,sum=0;
    temp=&count;
    *temp=20;
    temp=&sum;
    *temp=count;
    printf("%d %d %d ",count,*temp,sum);
    }
    Ans: 20 20 20
  15. There was question in c working only on unix machine with pattern matching.
  16. what is alloca() Ans : It allocates and frees memory after use/after getting out of scope
  17. main()
    {
    static i=3;
    printf("%d",i--);
    return i>0 ? main():0;
    }
    Ans: 321
  18. char *foo()
    {
    char result[100]);
    strcpy(result,"anything is good");
    return(result);
    }
    void main()
    {
    char *j;
    j=foo()
    printf("%s",j);
    }
    Ans: anything is good.
  19. void main()
    {
    char *s[]={ "dharma","hewlett-packard","siemens","ibm"};
    char **p;
    p=s;
    printf("%s",++*p);
    printf("%s",*p++);
    printf("%s",++*p);
    }Ans: "harma" (p->add(dharma) & (*p)->harma)
    "harma" (after printing, p->add(hewlett-packard) &(*p)->harma)
    "ewlett-packard"

Q1.Convert 0.9375 to binary
a) 0.0111
b) 0.1011
c) 0.1111
d) none
Ans. (c)

Q2.( 1a00 * 10b )/ 1010 = 100
a) a=0, b=0
b)a=0, b=1
c) none
Ans. (b)

Q3. In 32 bit memory machine 24 bits for mantissa and 8 bits for exponent. To increase the range of floating point.
a) more than 32 bit is to be there.
b) increase 1 bit for mantissa and decrease 1 bit for exponent
c) increase 1 bit for exponent and decrease one bit for mantissa

Q4.In C, "X ? Y : Z " is equal to
a) if (X==0) Y ;else Z
b) if (X!=0) Y ;else Z
c) if (X==0) Y ; Z
Ans. (b)

Q5. From the following program
foo()
int foo(int a, int b)
{
if (a&b) return 1;
return 0;
}
a) if either a or b are zero returns always 0
b) if both a & b are non zero returns always 1
c) if both a and b are negative returns 0

Q6. The following function gives some error. What changes have to be made
void ( int a,int b)
{
int t; t=a; a=b; b=t;
}
a) define void as int and write return t
b) change everywhere a to *a and b to *b

Q7. Which of the following is incorrect
a) if a and b are defined as int arrays then (a==b) can never betrue
b) parameters are passed to functions only by values
c) defining functions in nested loops

Q8. include<stdio.h>
void swap(int*,int*);
main()
{
int arr[8]={36,8,97,0,161,164,3,9}
for (int i=0; i<7; i++)
{
for (int j=i+1; j<8;j++)
if(arr[i]<arr[j]) swap(&arr[i],&arr[j]);
}
}
void swap(int*x,int*y)
{
int temp; static int cnt=0;
temp= *x;
*x=*y;
*y=temp;
cnt++;
}
What is cnt equal to
a) 7
b) 15
c) 1
d) none of these

Q9. int main()
{
FILE *fp;
fp=fopen("test.dat","w");
fprintf(fp,'hello\n");
fclose(fp);
fp=fopen ("test.dat","w");
fprintf (fp, "world");
fclose(fp);
return 0;
}
If text.dat file is already present after compiling and executionhow many bytes does the file occupy ?
a) 0 bytes
b) 5 bytes
c) 11 bytes
d) data is insufficient

Q10. f1(int*x,intflag)
int *y;
*y=*x+3;
switch(flag)
{
case 0:
*x=*y+1;
break;
case 1:
*x=*y;
break;
case 2:
*x=*y-1;
break;
}
return(*y)
main()
{
*x=5;
i=f1(x,0); j=f1(x,1);
printf("%d %d %d ",i,j,*x);
}
What is the output?
a) 8 8 8
b) 5 8 8
c) 8 5 8
d) none of these

1.
static int i;
{
i=10;
...
}
printf("%d",i);
Ans: 10
2.
#define func1(a) #a
#define func2(a,b,c) a##b##c
printf("%s",func1(func2(a,b,c)))
Ans: func2(a,b,c)
3.
const int* ptr;
int* ptr1;
int a=10;
const int p=20;
ptr=a;
ptr1=p;
4.
class a
virtual disp()
{ printf("In a");}
class b:public a
disp()
{ printf("In b");}
class c:public a
disp()
{ printf("In c");}
main()
{
a obj;
b objb;
c objc;
a=objb;
a.disp();
a=objc;
a.disp();
Ans: "In a" "In a"
5.
a="str";
char *b="new str";
char *temp;
malloc(sizeof(temp)+1,....
strcpy(a,temp);
malloc(sizeof(b)+1,....
strcpy(temp,b);
6.
int m,i=1,j=0,k=-1;
m=k++||j++&i++;
printf("%d...",m,i,j,k);
7.
class x
{
double b;
double *l;
float &c;
}
main()
{
double g=10.34;
double *f=1.3;
float k=9;
x o;
o.b=g;
o.l=f;
o.c=k;
}
Ans: Compiler Error

  1. Point out error, if any, in the following program
    main()
    {
    int i=1;
    switch(i)
    {
    case 1:
    printf("\nRadioactive cats have 18 half-lives");
    break;
    case 1*2+4:
    printf("\nBottle for rent -inquire within");
    break;
    }
    }
    Ans. No error. Constant expression like 1*2+4 are acceptable in cases of a switch.
  2. Point out the error, if any, in the following program
    main()
    {
    int a=10,b;
    a>= 5 ? b=100 : b=200;
    printf("\n%d",b);
    }
    Ans. lvalue required in function main().The second assignment should be written in parenthesis as follows:
    a>= 5 ? b=100 : (b=200);
  3. In the following code, in which order the functions would be called?
    a= f1(23,14)*f2(12/4)+f3();
    a) f1, f2, f3 b) f3, f2, f1
    c) The order may vary from compiler to compiler d) None of the above
  4. What would be the output of the following program?
    main()
    {
    int i=4;
    switch(i)
    {
    default:
    printf("\n A mouse is an elephant built by the Japanese");
    case 1:
    printf(" Breeding rabbits is a hair raising experience");
    break;
    case 2:
    printf("\n Friction is a drag");
    break;
    case 3:
    printf("\n If practice make perfect, then nobody's perfect");
    }
    }
    a)A mouse is an elephant built by the Japanese b) Breeding rabbits is a hare raising experience
    c) All of the above d) None of the above
  5. What is the output of the following program?
    #define SQR(x) (x*x)
    main()
    {
    int a,b=3;
    a= SQR(b+2);
    printf("%d",a);
    }
    a) 25 b) 11 c) error d) garbage value
  6. In which line of the following, an error would be reported?
    1. #define CIRCUM(R) (3.14*R*R);
    2. main()
    3. {
    4. float r=1.0,c;
    5. c= CIRCUM(r);
    6. printf("\n%f",c);
    7. if(CIRCUM(r))==6.28)
    8. printf("\nGobbledygook");
    9. }
    a) line 1 b) line 5 c) line 6 d) line 7
  7. What is the type of the variable b in the following declaration?
    #define FLOATPTR float*
    FLOATPTR a,b;
    a) float b) float pointer c) int d) int pointer
  8. In the following code;
    #include<stdio.h>
    main()
    {
    FILE *fp;
    fp= fopen("trial","r");
    }
    fp points to:
    a) The first character in the file.
    b) A structure which contains a "char" pointer which points to the first character in the file.
    c) The name of the file. d) None of the above.
  9. We should not read after a write to a file without an intervening call to fflush(), fseek() or rewind() < TRUE/FALSE>
    Ans. True
  10. If the program (myprog) is run from the command line as myprog 1 2 3 , What would be the output?
    main(int argc, char *argv[])
    {
    int i;
    for(i=0;i<argc;i++)
    printf("%s",argv[i]);
    }
    a)1 2 3 b) C:\MYPROG.EXE 1 2 3
    c) MYP d) None of the above
  11. If the following program (myprog) is run from the command line as myprog 1 2 3, What would be the output?
    main(int argc, char *argv[])
    {
    int i,j=0;
    for(i=0;i<argc;i++)
    j=j+ atoi(argv[i]);
    printf("%d",j);
    }
    a) 1 2 3 b) 6 c) error d) "123"
  12. If the following program (myprog) is run from the command line as myprog monday tuesday wednesday thursday,
    What would be the output?
    main(int argc, char *argv[])
    {
    while(--argc >0)
    printf("%s",*++argv);
    }
    a) myprog monday tuesday wednesday thursday b) monday tuesday wednesday thursday
    c) myprog tuesday thursday d) None of the above
  13. In the following code, is p2 an integer or an integer pointer?
    typedef int* ptr
    ptr p1,p2;
    Ans. Integer pointer
  14. Point out the error in the following program
    main()
    {
    const int x;
    x=128;
    printf("%d",x);
    }
    Ans. x should have been initialized where it is declared.
  15. What would be the output of the following program?
    main()
    {
    int y=128;
    const int x=y;
    printf("%d",x);
    }
    a) 128 b) Garbage value c) Error d) 0
  16. What is the difference between the following declarations?
    const char *s;
    char const *s;
    Ans. No difference
  17. What would be the output of the following program?
    main()
    {
    char near * near *ptr1;
    char near * far *ptr2;
    char near * huge *ptr3;
    printf("%d %d %d",sizeof(ptr1),sizeof(ptr2),sizeof(ptr3));
    }
    a) 1 1 1 b) 1 2 4 c) 2 4 4 d) 4 4 4
  18. If the following program (myprog) is run from the command line as myprog friday tuesday sunday,
    What would be the output?
    main(int argc, char*argv[])
    {
    printf("%c",**++argv);
    }
    a) m b) f c) myprog d) friday
  19. If the following program (myprog) is run from the command line as myprog friday tuesday sunday,
    What would be the output?
    main(int argc, char *argv[])
    {
    printf("%c",*++argv[1]);
    }
    a) r b) f c) m d) y
  20. If the following program (myprog) is run from the command line as myprog friday tuesday sunday,
    What would be the output?
    main(int argc, char *argv[])
    {
    while(sizeofargv)
    printf("%s",argv[--sizeofargv]);
    }
    a) myprog friday tuesday sunday b) myprog friday tuesday
    c) sunday tuesday friday myprog d) sunday tuesday friday
  21. Point out the error in the following program
    main()
    {
    int a=10;
    void f();
    a=f();
    printf("\n%d",a);
    }
    void f()
    {
    printf("\nHi");
    }
    Ans. The program is trying to collect the value of a "void" function into an integer variable.
  22. In the following program how would you print 50 using p?
    main()
    {
    int a[]={10, 20, 30, 40, 50};
    char *p;
    p= (char*) a;
    }
    Ans. printf("\n%d",*((int*)p+4));
  23. Would the following program compile?
    main()
    {
    int a=10,*j;
    void *k;
    j=k=&a;
    j++;
    k++;
    printf("\n%u%u",j,k);
    }
    a) Yes b) No, the format is incorrect
    c) No, the arithmetic operation is not permitted on void pointers
    d) No, the arithmetic operation is not permitted on pointers
  24. According to ANSI specifications which is the correct way of declaring main() when it receives command line arguments?
    a) main(int argc, char *argv[]) b) main(argc,argv) int argc; char *argv[];
    c) main() {int argc; char *argv[]; } d) None of the above
  25. What error would the following function give on compilation?
    f(int a, int b)
    {
    int a;
    a=20;
    return a;
    }
    a) missing parenthesis in the return statement b) The function should be declared as int f(int a, int b)
    c) redeclaration of a d) None of the above
  26. Point out the error in the following program
    main()
    {
    const char *fun();
    *fun()='A';
    }
    const char *fun()
    {
    return "Hello";
    }
    Ans. fun() returns to a "const char" pointer which cannot be modified
  27. What would be the output of the following program?
    main()
    {
    const int x=5;
    int *ptrx;
    ptrx=&x;
    *ptrx=10;
    printf("%d",x);
    }
    a) 5 b) 10 c) Error d) Garbage value
  28. A switch statement cannot include
    a) constants as arguments b) constant expression as arguments
    c) string as an argument d) None of the above
  29. How long the following program will run?
    main()
    {
    printf("\nSonata Software");
    main();
    }
    a) infinite loop b) until the stack overflows
    c) All of the above d) None of the above
  30. On combining the following statements, you will get char*p; p=malloc(100);
    a) char *p= malloc(100) b) p= (char*)malloc(100)
    c) All of the above d) None of the above
  31. What is the output of the following program?
    main()
    {
    int n=5;
    printf("\nn=%*d",n,n);
    }
    a) n=5 b) n=5 c) n= 5 d) error

1.main(){

float fl = 10.5;

double dbl = 10.5

if(fl ==dbl)

printf(“UNITED WE STAND”);

else

printf(“DIVIDE AND RULE”)

}

what is the output?

a)compilation error b)UNITED WE STAND c)DIVIDE AND RULE d)linkage error. Ans : c

2. main()

{

void vpointer;

char cHar = ‘g’, *cHarpointer = “GOOGLE”;

int j = 40;

vpointer = &cHar;

printf(“%c”,*(char*)vpointer);

vpointer = &j;

printf(“%d”,*(int *)vpointer);

vpointer = cHarpointer;

printf(“%s”,(char*)vpointer +3);

}

what is the output?

a)g40GLE b)g40GOOGLE c)g0GLE d)g4GOO Ans: a

3. .#define FALSE -1

#define TRUE 1

#define NULL 0

main() {

if(NULL)

puts(“NULL”);

else if(FALSE)

puts(“TRUE”);

else

puts(“FALSE”);

}

what is the output?

a)NULL b)TRUE c)FALSE d)0 Ans: b

4.what is done for push operation?? Ans: Stack Pointer in incremented and value is stored

5.The OR gate can be converted to the NAND function by adding----gate(s)to the input of the OR gate. ans. Not gate

6.combination of LOGIC GATEs

7.voltage requirment for pentium preocessor ??

8.K6 processor is from whic

 class Sample

{

public:

int *ptr;

Sample(int i)

{

ptr = new int(i);

}

~Sample()

{

delete ptr;

}

void PrintVal()

{

cout « "The value is " « *ptr;

}

};

void SomeFunc(Sample x)

{

cout « "Say i am in someFunc " « endl;

}

int main()

{

Sample s1= 10;

SomeFunc(s1);

s1.PrintVal();

}

Answer:

Say i am in someFunc
Null pointer assignment(Run-time error)

Explanation:

As the object is passed by value to SomeFunc the destructor of the object is called when the control returns from the function. So when PrintVal is called it meets up with ptr that has been freed.The solution is to pass the Sample object by reference to SomeFunc:

void SomeFunc(Sample &x)

{

cout « "Say i am in someFunc " « endl;

}

because when we pass objects by refernece that object is not destroyed. while returning from the function.

 Which is the parameter that is added to every non-static member function when it is called?

Answer: 'this' pointer

 class base

{

public:

int bval;

base(){ bval=0;}

};

class deri:public base

{

public:

int dval;

deri(){ dval=1;}

};

void SomeFunc(base *arr,int size)

{

for(int i=0; i‹size; i++,arr++)

cout«arr-›bval;

cout«endl;

}

int main()

{

base BaseArr[5];

SomeFunc(BaseArr,5);

deri DeriArr[5];

SomeFunc(DeriArr,5);

}

Answer:

00000
01010

Explanation:

The function SomeFunc expects two arguments.The first one is a pointer to an array of base class objects and the second one is the sizeof the array.The first call of someFunc calls it with an array of bae objects, so it works correctly and prints the bval of all the objects. When Somefunc is called the second time the argument passed is the pointeer to an array of derived class objects and not the array of base class objects. But that is what the function expects to be sent. So the derived class pointer is promoted to base class pointer and the address is sent to the function. SomeFunc() knows nothing about this and just treats the pointer as an array of base class objects. So when arr++ is met, the size of base class object is taken into consideration and is incremented by sizeof(int) bytes for bval (the deri class objects have bval and dval as members and so is of size ›= sizeof(int)+sizeof(int) ).

 class base

{

public:

void baseFun(){ cout«"from base"«endl;}

};

class deri:public base

{

public:

void baseFun(){ cout« "from derived"«endl;}

};

void SomeFunc(base *baseObj)

{

baseObj->baseFun();

}

int main()

{

base baseObject;

SomeFunc(&baseObject);

deri deriObject;

SomeFunc(&deriObject);

}

Answer:

from base
from base

Explanation:

As we have seen in the previous case, SomeFunc expects a pointer to a base class. Since a pointer to a derived class object is passed, it treats the argument only as a base class pointer and the corresponding base function is called.

 class base

{

public:

virtual void baseFun(){ cout«"from base"«endl;}

};

class deri:public base

{

public:

void baseFun(){ cout« "from derived"«endl;}

};

void SomeFunc(base *baseObj)

{

baseObj->baseFun();

}

int main()

{

base baseObject;

SomeFunc(&baseObject);

deri deriObject;

SomeFunc(&deriObject);

}

Answer:

from base
from derived

Explanation:

Remember that baseFunc is a virtual function. That means that it supports run-time polymorphism. So the function corresponding to the derived class object is called.

 void main()

{

int a, *pa, &ra;

pa = &a;

ra = a;

cout «"a="«a «"*pa="«*pa «"ra"«ra ;

}

Answer:

Compiler Error: 'ra',reference must be initialized

Explanation:

Pointers are different from references. One of the main differences is that the pointers can be both initialized and assigned, whereas references can only be initialized. So this code issues an error.

 const int size = 5;

void print(int *ptr)

{

cout«ptr[0];

}

void print(int ptr[size])

{

cout«ptr[0];

}

void main()

{

int a[size] = {1,2,3,4,5};

int *b = new int(size);

print(a);

print(b);

}

Answer:

Compiler Error : function 'void print(int *)' already has a body

Explanation:

Arrays cannot be passed to functions, only pointers (for arrays, base addresses) can be passed. So the arguments int *ptr and int prt[size] have no difference as function arguments. In other words, both the functoins have the same signature and so cannot be overloaded.

 class some{

public:

~some()

{

cout«"some's destructor"«endl;

}

};

void main()

{

some s;

s.~some();

}

Answer :

some's destructor
some's destructor

Explanation:

Destructors can be called explicitly. Here 's.~some()' explicitly calls the destructor of 's'. When main() returns, destructor of s is called again, hence the result.

 #include ‹iostream.h›

class fig2d

{

int dim1;

int dim2;

public:

fig2d() { dim1=5; dim2=6;}

virtual void operator«(ostream & rhs);

};

void fig2d::operator«(ostream &rhs)

{

rhs «this->dim1«" "«this->dim2«" ";

}

/*class fig3d : public fig2d

{

int dim3;

public:

fig3d() { dim3=7;}

virtual void operator«(ostream &rhs);

};

void fig3d::operator«(ostream &rhs)

{

fig2d::operator «(rhs);

rhs«this -> dim3;

}

*/

void main()

{

fig2d obj1;

//fig3d obj2;

obj1 « cout;

//obj2 « cout;

}

Answer :

5 6

Explanation:

In this program, the « operator is overloaded with ostream as argument. This enables the 'cout' to be present at the right-hand-side. Normally, 'cout' is implemented as global function, but it doesn't mean that 'cout' is not possible to be overloaded as member function.

Overloading « as virtual member function becomes handy when the class in which it is overloaded is inherited, and this becomes available to be overrided. This is as opposed to global friend functions, where friend's are not inherited.

 class opOverload{

public:

bool operator==(opOverload temp);

};

bool opOverload::operator==(opOverload temp){

if(*this == temp ){

cout«"The both are same objects\n";

return true;

}

else{

cout«"The both are different\n";

return false;

}

}

void main(){

opOverload a1, a2;

a1= =a2;

}

Answer :

Runtime Error: Stack Overflow

Explanation:

Just like normal functions, operator functions can be called recursively. This program just illustrates that point, by calling the operator == function recursively, leading to an infinite loop.

 class complex{

double re;

double im;

public:

complex() : re(1),im(0.5) {}

bool operator==(complex &rhs);

operator int(){}

};

bool complex::operator == (complex &rhs){

if((this->re == rhs.re) & (this->im == rhs.im))