Pages

Friday, December 12

Unconventional C Programming Interview Questions with Answers

Q. What is the difference between signed and unsigned integers?

A. Signed: It’s an integer with leftmost bit (known as sign bit) as 0, iff the number>=0 and 1 if it’s negative. Thus the largest 16 bit integer has binary representation
                0111 1111 1111 1111 which is equivalent to 32767 (215-1).
Similarly the largest 32 bit integer is (231-1) represented by
                                                0111 1111 1111 1111     1111 1111 1111 1111
Unsigned: It’s an integer with no sign bit i.e. leftmost bit is considered as part of number’s magnitude. The largest 16-bit unsigned integer is 65,535(216-1). Similarly, largest 32-bit unsigned integer is (232-1).
By default, integer variables are signed in C. To inform the compiler that we are using unsigned variable, we declare it in format “unsigned data_type variable_name;” Unsigned numbers are mainly used in Systems Programming, Machine dependent applications, Low-level Operations etc.



Q. What are the different numeric types in C?

A. Fundamentally 2 built-in numeric types: integer and float.
          int: Their values are whole numbers. They are further divided into signed and unsigned.
          float: Whole numbers as well as can accommodate fractional part.



Q. What is the size of an integer type?

A. An integer type in C can have different sizes. Typically its 32 bits, but 16-bits on older CPUs.



Q. What is the use of specifiers’ long and short w.r.t. to integer data type?

A. Some programs like factorial require numbers that too large to store in int form, we are provided with long integersSimilarly, we may be dealing with small numbers. At that time we need to conserve memory by instructing the compiler to store integer in less space than normal. Such a number is called short int. Hence, based on our requirement we can specify an integer variable to be long/short and signed/unsigned. We can combine specifiers and generate 6 valid combinations:
           short int
           unsigned short int

           int (OR signed int)
           unsigned int

           long int (OR long signed int)
           unsigned long int

By default, integers are always signed in C unless specified. The order of these specifiers doesn’t matter i.e. unsigned long int is equivalent to long unsigned int.



Q. Can we abbreviate the name of integer types?

A. Yes. We can do that by dropping the word int. Ex. unsigned long int can be written as unsigned long. Similarly short int can be written as just short. But its not a Good Programming Practice. We should explicitly mention it.



Q. Explain Integer Overflow?

A. It occurs when the result of an arithmetic operation is too large i.e. result of two int must be an int. If there is no place to accommodate (i.e. too many bits are needed) the resultant int, then we call it as an Overflow. The behavior will be defined based on the type of integer operands i.e. signed or unsigned. In case of:
Signed: Undefined Program behavior or program may even crash.
Unsigned: Defined result i.e. we get the correct answer modulo 2n, where n is the no of bits used to store the result. Ex. if we add 1 to an unsigned 16 bit number 65535, the result will definitely be 0.



Q. Explain the IEEE 754 Floating Point Standard?

A. It provides two primary formats for floating point numbers namely:
Single Precision: 32 bits
Double Precision: 64 bits
Numbers are stored in a form of scientific notation, with each number having three parts namely sign, exponent and fraction. Number of bits reserved for the exponent determines how large (or small) numbers can be; while the number of bits in the fraction determines the precision.
In single precision, exponent is 8 bits long whereas fraction occupies 23 bits. Hence it can have maximum value as 3.40 x 1038, with a precision of about 6 decimal digits.
To know more about floating point arithmetic see “What every computer scientist should know about floating point arithmetic” by David Goldberg (ACM Computing Surveys)



Q. What are the range of values represented by six combination of integer types?

A. It totally depends on the machine architecture i.e. 16, 32 or 64 bit machine.

Integer type on a 16-bit machine:


Data Type
Lowest Value
Highest Value
short int
-215
215-1
unsigned short int
0
216-1
int
-215
215-1
unsigned int
0
216-1
long int
-231
231-1
unsigned long int
0
232-1
Observation: short int and int have identical range.

Integer type on a 32-bit machine:


Data Type
Lowest Value
Highest Value
short int
-215
215-1
unsigned short int
0
216-1
Int
-231
231-1
unsigned int
0
232-1
long int
-231
231-1
unsigned long int
0
232-1
Observation: int and long int have identical range.

Integer type on a 64-bit machine:


Data Type
Lowest Value
Highest Value
short int
-215
215-1
unsigned short int
0
216-1
Int
-231
231-1
unsigned int
0
232-1
long int
-263
263-1
unsigned long int
0
264

All these ranges aren’t fixed. They may vary from one compiler to another. “limits.h” header, part of standard library can be used to determine the range for these integer types. This header defines macros representing the lowest and highest value of each integer type.



Q. Explain character types along with ASCII character set?

A. The values of char type can vary from one computer to another, because different machines may have different underlying character sets. A variable type char can be assigned any single character but must be enclosed in single quote (not double quote):
char ch;
ch= ‘z’;
ch= ‘Z’;
ch= ‘5’;
ch= ‘?’;
ASCII (American Standard Code for Information Interchange), a 7 bit character code capable of representing 128 characters. The character code ranges from 0000000 to 1111111 which we think as integers from 0 to 127. Ex.
Representation:
‘A’ - 65 and A-Z : 1000001 to 1011010
‘0’ - 48 and 0-9 : 0110000 to 0111001
a-z – 97 to 122
‘ ‘ – 32 (space)
ASCII is often extended to a 256 character code known as Latin-1 that provides the characters necessary for other languages.



Q. Define various types of operations that can be performed on Characters?

A. C treats all characters as small integers hence we can manipulate them. Characters are encoded binary which can be viewed as integer types. The character constants are of actually int type rather of char type. Hence if a character appears in an expression, C simply uses its integer value from ASCII character set.
Ex. ch=’a’; which means ‘a’ is not 97 ORR
ch = A; ch = ch + a; ch++; which means we started with ch = ’A’, then we made it ‘B’ and finally ‘C’.
Characters can be simply compared as numbers. The result depends on character set in use. So programs using the relational operators to compare characters may not be portable.
Ex. if (if ch>=’A’ && ch<=’Z’)
                ch = ch+1; ORR
for (ch=’a’; ch<=’z’; ch++){
…..
}
If we try to treat numbers as character, we may get logical errors. Remember logical errors aren’t caught by the compiler.
Ex. ‘x’ * ‘y’ / ‘a’ – ‘v‘;



Q. Differentiate between Signed and Unsigned Characters?

A. since C treats character as integers, so character types also exist as signed and unsigned versions. Hence character values for:
Signed Characters: -128 to 127.
Unsigned Characters: 0 to 255
we don’t know whether an ordinary char is a signed or an unsigned type. Some compilers treat them as signed while others as unsigned.
We should unsigned or unsigned char if we are storing small integer values. Ex.
signed char ch;
unsigned char ch;Never take any assumption about signed or unsigned character type. It may affect the portability of your program.
C89 uses the term integral types to refer to both integer types and the character types. Enumerated types are also integral types.C99 doesn’t use the term integral types. Instead it expanded the meaning of “integer types” to include the character types and enumerated types. C99’s _Bool type is considered to be an unsigned integer type.



Q. Explain about Arithmetic Types.

A. Integer and Floating type are collectively known as Arithmetic types.
Arithmetic types:
·         Integer types:
                 > char
                > signed integer types (signed char, short int, int , long int, long long int)
                > unsigned integer types (unsigned char, unsigned short int, unsigned int, unsigned long int, unsigned long long int, _Bool)
                > Enumerated Types
·         Floating Types
                > Real Floating Types (float, double, long double)
                > Complex types (float _Complex, double _Complex, long double _Complex)



Q. What are Escape Sequences?

A. A character constant is a single sequence which is enclosed in a single quote. However there are certain special characters which are entered which are entered using special notation called as escape sequence.
They are of two type: character escapes and numeric escapes. Table for character escapes:


Name
Escape Sequence
Bell alert
\a
Backspace
\b
Form feed
\f
New line
\n
Carriage return
\r
Horizontal Tab
\t
Vertical Tab
\v
Backslash
\\
Question Mark
\?
Single quote
\’
Double quote
\”


Character escapes have a problem: List of character escapes doesn’t include all the nonprinting (enter, backspace, tab etc.) ASCII characters. They cannot be used to represent beyond 128 ASCII characters. Hence to solve this problem, we have numeric escapes which can represent any character.
To write a numeric escape for a particular character, first lookup the character’s octal or hexadecimal value. Ex. ASCII escape character (decimal value: 27) has the value 33 in octal and 1B in hexadecimal. Either of these codes can be used to write an escape sequence.
          Octal Escape Sequence: consists of \ followed by an octal number with at most three digits. This number must be representable as an unsigned character, so its maximum value is normally 377 octal. Escape character could be written as \33 or \033. Unlike octal constants, they don’t have to begin with 0.
          Hexadecimal Escape Sequence: consists of \x (must be in lower case) followed by a hexadecimal number (character can be in any case). There is no limit on the number of digits on this number. But it must also be representable as an unsigned character. So, it can’t exceed FF if characters are 8 bits long. Ex. \x2C or \x2c
While using character constant as an escape sequence they must be enclosed in single quotes. Ex. ‘\33’ or ‘\x1b’. It’s a good programming practice to give them a name using #define:
#define ESC ‘\33’
They can be embedded in strings as well. There is something called as Trigraph Sequences, used for representing characters (^, {, ~, }, #, [, \, ], |) similar to escape sequences.



Q. What do you know about Character Handling Functions in C?

A. A lot of functionality is already provided in the form of library functions. Programs calling the tolower() must have #include directive <ctype.h>  at the top. This directive is used to deal with character types in C.
ch = tolower(ch);             //First check for uppercase. If not, converts to corresponding lowercase.


Q. What do you know about scanf() and printf() w.r.t. character handling?

A. To read a single character, we use “%c”. Ex.
scanf(“%c”,&ch);
printf(“%c”,ch);
scanf() – doesn’t skip white space characters before reading a character. If the next unread character is a space, then ch will contain space after scanf() returns.



No comments:

Post a Comment