©®™ Aeraxia - in C Programming Question Bank Part 1
©®™ Aeraxia - in C Programming Question Bank Part 1
Bitwise shift operators in C are used to shift the bits of a value left or
right by a specified number of positions. These operators are `<<`
for left shift and `>>` for right shift. Bitwise shifts can be used for
quick multiplication or division by powers of 2, as well as
manipulating individual bits in a value.
1. Left Shift (`<<`):
The left shift operator shifts the bits of a number to the left by the
specified number of positions. It's equivalent to multiplying the
number by 2 raised to the power of the shift count.
2. Right Shift (`>>`):
The right shift operator shifts the bits of a number to the right by the
specified number of positions. It's equivalent to integer division of
the number by 2 raised to the power of the shift count.
Q12) Distinguish entry and exit control loops:
C Language:
#include <stdio.h>
// Function prototype:
int calculateArea(int length, int width); // Declares the
function's signature
int main() {
// Function calls (can be made before the definition):
int area1 = calculateArea(10, 5);
int area2 = calculateArea(20, 15);
return 0;
}
// Function definition (can be placed later in the file or in a
separate file):
int calculateArea(int length, int width) { // Defines the
function's implementation
return length * width;
}
Q14) Explain conditional operator with example.
An operator that requires three operands is called as ternary
or conditional operator.
C Language:
#include <stdio.h>
1. Input Devices:
4. Motherboard:
• Function: The main circuit board that connects all
components of the computer.
5. Memory:
• Function: Stores data and instructions temporarily or
permanently.
• Types:
o Random Access Memory (RAM): Temporary
8. Software:
10. Firmware:
• Function: Permanent software embedded in
Syntax:
C Language:
returnType functionName(parameterType1 parameter1,
parameterType2 parameter2, ...);
Example:
C Language:
// Function prototype
int addNumbers(int a, int b); // Declares a function named
addNumbers that takes two integers and returns an integer
int main() {
int result = addNumbers(5, 3); // Calls the addNumbers
function
printf("Sum: %d", result);
return 0;
}
Key Points:
Syntax:
C Language :
How it works:
C
int age = 25;
char *status = (age >= 18) ? "Adult" : "Minor"; // status
will be "Adult"
Key Points:
1. floor():
• Purpose: Returns the largest integer that is less than or
equal to the given number. It rounds down to the
nearest integer.
• Example:
o floor(4.9) = 4
o floor(-3.2) = -4
• Common uses:
o Rounding down numbers for calculations
o Truncating decimals
2. ceil():
• Purpose: Returns the smallest integer that is greater
than or equal to the given number. It rounds up to the
nearest integer.
• Example:
o ceil(4.1) = 5
o ceil(-2.8) = -2
• Common uses:
3.sqrt():
• Purpose: Calculates the square root of a given number,
which is the number that, when multiplied by itself,
gives the original number.
• Example:
o sqrt(25) = 5
o sqrt(9) = 3
• Common uses:
o Finding the lengths of sides in geometry
o Calculating distances
root of 4)
• Common uses:
o Calculating exponential growth or decay
involving powers
Example:
C Language:-
b) strcmp()
Example:
C Language:
char name1[] = "Alice";
char name2[] = "Bob";
int result = strcmp(name1, name2); // result will be
negative
c) strcat()
Example:
C
char greeting[] = "Hello";
char name[] = "World";
strcat(greeting, name); // greeting becomes
"HelloWorld"
C Language :
struct structure_name {
data_type member_1;
data_type member_2;
// ... more members
};
Explanation:
• struct: Keyword indicating a structure declaration.
Example:
C Language
struct Student {
int roll_no;
char name[50];
float marks;
};
C Language :
Key Points:
C Language:
return_type function_name(parameter_list);
Example:
C Language:
// Function prototype
int add(int a, int b); // Declares a function
named add that takes two ints and returns
an int
int main() {
int result = add(5, 3); // Calls the add
function before its definition
printf("Sum: %d", result);
return 0;
}
Key Points:
Explanation:
1. Switch (expression): Evaluates the expression and
compares its value to the values in the case labels.
2. Case value1:: If expression matches value1, the code
within this case block executes.
3. Break;: Terminates the switch statement and
prevents execution from falling through to
subsequent cases.
4. Default:: Optional block that executes if expression
doesn't match any of the case values.
Example:
C Language:
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Another day\n");
}
Output:
Wednesday
Key Points:
Problem Definition:
a) Turing model:
• Concept: A theoretical model of computation
capabilities.
• Both involve a system that manipulates symbols based
on defined rules.
• Both are influential in understanding the nature of
computation.
Differences:
• Focus: Turing model is theoretical and focuses on
1, 0, 1, 2, 3, ...).
o Represent both positive and negative quantities.
• Rational Numbers:
o Numbers that can be expressed as a fraction (e.g.,
√2).
o Represent all numbers that can be measured on a
continuous scale.
• Complex Numbers:
o Numbers of the form a + bi, where a and b are
1. Input coefficients:
• Get the values of a, b, and c from the user.
2. Calculate discriminant:
• discriminant = b^2 - 4ac
Flowchart:
Explanation:
• The flowchart starts with an input symbol to get
coefficients a, b, and c.
• A process symbol calculates the discriminant.
• A decision symbol checks the discriminant's value.
• If discriminant > 0, a process symbol calculates real
and distinct roots.
• If discriminant == 0, a process symbol calculates real
and equal roots.
• If discriminant < 0, a process symbol calculates
complex roots.
• Output symbols display the calculated roots in each
case.
Key Points:
• The quadratic formula is used to calculate the roots: x
complexity:
▪ O(1): Constant time (no change with input
size)
▪ O(n): Linear time (grows proportionally to
input size)
▪ O(n^2): Quadratic time (grows as the square
of input size)
▪ O(log n): Logarithmic time (grows
logarithmically with input size)
Example :
C Language :
Explanation:
• Space complexity measures the amount of
Example
C Language :
void copy_array(int arr1[], int arr2[], int n) {
for (int i = 0; i < n; i++) {
arr2[i] = arr1[i]; // Copies elements to
a new array
}
}
• Space complexity: O(n) (linear space)
o Creates a new array arr2 of size n, using
additional memory proportional to input size.
Logical Operators in C
Explanation:
• Logical operators perform logical operations on
C Language
#include <stdio.h>
int main() {
int a = 10, b = 5, c = 20;
// AND operator
if (a > b && a > c) {
printf("a is the largest number.\n");
}
// OR operator
if (b > a || b > c) {
printf("b is greater than either a or c.\n");
}
// NOT operator
if (!(a == b)) { // Equivalent to "if (a != b)"
printf("a is not equal to b.\n");
}
return 0;
}
C language :
int max = (a > b) ? a : b; // Assigns the larger of
a and b to max
C Program to Determine Maximum of Three
Numbers Using Ternary Operator:
C Language:
#include <stdio.h>
int main() {
int a, b, c;
return 0;
}
Explanation:
• The program prompts for three numbers and
• Syntax Errors:
o Violations of the grammar rules of the
programming language.
o Examples: missing semicolons, incorrect
keywords, misspelled variable names.
o Caught by the compiler, preventing program
execution.
• Runtime Errors:
o Occur during program execution, often due to
• Linker Errors:
o Issues during the linking process, when
code translation.
o Examples: syntax errors, type mismatches,
undefined variables.
o Must be fixed before execution.
Prevention and Debugging:
• Practice good coding habits: consistent formatting,
Explanation:
C Language
int x = 10; // Binary representation: 00001010
int y = 15; // Binary representation: 00001111
int result = x ^ y; // Binary representation of
result: 00000101 (decimal 5)
Common Uses:
1.Keywords:
6.Special Characters:
Precedence:
• Specifies the relative priority or "ranking" of
1. goto Statement:
Purpose: Unconditionally transfers control to a
labeled statement within the same function.
Syntax:
C
goto label;
label:
// code to be executed after the jump
Example:
C
#include <stdio.h>
int main() {
int i = 0;
printf("\nLoop finished.\n");
return 0;
}
2. continue Statement:
Syntax:
C Language
continue;
Example:
C Language
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
printf("%d ", i);
}
Visual Representation:
3. break Statement:
Purpose: Terminates the current loop or switch
statement immediately, and control flow jumps to the
statement following the loop or switch.
Syntax:
C
break;
Example:
C
for (int i = 0; i < 10; i++) {
if (i == 5) {
break; // Exit the loop when i reaches 5
}
printf("%d ", i);
}
Switch-Case:
C Language
switch (expression) {
case value1:
// code to execute if expression equals
value1
break;
case value2:
// code to execute if expression equals
value2
break;
// ... more cases
default:
// code to execute if no case matches
}
Example:
C Language
#include
<stdio.h>
int
main()
{
int day = 3;
switch (day) {
case
1:
printf("Monday\n");
break;
case
2:
printf("Tuesday\n");
break;
case
3:
printf("Wednesday\n");
break;
// ... more cases for other days
default:
printf("Invalid day\n");
}
return 0;
}
If-Else Ladder:
Syntax:
C Language:
if (condition1) {
// code to execute if condition1 is true
} else
if (condition2) {
// code to execute if condition1 is false and
condition2 is true
} else if (condition3) {
// ... more else if statements
} else {
// code to execute if none of the above
conditions are true
}
Example:
C Language
#include <stdio.h>
int main() {
int grade = 85;
return 0;
}
Q41) Explain difference between call by value
and call by reference
Call by Value:
int main() {
int a = 10, b = 20;
printf("Before swap: a = %d, b = %d\n", a, b);
swap_by_value(a, b); // Call by value
printf("After swap: a = %d, b = %d\n", a, b);
// Values remain unchanged
return 0;
}
Output:
Before swap: a = 10, b = 20
After swap: a = 10, b = 20
Call by Reference:
int main() {
int a = 10, b = 20;
printf("Before swap: a = %d, b = %d\n", a, b);
swap_by_reference(&a, &b); // Call by
reference
printf("After swap: a = %d, b = %d\n", a, b);
// Values are swapped
return 0;
}
Output:
Before swap: a = 10, b = 20
After swap: a = 20, b = 10
Key Differences:
1. sqrt(x):
• Purpose: Calculates the square root of a non-
negative number x.
• Return value: Returns a double-precision
floating-point value representing the square root
of x.
Example:
C Language:
#include <stdio.h>
#include <math.h>
int main() {
double number = 25.0;
double square_root = sqrt(number);
printf("The square root of %.2f is %.2f\n",
number, square_root);
return 0;
}
• Output: The square root of 25.00 is 5.00
• Use cases:
expressed in radians.
• Return value: Returns a double-precision
floating-point value representing the sine of x.
Example:
C Language:
#include <stdio.h>
#include <math.h>
int main() {
double angle = 30.0 * M_PI / 180.0; //
Convert degrees to radians
double sine_value = sin(angle);
printf("The sine of %.2f radians is %.2f\n",
angle, sine_value);
return 0;
}
• Output: The sine of 0.52 radians is 0.50
• Use cases:
o Modeling periodic phenomena like sound
Examples :
C Language:
void myFunction() {
int x = 10; // Automatic variable
// x can be used only within this function
}
execution.
• Initialized only once, usually to 0 by default.
Declaration:
• Use the static keyword.
Examples:
C Language
static int global_count = 0; // Accessible
only within this file
void myFunction() {
global_count++;
printf("Global count: %d\n",
global_count);
}
• Static Local Variable:
C Language
void myFunction() {
static int local_count = 0; // Retains
value between calls
local_count++;
printf("Local count: %d\n", local_count);
}
Common Uses:
• Maintaining state across function calls.
Lifetime:
• Depends on where the variable is actually defined.
Declaration:
• Use the extern keyword.
Example:
C
// File1.c
int global_value = 10;
// File2.c
extern int global_value; // Declares access to
global_value from File1.c
void myFunction() {
printf("Global value: %d\n", global_value);
}
Common Use:
• Sharing variables across multiple files.
Call by Value:
int main() {
int a = 10, b = 20;
printf("Before swap: a = %d, b = %d\n", a, b);
swap_by_value(a, b); // Call by value
printf("After swap: a = %d, b = %d\n", a, b);
// Values remain unchanged
return 0;
}
Output:
Before swap: a = 10, b = 20
After swap: a = 10, b = 20
Call by Reference:
int main() {
int a = 10, b = 20;
printf("Before swap: a = %d, b = %d\n",
a, b);
swap_by_reference(&a, &b); // Call by
reference
printf("After swap: a = %d, b = %d\n", a,
b); // Values are swapped
return 0;
}
Output:
Before swap: a = 10, b = 20
After swap: a = 20, b = 10
Key Differences:
• Organizing Data:
o Store and manage large sets of related data
efficiently.
o Group elements of the same type for easier
handling and processing.
• Efficient Access and Manipulation:
o Access individual elements directly using their
indices, enabling fast operations.
o Perform operations on entire collections of
data using loops and array-based algorithms.
• Memory Efficiency:
o Require less memory compared to declaring
many individual variables.
• Structured Data Representation:
o Represent multi-dimensional data structures
like matrices, tables, and lists.
o Model real-world entities and relationships.
• Programming Foundation:
o Fundamental building blocks for advanced
data structures like linked lists, stacks, queues,
and trees.
• Algorithm Implementation:
o Essential for implementing various algorithms
in sorting, searching, numerical computations,
and data analysis.
Common Use Cases:
int main() {
char string[] = "Hello, world!";
int length = strlen(string);
printf("The length of the string is: %d\n",
length); // Output: 13
return 0;
}
• Common uses:
int main() {
char source[] = "Copy this string";
char destination[50];
strcpy(destination, source);
printf("Destination string: %s\n",
destination); // Output: Copy this string
return 0;
}
• Important note: Use strcpy with caution as it can
2. strcat(dest, src):
• Purpose: Concatenates two strings (appends src
(character by character).
• Return value:
o Returns 0 if the strings are equal.
o Returns a negative value if str1 is
lexicographically less than str2.
o Returns a positive value if str1 is
lexicographically greater than str2.
Example:
C Language:
char str1[] = "apple";
char str2[] = "banana";
int result = strcmp(str1, str2); // result will be
negative
string (dest).
• strncpy(dest, src, n): Copies a specified number
of characters from src to dest.
• strchr(str, ch): Finds the first occurrence of a
characte
Q49). Write a program to calculate sum of list by
passing array to function.
C Language:
#include <stdio.h>
return 0;
}
Q50). Explain reference and dereference
operators with example
Key Points:
calloc, etc.)
• Passing variables by reference to functions (for
modification)
• Working with arrays and pointers interchangeably
• Implementing data structures like linked lists and
trees
variable.
• Syntax: &variable_name
• Example:
C Language:
int x = 10;
int *ptr = &x; // ptr stores the address of x
printf("Address of x: %p\n", &x); // Output:
memory address of x
printf("Value of ptr: %p\n", ptr); // Output:
same as the address of x
Key Points:
calloc, etc.)
• Passing variables by reference to functions (for
modification)
• Working with arrays and pointers interchangeably
• Implementing data structures like linked lists and
trees
Q52). Define Union, differentiate structure and
union.
Both structures and unions in C are user-defined
data types used to group data of different types but
with distinct functionalities and memory allocation
practices.
Structure:
• Definition: A collection of named members of
In programming:
struct address {
char street[50];
char city[50];
char state[20];
int zipcode;
};
struct employee {
int id;
char name[50];
float salary;
struct address address; // Nested structure
};
Key points:
Key step
1.Include necessary header:
C Language:
#include <stdio.h>
o Reading characters:
C.Language :
char ch;
while ((ch = fgetc(fp)) != EOF) {
// Process the character
}
o Reading strings:
C Language:
char line[100];
fgets(line, sizeof(line), fp);
o Reading formatted data:
C Language:
int age;
char name[50];
fscanf(fp, "%d %s", &age, name);
4. Write contents (for writing):
o Writing characters:
C
char ch = 'A';
fputc(ch, fp);
o Writing strings:
C
char message[] = "Hello, world!";
fputs(message, fp);
o Writing formatted data:
C
int value = 10;
fprintf(fp, "Value: %d", value);
C Language :
fclose(fp);
Essential points: