Sunday, November 27, 2011

Test your C++ Programming skills Part 1






Q1. The class that in C++ for file input is?

Ans:
   ifstream


Q2. How would you define a class with a static data member and initialise it correctly?

Ans:


Q3. What is the error in the code snippet below?
int main()
{
int *p;
int q;
p = new int;
delete q;
return 0;
}

Ans:


Q4.
class Foo {

   char *buf;
public:
   Foo (char const * b = "default") {
      buf = new char[srd::strlen(b)+1];
      std::strcpy(buf, b);
   }

   ~Foo() {
     delete[] buf;
   }
};

Foo Func(Foo f) {
   return f;
}

Referring to the code above, when the "func" is called, the program may crash or have any unexpected behaviour. What is the reason of this problem?

Ans:



Q5. Find the o/p:
#include < cstudio >

cclass A {
public:
   A() { std::putchar('A); }
   ~A() { std::putchar('~); std::putchar('A); }
}

class B {
public:
   B() { std::putchar('B); }
   ~B() {std::putchar('~); std::putchar('B); }
}

A a;
static B b;
int main() {
   static B b; return 0;
}

Ans:


Q6.
class A {
public:
   A() { std::memset(buf, 0, sizeof(buf)); }

protected:
   char buf[256];
};

class B {
public:
   B(const A& arg) : a(arg) {} //Ctr1
   B(const A& arg) { a = arg;} //Ctr2

protected:
   A a;
};


Consider the two constructors given for B in the sample. Only one of the constructors can be used because they have the same signature, which one?

Ans:


Q7. Choose the correct option:

Mutable keyword can be applied to:
a) non-static data members of a class
b) non-const data members of a class
c) Both a and b
d) None of above

Ans:


Q8. Choose the correct option:
Objects have
a) Behaviour
b) State
c) both a and b
d) None

Ans:


Q9. The vtable entry for a pure virtual function in C++ is:
a) Zero
b) NULL
c) One
d) No entry in vtable

Ans:


Q10. Find the o/p of this:
main() {

   int i;
   float *pf;

   pf = (float *) &i;
   *pf = 100.00;
}

Ans:


Q11. Can we declare a static function as virtual?

Ans:


Q12. Is it compulsory to override a virtual function?

Ans:


Q13.Can we get the typeid of void pointers?

Ans:











Friday, November 25, 2011

Test your C Programming skills Part 4






Q1. How scanf will execute?
main(){

   char name[10],s[12];
   scanf(" \"%[^\"]\"",s);

}

Ans:
First it checks for the leading white space and discards it.Then it matches
with a quotation mark and then it reads all character upto another quotation mark.


Q2. What will be the position of the file marker?

a) fseek(ptr,0,SEEK_SET);

b) fseek(ptr,0,SEEK_CUR);

Ans:


Q3. What is the problem with the following code segment?

while ((fgets(receiving array,50,file_ptr)) != EOF)

Ans:


Q4. Find the o/p:
main(){

   char *cptr,c;
   void *vptr,v;

   c=10; v=0;
   cptr=&c; vptr=&v;

   printf("%c%v",c,v);

}

Ans:



Q5. Find the o/p:
#define max 5

#define int arr1[max]

main(){

   typedef char arr2[max];
   arr1 list={0,1,2,3,4};

   arr2 name="name";

   printf("%d %s",list[0],name);

}
Ans:


Q6. Find the o/p:
int i=10;

main() {

   extern int i; {

   int i=20;{
     int i=30;
     printf("%d",i);

   }

   printf("%d",i);

   }

   printf("%d",i);

}
Ans:


Q7. Find the o/p:
main(){

   int *j;{
     int i=10;
     j=&i;
   }

   printf("%d",*j);

}

Ans:


Q8. Find the o/p:
# include < stdio.h >
main() {

const int i=4;

float j;

   j = ++i;

   printf("%d %f", i,++j);

}
Ans:


Q9. Find the o/p:
main() {

int i=_l_abc(10);

   printf("%d\n",--i);

}

int _l_abc(int i) {

return(i++);

}

Ans:


Q10. Find the o/p of this:
main(int argc, char **argv) {

   printf("enter the character");
   getchar();

   sum(argv[1],argv[2]);

}

sum(num1,num2)int num1,num2; {

return num1+num2;

}
Ans:


Q11. Find the o/p:
int one_d[]={1,2,3};

main(){

int *ptr;

   ptr=one_d;
   ptr+=3;

   printf("%d",*ptr);
}

Ans:


Q12. Find the o/p:
main(){

FILE *ptr;
char i;

   ptr=fopen("zzz.c","r");
   while((i=fgetch(ptr))!=EOF)
      printf("%c",i);

}

Ans:


Q13.Find the o/p:
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

Ans:











Test your C Programming skills Part 3






Q1. Find the o/p:
main() {

   char *p;

   p="Hello";

   printf("%c\n",*&*p);

}
Ans:
     H


Q2. Find the o/p:
main() {

   int i=1;

   while (i<=5){      printf("%d",i);      if (i>2)

     i++;

   }

}

fun() {

   here:

   printf("PP");

}
Ans:


Q3. Find the o/p:
void main() {

   int i=5;

   printf("%d",i++ + ++i);

}

Ans:


Q4. Find the o/p:
main() {

   int i=1,j=2;

   switch(i) {

   case 1: printf("GOOD");

   break;

   case j: printf("BAD");

   break;

   }

}
Ans:



Q5. Find the o/p:
main() {

   int i;

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

}

Ans:


Q6. Find the o/p:
#define f(g,g2) g##g2

main() {

   int var12=100;

   printf("%d",f(var,12));

}
Ans:


Q7. Find the o/p:
main() {

   int i=0;

   for(;i++;printf("%d",i)) ;

}
Ans:


Q8. Find the o/p:
# include < stdio.h >
main() {

   printf("%d", out);

}

int out=100;

Ans:


Q9. Find the o/p:
#include <stdio.h>
main() {

   extern out;

   printf("%d", out);

}

int out=100;

Ans:


Q10. Find the o/p of this:
main( ) {

   int a[ ] = {10,20,30,40,50},j,*p;

   for(j=0; j<5; j++){      printf(“%d” ,*;      a++;    }    p = a;    for(j=0; j<5; j++){      printf(“%d ” ,*p);      p++;    } }



Ans:


Q11. Find the o/p:
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);

}

Ans:


Q12. Find the o/p:
main() {
   int i,j;

   for(i=0;i<=10;i++){    j+=5;    assert(i<5); }



Ans:


Q13.What are the files which are automatically opened when a C file is executed?

Ans:






Test your C Programming skills Part 2







Q1. How would you declare a pointer variable ptr so that it can not be used to modify the string it points to (although ptr itself can be changed to point to some other string)?

Ans:
     char * const ptr = "Hello";


Q2. How are the string literals are processed by compiler?

Ans:


Q3.
int a[25];
int * p = a + 3;

Which one of the following is equivalent to the above code snippet?
a) int a[25];
int *p = &a[3];

b) int a[25];
int * p = &(a + 3);

c) int a[25];
int * p = (&a)[3];

d) int a[25];
int * p = &a[2];

e) int a[25];
int * p = a[3];


Ans:


Q4. How would you declare a constant pointer to a constant string?
Ans:



Q5. Find the o/p:
int divider(double m, double n)
{
     return m/n;
}

main()
{
     printf("%d", divider(10/3);
}


Ans:


Q6. Find the o/p:
main(){

     struct xx{

          char name[]="hello";

};

     struct xx *s;

     printf("%s",s->name);

}
Ans:


Q7. Find the o/p:
void main()
{
     int const *p=5;
     printf("%d",++(*p));

}
Ans:


Q8. Find the o/p:
# include < stdio.h >
main()
{
     float me = 1.1;
     double you = 1.1;

     if(me==you)
          printf("I love U");

     else
          printf("I hate U");

}

Ans:


Q9. Find the o/p:
#include <stdio.h>
main() {
     static int var = 5;
     printf("%d ",var--);
     if(var)
          main();
}

Ans:


Q10. Find the o/p of this:
main(){

     extern int i;

     i=20;

     printf("%d",i);

}
Ans:


Q11. Find the o/p:
main(){

     char *p;

     printf("%d %d ",sizeof(*p),sizeof(p));

}
Ans:


Q12. Find the o/p:
main(){

     char string[]="Hello World";

     display(string);

}

void display(char *string){

     printf("%s",string);

}
Ans:


Q13. What is output?
main(){

     int i=10;

     i=!i>14;

     printf("i=%d",i);

}

Ans:











Thursday, May 26, 2011

Test your C Programming skills Part 1






Q1. Find the o/p:
main()
{
    char *ptr="hello";
    char ary[]="world";

printf("PTR: &ptr=%x, ptr=%x, *ptr=%x, ptr[0]=%x\n", &ptr,ptr, *ptr, ptr[0]);
printf("ARY: &ary=%x,ary=%x, *ary=%x, ary[0]=%x\n", &ary, ary, *ary, ary[0]);
}

Ans:
PTR: &ptr=bfb86990, ptr=8048524, *ptr=68, ptr[0]=68
ARY: &ary=bfb8698a,ary=bfb8698a, *ary=77, ary[0]=77


Q2. Find the o/p:
main()
{
    struct node {
        char a;
        int b; 
        short c;
        struct node next;
    }n1;   

printf("Ans1:%d\n", sizeof(n1));

return 0;
}

Ans:


Q3. Find the o/p:
main()
{
    struct node {
        char a;
        int b; 
        short c;
        struct node *next;
    }n1;   

printf("Ans1:\n\t node=%x\n \t node+2=%x\n", (char *)&n1, (char *)&n1+2);
printf("Ans2:%d\n", sizeof(n1));

return 0;
}
Ans:


Q4. What is the difference between fopen() and open()? When do you choose to use one over the other?

Ans:



Q5. Find the o/p:
main()
{
if(!printf("Hello ")){}
else
printf("World!");
}
Ans:


Q6. Find the o/p:
main()
{
struct i{
int a;    //4
double b;    //8
char * c;    //4
char d[7];    //8
short e;    //4
int f;    //4
};
printf("sizeof(i)= %d", sizeof(struct i));
}
Ans:


Q6.1. Find the o/p:
main()
{
struct i{
int a;    //4
double b;    //8
char * c;    //4
char d[3];    //4
short e;    //4
int f;    //4
};
printf("sizeof(i)= %d", sizeof(struct i));
}
Ans:



Q7. Find the o/p:
# include < stdio.h >
void main()
{
printf(NULL) ;
return ;
}
Ans:


Q8. Find the o/p:
# include < stdio.h >
void main()
{
int i = 0;
printf(i) ;
return ;
}
Ans:


Q9. Find the o/p:
#include <stdio.h>
void main()
{
printf("%s",NULL) ;
return ;
}
Ans:


Q10. Find the o/p of this last one:
main()
{
printf("%%",7);
}
Ans:


Q11. How do I declare an array of N pointers to functions returning pointers to functions returning pointers to characters?
Ans:


Q12. What is asmlinkage and what is it's use?
Ans:


Q13. What is output?
main()
{
printf("%x",-1<<4); }


Ans: