0% found this document useful (0 votes)
12 views42 pages

MidTerm

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

MidTerm

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

Computer Programming Language - C Programming

Programming basic and C Programming

What is computer programming? Explain its importance in


modern computing.
Describe the evolution of the C programming language.
What are the key features that made C a popular
programming language?
Differentiate between low-level and high-level programming
languages. Where does C fit in?
Explain the term "structured programming." How does C
support structured programming?
What is compartmentalization in programming? Give
examples of how it is implemented in C.
Compare and contrast compiler and interpreter. Which one
does C use, and why?
Explain how memory is allocated and managed during
program execution.
Describe the importance of main memory (RAM) during
program execution.
Define the terms syntax and semantics with reference to C
programming.
How do functions help in program compartmentalization?
Explain the difference between source code and object code.
Why is C often referred to as a "middle-level" language?
Provide examples.
Define a token in C programming. What are the types of
tokens in C?
What are keywords in C? Why can’t we use them as variable
names?
What is meant by the statement "C is close to hardware"?
Justify with examples.
Why is it necessary to understand the memory layout of a
program (stack, heap, data segment)?
What is a source file in C? What file extensions are typically
used?
What are comments in C? Why are they important?
Variables, Data Types, and Operators
What is a variable in C? How is it different from a constant?
List and describe the basic data types available in C.
Define identifiers. What are the rules for naming variables in
C?
How do you declare and initialize a variable? Give examples.
What is the difference between declaration and definition of
a variable?
Write a C program that declares and initializes variables of
different types.
What are format specifiers? Provide examples for int, float,
char, and double.
What is type conversion in C? Distinguish between implicit
and explicit type conversion.
What is type casting? How is it performed in C?
Write a program to demonstrate type casting between int and
float.
Explain the use of the const keyword with examples.
List the different types of operators in C.
Explain the arithmetic operators with suitable examples.
Write a program to evaluate a complex arithmetic expression
using variables.
Describe the relational operators. Give examples of their
usage in a program.
What are logical operators? Explain their usage in
conditional expressions.
Write a program that uses logical operators to validate a
condition.
Explain the use of assignment operators. How is the
compound assignment different?
What are increment and decrement operators? Give examples
using both prefix and postfix forms.
Explain the use of the sizeof operator with an example.

Error and Correction related questions


Error Correction Questions (Chapter 2)
Instructions:
Each of the following C code snippets has one or more errors.
Identify and correct them.

🔹 Expression and Operator Errors


c
Copy
Edit
int a = 5, b = 10;
int c;
c = a + b *;
printf("Sum: %d", c);
c
Copy
Edit
float x = 2, y = 5;
int result;
result = x / y;
printf("Result = %f", result);
c
Copy
Edit
int a = 10;
float b = 3.5;
int c = a / b;
printf("c = %d", c);
c
Copy
Edit
int a = 4, b = 2;
printf("Value = %d", a + b /);
c
Copy
Edit
int x = 5;
printf("Value: %d", ++x++);
🔹 Data Type Errors
c
Copy
Edit
char letter = "A";
printf("Letter: %c", letter);
c
Copy
Edit
int num = 50000;
short int small_num = num;
printf("%d", small_num);
c
Copy
Edit
float pi;
pi = 3.14159
printf("Value of pi: %f", pi);
c
Copy
Edit
double d = 10.5;
int i = d;
printf("%d", i);
c
Copy
Edit
int value = 10;
float price = value;
printf("Price: %d", price);
🔹 Initialization and Declaration Errors
c
Copy
Edit
int a, b = 5, c;
a = b + c;
printf("%d", a);
c
Copy
Edit
int x y;
x = 5;
y = 10;
printf("%d", x + y);
c
Copy
Edit
int a, b;
a = 10
b = 20;
printf("%d", a + b);
c
Copy
Edit
int a = 5, b;
printf("%d", a + b);
c
Copy
Edit
float a = 10, b = 20;
float sum = a + b;
printf("Sum = %d", sum);
🔹 Operator Precedence and Logical Error Corrections
c
Copy
Edit
int x = 10, y = 5, z = 2;
int result = x + y / z;
printf("Result = %d", result);
(Expected behavior: (x+y)/z instead)

c
Copy
Edit
int a = 5;
int b = 10;
printf("%d", a > b && a != b);
(What is printed? Is the output 1 or 0?)

c
Copy
Edit
int x = 5, y = 0;
if(x = y)
printf("Equal");
else
printf("Not Equal");
c
Copy
Edit
int a = 10;
int b = 20;
if (a = b)
printf("Equal");
else
printf("Not Equal");
c
Copy
Edit
int result;
result = 10 / (5 - 5);
printf("%d", result);
(What runtime error will occur?)

🔹 Key Skills Tested:


Syntax correction

Data type matching

Correct use of format specifiers (%d, %f, %c)

Operator precedence and associativity

Logical thinking (common errors like assignment = vs


comparison ==)

Handling divide-by-zero and memory errors

Would you also like a few "spot the error" multiple-choice


questions (like exam MCQs)?
They are also very effective for practice! 🔹
(If yes, I can prepare them too!)

You said:
operator related error and correction questions

ChatGPT said:
Perfect!
Here’s a full set of Operator-Related Error and Correction
Questions for Chapter 2 — focused only on mistakes involving
operators like +, -, *, /, %, =, ==, ++, --, logical operators,
bitwise operators, etc.

🔹 Operator Error Correction Questions


Instructions:
Each code snippet has one or more operator-related mistakes.
Find and correct them.

🔹 Basic Arithmetic and Assignment Operators


c
Copy
Edit
int a = 5, b = 3;
int c = a + * b;
printf("%d", c);
c
Copy
Edit
int x = 10;
int y = 0;
int z = x / y;
printf("%d", z);
c
Copy
Edit
int result;
result = 5 % 0;
printf("%d", result);
c
Copy
Edit
int a = 5, b = 2;
printf("%d", a + b = 7);
c
Copy
Edit
int a = 10;
printf("%d", a +=);
🔹 Relational and Logical Operators
c
Copy
Edit
int a = 10, b = 20;
if(a = b)
printf("Equal\n");
else
printf("Not Equal\n");
c
Copy
Edit
int x = 5, y = 10;
if(x > y && x != y)
printf("x is greater\n");
else
printf("x is smaller\n");
(Problem: Think if the logical condition is proper!)

c
Copy
Edit
int p = 5;
if(p => 5)
printf("P is greater than or equal to 5\n");
c
Copy
Edit
int a = 10;
if(a !> 5)
printf("Not greater\n");
else
printf("Greater\n");
c
Copy
Edit
int x = 3, y = 7;
if(x && y)
printf("True\n");
else
printf("False\n");
(What exactly does if(x && y) check? Is it comparing values?)
🔹 Increment and Decrement Operators
c
Copy
Edit
int i = 5;
printf("%d", ++i++);
c
Copy
Edit
int a = 10;
printf("%d", a--++);
c
Copy
Edit
int x = 3;
x = ++(x++);
printf("%d", x);
🔹 Bitwise Operators
c
Copy
Edit
int a = 5, b = 9;
int c;
c = a and b;
printf("%d", c);
c
Copy
Edit
int x = 6;
int y = 3;
printf("%d", x xor y);
(Problem: and, xor are not C keywords — C uses & and ^!)

🔹 Operator Precedence Problems


c
Copy
Edit
int a = 2, b = 3, c = 4;
int result;
result = a + b * c / 2 - 1;
printf("%d", result);
(Check: Does it calculate as expected?)

c
Copy
Edit
int x = 5;
int y = 2;
int z = x + y / 2;
printf("%d", z);
(Check: Division happens before addition?)

c
Copy
Edit
int a = 8, b = 4, c = 2;
int r = (a + b) * c / a + b;
printf("%d", r);
(Where brackets change the meaning!)

🔹 Assignment vs Comparison
c
Copy
Edit
int marks = 85;
if(marks = 100)
printf("Full marks!\n");
(Assignment done instead of checking?)

c
Copy
Edit
int a = 10, b = 20;
if(a == b == 0)
printf("Both are zero\n");
(Logical mistake: double comparison confusion!)

Programming Questions on Expressions


(Basics to Moderate)

🔹 Basics

1.

Write a program to calculate the sum of two integers entered by the user.

2.

Write a program to calculate the area of a rectangle given its length and width.

3.

Write a program to find the average of three numbers.

4.

Write a program to swap two numbers using a temporary variable.

5.

Write a program to calculate the simple interest using the formula:


Simple Interest = (Principal × Rate × Time) / 100

6.

Write a program to evaluate the expression:


(a + b) * (c - d)
where a, b, c, and d are input by the user.

7.

Write a program to calculate the perimeter of a circle given its radius.


(Perimeter = 2 × π × radius)
8.

Write a program to convert temperature from Celsius to Fahrenheit.


(F = (C × 9/5) + 32)

9.

Write a program to find the remainder when one integer is divided by another.

10.

Write a program to compute the total marks and average marks of 5 subjects.

🔹 Slightly Moderate

11.

Write a program to calculate the compound interest for given Principal, Rate, and Time.

12.

Write a program that takes two integers and displays their sum, difference, product, and division
result (with decimal precision).

13.

Write a program to evaluate the expression:


(a² + b² + c²) / (a + b + c)

14.

Write a program to calculate the value of


(x³ + y³)
where x and y are two numbers input by the user.

15.

Write a program to find the maximum of three numbers using only expressions (no if-else yet).

16.

Write a program that reads two numbers and swaps them without using a temporary variable
(using expressions).
17.

Write a program to convert minutes into hours and minutes (e.g., 130 minutes = 2 hours and 10
minutes).

18.

Write a program that computes the value of a quadratic expression:


ax² + bx + c
for given a, b, c, and x.

19.

Write a program that evaluates this expression and shows the result:
(m+n) / (p*q - r)
taking m, n, p, q, and r as inputs.
(Handle if p*q - r == 0.)

20.

Write a program that reads three sides of a triangle and calculates its perimeter and semi-
perimeter.

Statement related questions


Fundamental Concepts

1.

Define a statement in C. How is it different from an expression?

2.

What is a compound statement in C? Explain with an example.

3.

What is a null statement in C? Give two examples where it is useful.

4.
Differentiate between a declaration statement and an executable statement in C.

5.

Explain the role of a semicolon (;) in C programming. What happens if we omit it after a
statement?

6.

What is the purpose of grouping statements inside curly braces {}? Give an example where it is
necessary.

7.

Describe what happens during the execution of an assignment statement in C.

8.

What is a control statement in C? Classify control statements into categories with examples.

9.

Explain the difference between selection, iteration, and jump control statements in C.

10.

Why are blocks (compound statements) important when writing if-else or loop bodies in C?
Illustrate with an example.

🔹 Statement Execution and Behavior

11.

How does C execute multiple statements inside an if block? What happens if we omit curly
braces {}?

12.

Explain the meaning and use of a single-entry, single-exit principle for statements inside blocks.

13.

How can improper placement of semicolons cause logical errors in if-else statements? Explain
with a code example.
14.

What is a dangling else problem in C? How does C resolve it?

15.

Describe how nesting of statements works in C. Give an example using nested if or nested
loops.

16.

What is the role of scope when we declare variables inside compound statements {}? Explain
with an example.

17.

Differentiate between break, continue, and goto statements in C. When would you use each?

18.

Write a short note on the execution flow of statements inside a for loop and explain how
initialization, condition-checking, and updating happen.

19.

Explain with an example how a misplaced else can change the meaning of a C program.

20.

Discuss common statement-related errors in C programming and how to avoid them.

Find the Error and Correct It:

1.

c
CopyEdit
int a = 5
printf("%d", a);

(Find the error and correct.)

2.

c
CopyEdit
if (a > b)
printf("A is greater");
printf("B is greater");

(What is wrong? Correct it.)

3.

c
CopyEdit
int a;
a = 10;
b = 20;
printf("%d", a+b);

(Find the error.)

4.

c
CopyEdit
int x = 10;
if x > 5 {
printf("X is large");
}

(Find and fix.)

5.

c
CopyEdit
for (int i = 0; i < 10; i++)
{
printf("%d", i);

(Identify the error.)

6.

c
CopyEdit
if (x == 10);
{
printf("X is ten");
}

(What's wrong here?)

7.

c
CopyEdit
{
int a = 5;
printf("%d", a);
}
printf("%d", a);

(Why is there an error? Correct it.)

8.

c
CopyEdit
if (num % 2 = 0)
{
printf("Even number");
}

(Identify and correct the mistake.)

9.

c
CopyEdit
int a = 10;
if (a > 5)
printf("Greater");
else
printf("Smaller")

(Fix the error.)

10.

c
CopyEdit
int x = 5;
int y = 10;
if (x > y)
{
printf("X is greater");
}
else
printf("Y is greater")
}

(Where is the problem?)

🔹 Describe the Error Conceptually:


11.

Explain what happens if you miss the semicolon ; after an assignment statement.

12.

What is the error if multiple statements are written inside an if without using {}?

13.

What happens if we put an extra semicolon after an if condition?

14.

Explain how missing {} can cause logical errors even if there is no compilation error.

15.

What is wrong with the following declaration inside a block?

c
CopyEdit
{
int a = 5;
}
a = 10;

16.

Explain the error if you write if a == 10 instead of if (a == 10).

17.

What type of error occurs when = is used instead of == in a condition?

18.

What happens if you open a block { but forget to close it }?

19.

Explain the effect of missing parentheses () in control statements like if, while, for.

20.

What is the logical error in the following code even though it compiles successfully?
c
CopyEdit
if (x = 0)
printf("Zero");
else
printf("Non-zero");

🔹
Fast Problem Solving – Basic to Moderate

1.

Write a C program to print "YES" if a number is divisible by both 3 and 5, otherwise print "NO".

2.

Given two integers, output the maximum without using any built-in functions like max().

3.

Write a C program that checks if the given year is a leap year.


(Leap year rule: divisible by 4 and (not divisible by 100 unless divisible by 400))

4.

Given an integer n, print "Even" if it is even, otherwise print "Odd", without using modulo
operator %.

5.

Write a program that takes three numbers and prints them in ascending order using only if-else
statements (no arrays).

6.

Given a character input, check if it is a vowel (a, e, i, o, u) or consonant using only if-
else.

7.

Write a C program to find the absolute value of a number without using abs() function.

8.
Given two numbers, swap them without using a third variable.

9.

Write a program that prints numbers from 1 to 100, but for multiples of 3 print "Fizz" instead of
the number, and for multiples of 5 print "Buzz". For numbers which are multiples of both 3 and
5, print "FizzBuzz".

10.

Given an integer n, print "Prime" if it is a prime number, otherwise print "Not Prime". (Use basic
loops and conditions.)

🔹 Logic Building – Little Faster Thinking

11.

Given two numbers a and b, print the smaller one without using if-else, only using ternary
operator (? :).

12.

Find the maximum of three numbers using nested if-else conditions.

13.

Write a C program that reads an integer and prints "Positive", "Negative", or "Zero" based on the
value.

14.

Given a number, check if it lies between 100 and 200 (inclusive) using a single if condition.

15.

Given a number, print "Multiple of 7" if it is a multiple of 7, otherwise print "Not Multiple".
Array related questions
Descriptive Questions (Theory-Based)
1.

What is an array in C? How is it different from a normal variable?

2.

Explain how memory is allocated for a 1D array and a 2D array in C.

3.

What is the importance of index in an array? Why does indexing in C start from 0?

4.

Differentiate between a 1D and a 2D array with suitable examples.

5.

How can you initialize an array at the time of declaration? Give examples for 1D and 2D arrays.

6.

What happens if you access an element outside the size of an array in C?

7.

Explain how a 2D array is stored internally in memory (Row-major order).

8.

How can you pass a 1D array to a function in C? Write the function signature and explain.

9.

Can you pass a 2D array to a function? How? (Mention what information must be passed.)

10.

Explain the difference between static and dynamic array allocation in C.

11.
What happens if you do not initialize an array in C? How are the elements set?

12.

Why do we use nested loops when working with 2D arrays?

13.

Discuss the advantages and disadvantages of using arrays.

14.

How is a character array different from an integer array? How are strings handled in C?

15.

Explain the relationship between arrays and pointers in C.

16.

How can you find the total number of elements in a 1D array using sizeof operator?

17.

What are jagged arrays? Are they possible in C?

18.

Explain the use of arrays in solving real-life problems. Give two examples.

19.

What is the difference between an array name and a pointer to its first element?

20.

Describe the concept of multi-dimensional arrays (beyond 2D). How are they declared?

🔹 2. Error and Correction Questions


(Finding syntax, logic, or concept errors related to arrays)

1.
c
CopyEdit
int arr[5];
arr[5] = 10;

(Identify the error.)

2.

c
CopyEdit
int arr[] = {1, 2, 3, 4, 5, 6};
printf("%d", arr[6]);

(Find the issue.)

3.

c
CopyEdit
int a[3] = {1, 2};
printf("%d", a[2]);

(Is there any problem? Explain.)

4.

c
CopyEdit
int arr[2][2];
arr[1,1] = 5;

(Find and correct the mistake.)

5.

c
CopyEdit
int arr[3];
scanf("%d", arr[0]);

(Where is the mistake?)

6.

c
CopyEdit
int a[5];
for(int i=0; i<=5; i++) {
a[i] = i;
}
(Identify and explain the error.)

7.

c
CopyEdit
int arr[3] = {1,2,3,4};

(What's wrong here?)

8.

c
CopyEdit
int matrix[2][3] = {{1,2,3},{4,5}};

(Is this valid initialization?)

9.

c
CopyEdit
int arr[3];
printf("%d", *arr[1]);

(Find the syntax issue.)

10.

Explain the problem if you try to assign a full array like this:

c
CopyEdit
int a[3], b[3];
a = b;

🔹🔹 3. Programming Problems (Basic to Moderate)


1.

Write a C program to input 5 integers into an array and print them.

2.

Write a C program to find the largest and smallest elements in a 1D array.

3.
Write a C program to reverse an array.

4.

Write a program to search an element in a 1D array (Linear Search).

5.

Write a program to input a 3x3 matrix and display the sum of all elements.

6.

Write a program to calculate the row-wise sum and column-wise sum of a 2D array.

7.

Input two 3x3 matrices and print their addition.

8.

Write a C program to transpose a 3x3 matrix (i.e., interchange rows and columns).

9.

Find the maximum element in each row of a 2D array.

10.

Find the minimum element in each column of a 2D array.

11.

Write a C program to multiply two 2D matrices.

12.

Input an array of integers and count how many numbers are even and how many are odd.

13.

Write a program to find the second largest element in a 1D array.

14.

Write a program to input 10 numbers and remove duplicates (only unique numbers remain).
15.

Sort an array in ascending order using Bubble Sort.

16.

Write a program to rotate a 1D array to the right by 2 positions.

17.

Find the sum of the diagonal elements of a 3x3 matrix.

18.

Check if two arrays are identical or not (element-wise comparison).

19.

Check whether a given matrix is symmetric or not (A matrix is symmetric if A[i][j] == A[j][i]).

20.

Input a matrix and print only its boundary elements.

Identify the Error and Correct It:

1.

c
CopyEdit
int arr[5];
arr[5] = 10;

(What’s wrong with accessing arr[5]?)

2.

c
CopyEdit
int arr[];
arr[0] = 10;

(Where is the mistake?)


3.

c
CopyEdit
int a[3] = {1, 2, 3, 4};

(Explain the error.)

4.

c
CopyEdit
int arr[5] = {1,2};
printf("%d", arr[2]);

(Will the output be predictable?)

5.

c
CopyEdit
int arr[3] = {1,2,3};
printf("%d", arr[3]);

(What happens here?)

6.

c
CopyEdit
int matrix[2][3] = {1,2,3,4,5,6};
printf("%d", matrix[3][2]);

(Find and correct the error.)

7.

c
CopyEdit
int matrix[2][2];
matrix[0,1] = 5;

(Find and fix the mistake.)

8.

c
CopyEdit
int arr[5];
for(int i = 1; i <= 5; i++)
arr[i] = i * 2;

(What’s the off-by-one error?)

9.

c
CopyEdit
int arr[3] = {1, 2};
printf("%d", arr[2]);

(Explain what value will be printed.)

10.

c
CopyEdit
int arr[4];
scanf("%d", arr[0]);
scanf("%d", arr[1]);
scanf("%d", arr[2]);
scanf("%d", arr[4]);

(What mistake is here?)

11.

c
CopyEdit
int a[5];
a = {1,2,3,4,5};
(Why is this assignment wrong?)

12.

c
CopyEdit
int arr[3] = {1,2,3};
arr++;

(Can we do this?)

13.

c
CopyEdit
char str[5] = "Hello";

(What's wrong?)

14.

c
CopyEdit
int matrix[3][2] = {{1,2}, {3,4}, {5}};
printf("%d", matrix[2][1]);

(Explain the initialization behavior.)

15.

c
CopyEdit
int arr[5] = {1, 2, 3, 4, 5};
printf("%d", arr[-1]);

(What happens with negative indexing?)

16.
c
CopyEdit
int a[3] = {1,2,3};
int b[3] = {4,5,6};
a = b;

(Why is this wrong in C?)

17.

c
CopyEdit
int arr[2][2];
arr[1][2] = 10;

(Explain the problem.)

18.

c
CopyEdit
int arr[5];
printf("%d", arr);

(What will be printed?)

19.

c
CopyEdit
int arr[5];
arr[1] = 10;
printf("%d", arr[1][2]);

(Identify the type mismatch.)

20.

c
CopyEdit
int matrix[2][2] = {1,2,3,4,5,6};
(Why is this initialization wrong?)

You might also like