Last Updated on July 11, 2024 by Abhishek Sharma
Infosys, a global leader in technology services and consulting, often seeks candidates proficient in various programming languages, including C. The C language, known for its efficiency and control, is a foundational language in computer science and is often a critical component of Infosys’ technical interview process. Preparing for C language questions can give candidates a competitive edge, as it demonstrates their understanding of fundamental programming concepts and problem-solving abilities. This article provides insights into the types of C language questions commonly asked during Infosys interviews and answers frequently asked questions to help candidates prepare effectively.
The company’s services include digital transformation, application development, data analytics, cloud computing, and cybersecurity. Infosys has also developed proprietary software products for various industries, such as Finacle for banking and Edge for the supply chain management.
Infosys Recruitment Process
Infosys recruitment process typically involves several stages:
- Online Test
- Technical Round
- HR Round
Online Test
The Infosys online test contains questions on quantitative ability, reasoning ability, and verbal ability. The test lasts 95 minutes. Each section of the test, such as Quantitative Ability, Reasoning Ability, and Verbal Ability, has a specific time allotted to it, and all questions in that area must be completed within that time span. The candidate can attempt questions in any sequence in a specific area but cannot move across parts, which means the candidate must first complete one section before moving on to the others.
Technical Round
Candidates who passed the online test proceed to the technical round. This round evaluates your concepts together with your technical skill. The topics covered by the questions will be diverse. Data structures and algorithms, DBMS, operating systems, networking, OOPs concepts, and a programming language of choice should all be prepared. Students from fields other than computer science should study the fundamental concepts of their field. Some hard questions may be asked.
HR Round
This is the final step in the hiring process. Questions about your resume, job, and company may be asked, along with standard HR questions like ‘What are your Career Goals?’, ‘Why Infosys?’ and so on. They will evaluate your communication skills, confidence, and attitude. If you think clearly and confidently, you will easily pass this round.
Infosys C Language Questions: Fundamentals
In this section, we will look at several basic Infosys C Language Questions
Q – 1 Why is C called a mid-level programming language?
Answer – C is considered a mid-level programming language because it provides a level of abstraction that is higher than low-level assembly languages but lower than high-level languages like Python or Java.
C is closer to the hardware and provides low-level access to system resources such as memory, I/O operations, and other hardware components, making it suitable for systems programming, embedded systems, and operating system development.
However, C also has higher-level constructs like functions, structures, and pointers, which make it easier to write complex programs and manage data structures. This combination of low-level and high-level features makes C a mid-level programming language.
Q – 2 What is a structure in C language?
Answer – A structure in C language is a user-defined data type that groups together variables of different data types under a single name. It allows the programmer to create a composite data type that can represent a complex entity, such as a record, a point in space, or a person’s information.
The syntax for structure in C is :
struct structure_name {
data_type1 variable1;
data_type2 variable2;
// ... more
};
Here, structure_name is the name of the structure, and variable1, variable2, etc. are the members of the structure, each with their own data type.
To declare a variable of the structure type, the following syntax is used:
struct structure_name variable_name;
After defining the structure, the members can be accessed using the dot (.) operator as follows:
variable_name.variable1 = value1;
variable_name.variable2 = value2;
// ... more other members
Structures in C are commonly used for creating data structures, passing complex parameters to functions, and representing records in a file or a database. They are an important feature of the language and are used extensively in system programming, network programming, and game development.
Q – 3 What is the difference between a structure and a union in C language?
Answer – In C language, a structure is a composite data type that groups together variables of different data types under a single name, while a union is a special data type that allows the same memory location to be used for different variable types at different times.
In a structure, each member has its own memory location, and the total size of the structure is the sum of the sizes of its members. In contrast, a union uses the same memory location for all of its members, and the size of the union is the size of its largest member.
Structures are typically used when you need to group related data together, while unions are typically used when you need to represent the same data in different ways or when you want to save memory by reusing the same memory location for different purposes.
Q – 4 What is dynamic memory allocation in C programming language?
Answer – Dynamic memory allocation in C programming language is the process of allocating and deallocating memory for variables during program execution. In contrast to static memory allocation, where memory is allocated at compile-time and the size is fixed throughout the program, dynamic memory allocation allows the program to request memory from the operating system during runtime and free it when it is no longer needed.
In C, dynamic memory allocation is typically performed using the functions malloc(), calloc(), and realloc(). malloc() allocates a block of memory of a specified size and returns a pointer to the first byte of the block. calloc() allocates a block of memory and initializes it to zero. realloc() is used to resize a previously allocated block of memory.
Once memory has been dynamically allocated, it can be accessed using pointers. It is important to manage dynamic memory carefully to avoid memory leaks, where memory is allocated but never freed, or invalid memory accesses, where memory is accessed after it has been freed. Memory leaks and invalid memory accesses can cause program crashes or other unexpected behavior.
Q – 5 What are the storage classes in the C programming language?
Answer – The C programming language provides four storage classes for variables and functions, which control the lifetime, scope, and visibility of the identifiers in the program:
- auto: The auto storage class is the default storage class for local variables, which are declared inside a function or block. The variables are allocated on the stack and are automatically initialized to garbage values.
- register: The register storage class is used to suggest the compiler to store the variable in a CPU register instead of memory. This can lead to faster access times for frequently used variables.
- static: The static storage class is used to declare variables with a lifetime that extends throughout the program’s execution. These variables are allocated in the data segment and are initialized to zero by default. Static variables can be either global or local to a function.
- extern: The extern storage class is used to declare variables or functions that are defined in another file or module. This enables the variables or functions to be shared across different files and modules in a program.
Q – 6 What are static variables in C programming language?
Answer – In C programming language, a static variable is a type of variable that has a lifetime that extends throughout the program’s execution, rather than just within a particular function or block. Static variables are declared using the static keyword, either within a function or outside of any function, and are allocated in the data segment of the program’s memory.
Static variables are initialized to zero by default, and they retain their values between function calls. This makes them useful for maintaining state across function calls, or for storing values that should not be reinitialized each time a function is called.
Static variables declared within a function are only visible within that function, and are not accessible from outside the function. However, static variables declared outside of any function are visible to all functions in the same file, and can be accessed from any function within that file.
One important thing to keep in mind when using static variables is that they are not thread-safe. If multiple threads access the same static variable simultaneously, it can lead to race conditions and other synchronization issues. Therefore, static variables should be used with caution in multithreaded programs.
Q – 7 Why doesn’t C support function overloading?
Answer – C programming language does not support function overloading, which is the ability to define multiple functions with the same name but different parameter lists, because C uses a simple name mangling scheme based on the function name only.
In C, the function name is not decorated with additional information about the function’s parameter types or return type. Therefore, the compiler cannot distinguish between different overloaded functions based on their parameter types, which can lead to ambiguity and confusion.
Instead, in C, functions that perform similar operations but have different input or output types are typically given different names, or the function names are made more specific to differentiate them. This can lead to longer and more descriptive function names, but it helps to avoid confusion and ambiguity.
However, it is worth noting that some C libraries or frameworks implement similar functionality to function overloading by defining different functions with different names but the same functionality.
Q – 8 What are Enumerations?
Answer – Enumerations, also known as enums, are user-defined data type in C programming language that enable developers to define a set of named values, where each value represents a distinct constant. Enums are typically used to improve code readability and maintainability by providing a descriptive name for each value.
An enumeration is declared using the enum keyword, followed by a list of named values, separated by commas and enclosed in curly braces. For example,
enum color {
RED,
GREEN,
BLUE
};
In this example, we have defined an enumeration named color with three named values: RED, GREEN, and BLUE. The names are case-sensitive and must be unique within the enumeration.
Enums can be used to define variables with the enum type, as well as function parameters and return types. For example,
enum color c = RED;
void setColor(enum color c) {...}
enum color getColor() {...}
Enums are implemented as integer constants by default, where the first value is assigned the integer value 0, the second value is assigned the integer value 1, and so on. However, you can also explicitly assign integer values to individual names in the enumeration. For example
enum color {
RED = 1,
GREEN = 2,
BLUE = 4
};
In this example, RED has the integer value 1, GREEN has the integer value 2, and BLUE has the integer value 4. The remaining values are assigned the next integer values in sequence, starting from the previous value plus one.
Q – 9 What is the difference between malloc() and calloc()?
Answer – Both malloc() and calloc() are used for dynamic memory allocation in the C programming language. However, they differ in how they allocate and initialize the memory.
malloc() is used to allocate a block of memory of a specified size, in bytes. It returns a pointer to the beginning of the block of memory, which can be used to store data. The malloc() function does not initialize the memory it allocates, so the memory may contain garbage values. It is up to the programmer to initialize the memory before using it.
calloc() is also used to allocate a block of memory of a specified size, in bytes. However, it differs from malloc() in that it initializes the memory to zero. This can be useful when you need to allocate an array of elements and you want to make sure that all elements are initially set to zero.
The calloc() function takes two arguments: the number of elements to allocate, and the size of each element in bytes. It returns a pointer to the beginning of the block of memory, which can be used to store data.
In summary, the main difference between malloc() and calloc() is that malloc() only allocates memory, while calloc() allocates and initializes memory to zero. calloc() is often used for allocating arrays of elements, while malloc() is used for allocating single blocks of memory.
Q – 10 What is the use of printf() and scanf() functions?
Answer – printf() and scanf() are two of the most commonly used input/output functions in the C programming language.
printf() is used to print output to the console or terminal. It allows you to format and print data, such as numbers, characters, and strings. The printf() function takes one or more arguments, which specify the data to be printed and how to format it.
For example, you can use the printf() function to print a message to the console, like this:
printf("Hello, world!\n");
The \n character is used to add a newline after the message.
scanf() is used to read input from the console or terminal. It allows you to read data, such as numbers, characters, and strings, from the user. The scanf() function takes one or more arguments, which specify the type of data to be read and where to store it.
For example, you can use the scanf() function to read an integer value from the user, like this:
int n;
printf("Enter an integer: ");
scanf("%d", &n);
The %d format specifier is used to read an integer value, and the & operator is used to pass the address of the variable n to scanf() so that the input value can be stored in n.
In summary, printf() is used to print output to the console or terminal, while scanf() is used to read input from the console or terminal. Both functions are essential for interacting with users in console-based C programs.
Infosys C Language Questions: Coding
In this section, we will discuss Infosys C Language: Coding Questions.
Q – 1 Write a C program to check if it is a palindrome number or not using a recursive method.
Ans –
#include <stdio.h> int isPalindrome(int num, int temp) { if (num == 0) { return temp; } temp = (temp * 10) + (num % 10); return isPalindrome(num / 10, temp); } int main() { int num, temp; scanf("%d", &num); temp = isPalindrome(num, 0); if (temp == num) { printf("%d is a palindrome number.", num); } else { printf("%d is not a palindrome number.", num); } return 0; }
Input
1221
Output
1221 is a palindrome number.
Q – 2 Write a C program to Add Two Numbers Without Using the Addition Operator
Ans –
#include <stdio.h> int addWithoutPlus(int num1, int num2) { int carry; while (num2 != 0) { carry = num1 & num2; num1 = num1 ^ num2; num2 = carry << 1; } return num1; } int main() { int num1, num2, sum; scanf("%d", &num1); scanf("%d", &num2); sum = addWithoutPlus(num1, num2); printf("The sum of %d and %d is %d\n", num1, num2, sum); return 0; }
Input
12 17
Output
The sum of 12 and 17 is 29
Q – 3 Write a C program to check the given number format is in binary or not.
Ans –
#include <stdio.h> #include <stdbool.h> bool isBinary(int num) { while (num != 0) { int digit = num % 10; if (digit != 0 && digit != 1) { return false; } num /= 10; } return true; } int main() { int num; scanf("%d", &num); if (isBinary(num)) { printf("%d is in binary format.\n", num); } else { printf("%d is not in binary format.\n", num); } return 0; }
Input
101
Output
101 is in binary format.
Q – 4 Write a C program to Subtract Two Numbers Without Using the Subtraction Operator
Ans –
#include <stdio.h> int subtractWithoutMinus(int num1, int num2) { while (num2 != 0) { int borrow = (~num1) & num2; num1 = num1 ^ num2; num2 = borrow << 1; } return num1; } int main() { int num1, num2, difference; scanf("%d", &num1); scanf("%d", &num2); difference = subtractWithoutMinus(num1, num2); printf("The difference between %d and %d is %d\n", num1, num2, difference); return 0; }
Input
15 7
Output
The difference between 15 and 7 is 8
Q – 5 Write a C program to remove duplicates in a given array?
Ans –
#include <stdio.h> int removeDuplicates(int arr[], int n) { int count = 0; for (int i = 0; i < n; i++) { int j; for (j = 0; j < i; j++) { if (arr[i] == arr[j]) { break; } } if (j == i) { arr[count++] = arr[i]; } } return count; } int main() { int n; scanf("%d", &n); int arr[n]; for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } n = removeDuplicates(arr, n); printf("The array after removing duplicates is: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; }
Input
8
1 4 6 4 1 5 6 9
Output
The array after removing duplicates is: 1 4 6 5 9
Conclusion
Preparing for C language questions is an essential part of the Infosys interview process for technical roles. A strong foundation in C programming, coupled with consistent practice and a thorough understanding of key concepts, can significantly enhance your chances of success. By reviewing the common questions and answers provided in this FAQ, candidates can better prepare themselves and approach the interview with confidence. Remember, proficiency in C not only showcases your technical skills but also reflects your problem-solving abilities and attention to detail, qualities highly valued by Infosys.
Frequently Asked Questions (FAQs) Related to Infosys C Language Questions
Below are some FAQs on Infosys C language Questions
1. What types of C language questions are asked in Infosys interviews?
Infosys interview questions on the C language can vary from basic to advanced topics. Candidates can expect questions on basic syntax, data types, control structures like if-else statements and loops, functions, and recursion. Additionally, there are usually questions on pointers and memory management, such as pointer arithmetic and dynamic memory allocation. Arrays and strings, including manipulation and multi-dimensional arrays, are also commonly tested. Advanced topics might include structures, unions, file I/O operations, and algorithms related to problem-solving. Understanding and being able to write efficient code for these areas are critical for performing well in Infosys technical interviews.
2. How should I prepare for C language questions in Infosys interviews?
Preparation for C language questions should be comprehensive and focused. Start with reviewing basic concepts like syntax, data types, and control structures. Practice coding regularly on platforms like LeetCode, HackerRank, and CodeChef to enhance problem-solving skills. Pay special attention to understanding pointers and memory management as these are crucial in C programming. Study common algorithms and data structures such as sorting, searching, linked lists, stacks, and queues. Reading standard C programming books like "The C Programming Language" by Kernighan and Ritchie can also be very helpful. Participating in mock interviews can provide a realistic practice environment and boost confidence.
3. Can you provide an example of a C language question asked in Infosys interviews?
A typical example of a C language question that might be asked in an Infosys interview is: "Write a C program to reverse a string without using any library functions." This question tests a candidate’s understanding of string manipulation and their ability to implement logic without relying on built-in functions. The answer would involve writing a program that manually swaps characters in the string from both ends until the middle is reached. This type of question assesses both the candidate’s coding skills and their grasp of fundamental programming concepts.
4. What are some common mistakes to avoid when answering C language questions in an interview?
Common mistakes candidates should avoid include syntax errors and not adhering to C language conventions. It’s important to handle edge cases properly, such as empty strings or null pointers, to ensure the robustness of your code. Memory leaks are another critical aspect to watch out for; candidates should ensure they properly allocate and deallocate memory. Writing clear comments is also essential to explain the logic behind your code. Finally, it’s crucial to test your code thoroughly with various inputs to ensure it works correctly under different scenarios. Avoiding these mistakes can significantly improve your performance in the interview.
5. How important is C language proficiency for a role at Infosys?**4. What are some common mistakes to avoid when answering C language questions in an interview?
**
C language proficiency is crucial for many technical roles at Infosys, especially those involving low-level programming, embedded systems, and performance-critical applications. A strong understanding of C programming demonstrates a candidate’s ability to handle fundamental programming concepts, manage memory efficiently, and write optimized code. These skills are highly valued at Infosys, as they reflect a candidate’s technical prowess and problem-solving capabilities. Therefore, having a solid grasp of the C language can give candidates a significant advantage in the hiring process.
6. What resources can I use to improve my C language skills?
There are numerous resources available to improve C language skills. Standard textbooks like "The C Programming Language" by Brian W. Kernighan and Dennis M. Ritchie, and "C Programming: A Modern Approach" by K. N. King are highly recommended. Online courses from platforms like Coursera, Udemy, and edX offer structured learning paths. Practicing problems on coding platforms like LeetCode, HackerRank, CodeChef, and GeeksforGeeks can enhance problem-solving skills. Additionally, referring to official documentation and participating in study groups or forums can provide valuable insights and support. Utilizing these resources can significantly improve proficiency in C programming.
7. Can I use C++ instead of C in the Infosys interview?
While Infosys primarily focuses on C language questions for certain roles, demonstrating proficiency in C++ can also be advantageous. However, it is essential to clarify with the interviewer if C++ can be used to answer specific questions, as some interviewers may strictly require solutions in C. Understanding the core differences between C and C++ and being prepared to switch between the two languages if necessary can be beneficial. Nonetheless, candidates should primarily focus on mastering C programming concepts to align with the typical requirements of Infosys technical interviews.