0% found this document useful (0 votes)
33 views

Some Important Questions With Answers

The document discusses various programming concepts in C including: 1. The difference between for and while loops, and while and do-while loops. 2. The three steps to create a user-defined function: declaration, definition, and call. 3. The definitions of function signature, function prototype, and recursive functions with examples. 4. The differences between arrays and linked lists in terms of size, memory allocation, access time, and insertion/deletion efficiency. 5. The types of errors in C programming: syntax errors, logical errors, and runtime errors with examples. 6. The four main storage classes in C: auto, register, static, and extern and their usage.

Uploaded by

chandramaity673
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Some Important Questions With Answers

The document discusses various programming concepts in C including: 1. The difference between for and while loops, and while and do-while loops. 2. The three steps to create a user-defined function: declaration, definition, and call. 3. The definitions of function signature, function prototype, and recursive functions with examples. 4. The differences between arrays and linked lists in terms of size, memory allocation, access time, and insertion/deletion efficiency. 5. The types of errors in C programming: syntax errors, logical errors, and runtime errors with examples. 6. The four main storage classes in C: auto, register, static, and extern and their usage.

Uploaded by

chandramaity673
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Some important questions with answers

(based on the question papers of the previous years)

Q) Distinguish between for loop and while loop


Q) Differentiate between while and do while loop

Q) What is a function? Write the 3 steps to create a user defined function in C.

A function in C is a self-contained block of code that performs a specific task. It is designed to be reusable
and modular, allowing you to organize your code efficiently. Functions in C have a name, a return type
(which can be void if the function doesn't return a value), and a set of parameters (input values) that it can
take.

Here are the three steps to create a user-defined function in C:

Step 1: Function Declaration

Declare the function at the beginning of your C program or in a separate header file. The declaration
specifies the function's name, return type, and parameter types (if any). It tells the compiler what to expect
when the function is called.
// Function declaration
int addNumbers(int a, int b);

Step 2: Function Definition

Define the function by providing the actual code that will be executed when the function is called. The
definition includes the function's name, return type, parameters (if any), and the body of the function
enclosed in curly braces {}.

// Function definition
int addNumbers(int a, int b) {
int sum = a + b;
return sum;
}

Step 3: Function Call

Call the function from your program by using its name, passing any required arguments. The function will
execute its code and return a result if it has a return type.

#include <stdio.h>

int main() {
// Function call
int result = addNumbers(5, 7);

// Print the result


printf("Sum: %d\n", result);

return 0;
}

In this example, addNumbers is a user-defined function that takes two integers as parameters (a and b) and
returns their sum. The main function calls addNumbers(5, 7), and the result is printed to the console.

Q) Describe the following with examples:

a) Function signature

b) Function Prototype

c) Recursive function

a) Function Signature:

The function signature is a part of a function declaration or definition that includes the function's name,
return type, and parameter types. It provides essential information about the function's interface, allowing
the compiler to understand how the function should be called and used.

In C, the function signature includes the following elements:

return_type function_name(parameter_type1 param1, parameter_type2 param2, ...);

For example, consider the following function signature:


int addNumbers(int a, int b);

In this signature:

 int is the return type, indicating that the function returns an integer.
 addNumbers is the function name.
 (int a, int b) specifies two parameters, both of type int.

b) Function Prototype:

A function prototype is a declaration of a function that provides the necessary information to the compiler
without the actual function implementation. It typically appears at the beginning of a C program or in a
header file, allowing the compiler to recognize the function's existence and verify that it is used correctly in
the program.

The syntax for a function prototype is similar to the function signature:

return_type function_name(parameter_type1 param1, parameter_type2 param2, ...);

Here's an example of a function prototype:

int addNumbers(int a, int b);

The function prototype informs the compiler that there is a function named addNumbers that takes two int
parameters and returns an int. This allows the compiler to catch errors related to incorrect function usage
before the actual function definition is encountered.

c) Recursive Function:

A recursive function is a function that calls itself, either directly or indirectly, to solve a smaller instance of
the same problem. Recursive functions are based on the principle of breaking down a problem into simpler
subproblems and solving them. In C, recursion is a powerful technique but should be used with caution to
avoid stack overflow.

Example of a recursive function to calculate the factorial of a number:

#include <stdio.h>

// Recursive function to calculate factorial


int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}

int main() {
int result = factorial(5);
printf("Factorial of 5 is: %d\n", result);
return 0;
}

In this example, the factorial function calls itself to calculate the factorial of a number. The base case ( n
== 0 or n == 1) provides a termination condition for the recursion. The recursive call ( return n *
factorial(n - 1);) breaks the problem into smaller instances until reaching the base case.
Q) What is an array? How is it different from a linked list?

Array:

An array is a data structure that stores a fixed-size, sequential collection of elements of the same type. Each
element in an array is identified by an index or a key, which represents its position in the collection. Arrays
are commonly used for organizing and manipulating a set of similar data items, such as a list of numbers or
a series of characters.

Key characteristics of arrays:

1. Fixed Size: Arrays have a fixed size determined at the time of declaration. Once the size is set, it
cannot be changed during runtime.
2. Random Access: Elements in an array can be directly accessed using their index. This allows for
constant-time access to any element in the array.
3. Contiguous Memory: Array elements are stored in contiguous memory locations. This feature
contributes to efficient memory access and retrieval.
4. Memory Efficiency: Arrays are memory-efficient as they require a single block of memory to store
elements.

Linked List:

A linked list is a data structure that consists of nodes, where each node contains data and a reference (link or
pointer) to the next node in the sequence. Unlike arrays, linked lists can dynamically grow or shrink during
runtime, and their elements are not required to be stored in contiguous memory locations.

Differences between Arrays and Linked Lists:

1. Size:
o Array: Fixed size.
o Linked List: Dynamic size.
2. Memory Allocation:
o Array: Contiguous memory allocation.
o Linked List: Non-contiguous memory allocation.
3. Access Time:
o Array: Constant-time access (O(1)) using index.
o Linked List: Sequential access, typically linear time (O(n)).
4. Insertion and Deletion:
o Array: Inefficient for insertion and deletion in the middle, as it requires shifting elements.
o Linked List: Efficient for insertion and deletion anywhere in the list, as it involves updating
references.
5. Memory Efficiency:
o Array: Generally more memory-efficient due to contiguous storage.
o Linked List: May consume more memory due to the additional space needed for pointers.
Q) Describe syntax error, logical error, runtime error

Syntax Error:

A syntax error in C occurs when the rules of the C programming language are violated, leading to a failure
in the compilation process. Here's an example:

#include <stdio.h>

int main() {
printf("Hello, World!"
return 0;
}

In this C program, a syntax error is present due to the missing closing parenthesis in the printf statement.
The compiler will detect this error during the compilation phase.

Examples of syntax errors include:

 Missing semicolon at the end of a statement.


 Mismatched parentheses or brackets.
 Incorrect use of keywords.
 Misspelled variable or function names.

Logical Error:

A logical error in C occurs when the code is syntactically correct but does not produce the expected output
due to a flaw in the logic or algorithm. Consider the following example:

c
#include <stdio.h>

int main() {
// Calculate the average of two numbers
int num1 = 10;
int num2 = 20;

// Incorrect calculation of average


int average = num1 + num2 / 2;

printf("Average: %d\n", average);


return 0;
}

In this C program, a logical error is present in the calculation of the average. The correct calculation should
be (num1 + num2) / 2 instead of num1 + num2 / 2. This will lead to incorrect output.

Runtime Error:
A runtime error in C occurs during the execution of a program when an operation cannot be carried out,
resulting in abnormal termination. Here's an example:

#include <stdio.h>

int main() {
// Division by zero runtime error
int num1 = 10;
int num2 = 0;
int result = num1 / num2;

printf("Result: %d\n", result);


return 0;
}

In this C program, a runtime error is present due to division by zero. Attempting to divide num1 by num2
(which is zero) leads to a runtime error. Runtime errors are typically detected during the program's
execution.

Examples of runtime errors include:

 Division by zero.
 Accessing an array index out of bounds.
 Attempting to open a file that doesn't exist.

Q) Describe different storage classes in C

In C programming, storage classes define the scope, lifetime, and visibility of variables within a program.
There are four main storage classes in C:

1. Automatic Storage Class (auto):


o Variables declared with the auto keyword are automatically allocated memory within a
function block.
o They have automatic storage duration, meaning they exist only within the block in which
they are defined.
o The default storage class for local variables if no storage class is specified.
o Example:

#include <stdio.h>

int main() {
auto int x = 10;
printf("x: %d\n", x);
return 0;
}

2. Register Storage Class (register):

 The register keyword suggests the compiler to store the variable in a CPU register for faster
access.
 It is suitable for variables that are frequently used and need quick access.
 Like auto variables, register variables have automatic storage duration.
 Example:

#include <stdio.h>

int main() {
register int count = 5;
printf("count: %d\n", count);
return 0;
}

3. Static Storage Class (static):

 Variables with the static keyword have a lifetime that extends throughout the program's execution.
 They are allocated memory when the program starts and retain their values between function calls.
 Static variables have internal linkage by default, limiting their visibility to the file in which they are
declared.
 Example:

#include <stdio.h>

void increment() {
static int counter = 0;
counter++;
printf("Counter: %d\n", counter);
}

int main() {
increment();
increment();
return 0;
}

4. External Storage Class (extern):

 The extern keyword is used to declare variables that are defined in another file.
 It provides a reference to a global variable declared elsewhere.
 External variables have external linkage by default, making them visible across multiple files.
 Example:

// file1.c
int globalVar = 100;

// file2.c
#include <stdio.h>

extern int globalVar; // Declaration of the external variable

int main() {
printf("Global Variable: %d\n", globalVar);
return 0;
}
Q) Distinguish between iterative and recursive functions

Defining Recursion

Recursion in computer science refers to a function that calls itself within its own code. This self-referential
function is known as a recursive function. It is essential to define an exit or termination condition in
recursion to avoid potential infinite loops.

Understanding Iteration

Iteration, on the other hand, involves a loop or repetition of a set of instructions until a specific condition is
met. This condition signals the end of the iteration.

Comparing Recursion and Iteration

S.No. Recursion Iteration


1 A recursive function calls itself. Iteration involves repeating a set of instructions.
A termination condition must be defined in a The termination condition and loop definition are
2
recursive function. crucial in iteration.
3 The code size in recursion is generally smaller. Iteration usually involves larger code size.
4 Recursion is often slower in speed. Iteration is typically faster than recursion.
Recursion is preferred when time complexity is not Iteration is chosen when time complexity and
5
a concern and the code size is small. larger code size need to be managed.
Iteration has lower time complexity than
6 Recursion has higher time complexity.
recursion.
7 Recursion requires more memory. Iteration requires less memory.

Q) Compare call by value with call by reference with the help of an example program.

In C, function parameters can be passed using two different mechanisms: call by value and call by reference.
Let's compare these two approaches with the help of an example program.

Call by Value: In call by value, the actual values of the arguments are passed to the function, and any
modifications made to the parameters within the function do not affect the original values outside the
function.

#include <stdio.h>

void incrementByValue(int x) {
x++; // Increment the local copy of x
printf("Inside function: %d\n", x);
}

int main() {
int num = 5;
printf("Before function call: %d\n", num);
incrementByValue(num);

printf("After function call: %d\n", num); // num remains unchanged


return 0;
}

Output:

Before function call: 5


Inside function: 6
After function call: 5

In this example, incrementByValue receives a copy of the value of num. Any modifications made to x
within the function do not affect the original value of num in the main function.

Call by Reference: In call by reference, the memory address (reference) of the actual arguments is passed to
the function. As a result, any changes made to the parameters within the function directly affect the original
values outside the function.

#include <stdio.h>

void incrementByReference(int *x) {


(*x)++; // Increment the value at the memory location pointed by x
printf("Inside function: %d\n", *x);
}

int main() {
int num = 5;
printf("Before function call: %d\n", num);

incrementByReference(&num);

printf("After function call: %d\n", num); // num is modified


return 0;
}

Output:

Before function call: 5


Inside function: 6
After function call: 6

In this example, incrementByReference receives the memory address of num using a pointer. The value at
that memory address is modified within the function, affecting the original value of num in the main
function.

Comparison:

 Call by Value:
o Passes the actual values.
o Changes made within the function do not affect the original variables.
o Suitable for cases where modification is not required outside the function.
 Call by Reference:
o Passes the memory address (reference) of the variables.
o Changes made within the function directly affect the original variables.
o Suitable when modifications to variables are required outside the function.

Q) What is debugging?

Debugging is the process of identifying, isolating, and fixing errors or bugs in a computer program. The
term "bug" was coined by Grace Hopper when an actual moth caused a malfunction in an early computer.
Debugging is an essential and iterative part of the software development lifecycle, and it involves various
techniques and tools to identify and correct issues in the code.

Here are key aspects of debugging:

1. Error Identification:
o Compile-Time Errors: Detected by the compiler during the compilation process due to
syntax or semantic issues.
o Runtime Errors: Occur while the program is running and often result in abnormal
termination.
2. Error Isolation:
o Identifying the specific part of the code where the error occurs.
o Using techniques such as print statements, logging, or specialized debugging tools to narrow
down the problematic area.
3. Error Correction:
o Making necessary changes to the code to fix the identified issues.
o This may involve modifying the logic, correcting syntax errors, or addressing issues related
to data flow.
4. Debugging Techniques:
o Print Statements: Adding print statements in the code to display variable values or messages
at specific points for analysis.
o Interactive Debuggers: Using integrated development environments (IDEs) with built-in
debugging tools that allow developers to set breakpoints, step through code, and inspect
variables interactively.
o Logging: Writing messages or variable values to log files for analysis.
o Unit Testing: Writing test cases to verify the behavior of individual units of code, helping
identify errors early in the development process.
5. Common Debugging Tools:

 GDB (GNU Debugger): A powerful debugger for C, C++, and other languages.
 Visual Studio Debugger: Integrated into Microsoft Visual Studio for debugging applications written
in languages like C, C++, C#, and more.
 pdb (Python Debugger): Debugger for Python programs.
 Xcode Debugger: Debugger integrated into Apple's Xcode IDE for debugging macOS and iOS
applications.

Q) Differentiate between prefix and postfix operators in C

In C programming, prefix and postfix operators refer to the position of the operator concerning the operand
in an expression. These operators are often associated with increment and decrement operations.

1. Prefix Operators:

 Syntax: ++variable or --variable


 Operation: The increment (++) or decrement (--) operation is applied to the variable before its
value is used in the expression.
 Example:

#include <stdio.h>

int main() {
int x = 5;
int result = ++x; // Prefix increment

printf("x: %d\n", x); // Output: x: 6


printf("Result: %d\n", result); // Output: Result: 6

return 0;
}

In this example, ++x increments the value of x before using it in the assignment.

2. Postfix Operators:

 Syntax: variable++ or variable--


 Operation: The increment (++) or decrement (--) operation is applied to the variable after its current
value is used in the expression.
 Example:

#include <stdio.h>

int main() {
int x = 5;
int result = x++; // Postfix increment

printf("x: %d\n", x); // Output: x: 6


printf("Result: %d\n", result); // Output: Result: 5

return 0;
}

In this example, x++ uses the current value of x in the assignment and then increments x afterward

Differences:

 Order of Operation:
o Prefix: Operation is applied before using the variable in the expression.
o Postfix: Operation is applied after using the variable in the expression.
 Effect on Operand:
o Prefix: Changes the value of the operand before its value is used.
o Postfix: Uses the current value of the operand in the expression and then changes it
afterward.
 Value Returned:
o Prefix: Returns the updated value of the operand after the operation.
o Postfix: Returns the original value of the operand before the operation.
Q) Explain enumerated data types in C briefly

In C, an enumerated data type is a user-defined data type that consists of a set of named integer
constants, known as enumerators. Enumerated types provide a way to create symbolic names for a set of
related integer values, making the code more readable and maintainable.

The syntax for declaring an enumerated data type is as follows:

enum enumeration_name {
enumerator1,
enumerator2,
// ... additional enumerators
};

Here's a brief explanation of key elements:

 enum: Keyword indicating the declaration of an enumerated data type.


 enumeration_name: The name given to the enumerated data type.
 enumerator1, enumerator2, ...: Named constants representing integer values.

Example of an enumerated data type:

#include <stdio.h>

enum Weekday {
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
};

int main() {
enum Weekday today = Wednesday;

printf("Today is: %d\n", today); // Output: Today is: 3


return 0;
}

In this example, Weekday is an enumerated data type with seven enumerators representing the days of the
week. The variable today is declared as a Weekday type and assigned the value corresponding to
Wednesday.

Enumerated data types offer several benefits:

1. Readability: Enumerators provide meaningful names to represent integer values, improving code
readability.
2. Maintainability: If the underlying numeric values need to be changed, you only need to modify the
enumeration, and the rest of the code remains unchanged.
3. Type Safety: Enumerated types are strongly typed, and variables of different enumerations cannot
be assigned to each other without explicit type conversion.

You might also like