Cognizant Technical Interview Questions


Questions mainly asked from:

  • C Theory/Programs
  • C++ or Java
  • OOPS
  • Software Engineering
  • Optional – DBMS, Operating Systems, Data Structures

Cognizant C Programming Interview Questions

1) What is a pointer? Define it.

Pointer is a variable which stores the address of other variables which hold some value in it. Directly pointer is used to point values of variables indirectly. We can manipulate its values.


2) What is a dangling pointer in C?

Dangling pointer is a pointer which does not point to a valid object of the appropriate type. It appears when a pointer is in the stack but not in the memory in a heap. Char *p =NULL; A dangling pointer attempt to deallocate without allocating space will result in a segmentation fault.

Dangling pointers arise during object destruction, when an object that has an incoming reference is deleted or deallocated, without changing the data of the pointer, so that the pointer points to the memory location of the deallocated memory.


3) What is a memory leak in a heap?

When there is a space area in a heap, but no variable is in the stack pointing to that memory.


4) What is a data type?

Data Type defines the type of value which are allocated by us to a variable and have a range in between we assign values. E.g., “Int” it is a data type which able to store data between “-32768 to +32768” in C.


5) What is the size of the integer data type?

Size of it is 2 Byte or 4 Byte.


6) What is malloc?

Allocates requested size of bytes and returned a pointer first byte of allocated space. Malloc is using for dynamic memory allocation.

Syntax:

  1. ptr = (cast-type*) malloc(byte-size)

7) What is a string?

A string is a sequence of characters. It can be defined using the array or by using String header file.


8) “/0” in a string?

This symbol shows the ending of the string.

E.g. char a[] = {“s”,”e”,”t”,”/0″};.


9) What is recursion?

The process by which a method calls itself directly or indirectly, again and again, is called recursion and the corresponding function.


10) What is the difference between a pre-increment operator and post-increment operator?

Pre-increment operator used incrementing the variable value by one before assigning the cost to the variable. Post-increment operator use to incrementing the variable value by one after assigning the value to the variable.


11) What are the key features or characteristics OFC language?

  • Reliability
  • Portability
  • Flexibility
  • Interactivity
  • Modularity
  • Efficiency and Effectiveness

12) What is embedded C?

Embedded C is the extension of C programming language.

Embedded C is used to develop microcontroller-based applications.

Embedded C includes features not available in standard C like fixed-point arithmetic, named address spaces, and necessary I/O hardware addressing.

Cell phones, MP3 players are some example of embedded systems in which integrated C is used to program and control these devices.


13) Which level is C language belonging?

C language is belonging to middle-level language. C language behaves as a bridge between machine level (low level) languages and high-level languages.

C language is more user-friendly than machine level languages. And, C language does not support all the concepts that high-level languages offer. So, C programming language called as middle-level language.


14) What is the difference between structured oriented, object-oriented and non-structure oriented programming language?

Structured oriented programming language –

In this type of language, large codes fragmented into small programs called functions.
The main focus is on procedures and functions that operate on values
Data moves without any restrictions around the systems from one procedure to another
Program structure follows “Top-Down Approach.”
Example: C, Pascal, ALGOL, and Modula-2

Object-oriented programming language –

In this type of language, programs fragmented into objects
The main focus is on the data/values that are manipulating and not on the procedures or functions.
Data is hidden from functions and cannot access by external functions
Program structure follows the “Bottom UP Approach.”
Example: C++, and C# (C sharp)

Non-structure oriented programming language –

There is no specific structure rule for programming this language.
Example: BASIC, COBOL, FORTRAN


14) What is modifier in C?

Modifiers derive the space to allocate for a variable.

Modifiers are fixed with basic data types to modify (either increase or decrease) the amount of storage allocated to a variable.

For example, storage requirements for int data type are 4 bytes for a 32-bit processor. We can increase range by using long int data type which is 8 byte. We can decrease the range by using short int which is 2 byte.


15) What is the difference between variable declaration and variable definition in C?

Variable declaration tells the compiler about data type and size of the variable. Whereas, variable definition allocates memory to the variable

Variable can be declared many times in a program. But, the definition can happen only one time for a variable in a program.

Variable declaration is for assignment of properties and identification to a variable. Whereas, a variable definition is for assignments of storage space to a variable


Cognizant OOPs Interview Questions


1) What are virtual Functions?

In object-oriented programming, in languages such as C++, and Object Pascal, a virtual function or virtual method is an inheritable and overrideable function or method for which dynamic dispatch facilitated. This concept is an essential part of the (runtime) polymorphism portion of object-oriented programming (OOP).


2) What is overloading in OOPs?

Overloading is a process used to avoid redundant code where the same method name used multiple times but with a different set of parameters. The actual method that gets called during runtime is resolved at compile time, thus avoiding runtime errors.


3) What is overriding?

In any object-oriented programming, Overriding is a process that allows a child class or subclass to provide a specific implementation of functions that is already provided by one of its super-classes or parent classes.


4) What is polymorphism?

By using the ability of polymorphism an object can take on multiple types. The frequent use of polymorphism occurs when a parent class refers to an object of the child class. The Java object that can pass more than one IS-A test is polymorphic.


5) What is data abstraction?

In oops, abstraction is one of three main principles (along with encapsulation and inheritance). Through the process of abstraction, a programmer hides all but the relevant data about an object to reduce complexity and increase efficiency.


6) What is encapsulation?

Encapsulation is one of the primary concepts in object-oriented programming. It describes the idea of combining data in a single class and methods that work on that data, e.g., a class which is a blueprint in java. This concept is also often used to preserve the internal state, of an object from the outside.


7) What is Inheritance in java?

In object-oriented programming, inheritance helps new objects to take on the properties of old objects. A class that uses for inheritance is called a base class or superclass. A class that inherits data from a base class is called a subclass or derived class


8) What is the constructor?

A constructor is a method which is used to initialize a newly created object and is called just after when memory allocated to the object. It can be used to implement the objects to desired values or default values at the time of object creation.


9) What is the destructor?

A destructor used when the initialized object is destructing it called automatically during the destruction of an object. It helps in recovering the heap space and remove files.


Cognizant Data structure Interview Questions


1) What is a Data Structure?

Data Structure is a process in which management or organization of data that enable a more efficient way to organize the data.


2) What have linked lists?

A linked list is a set of ordered data and each of its previous nodes connected to its succeeding node. It is like an array but more efficient then array because we can insert or delete data in between the nodes.


3) Is the array of data structure?

Yes, because it also handles the data in a structured way.


4) Define a binary search Tree?

It is also known as ordered or sorted binary tree. It keeps there nodes or keys in sort form, and the root node of BST is bigger than the left node and smaller than the right node.


5) What is a Binary Tree in the data structure?

A binary tree is part of a data structure that has two sub-nodes, a right node, and a left node. In programming, binary trees are an advanced version of the linked list.


6) What is the dissimilarity between Push and Pop methods of the stack?

Pushing and popping applies to the way data is stored and fetched from a stack. A push method denotes data being added to it, meaning information is being “pushed” in the stack. On the other hand, a pop method denotes data retrieval/fetch, and in particular, refers to the first data is being fetched.


7) What is the dissimilarity between a stack and the array?

A stack base on LIFO pattern. It means that data access follows a sequential process wherein the last data to be entered when the first one deleted. Arrays do not follow a particular order and instead can access by referring to the indexed element within the array.


8) What are doubly linked Lists?

Doubly linked lists are a particular part of linked list wherein traversal across the data elements can be done in any directions. This is possible by having two links in every node, one that connects to the next node and another one that links to the previous node.


9) What is a queue data structure?

A Queue is a data structure which is linear and follows a fixed order in which the operations occur. The order is always First In First Out (FIFO). An excellent example of a queue is any queue of the customer for a resource where the customer that came first served first. The difference between stacks and queues is while deleting.


10) What is a stack data structure?

Basic features of Stack are an ordered list of the similar data type. The stack is LIFO (Last In First Out) data structure, or we can say FILO (First In Last Out) data structure. Push () method is used to insert/enter new elements into the Stack and pop() method is used to remove/delete an element from the stack.


11) What is a graph in the data structure?

A graph shows a representation of a set of objects/nodes where some pairs of object/nodes are connected by links/connection. The interconnection of objects are represented by points termed as vertices, and the links that connect the vertices are called edges.


12) What is a priority queue?

A priority queue is an abstracted data type which is like a queue or stack data structure, but where additionally each element has a “priority” associated with it. In a priority queue, an element with high priority is served before an element with low priority.


13) Show the time complexity of the linked list during insertion?

The time complexity of insertion/deletion in a singly linked list depends upon the position where you want to perform them. For example, if you want to insert an element at the end of the linked list, then you have to traverse/travel the entire list, and hence the complexity will be O(n).


Cognizant Programming Questions


1) Write a program to reverse any number.

This code below is written in Java.

  1. package javaapplication6;
  2. import java.util.Scanner;
  3. public class JavaApplication6 {
  4.     public static void main(String[] args)
  5.     {
  6.         int i, temp, sum=0, n;
  7.         Scanner sc = new Scanner(System.in);
  8.         n=sc.nextInt();
  9.         temp=n;
  10.         while(n>0)
  11.         {
  12.             int r = n%10;
  13.             sum = sum*10+r;
  14.             n = n/10;
  15.         }
  16.         System.out.println(“Reverse of Number is:-“ +sum);
  17.         if(temp==sum)
  18.         {
  19.             System.out.println(“Palindrom”);
  20.         }
  21.         else
  22.         {
  23.             System.out.println(“Not Palindrom”);
  24.         }
  25.     }
  26. }

2) Write a program to find out some of the digits of the given number.

  1. package javaapplication6;
  2. import java.util.Scanner;
  3. public class JavaApplication6 {
  4.     public static void main(String[] args)
  5.     {
  6.         int i, temp, sum=0, n;
  7.         Scanner sc = new Scanner(System.in);
  8.         n=sc.nextInt();
  9.         temp=n;
  10.         while(n>0)
  11.         {
  12.             int r = n%10;
  13.             sum = sum+r;
  14.             n = n/10;
  15.         }
  16.         System.out.println(“Reverse of Number is:-“ +sum);
  17.     }
  18. }

3) Write a program to find out the power of a number.

  1. package javaapplication6;
  2. import java.util.Scanner;
  3. public class JavaApplication6 {
  4. public static void main(String[] args)
  5.     {
  6.         int result=1, n;
  7.         Scanner sc = new Scanner(System.in);
  8.         System.out.println(“the Exoponent is:- “);
  9.         n=sc.nextInt();
  10.         System.out.println(“the base is:- “);
  11.         int base = sc.nextInt();
  12.         while (n != 0)
  13.     {
  14.         result *= base;
  15.         –n;
  16.     }
  17.         System.out.println(“Power of Number is:-“ +result);
  18.     }
  19. }

4) Write a program to add two numbers without using the addition operator.

  1. package javaapplication6;
  2. import java.util.Scanner;
  3. public class JavaApplication6 {
  4.    static int Add(int x, int y)
  5.     {
  6.         while (y != 0)
  7.         {
  8.             int carry = x & y;
  9.             x = x ^ y;
  10.             y = carry << 1;
  11.         }
  12.         return x;
  13.     }
  14.     public static void main(String arg[])
  15.     {
  16.         Scanner sc = new Scanner(System.in);
  17.         System.out.println(“First value is:- “);
  18.         int a=sc.nextInt();
  19.         System.out.println(“Second value is:- “);
  20.         int b=sc.nextInt();
  21.         System.out.println(“Sum of both digits:- “+Add(a, b));
  22.     }
  23. }

5) Write a program to subtract two numbers without using a subtraction operator.

  1. package javaapplication6;
  2. import java.util.Scanner;
  3.      public class JavaApplication6 {
  4.    static int Add(int x, int y)
  5.     {
  6.         while (y != 0)
  7.         {
  8.             int borrow = ~ x & y;
  9.             x = x ^ y;
  10.             y = borrow << 1;
  11.         }
  12.         return x;
  13.     }
  14.     public static void main(String arg[])
  15.     {
  16.         Scanner sc = new Scanner(System.in);
  17.         System.out.println(“First value is:- “);
  18.         int a=sc.nextInt();
  19.         System.out.println(“Second value is:- “);
  20.         int b=sc.nextInt();
  21.         System.out.println(“Sum of both digits:- “+Add(a, b));
  22.     }
  23.     }

6) Write a program to show largest among three numbers using binary minus operator.

  1. package javaapplication9;
  2. import java.util.Scanner;
  3. public class JavaApplication9 {
  4.     public static void main(String[] args)
  5.     {
  6.         Scanner s = new Scanner(System.in);
  7.         int a = s.nextInt();
  8.         int b = s.nextInt();
  9.         int c = s.nextInt();
  10.         if(a-b>0 && a-c>0)
  11.             System.out.println(“Greatest is a :-“+a);
  12.     else
  13.          if(b-c>0)
  14.              System.out.println(“\nGreatest is b :-“+b);
  15.          else
  16.              System.out.println(“\nGreatest is b :-“+c);
  17.     }
  18. }

7) Write a program to show the largest among three numbers using conditional operator.

  1. package javaapplication9;
  2. import java.util.Scanner;
  3. public class JavaApplication9 {
  4.     public static void main(String[] args)
  5.     {
  6.         Scanner s = new Scanner(System.in);
  7.         int a = s.nextInt();
  8.         int b = s.nextInt();
  9.         int c = s.nextInt();
  10.         int big = a > b ? (a > c ? a : c) : (b > c ? b : c) ;
  11.              System.out.println(“\nGreatest elements is :-“+ big);}}

8) Write a program to show the generic root of any number.

  1. package javaapplication9;
  2. import java.util.Scanner;
  3. public class JavaApplication9 {
  4.     public static void main(String[] args)
  5.     {
  6.         Scanner s = new Scanner(System.in);
  7.         int a = s.nextInt();
  8.         int b=0,c;
  9.         while(a>10){
  10.         b=0;
  11.         while(a>0){
  12.         c=a%10;
  13.         a=a/10;
  14.         b+=c;
  15.         }
  16.         if(b>10)
  17.         a=b;
  18.         else
  19.         break;
  20.     }
  21. System.out.println(“\nSum of the digits in single digit is:-“+b);}}

9) Write a c program to show the prime factor of a given number.

  1. package javaapplication9;
  2. import java.util.Scanner;
  3. public class JavaApplication9 {
  4.     public static void main(String[] args)
  5.     {
  6.         Scanner s = new Scanner(System.in);
  7.         int a = s.nextInt();
  8.         int b=0,c,j,i,isPrime;
  9.        for(i=2; i<=a; i++)
  10.     {
  11.         /* Check ‘i’ for factor of num */
  12.         if(a%i==0)
  13.         {
  14.             /* Check ‘i’ for Prime */
  15.             isPrime = 1;
  16.             for(j=2; j<=i/2; j++)
  17.             {
  18.                 if(i%j==0)
  19.                 {
  20.                     isPrime = 0;
  21.                     break;
  22.                 }
  23.             }
  24.             /* If ‘i’ is Prime number and factor of num */
  25.             if(isPrime==1)
  26.             {
  27.                 System.out.println(i);
  28.             }
  29.         }
  30.     }
  31. }
  32. }

Get Free Job Alerts on eMail !

Follow us on Telegram !

Campus Placement Guaranteed !

Follow us on Instagram !

Follow us on Facebook !

Follow us on LinkedIn !

Download Our APP !

Get Access To Our Premium Courses
Install our application from PlayStore and get discounts on our new courses.

Pin It on Pinterest