Cognizant Interview questions for freshers are discussed here. These questions were asked in recent Cognizant on-campus and off-campus drives. Going through these can help you understand what questions you can expect.
Cognizant has changed its recruitment pattern recently. A new section called “Automata fix questions” is included in Cognizant’s online test via Amcat. So it is advisable to go through Previous year Cognizant placement papers before you give your exam.
Also for more updates on Cognizant off campus drives, keep checking here.
1) What are different types of modifiers in C?
There are 5 modifiers available in C language. They are: short, long, signed, unsigned, and long long
2) What is a static variable?
A static local variable retains its value between the function call and the default value is 0. The following function will print 1 2 3 if called thrice.
3) a++ or a = a+1, which can be recommended to increment the value by 1 and why?
a++, as it is single machine instruction (INC) internally.
4) What is a dangling pointer?
A pointer initially holding a valid address, but later the held address is released or freed. Then such a pointer is called as a dangling pointer.
5) What are lvalue and rvalue?
The expression appearing on the right side of the assignment operator is called as rvalue. Rvalue is assigned to an lvalue, which appears on the left side of the assignment operator. The lvalue should designate to a variable not a constant.
6) Explain the use of %i format specifier w.r.t scanf().
Can be used to input integer in all the supported format.
7) When to use -> (arrow) operator.
If the structure/union variable is a pointer variable, to access structure/union elements the
arrow operator is used.
8) Which built-in library function can be used to re-size the allocated dynamic memory?
9) What is typecasting?
Typecasting is a way to convert a variable/constant from one type to another type.
10) What is meant by Compilation?
The process of translating source code written in high level to low-level machine code is called as Compilation. The compilation is done by special software known as a compiler. The compiler checks source code for any syntactical or structural errors and generates object code with extension .obj (in Windows) or .o (in Linux) if source code is error free.
11) What are the stages in C compilation?
12) What do you mean by pre-processing a source file?
The C compilation begins with pre-processing of a source file. Pre-processor is a small software that accepts C source file and performs below tasks.
– Remove comments from the source code.
– Macro expansion.
– Expansion of included header files.
After pre-processing, it generates a temporary file with .i extension. Since it inserts the contents of header files to our source code file. Pre-processor generated file is larger than the original source file.
13) Difference between the header file and library file.
In simple terms, the library contains function body whereas header file contains function prototype.
14) What is the difference between far and near pointers?
In the first place, they are non-standard keywords. A near pointer can access only 2^15 memory space and the far pointer can access 2^32 memory space. Both the keywords are implementation specific and are non-standard.
15) What is a ternary operator and define its syntax?
Ternary operator is the same as if else control statement in C.
Syntax : (Condition? true_value: false_value);
Example: (A > 100? 0: 1);
16) Difference between memcpy() and strcpy() functions in C.
memcpy() function is used to copy a specified number of bytes from one memory to another.
Whereas, strcpy() function is used to copy the contents of one string into another string.
memcpy() function acts on memory rather than value. Whereas, strcpy() function acts on value rather than memory.
17) What is const pointer in C?
Const pointer is a pointer that can’t change the address of the variable that is pointing to. Once const pointer is made to point one variable, we can’t change this pointer to point to any other variable. This pointer is called const pointer.
18) List out some of C compilers.
• Digital mars
• GCC compiler
• MikroC Compiler
19) What is the difference between variable declaration and variable definition in C?
20) What is enum in C?
21) What will i and j equal after the code below is executed?
int i = 5; int j = i++;
When these operators precede a variable, the value of the variable is modified first and then the modified value is used. For example, if we modified the above code snippet to instead say int j = ++i;, i would be incremented to 6 and then j would be set to that modified value, so both would end up being equal to 6.
However, when these operators follow a variable, the unmodified value of the variable is used and then it is incremented or decremented. That’s why, in the statement int j = i++; in the above code snippet, j is first set to the unmodified value of i(i.e., 5) and then i is incremented to 6.
22) Is there a difference between class and struct?
The only difference between a class and struct are the access modifiers. Struct members are
public by default; class members are private. It is good practice to use classes when you need an
object that has methods and structs when you have a simple data object.
23) Explain the volatile and mutable keywords.
The volatile keyword informs the compiler that a variable may change without the compiler knowing it. Variables that are declared as volatile will not be cached by the compiler, and will thus always be read from memory. The mutable keyword can be used for class member variables. Mutable variables are allowed to change from within const member functions of the class.
24) How many times will this loop execute? Explain your answer.
unsigned char half_limit = 150;
for (unsigned char i = 0; i < 2 * half_limit; ++i)
// do something;
Code will result in an infinite loop.
The expression 2 * half_limit will get promoted to an int (based on C++ conversion rules) and will have a value of 300. However, since i is an unsigned char, it is represented by an 8-bit value which, after reaching 255, will overflow (so it will go back to 0) and the loop will, therefore, go on forever.
25) What is a main() and difference b/w void main() and int main()?
main() is an entry point ( main function) which is in most programming languages, When compiler begins compile the program, It looks for an entry point, and main() acts as an entry point in C program, or we can say main is a thread/ process/ function that invokes automatically by the compiler when program is being executed.
Every function returns a value to the calling function, at that time main will be a called function for compiler/OS and it will return some value to the compiler before exit, here void and int defines that main will return a void (nothing) and int will return an integer value to the compiler.