Top 10 Cognizant Interview Questions

Want to work for Cognizant? Here we discuss some of the commonly asked interview questions.

What is a stack?

A stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle. In the pushdown stacks only two operations are allowed: push the item into the stack, and pop the item out of the stack.

A stack is a limited access data structure – elements can be added and removed from the stack only at the top. push adds an item to the top of the stack, pop removes the item from the top. A helpful analogy is to think of a stack of books; you can remove only the top book, also you can add a new book on the top.

What is a deadlock situation?

In a multiprogramming environment, several processes may compete for a finite number of resources. A process requests resources, if the resources are not available at that time, the process enters a waiting state.

Sometimes a waiting process is never again able to change state, because the resources it has requested are held by other waiting processes. This situation is called deadlock

What is the definition of primary key, foreign key, candidate key & super key?

Candidate Key: Candidate keys are those keys which is candidate for primary key of a table. In simple words we can understand that such type of keys which full fill all the requirements of primary key which is not null and have unique records is a candidate for primary key. So thus type of key is known as candidate key. Every table must have at least one candidate key but at the same time can have several.

Primary Key: Such type of candidate key which is chosen as a primary key for table is known as primary key. Primary keys are used to identify tables. There is only one primary key per table.

Foreign Key: Foreign key are those keys which is used to define relationship between two tables. When we want to implement relationship between two tables then we use concept of foreign key. It is also known as referential integrity. We can create more than one foreign key per table. foreign key is generally a primary key from one table that appears as a field in another where the first table has a relationship to the second. In other words, if we had a table A with a primary key X that linked to a table B where X was a field in B, then X would be a foreign key in B.

Super Key: The set of attributes which can uniquely identify a tuple is known as Super Key.
Adding zero or more attributes to candidate key generates super key.
A candidate key is a super key but vice versa is not true.

What is an Operating System?

An operating system is a program that manages the computer hardware . It acts as an intermediary between the computer user and the computer hardware.

An operating system manages overall activities of a computer and the input/output devices attached to the computer . Operating system is a program most intimately involved with the hardware.

It also acts as a resource allocator , A computer system has many resources that may be required to solve a problem : CPU time , memory space , file storage space , I/O device and so on . The operating system acts as a manager to these resources.

Write a function to take an argument and return 0 if the given number is prime otherwise return -1

A no ‘n’ is prime if it is divisible only by 1 and n itself. To check if a number is divisible by any other number, we need to divide it by numbers from 2 to n/2 and check if remainder is 0.
Function to check if a number is prime or not and return values accordingly

int isPrime(int n) {

  if (n <= 1) return -1
  if (n <= 3) return 0

  for (int i = 5; i * i <= n; i = i + 6) {
    if (n % i == 0 || n % (i + 2) == 0)
      return -1;
  }
  return 0;
}

What is a gateway or router?

A node that is connected to two or more networks is commonly called as router or gateway.

These are intelligent network devices that store information in its routing table such as paths, hops and bottlenecks. With this info, they are able to determine the best path for data transfer.

Routers operate at the OSI Network Layer.

What is the difference between outer join and inner join?

Inner Join: Returns records that have matching values in both tables
Outer Join: An outer join returns a set of records (or rows) that include what an inner join would return but also includes other rows for which no corresponding match is found in the other table.
There are three types of outer joins:

  • LEFT (OUTER) JOIN: Return all records from the left table, and the matched records from the right table
  • RIGHT (OUTER) JOIN: Return all records from the right table, and the matched records from the left table
  • FULL (OUTER) JOIN: Return all records when there is a match in either left or right table

What is recursion? Give an example.

When a called function in turn calls another function a process of chaining occurs.Recursion is a special case of this process , where a function calls itself.
Example of recursion is the evaluation of factorials of given number. The factorial of a number is expressed as a series of repetitive multiplications as shown below :
Factorial of n = n(n-1)(n-2)………..1
Factorial can be calculated using recursion as follows :

factorial(int n) {
  int fact;

  if (n == 1) {
    return 1;
  } else {
    fact = n * factorial(n - 1);
  }

  return (fact);
}

Write a C code for swapping two numbers

Swapping means exchanging value of two variables.If you have taken two variables a and b where a = 4 and b = 5, then before swapping a = 4, b = 5 after swapping a = 5, b = 4. In the first C program to swap numbers we will use a temporary variable to swap two numbers.

C Program to swap two numbers in C

int main() {
    int x, y, temp;

    printf("Enter the value of x and y
        ");
        scanf("%d%d", & x, & y);

        printf("Before Swapping
          x = % d y = % d ",x,y);

          temp = x; x = y; y = temp;

          printf("After Swapping
            x = % d y = % d ",x,y);

            return 0;
          }

What are pointers in C?

A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.

The general form of a pointer variable declaration is:
type *var-name;
Here, type is the pointer’s base type; it must be a valid C data type and var-name is the name of the pointer variable. The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer.

Take a look at some of the valid pointer declarations:

int * ip; /* pointer to an integer */
double * dp; /* pointer to a double */
float * fp; /* pointer to a float */
char * ch /* pointer to a character */

The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long hexadecimal number that represents a memory address. The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to.
Example:

int main() {
  int
  var = 20; /* actual variable declaration */
  int * ip; /* pointer variable declaration */
  ip = &
    var; /* store address of var in pointer variable*/
  printf(“Address of
    var variable: % x”, &
      var);
  printf(“Address stored in ip variable: % x”, ip); /* address stored in pointer variable */
  printf(“Value of * ip variable: % d”, * ip); /* access the value using the pointer */

  return 0;
}

Output:

Address of var variable: 0x7fffa057dd4
Address stored in ip variable: 0x7fffa057dd4
Value of *ip variable: 20

Previous post Top 10 HCL Interview Questions
Next post Top 10 Mahindra-Comviva Interview Questions

Leave a Reply

Your email address will not be published. Required fields are marked *