Monday 20 July 2015

Most useful and basic intervews Question And Answer

In this blog we provide most useful c language interviews question and answer for those freshers who seeking job in IT sector can find some basic definitions related to c language. Such definitions are usually asked by all major it companies such as TCS, CTS, Infosys, Accenture,IBM and Wipro and many more during mass recruitment.

Most useful and basic intervews Question And Answer

What is the difference between declaration and definition of a variable/function.

Ans: Declaration of a variable/function simply declares that the variable/function exists somewhere in the program but the memory is not allocated for them. But the declaration of a variable/function serves an important role. And that is the type of the variable/function. Therefore, when a variable is declared, the program knows the data type of that variable. In case of function declaration, the program knows what are the arguments to that functions, their data types, the order of arguments and the return type of the function. So that’s all about declaration. Coming to the definition, when we define a variable/function, apart from the role of declaration, it also allocates memory for that variable/function. Therefore, we can think of definition as a super set of declaration. (or declaration as a subset of definition). From this explanation, it should be obvious that a variable/function can be declared any number of times but it can be defined only once. (Remember the basic principle that you can’t have two locations of the same variable/function).

Why use C?

Ans : C has been used successfully for every type of programming problem imaginable from operating systems to spreadsheets to expert systems - and efficient compilers are available for machines ranging in power from the Apple Macintosh to the Cray supercomputers. 

Uses of C?
Ans: Operating Systems
Language Compilers
Assemblers
Text Editors
Print Spoolers
Network Drivers
Modern Programs
Data Bases
Language Interpreters
Utilities

The largest measure of C's success seems to be based on purely practical considerations:
the portability of the compiler;
the standard library concept;
a powerful and varied repertoire of operators;
an elegant syntax;
ready access to the hardware when needed;
and the ease with which applications can be optimised by hand-coding isolated procedures

What are reserved words?

Ans: a word in a programming language which has a fixed meaning and cannot be redefined by the programmer.

What is debugging?

Ans:Debugging is the process of locating and fixing or bypassing bugs (errors) in computer program code or the engineering of a hardware device.


What is a header file?

Ans:Header files provide the definitions and declarations for the library functions. Thus, each header file contains the library functions along with the necessary definitions and declarations. For example, stdio.h, math.h, stdlib.h, string.h etc.

What are C identifiers?

Ans: C identifiers are the names given to different programming elements like arrays, functions and variables. It is usually a combination of a letter, a digit and an underscore. It should start with a letter, and backspace is not allowed.

Data Types of C?

Ans:There are five basic data types associated with variables:
int - integer: a whole number.
float - floating point value: ie a number with a fractional part.
double - a double-precision floating point value.
char - a single character.
void - valueless special purpose type which we will examine closely in later sections.

What is the purpose of main() function?

Ans: The function main() invokes other functions within it.It is the first function to be called when the program starts execution.

It is the starting function.
It returns an int value to the environment that called the program.
Recursive call is allowed for main( ) also.
It is a user-defined function.

What is scope of a variable? How are variables scoped in C?

Ans: Scope of a variable is the part of the program where the variable may directly be accessible. In C, all identifiers are lexically (or statically) scoped. See this for more details.
How will you print “Hello World” without semicolon?
Ans:
int main(void)
{
    if (printf("Hello World")) ;
}   

When should we use pointers in a C program?

1. To get address of a variable
2. For achieving pass by reference in C: Pointers allow different functions to share and modify their local variables.
3. To pass large structures so that complete copy of the structure can be avoided.
C
4. To implement “linked” data structures like linked lists and binary trees.

What is NULL pointer?

Ans: NULL is used to indicate that the pointer doesn’t point to a valid location. Ideally, we should initialize pointers as NULL if we don’t know their value at the time of declaration. Also, we should make a pointer NULL when memory pointed by it is deallocated in the middle of a program.

What are local static variables? What is their use?

Ans:A local static variable is a variable whose lifetime doesn’t end with a function call where it is declared. It extends for the lifetime of complete program. All calls to the function share the same copy of local static variables. Static variables can be used to count the number of times a function is called. Also, static variables get the default value as 0. For example, the following program prints “0 1?
#include <stdio.h>
void fun()
{
    // static variables get the default value as 0.
    static int x;
    printf("%d ", x);
    x = x + 1;
}

int main()
{
    fun();
    fun();
    return 0;
}
// Output: 0 1

What are static functions? What is their use?

Ans:In C, functions are global by default. The “static” keyword before a function name makes it static. Unlike global functions in C, access to static functions is restricted to the file where they are declared. Therefore, when we want to restrict access to functions, we make them static. Another reason for making functions static can be reuse of the same function name in other files. See this for examples and more details.

Can a variable be both constant and volatile?

Yes. The const modifier means that this code cannot change the value of the variable, but that does not mean that the value cannot be changed by means outside this code.

The function itself did not change the value of the timer, so it was declared const. However, the value was changed by hardware on the computer, so it was declared volatile. If a variable is both const and volatile, the two modifiers can appear in either order.

What are binary trees?

Binary trees are actually an extension of the concept of linked lists. A binary tree has two pointers, a left one and a right one. Each side can further branch to form additional nodes, which each node having two pointers as well.

What is typecasting ?

In C programs involving arithmetic expressions, often need arises to convert a variable/expression of one data type to another. This process is called typecasting.

What is the use of typedef?

Ans. (i)It increases the portability.
(ii) It simplify the complex declaration and improve readability of the program.

What is storage class? What are the different storage classes in C?

Storage class is an attribute that changes the behavior of a variable. It controls the lifetime,scope and linkage. The storage classes in c are auto, register, and extern, static, typedef.

What is recursion?

C language a function may call another function. When a function calls itself, it is referred to as recursive call and the process is known as recursion. C provides very good facilities for recursion.

Difference between arrays and linked list?

Ans. Major differences between arrays and linked lists are: 
(i)  In array consecutive elements are stored in consecutive memory locations whereas in linked list it not so. (ii)  In array address of next element is consecutive and whereas in linked list it is specified in the address part of each node.(iii) Linked List makes better use of memory than arrays.(iv) Insertion or deletion of an element in array is difficult than insertion or deletion in linked list

What are macros? What are its advantages and disadvantages?

Ans. Macro is a Pre-processor.Major advantage of using the macro is to increase the speed of the execution of the program.
Major disadvantage of the macros are:
(i) No type checking is performed in macro. This may cause error.
(ii)  A macro call may cause unexpected results.


What is difference between Structure and Unions?

Ans. (i)    In structure every member has its own memory whereas in union its members share the same member space.
(ii)  In structure, it is possible to initialize all the members at the same time which is not possible in case of union.
(iii) A structure requires more space than union(for the same type of members).
(iv) In union different interpretations of the same memory space are possible which is not so in case of structures.



Basic Knowledge about C language

What is C Language?

C is a programming language. The C language was developed by Dennis Ritchie in 1972 at AT&T Bell Labs. It was called his newly developed language C simply because there was a B programming language already and the B language led to the development of C Language. C language is based on ALGOL and B languages.

As said above C language is a programming language. Programming language is any language that computer system can understand directly or indirectly to can perform the actions asked by the programmer as set of instructions in form of a computer program.

C language is a high-level programming language. In the computer world, the further a programming language is from the computer architecture, the higher the language's level. You can imagine that the lowest-level languages are machine languages that computers understand directly. The high-level programming languages, on the other hand, are closer to our human languages.

A computer program written in a high-level language, such as C, Java, or Perl, is just a text file, consisting of English-like characters and words. We have to use some special programs, called compilers or interpreters, to translate such a program into a machine-readable code. That is, the text format of all instructions written in a high-level language has to be converted into the binary format. The code obtained after the translation is called binary code. Prior to the translation, a program in text format is called source code. The smallest unit of the binary code is called a bit (from binary digit), which can have a value of 0 or 1. 8 bits make up one byte, and half a byte (4 bits) is one nibble.

·   C is a general-purpose programming language.
·   C is a high-level language that has the advantages of readability, maintainability, and portability.
·    C is a very efficient language that allows you to get control of computer hardware and peripherals.
·     C is a small language that you can learn easily in a relatively short time.
·     Programs written in C can be reused.
·    Programs written in C must be compiled and translated into machine-readable code before the computer can execute them.

What is C language in simple words?

C is a general-purpose programming language. It's a high-level language that has advantages such as readability, maintainability, and portability. Also, C allows you to get down to the hardware to increase the performance speed if needed. A C compiler is needed to translate programs written in C language into machine-understandable code. The portability of C is realized by recompiling the C programs with different C compilers specified for different types of computers.


Write a c program to print Hello world without using any semicolon.

C hello world program: c programming language code to print hello world. This program prints hello world, printf library function is used to display text on screen, '\n' places cursor on the beginning of next line, stdio.h header file contains declaration of printf function. The code will work on all operating systems may be its Linux, Mac or any other and compilers. To learn a programming language you must start writing programs in it and may be your first c code while learning programming.

Write a c program to print Hello world without using any semicolon.

Solution: 1

void main(){

if(printf(“Hello world”)){

}

}

Solution: 2

/* Hello World program */

void main(){

while(!printf(“Hello world”)){

}

}


Solution: 3

void main(){

switch(printf(“Hello world”)){

}

}