Wednesday, January 11, 2012

Top 50 Interview Questions & Answers in C Programming (contd...)

CHAPTER 2: Operators, Constants & Structures

1. Which bitwise operator is suitable for checking whether a particular bit is ON or OFF?

Bitwise AND operator.

Example:

Suppose in byte that has a value 10101101 . We wish to check whether bit number 3 is ON (1) or OFF (0). Since we want to check the bit number 3, the second operand for AND operation we choose is binary
00001000, which is equal to 8 in decimal.

Explanation:

ANDing operation :

10101101 original bit pattern
00001000 AND mask
---------
00001000 resulting bit pattern
---------
The resulting value we get in this case is 8, i.e. the value of the second operand. The result turned out to be a 8 since the third bit of operand was ON. Had it been OFF, the bit number 3 in the resulting bit pattern would have evaluated to 0 and complete bit pattern would have been 00000000. Thus depending upon the bit number to be checked in the first operand we decide the second operand, and on ANDing these two operands the result decides whether the bit was ON or OFF.

2. Which bitwise operator is suitable for turning OFF a particular bit in a number?

Bitwise AND operator (&), one's complement operator(~)

Example:

To unset the 4th bit of byte_data or to turn off a particular bit in a number.

Explanation:

Consider,
char byte_data= 0b00010111;
byte_data= (byte_data)&(~(1<<4));
1 can be represented in binary as 0b00000001 = (1<<4)
<< is a left bit shift operator,
it shifts the bit 1 by 4 places towards left.
(1<<4) becomes 0b00010000
And ~ is the one's complement operator in C language.
So ~(1<<4) = complement of 0b00010000
= 0b11101111
Replacing value of byte_data and ~(1<<4) in
(byte_data)&(~(1<<4));

we get (0b00010111) & (0b11101111)
Perform AND operation to below bytes.
00010111
11101111
-----------
00000111
-----------
Thus the 4th bit is unset.

3. What is equivalent of multiplying an unsigned int by 2: left shift of number by 1 or right shift of number by 1?

Left shifting of an unsigned integer is equivalent to multiplying an unsigned int by 2.

Eg1:

14<<1;
Consider a number 14-----00001110 (8+4+2)is its binary equivalent
left shift it by 1--------------00011100(16+8+4) which is 28.

Eg2:

1<<1;
consider the number as 1---00000001(0+0+1).
left shift that by 1------------00000010(0+2+0) which is 2.
left shift by 1 bit of a number=2*number
left shift by 1 bit of 2*number=2*2*number
left shift by n bits of number=(2^n)*number

Program:

Program to illustrate left shift and right shift operations.

#include<stdio.h>
int main(void)
{
int x=10,y=10;
printf("left shift of 10 is %d \n",x<<1);
printf("right shift of 10 is %d \n",y>>1);
return 0;
}

Output:

left shift of 10 is 20
right shift of 10 is 5

Explanation:

Left shift (by 1 position) multiplies a number by two. Right shift divides a number by 2.
.
4. What is an Enumeration Constant?

Enumeration is a data type. We can create our own data type and define values that the variable can take. This can help in making program more readable. enum definition is similar to that of a structure.

Example:

consider light_status as a data type. It can have two possible values - on or off.
enum light_status
{
on, off
};
enum light_status bulb1, bulb2;
/* bulb1, bulb2 are the variables */

Declaration of enum has two parts:

a) First part declares the data type and specifies the possible values, called 'enumerators'.
b) Second part declares the variables of this data type. We can give values to these variables:
bulb1 = on;
bulb2 = off;

5. What is a structure?

A structure is a collection of pre-defined data types to create a user-defined data type. Let us say we need to create records of students. Each student has three fields:

int roll_number;
char name[30];
int total_marks;

This concept would be particularly useful in grouping data types. You could declare a structure student as:

struct student {
int roll_number;
char name[30];
int total_marks;
} student1, student2;

The above snippet of code would declare a structure by name student and it initializes two objects student1,student2. Now these objects and their fields could be accessed by saying student1.roll_number for accesing roll number field of student1 object, similarly student2.name for accesing name field of student2 object.

6. What are the differences between a structure and a union?

Structures and Unions are used to store members of different data types.

STRUCTURE UNION

a) Declaration:

struct
{
data type member1;
data type member2;
};

a) Declaration:

union
{
data type member1;
data type member2;
};

b) Every structure member is allocated memory when a structure variable is defined.

Example:

b) The memory equivalent to the largest item is allocated commonly for all members.

Example:

struct emp {
char name[5];
int age;
float sal;
};
struct emp e1;

Memory allocated for structure is 1+2+4=7 bytes. 1 byte for name, 2 bytes for age and 4 bytes for sal.

union emp1 {
char name[5];
int age;
float sal;
};
union emp1 e2;

Memory allocated to a union is equal to size of the largest member. In this case, float is the largest-sized data type. Hence memory allocated to this union is 4 bytes.

c) All structure variables can be initialized at a time

struct st {
int a;
float b;
};
struct st s = { .a=4, .b=10.5 };

Structure is used when all members are to be independently used in a program.

c) Only one union member can be initialized at a time

union un {
int a;
float b;
};
union un un1 = { .a=10 };

Union is used when members of it are not required to be accessed at the same time.


7. What are the advantages of unions?

Union is a collection of data items of different data types. It can hold data of only one member at a time though it has members of different data types. If a union has two members of different data types, they are allocated the same memory. The memory allocated is equal to maximum size of the members. The data is interpreted in bytes depending on which member is being accessed.

Example:

union pen {
char name;
float point;
};

Here name and point are union members. Out of these two variables, 'point' is larger variable which is of float data type and it would need 4 bytes of memory. Therefore 4 bytes space is allocated for both the variables. Both the variables have the same memory location. They are accessed according to their type. Union is efficient when members of it are not required to be accessed at the same time.

8. How can typedef be to define a type of structure?

typedef declaration helps to make source code of a C program more readable. Its purpose is to redefine the name of an existing variable type. It provides a short and meaningful way to call a data type. typedef is useful when the name of the data type is long. Use of typedef can reduce length and complexity of data types.

Note: Usually uppercase letters are used to make it clear that we are dealing with our own data type.

Example:

struct employee {
char name[20];
int age;
};
struct employee e;
The above declaration of the structure would be easy to use when renamed using typedef as:

struct employee {
char name[20];
int age;
};
typedef struct employee EMP;
EMP e1, e2;

9. Write a program that returns 3 numbers from a function using a structure.

A function in C can return only one value. If we want the function to return multiple values, we need to create a structure variable, which has three integer members and return this structure.

Program: Program with a function to return 3 values

#include<stdio.h>
//sample structure which has three integer variables.
struct sample {
int a, b, c;
};
//this is function which returns three values.
struct sample return3val() {
struct sample s1;
s1.a = 10;
s1.b = 20;
s1.c = 30;
//return structure s1, which means return s1.a ,s1.b and s1.c
return s1;
}
int main() {
struct sample accept3val;
//three values returned are accepted by structure accept3val.
accept3val = return3val();
//prints the values
printf(" \n %d", accept3val.a);
printf("\n %d", accept3val.b);
printf(" \n %d", accept3val.c);
return 0;
}

Output:
10
20
30.

Explanation:

In this program, we use C structure to return multiple values from a function. Here we have a structure holding three int variables and a function which returns it. 'return3val' is a function which assigns 10, 20, 30 to its integer variables and returns this structure. In this program, 'accept3val' is a structure used to accept the values returned by the function. It accepts those values and shows the output.

10. In code snippet below:
struct Date {
int yr;
int day;
int month;
} date1,date2;
date1.yr = 2004;
date1.day = 4;
date1.month = 12;
Write a function that assigns values to date2. Arguments to the function must be pointers to the structure, Date and integer variables date, month, year.

Date is a structure with three int variables as members. set_date(..) is a function used to assign values to the structure variable.

Program:
Program to illustrate a function that assigns value to the structure.

#include<stdio.h>
#include<stdlib.h>
//declare structure Date
struct Date {
int yr;
int day;
int month;
} date1, date2;
//declare function to assign date to structure variable
void set_date(struct Date *dte, int dt, int mnt, int year) {
dte->day = dt;
dte->yr = year;
dte->month = mnt;
}
int main(void) {
date1.yr = 2004;
date1.day = 4;
//assigning values one by one
date1.month = 12;
//assigning values in a single statement
set_date(&date2, 05, 12, 2008);
//prints both dates in date/month/year format
printf("\n %d %d %d ", date1.day, date1.month, date1.yr);
printf("\n %d %d %d ", date2.day, date2.month, date2.yr);
return 0;
}

Output:
4 12 2004
5 12 2008

Explanation:

Two variables of type Date are created and named 'date1', 'date2'. 'date2' is assigned by using the function set_date(..). Address of 'date2' is passed to set_date function.

No comments:

Post a Comment