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

C Questions

Uploaded by

allammd6786
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

C Questions

Uploaded by

allammd6786
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

PART-A

1(a) Evaluate A&B, A|B, A^B when A=8, B=4.

Bitwise Representation:

• A = 8 in binary: 1000 (8 in decimal is 1000 in binary).

• B = 4 in binary: 0100 (4 in decimal is 0100 in binary).

1. A & B (Bitwise AND):

• This operation performs a bitwise AND on each corresponding bit of A and B.

• 1000 (A)

• 0100 (B)

AND operation result: 0000 (which is 0 in decimal).

2. A | B (Bitwise OR):

• This operation performs a bitwise OR on each corresponding bit of A and B.

• 1000 (A)

• 0100 (B)

OR operation result: 1100 (which is 12 in decimal).

3. A ^ B (Bitwise XOR):

• This operation performs a bitwise XOR on each corresponding bit of A and B.

• 1000 (A)

• 0100 (B)

XOR operation result: 1100 (which is 12 in decimal).

Summary:

• A&B=0

• A | B = 12

• A ^ B = 12

1(b) Explain Linker and Loader in C.

Linker:

A linker combines object files and external libraries into a single executable. It resolves
function/variable references and assigns memory addresses to code and data.

Loader:

A loader loads the executable into memory, allocates memory, adjusts addresses (if needed), and
starts the program’s execution.

Summary:
• Linker: Combines and resolves code for execution.

• Loader: Loads and runs the executable in memory.

1©Explain difference between if-else and switch statement.

Difference Between if-else and switch:

• Control Flow:

o if-else: Handles complex conditions.

o switch: Evaluates a single variable with multiple cases.

• Performance:

o if-else: Slower with many conditions.

o switch: Faster for matching fixed values.

• Readability:

o if-else: Better for complex logic.

o switch: Cleaner for simple value matching.

1(d) Draw do while loop execution flow chart.

1(e) How one- and two-dimensional subscripted variables (arrays) are declared?

One-Dimensional Arrays:

A one-dimensional array is a list of elements of the same type.

Syntax: type arrayName[arraySize];

Example: int numbers[5];

Two-Dimensional Arrays:

A two-dimensional array is essentially an array of arrays, allowing for the representation of a table or
matrix.

Syntax: type arrayName[rowSize][columnSize];


Example: int matrix[3][4];

PART-B

2(a) Make use of conditional operators in C and write a program to determine the largest value
among three positive integer numbers.

#include <stdio.h>

int main() {

int a, b, c;

printf("Enter three positive integers:\n");

scanf("%d %d %d", &a, &b, &c);

int largest = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);

printf("The largest number is: %d\n", largest);

return 0;

Explanation:

1. Input: The program prompts the user to enter three positive integers.

2. Conditional Operators: The largest number is determined using nested conditional


operators:

o First, it compares a and b.

o Then, it compares the larger of those with c.

3. Output: Finally, it prints the largest number.

Example

Enter three positive integers:

5 12 8

The largest number is: 12

2(b) Evaluate the following expression.

X=A/B*C-B+A*D/3

Y=A+B*C+(D*E)+F*G

Where A=1, B=2, C=3, D=4, E=5, F=6, G=7

Expression 1: X=AB⋅C−B+A⋅D3X = \frac{A}{B} \cdot C - B + \frac{A \cdot D}{3}X=BA⋅C−B+3A⋅D

Step-by-Step Evaluation:

1. Calculate AB\frac{A}{B}BA:

AB=12=0.5\frac{A}{B} = \frac{1}{2} = 0.5BA=21=0.5


2. Multiply by CCC:

0.5⋅C=0.5⋅3=1.50.5 \cdot C = 0.5 \cdot 3 = 1.50.5⋅C=0.5⋅3=1.5

3. Subtract BBB:

1.5−B=1.5−2=−0.51.5 - B = 1.5 - 2 = -0.51.5−B=1.5−2=−0.5

4. Calculate A⋅DA \cdot DA⋅D:

A⋅D=1⋅4=4A \cdot D = 1 \cdot 4 = 4A⋅D=1⋅4=4

5. Divide by 3:

A⋅D3=43≈1.3333\frac{A \cdot D}{3} = \frac{4}{3} \approx 1.33333A⋅D=34≈1.3333

6. Add to the previous result:

−0.5+1.3333=0.8333-0.5 + 1.3333 = 0.8333−0.5+1.3333=0.8333

So,

X≈0.8333X \approx 0.8333X≈0.8333

Expression 2: Y=A+B⋅C+(D⋅E)+F⋅GY = A + B \cdot C + (D \cdot E) + F \cdot GY=A+B⋅C+(D⋅E)+F⋅G

Step-by-Step Evaluation:

1. Calculate B⋅CB \cdot CB⋅C:

B⋅C=2⋅3=6B \cdot C = 2 \cdot 3 = 6B⋅C=2⋅3=6

2. Calculate D⋅ED \cdot ED⋅E:

D⋅E=4⋅5=20D \cdot E = 4 \cdot 5 = 20D⋅E=4⋅5=20

3. Calculate F⋅GF \cdot GF⋅G:

F⋅G=6⋅7=42F \cdot G = 6 \cdot 7 = 42F⋅G=6⋅7=42

4. Combine all parts:

Y=A+B⋅C+(D⋅E)+F⋅G=1+6+20+42Y = A + B \cdot C + (D \cdot E) + F \cdot G = 1 + 6 + 20 +


42Y=A+B⋅C+(D⋅E)+F⋅G=1+6+20+42

5. Add them together:

1+6=71 + 6 = 71+6=7 7+20=277 + 20 = 277+20=27 27+42=6927 + 42 = 6927+42=69

So,

Y=69Y = 69Y=69

Final Results:

• X ≈ 0.8333

• Y = 69

3(a) Differentiate between entry-control and exit control loops with example
1. Entry-Control Loops:

Entry-control loops check the loop condition before executing the loop body. If the condition is false
from the beginning, the loop body is never executed.

• Examples:

o for loop

o while loop

Example of an Entry-Control Loop (Using a while loop):

#include <stdio.h>

int main() {

int i = 0;

while (i < 5) {

printf("%d\n", i);

i++;

return 0;

2. Exit-Control Loops:

Exit-control loops check the loop condition after executing the loop body. This guarantees that the
loop body is executed at least once, even if the condition is false.

• Example:

o do...while loop

Example of an Exit-Control Loop (Using a do...while loop):

#include <stdio.h>

int main() {

int i = 5;

do {

printf("%d\n", i);

i++;

} while (i < 5); // Condition is checked after the loop body

return 0;

}
Summary of Differences:

Feature Entry-Control Loops Exit-Control Loops

Condition Check Before executing the loop body After executing the loop body

Execution Guarantee May not execute if condition is false Always executes at least once

Examples for, while do...while

3(b) Write a C program to print. and explain the looping concepts used in it.
1
12
123
1234
12345

#include <stdio.h>
int main() {
int i, j, rows = 5;
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
OUTPUT:
1

12

123

1234

12345

Summary:

• The program uses nested loops where the outer loop determines the number of rows, and
the inner loop controls the number of printed numbers in each row.
• This structure allows for a clear and organized way to build the desired pattern through
controlled iterations.

4(a) Write a C program to perform addition of two matrices?

#include <stdio.h>

#define MAX 10

int main() {

int matrix1[MAX][MAX], matrix2[MAX][MAX], sum[MAX][MAX];

int rows, cols, i, j;

printf("Enter the number of rows and columns of the matrices: ");

scanf("%d %d", &rows, &cols);

printf("Enter elements of the first matrix:\n");

for (i = 0; i < rows; i++) {

for (j = 0; j < cols; j++) {

printf("Enter element [%d][%d]: ", i + 1, j + 1);

scanf("%d", &matrix1[i][j]);

printf("Enter elements of the second matrix:\n");

for (i = 0; i < rows; i++) {

for (j = 0; j < cols; j++) {

printf("Enter element [%d][%d]: ", i + 1, j + 1);

scanf("%d", &matrix2[i][j]);

for (i = 0; i < rows; i++) {

for (j = 0; j < cols; j++) {

sum[i][j] = matrix1[i][j] + matrix2[i][j];

printf("Sum of the two matrices:\n");


for (i = 0; i < rows; i++) {

for (j = 0; j < cols; j++) {

printf("%d ", sum[i][j]);

printf("\n");

return 0;

OUTPUT:

Enter the number of rows and columns of the matrices: 2 2

Enter elements of the first matrix:

Enter element [1][1]: 1

Enter element [1][2]: 2

Enter element [2][1]: 3

Enter element [2][2]: 4

Enter elements of the second matrix:

Enter element [1][1]: 5

Enter element [1][2]: 6

Enter element [2][1]: 7

Enter element [2][2]: 8

Sum of the two matrices:

68

10 12

4(b) Develop a C program to count and sum of even numbers in an array.

#include <stdio.h>

int main() {

int arr[100], n;

int count = 0;

int sum = 0;

printf("Enter the number of elements in the array: ");

scanf("%d", &n);
printf("Enter %d elements:\n", n);

for (int i = 0; i < n; i++) {

printf("Element [%d]: ", i + 1);

scanf("%d", &arr[i]);

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 0) {

count++;

sum += arr[i];

printf("Count of even numbers: %d\n", count);

printf("Sum of even numbers: %d\n", sum);

return 0;

OUTPUT:

Enter the number of elements in the array: 5

Enter 5 elements:

Element [1]: 1

Element [2]: 2

Element [3]: 3

Element [4]: 4

Element [5]: 5

Count of even numbers: 2

Sum of even numbers: 6


PART-A

1(.a) Organize your thoughts to present a structured overview of algorithm characteristics.

1. Correctness: Delivers the expected result.

2. Efficiency: Optimizes time and memory use.

3. Scalability: Performs well with larger inputs.

4. Determinism: Always gives the same output for the same input.

These traits ensure an algorithm is reliable, fast, and scalable.

(b) Explain compiler and compilation in C

A compiler in C is a program that translates the source code written in C into machine code (binary)
that a computer can execute. It checks for syntax errors and optimizes the code during this process.

Compilation is the process of converting C source code into an executable file. It involves multiple
stages: preprocessing, compilation, assembly, and linking, transforming the human-readable code
into machine-readable instructions

©Write a C program to read a number to find the number is even or odd.


#include <stdio.h>

Void main() {

int num;

printf("Enter an integer: ");

scanf("%d", &num);

if (num % 2 == 0) {

printf("%d is even.\n", num);

else {

printf("%d is odd.\n", num);

(d) What is the difference between while loop and do…while loop?

The main difference between a while loop and a do...while loop is:

• While Loop:

o The condition is checked before executing the loop body.

o The loop may not execute at all if the condition is false initially.
• Do...While Loop:

o The loop body is executed at least once, as the condition is checked after executing
the loop.

(e) Explain accessing elements of array with example?

In C, you can access elements of an array using the index of the element, starting from 0 for the first
element. The syntax is array_name[index], where index is the position of the element you want to
access.

#include <stdio.h>

int main() {

int numbers[5] = {10, 20, 30, 40, 50};

printf("First element: %d\n", numbers[0]);

printf("Third element: %d\n", numbers[2]);

numbers[1] = 25;

printf("Modified second element: %d\n", numbers[1]);

return 0;

PART-B

2(a) Write the importance of precedence and associativity? Write the table for operator precedence?

Importance of Precedence and Associativity:

• Precedence determines the order in which operators are evaluated in an expression.


Operators with higher precedence are evaluated before those with lower precedence.

• Associativity defines the order in which operators of the same precedence level are
processed. It can be either left-to-right or right-to-left.

• Operator Precedence Table:

Precedence Operator Description Associativity


Function call, array subscript,
1 (), [], ->, . Left-to-right
member access
++, --, +, -, !, ~, *, &,
2 Unary operators, type cast Right-to-left
sizeof, (type)
Multiplication, division,
3 *, /, % Left-to-right
modulus
4 +, - Addition, subtraction Left-to-right
5 <<, >> Bitwise shift Left-to-right
6 <, <=, >, >= Relational operators Left-to-right
7 ==, != Equality operators Left-to-right
Precedence Operator Description Associativity
8 & Bitwise AND Left-to-right
9 ^ Bitwise XOR Left-to-right
10 ` ` Bitwise OR
11 && Logical AND Left-to-right
12 ` `
13 ?: Ternary conditional Right-to-left
=, +=, -=, *=, /=, %=, &=, Assignment
14 =, <<=, >>=`
^=, ` operators
15 , Comma operator Left-to-right

2(b) What is meant by type conversion? Why is necessary? Explain about implicit and explicit type
conversion with examples.

What is Type Conversion?

Type conversion is the process of converting a value from one data type to another. In C, it occurs
when an expression or variable of one type needs to be interpreted or used as another type.

Why is Type Conversion Necessary?

Type conversion is necessary to:

1. Avoid data loss when assigning values between different types.

2. Ensure that operations involving different types are handled correctly.

3. Facilitate compatibility when combining various data types in expressions, function


arguments, or assignments.

Types of Type Conversion:

1. Implicit Type Conversion (Automatic/Coercion):

o This is performed automatically by the compiler when it encounters mixed data


types in an expression.

o The conversion follows predefined rules, typically promoting smaller types to larger
types (e.g., converting int to float) to avoid data loss.

Example:

int x = 10;

float y = 5.5;

float result = x + y;

printf("Result: %.2f\n", result);

In this example, x (int) is implicitly converted to float to perform the addition with y.

Explicit Type Conversion (Type Casting):


• Explicit conversion, also known as type casting, is performed manually by the programmer to
convert a value from one type to another.

• It’s used when precise control over the conversion is needed or when automatic conversion
could lead to incorrect results or data loss.

Syntax: (type) expression

Example:

float x = 9.7;

int y = (int) x; // Explicitly convert float to int

printf("Converted value: %d\n", y);

this example, the float x is explicitly cast to an integer, truncating the decimal part.

Summary:

• Implicit type conversion happens automatically when needed by the compiler.

• Explicit type conversion requires the programmer to manually specify the new type using
casting.

3(a) Select an appropriate method and write a C program that validates whether a given integer is a
palindrome.

To validate whether a given integer is a palindrome, we can reverse the digits of the number and
check if the reversed number is the same as the original number.

#include <stdio.h>

int main() {

int num, originalNum, reversedNum = 0, remainder;

printf("Enter an integer: ");

scanf("%d", &num);

originalNum = num;

while (num != 0) {

remainder = num % 10;

reversedNum = reversedNum * 10 + remainder;

num /= 10;

if (originalNum == reversedNum) {

printf("%d is a palindrome.\n", originalNum);

} else {

printf("%d is not a palindrome.\n", originalNum);


}

return 0;

Explanation:

1. The user enters an integer.

2. The program stores the original number and then reverses it by extracting digits one by one.

3. After reversing the number, it compares the reversed number with the original one.

4. If they are equal, the number is a palindrome; otherwise, it is not.

Example:

• Input: 121

• Output: 121 is a palindrome.

• Input: 123

• Output: 123 is not a palindrome.

3(b) Write a program using do-while loop to read the numbers until -1 and also
count the positive, negative and zeros encountered by the users.
#include <stdio.h>

int main() {

int number;

int positiveCount = 0, negativeCount = 0, zeroCount = 0;

do {

printf("Enter a number (-1 to stop): ");

scanf("%d", &number);

if (number > 0) {

positiveCount++;

} else if (number < 0) {

negativeCount++;

} else if (number == 0) {

zeroCount++;

} while (number != -1);


printf("Positive numbers: %d\n", positiveCount);

printf("Negative numbers: %d\n", negativeCount);

printf("Zeros: %d\n", zeroCount);

return 0;

Explanation:

1. The program initializes counters for positive, negative, and zero values.

2. A do-while loop prompts the user to enter numbers repeatedly until -1 is entered.

3. Inside the loop, the program checks the value of each number and updates the respective
counters accordingly.

4. After exiting the loop, the program prints the counts of positive numbers, negative numbers,
and zeros.

Enter a number (-1 to stop): 5

Enter a number (-1 to stop): -3

Enter a number (-1 to stop): 0

Enter a number (-1 to stop): 10

Enter a number (-1 to stop): -1

Positive numbers: 2

Negative numbers: 1

Zeros: 1

4(a) Solve the problem of finding the largest element in an array using a C program.

#include <stdio.h>

int main() {

int n, i, largest;

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);

for (i = 0; i < n; i++) {

printf("Element %d: ", i + 1);

scanf("%d", &arr[i]);
}

largest = arr[0];

for (i = 1; i < n; i++) {

if (arr[i] > largest) {

largest = arr[i];

printf("The largest element in the array is: %d\n", largest);

return 0;

Explanation:

1. The program prompts the user to enter the size of the array.

2. It initializes an array of that size and reads the elements from the user.

3. It assumes the first element of the array is the largest and then iterates through the rest of
the array.

4. If it finds an element larger than the current largest, it updates the largest variable.

5. Finally, it prints the largest element found in the array.

Enter the number of elements in the array: 5

Enter 5 elements:

Element 1: 10

Element 2: 5

Element 3: 20

Element 4: 15

Element 5: 30

The largest element in the array is: 30

4(b) Develop a C program to perform matrix multiplication.


#include <stdio.h>

int main() {

int row1, col1, row2, col2;

printf("Enter rows and columns for the first matrix: ");

scanf("%d %d", &row1, &col1);


printf("Enter rows and columns for the second matrix: ");

scanf("%d %d", &row2, &col2);

if (col1 != row2) {

printf("Matrix multiplication not possible. Number of columns in the first matrix must equal the
number of rows in the second matrix.\n");

return 1;

int matrix1[row1][col1], matrix2[row2][col2], result[row1][col2];

printf("Enter elements of the first matrix:\n");

for (int i = 0; i < row1; i++) {

for (int j = 0; j < col1; j++) {

printf("Element [%d][%d]: ", i + 1, j + 1);

scanf("%d", &matrix1[i][j]);

printf("Enter elements of the second matrix:\n");

for (int i = 0; i < row2; i++) {

for (int j = 0; j < col2; j++) {

printf("Element [%d][%d]: ", i + 1, j + 1);

scanf("%d", &matrix2[i][j]);

for (int i = 0; i < row1; i++) {

for (int j = 0; j < col2; j++) {

result[i][j] = 0;

for (int i = 0; i < row1; i++) {

for (int j = 0; j < col2; j++) {

for (int k = 0; k < col1; k++) {

result[i][j] += matrix1[i][k] * matrix2[k][j];


}

printf("Resulting Matrix after multiplication:\n");

for (int i = 0; i < row1; i++) {

for (int j = 0; j < col2; j++) {

printf("%d ", result[i][j]);

printf("\n");

return 0;

Explanation:

1. The program first prompts the user for the dimensions of the two matrices.

2. It checks if the multiplication is possible by comparing the number of columns in the first
matrix to the number of rows in the second matrix.

3. It then takes input for both matrices.

4. A result matrix is initialized to zero, and the program computes the multiplication using three
nested loops:

o The outer two loops iterate over the rows of the first matrix and the columns of the
second matrix.

o The innermost loop calculates the dot product for each element in the resulting
matrix.

5. Finally, it prints the resulting matrix.

Enter rows and columns for the first matrix: 2 3

Enter rows and columns for the second matrix: 3 2

Enter elements of the first matrix:

Element [1][1]: 1

Element [1][2]: 2

Element [1][3]: 3

Element [2][1]: 4
Element [2][2]: 5

Element [2][3]: 6

Enter elements of the second matrix:

Element [1][1]: 7

Element [1][2]: 8

Element [2][1]: 9

Element [2][2]: 10

Element [3][1]: 11

Element [3][2]: 12

Resulting Matrix after multiplication:

58 64

139 154

You might also like