Mid-1 (QA 2024)
Mid-1 (QA 2024)
Mid-1 (QA 2024)
Ans) A variable is a value that can change during program execution, while a constant is a
value that remains fixed and cannot be altered once defined.
Example for variable
int x = 10;
x = 20; // Value of x changes from 10 to 20
Example for Constant
const float PI = 3.14;
// PI cannot be changed later in the program
Here const is a key word which tells PI has a constat value.
2. What function is used to print output in C? Give its syntax.
Ans) The function used to print output in C is printf().
printf("format string", arguments);
The format string contains text to display and format specifiers (like %d for integers, %f for
floating-point numbers, etc.).
The arguments ( means variable names ) are the values to be printed, which correspond to
the format specifiers.
3. How do logical operators differ from arithmetic operators in C?
Ans) In C, logical operators are used to evaluate logical expressions (true or false), while
arithmetic operators perform mathematical calculations.
Common operators: && (AND), || (OR), ! (NOT)
Common operators: + (addition), - (subtraction), * (multiplication), / (division), % (modulus).
4. How does operator precedence affect the evaluation of expressions?
Operator precedence in C determines the order in which operators are evaluated in an
expression. Operators with higher precedence are evaluated before those with lower
precedence.
int result = 5 + 3 * 2;
Here, * has higher precedence than +, so 3 * 2 is evaluated first, then + 5.
5. Identify the role of an identifier. Choose various basic datatypes used in C?
An identifier is a name given to elements like variables, functions, arrays, etc., to uniquely
identify them in the program.
It allows programmers to refer to these elements throughout the code.
Identifiers must follow naming rules, such as starting with a letter or underscore and not
containing special characters or spaces.
Basic Data Types in C:
1. int: Used to store integers (whole numbers).
o Example: int age = 25;
2. float: Used to store floating-point (decimal) numbers.
o Example: float height = 5.9;
3. char: Used to store a single character.
o Example: char grade = 'A';
4. double: Used to store double-precision floating-point numbers, providing more
precision than float.
o Example: double salary = 12345.67;
5. void: Represents the absence of any data type, commonly used for functions that don't
return a value.
o Example: void displayMessage() { /* code */ }
6. Identify different types of operators and explain any five of them with an
example code snippet.
if-else:
This is a bi-directional conditional control statement.
This statement is used to test a condition and take one of the two possible actions.
If the condition is true,(non-Zero) then a single statement or a block of statements is
executed (inside of if block).
Otherwise another single statement or a block of statements is executed (inside of else
block).
Syntax:-
if(condition)
{
statement 1;
}
else
{
statement 2;
}
Next Statement; ( it is optional)
The main difference between a while loop and a do-while loop is in when the condition is
checked:
while loop: The condition is checked before the loop executes. If the condition is
false initially, the loop may not execute even once.
while (condition)
{
// Code to execute
}
do-while loop: The loop executes at least once, and the condition is checked after the
loop has executed.
do
{
// Code to execute
} while (condition);
• This statement causes an immediate exit from that loop in which this statement
appears.
• It can be written as:
break;
• Syntax:
if(condition)
{
Statement 1;
}
Next Statement; ( it is optional)
b) if-else:
This is a bi-directional conditional control statement.
This statement is used to test a condition and take one of the two possible actions.
If the condition is true,(non-Zero) then a single statement or a block of statements is
executed (inside of if block).
Otherwise another single statement or a block of statements is executed (inside of else
block).
Syntax:-
if(condition)
{
statement 1;
}
else
{
statement 2;
}
Next Statement; ( it is optional)
8.Make use of switch-case-default statement to explain its working.
switch:
• This is a multi-directional conditional control statement. Sometimes there is a need in
program to make a choice among a number of alternatives. For making this choice,
we use the switch statement. This can be written as.
• Syntax:
switch(expression)
{
case constant1:
Block_A1;
break;
case constant2:
Block_A2;
break;
……….
case constantN:
Block_AN;
break;
default: Block_D;
}
Out_of_switch statements;
• Firstly, the switch expression is evaluated, then the value of this expression is
compared one by one with every case constant.
• If the value of the expression matches with any case constant, then all statements
under that particular case are executed. If none of the case constants matches with the
value of the expression, then the block of statements under 'default' is executed.
• 'default' is optional; if it is not present and no case matches, then no action takes
place.
• These cases and default can occur in any order.
9.Identify the difference while loop with for loop with suitable example?
The primary difference between a while loop and a for loop lies in how they control the loop
execution and initialization:
1. Syntax and Use
while loop: Typically used when the number of iterations is not known beforehand. It keeps
executing as long as the condition remains true.
Example:
int i = 0;
while (i < 5)
{
printf("%d ", i);
i++;
}
Output: 0 1 2 3 4
o Here, the initialization (i = 0), condition (i < 5), and increment (i++) are
handled separately.
o
for loop: Typically used when the number of iterations is known in advance. It has a more
concise structure with initialization, condition, and increment in one line.
Example:
for (int i = 0; i < 5; i++)
{
printf("%d ", i);
}
Output: 0 1 2 3 4
o In a for loop, the initialization (i = 0), condition (i < 5), and increment (i++)
are bundled into the loop's structure.
2. Key Differences:
while loop: Suited for scenarios where the number of iterations depends on external
conditions that might change during runtime.
for loop: Ideal for situations where the iteration count is fixed or predictable
10 Build a program to print the below kind of patterns for the given value of n.
a)
#include<stdio.h>
int main()
{
int i,j;
for(i=1; i<=4; i++)
{
for(j=1; j<=i; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
b)
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=4;i++)
{
for(j=4;j>=i;j--)
{
printf("*");
}
printf("\n");
}
return 0;
}
c)
#include <stdio.h>
int main()
{
int i, j, space, rows = 4;
for(i = 1; i <= rows; i++)
{
printf("\n");
}
return 0;
}
11)Build a program to find the sum of the series up to n terms where n is an integer
entered by the user.
1+2+4+7+11+16+....
#include <stdio.h>
int main()
{
int n,i,term = 1, sum = 0, diff = 1;
return 0;
}
12) Build a program to find out the value of x raised to the power y, where x and yare
positive integers without using pow() function.
#include <stdio.h>
int main()
{
int x, y, i, result = 1;
// Multiply x, y times
for (i = 1; i <= y; i++)
{
result = result * x;
}
return 0;
}