© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

C and C++ Interview Questions

Ex Q1:Is the following code legal?
typedef struct a aType;
struct a
{
int x;
aType *b;
};
Ans:
      Yes
Explanation

The typename aType is known at the point of declaring the structure, because it is already typedefined.

Ex - Q2:  Is the following code legal?
void main()
{
typedef struct a aType;
aType someVariable;
struct a
{
int x;
      aType *b;
              };
}
Ans:
            No
Explanation:            

When the declaration, typedef struct a aType; is encountered body of struct a is not known. This is known as ‘incomplete types’.

Ex - Q3:  What is the output?
void main()
{
printf(“sizeof (void *) = %d \n“, sizeof( void *));
printf(“sizeof (int *)    = %d \n”, sizeof(int *));
printf(“sizeof (double *)  = %d \n”, sizeof(double *));
printf(“sizeof(struct unknown *) = %d \n”, sizeof(struct unknown *));
}
Ans :
sizeof (void *) = 2
sizeof (int *)    = 2
sizeof (double *)  =  2
sizeof(struct unknown *) =  2
Explanation:
The pointer to any type is of same size.

Ex - Q4: What is the output?
char inputString[100] = {0};
To get string input from the keyboard which one of the following is better?
      1) gets(inputString)
      2) fgets(inputString, sizeof(inputString), fp)

Answer & Explanation:

The second one is better because gets(inputString) doesn't know the size of the string passed and so, if a very big input (here, more than 100 chars) the charactes will be written past the input string. When fgets is used with stdin performs the same operation as gets but is safe.


SLogix Student Projects
bottom