CSE PME Semester Final Question Solve
CSE PME Semester Final Question Solve
Determine which of the following are valid identifiers. If invalid, state why.
Ans.: -
1. a&b - Invalid.
Reason: Contains the special character &.
2. 203N - Invalid.
Reason: Starts with a digit (2).
3. brake - Valid.
Reason: Follows all the rules.
4. return - Invalid.
Reason: return is a reserved keyword in most programming languages.
5. 72Ekattor - Invalid.
Reason: Starts with a digit (7).
Question:
What is a datatype format specifier? Write the format specifiers for the basic datatypes of C
language.
Ans.:-:
int %d or %i
char %c
float %f
double %lf
unsigned int %u
octal integer %o
hexadecimal integer %x or %X
pointer %p
Question:
What will be the output of the following C program: #include<stdio.h> void main() {int a=8,
b=7; a<b ? a++:a=b; printf("%d",a); }
Ans.:-:
Code:
c
Copy code
#include<stdio.h>
void main() {
int a = 8, b = 7;
a < b ? a++ : a = b;
printf("%d", a);
}
Explanation:
1. Variable Initialization:
o a is initialized to 8.
o b is initialized to 7.
2. Ternary (Conditional) Operator:
o The expression a < b is evaluated.
o Since 8 is not less than 7, the condition a < b is false.
o When the condition is false, the expression after the colon ( :) is executed: a
= b.
3. Assignment:
o The statement a = b assigns the value of b (which is 7) to a.
o So, a becomes 7.
4. Print Statement:
o printf("%d", a); will print the value of a, which is now 7.
Output:
Copy code
7
4o
Question:
What are the static variables & functions? What is their scope?
Ans.:-:
ChatGPT
Static Variables:
A static variable in C is a variable that retains its value across function calls. Unlike
regular local variables that are created and destroyed each time a function is executed,
static variables persist throughout the lifetime of the program.
Key Characteristics:
1. Lifetime: The lifetime of a static variable is the entire duration of the program. It is
initialized only once and retains its value between function calls.
2. Scope: If declared inside a function, the scope of the static variable is limited to that
function. If declared outside all functions (at the global level), it is accessible only
within the same file, not from other files.
3. Initialization: Static variables are initialized to 0 by default if no explicit
initialization is provided.
Static Functions:
A static function in C is a function that can only be accessed within the file in which it is
declared. By declaring a function as static, it is made private to the file, preventing its use
in other source files, even if those files are linked together.
Key Characteristics:
1. Scope: The scope of a static function is limited to the file where it is defined. It
cannot be called from other files.
2. Use Case: Useful for implementing helper functions that are not meant to be
exposed to other parts of the program, maintaining encapsulation.
Question:
What is typecasting? Write the syntax of typecasting with an example.
Ans.:-:
What is Typecasting?
Typecasting is the process of converting a variable from one data type to another. In C,
typecasting allows you to manually change the data type of a variable, which can be
useful for various operations, such as mathematical calculations where different data
types need to be mixed, or to control how data is interpreted.
Types of Typecasting:
Example:
c
Copy code
#include <stdio.h>
int main() {
int a = 5, b = 2;
float result;
// Without typecasting
result = a / b;
printf("Without typecasting: %f\n", result); // Output: 2.000000
// With typecasting
result = (float) a / b;
printf("With typecasting: %f\n", result); // Output: 2.500000
return 0;
}
Explanation:
• Without Typecasting: When a (an int) is divided by b (another int), the result is
also treated as an int, which results in 2.
• With Typecasting: (float) a converts a to a float, so the division a / b is
treated as float division, giving a more precise result of 2.5.
4o
Question:
Differentiate between calloc() & malloc()
Ans.:-:
ChatGPT
calloc() and malloc() are two functions in C that are used for dynamic memory
allocation. Both are part of the C standard library (<stdlib.h>), but they have some key
differences in terms of usage and behavior.
Parameters Takes one parameter: the total size Takes two parameters: the number of
Feature malloc() calloc()
Generally faster because it does not Slightly slower due to the initialization
Speed
initialize memory. of memory.
When you do not need the memory When you want to ensure the memory
Use Case
to be initialized. is initialized to zero.
Question:
What is the difference between an array & a pointer.
Ans.:-:
function.
Example
int arr[5]; int *ptr;
Declaration
Question:
Write the outputs of the following expressions: 1) y=pow(4,3); 2) y=abs(2-3*5); 3)
y=ceil(3+5.1); 4) c=toupper('c'); 5) y=strlen("Flat No");
Ans.:-:
1. y = pow(4,3);
o Explanation: The pow() function calculates the power of a number. Here, 4
is raised to the power of 3.
o Calculation: 43=644^3 = 6443=64
o Output: y = 64
2. y = abs(2 - 3 * 5);
o Explanation: The abs() function returns the absolute value of an integer.
First, evaluate the expression 2 - 3 * 5.
o Calculation: 2 - 15 = -13, and abs(-13) = 13
o Output: y = 13
3. y = ceil(3 + 5.1);
o Explanation: The ceil() function returns the smallest integer greater than
or equal to the given number. First, evaluate 3 + 5.1.
o Calculation: 3 + 5.1 = 8.1, and ceil(8.1) = 9
o Output: y = 9
4. c = toupper('c');
o Explanation: The toupper() function converts a lowercase letter to its
uppercase equivalent.
o Output: c = 'C'
5. y = strlen("Flat No");
o Explanation: The strlen() function returns the length of a string,
excluding the null terminator (\0).
o Calculation: The string "Flat No" has 7 characters (F, l, a, t,
space, N, o).
o Output: y = 7
Summary of Outputs:
1. y = 64
2. y = 13
3. y = 9
4. c = 'C'
5. y = 7
Question:
Find out & explain the errors in the following program- #include<stdo.h> #define PI 3.14
float process(float r); main() { float radius, area, printf('Radius=?'); scanf("%d", radius);
float process(float r) {int a; a=PI*r*r;} if(radius<0) area=0; else area=process(radius);
printf("Area=%f",&area) ; }
2/2
Ans.:-:
ChatGPT
Let's identify and explain the errors in the provided C program. Here’s the original code
for reference:
Original Code:
c
Copy code
#include<stdo.h>
#define PI 3.14
float process(float r);
main()
{
float radius, area,
printf('Radius=?');
scanf("%d", radius);
float process(float r)
{
int a;
a = PI * r * r;
}
if (radius < 0)
area = 0;
else
area = process(radius);
printf("Area=%f", &area);
}
Corrected Code:
c
Copy code
#include<stdio.h>
#define PI 3.14
int main()
{
float radius, area;
printf("Radius=?");
scanf("%f", &radius);
if (radius < 0)
area = 0;
else
area = process(radius);
printf("Area=%f", area);
return 0;
}
Question:
Write equivalent if-else statement for the following- #include<stdio.h> int main() { int num;
scanf("%d", &num); switch (num) { case 1: printf("1st Block"); break; case 2: printf("2nd
Block"); break; case 3: printf("3rd Block"); break; default: printf("Out of range"); break; }
return 0; }
Ans.:-:
ChatGPT
To convert the switch statement into an equivalent if-else statement, we'll need to use
multiple if and else if conditions to check each case. Below is the equivalent code:
int main() {
int num;
scanf("%d", &num);
if (num == 1) {
printf("1st Block");
}
else if (num == 2) {
printf("2nd Block");
}
else if (num == 3) {
printf("3rd Block");
}
else {
printf("Out of range");
}
return 0;
}
Question:
What is the purpose of the continue statement? Within which control statements can the
continue statement be included? Compare it with the break statement.
Ans.:-:
ChatGPT
The continue statement in C is used to skip the rest of the code inside a loop for the
current iteration and move directly to the next iteration of the loop. When continue is
encountered, the loop does not terminate; instead, the control jumps to the beginning of
the loop, where the condition is checked, and the next iteration starts.
Use Cases:
• Skipping Iterations: It is useful when you want to skip certain iterations based on a
condition without exiting the loop completely.
1. for Loops
2. while Loops
3. do-while Loops
Continues to the next iteration of the Breaks out of the loop or switch
Control Flow
loop. and proceeds to the code after it.
Where It Can Can be used within loops (for, while, Can be used within loops and
Be Used do-while). switch statements.
Effect on Does not terminate the loop; just skips Terminates the loop or switch
Loop the current iteration. statement completely.
Question:
Take some numbers & display them in ascending order using array, where values are given
by the user.
Ans.:-:
ChatGPT
To display numbers in ascending order using an array, where the values are given by the
user, you can use a sorting algorithm like Bubble Sort or Selection Sort. Below is an
example program in C that takes user input for an array of integers, sorts them in
ascending order, and then displays the sorted values.
int main() {
int n, i, j, temp;
return 0;
}
Example Output:
yaml
Copy code
Enter the number of elements: 5
Enter 5 numbers:
45 12 78 34 9
Numbers in ascending order:
9 12 34 45 78
This program will successfully take an array of numbers from the user, sort them using
Bubble Sort, and print them in ascending order.
4o
Question:
Differentiate between a do-while & a while loop.
Ans.:-:
ChatGPT
Minimum May execute zero times if the Executes at least once, even if the
Iterations condition is false initially. condition is false on the first check.
Suitable when you want the loop Useful when you need to ensure that the
Use Case to execute only if a condition is loop runs at least once before checking
true. the condition.
Question:
Explain the difference between pass by reference & pass by value.
Ans.:-:
ChatGPT
Question:
Write a simple example of a structure in C language.
Ans.:-:
ChatGPT
In C, a structure (struct) is used to group different data types together under a single
name. Here's a simple example of how to define and use a structure in C:
Example of a Structure in C:
c
Copy code
#include <stdio.h>
#include <string.h>
int main() {
// Declaring a variable of type 'struct Person'
struct Person person1;
return 0;
}
Explanation:
Output:
makefile
Copy code
Name: John Doe
Age: 25
Height: 5.9
Question:
What is a preprocessor? How do you define a constant in C programming?
Ans.:-:
ChatGPT
What is a Preprocessor in C?
The preprocessor in C is a tool that processes the source code before it is compiled. It
performs tasks such as including header files, macro expansion, and conditional
compilation. Preprocessor commands, also known as directives, begin with a # symbol
and are executed before the actual compilation of the code starts.
In C, you can define a constant using either the #define preprocessor directive or the
const keyword.
c
Copy code
#define CONSTANT_NAME value
o Example:
c
Copy code
#define PI 3.14159
#define MAX_SIZE 100
int main() {
printf("The value of PI is: %f\n", PI);
printf("The maximum size is: %d\n", MAX_SIZE);
return 0;
}
c
Copy code
const data_type variable_name = value;
o Example:
c
Copy code
const float PI = 3.14159;
const int MAX_SIZE = 100;
int main() {
printf("The value of PI is: %f\n", PI);
printf("The maximum size is: %d\n", MAX_SIZE);
return 0;
}
Question:
Which of the following are valid integer constants? Justify your answer. i) 750 ii) '750' iii)
075
Ans.:-:
ChatGPT
Let's analyze each of the given examples to determine if they are valid integer constants
in C:
i) 750
ii) '750'
iii) 075
• Valid: Yes, this is a valid integer constant, but it is not a decimal number.
• Explanation: In C, an integer that starts with a 0 is interpreted as an octal (base 8)
number. The value 075 is an octal constant, which translates to 7 * 8 + 5 = 61 in
decimal. It is still valid as an integer constant, but the interpretation of the value
changes because it is read as an octal number.
Summary:
Constant Valid or Invalid Explanation
Thus, 750 and 075 are valid integer constants, while '750' is not.
4o
Question:
What are the advantages of using preprocessors?
Ans.:-:
ChatGPT
Preprocessors are tools that allow the pre-compilation of code, performing various text
substitutions and manipulations before the actual compilation process begins. Using
preprocessors provides several advantages, including:
1. Code Reusability
• By using the #include directive, you can include the contents of a header file in
multiple programs. This allows you to write reusable code, such as commonly used
functions, macros, or variable definitions, in a separate file and include them
wherever needed.
• Example:
c
Copy code
#include <stdio.h> // Includes the standard input-output header
file
2. Ease of Maintenance
• Preprocessors help in maintaining the code. When you define constants or macros
using #define, you can change their values in one place, and it will automatically be
reflected throughout the code. This is particularly useful for constants or settings
that might change frequently.
• Example:
c
Copy code
#define MAX_SIZE 100 // Define a constant for array size
3. Conditional Compilation
• Using directives like #ifdef, #ifndef, #if, #else, and #endif, you can include or
exclude portions of the code during compilation. This is useful for:
o Creating code that works on multiple platforms (e.g., Windows, Linux).
o Enabling or disabling debugging code without deleting it.
o Compiling code based on certain conditions or configurations.
• Example:
c
Copy code
#ifdef DEBUG
printf("Debugging mode is on.\n");
#endif
4. Code Optimization
• Preprocessors can be used to replace repetitive code with macros, which can
make the code shorter, cleaner, and easier to read. This reduces typing effort and
can help with code readability.
• Example:
c
Copy code
#define SQUARE(x) ((x) * (x)) // Macro to calculate the square
of a number
5. Platform Independence
• Preprocessor directives can be used to write code that works across multiple
platforms. For example, you can include platform-specific code based on conditions
checked by the preprocessor.
• Example:
c
Copy code
#ifdef _WIN32
// Windows-specific code
#else
// Code for other platforms
#endif
c
Copy code
#define PI 3.14159
c
Copy code
#ifndef HEADER_FILE
#define HEADER_FILE
// Code inside this block will be included only once
#endif
Question:
How is a function declared in c language.
Ans.:-:
In C, a function declaration (or function prototype) specifies the function name, return
type, and parameters. It tells the compiler what kind of function it is, how many
parameters it takes, and what types those parameters are. The function declaration
typically appears before the main() function or in a header file to ensure the compiler
recognizes the function when it is called.
In this example:
• int: The return type indicates that the function will return an integer.
• add: The name of the function.
• int a, int b: Two integer parameters the function will accept.
Question:
Write a C program to find out the factorial of an integer number.
Ans.:-:
ChatGPT
int main() {
int number;
return 0;
}
Question:
Differentiate between actual parameters & formal parameters.
Ans.:-:
Provide the input data for the Receive and store the input data provided
Role
function to operate on. by the actual parameters.
Exist outside the function, where Exist inside the function, only accessible
Scope
the function is called. within the function.
Question:
What advantage is there in defining an array size in terms of symbolic constant rather than
a fixed integer quantity?
Ans.:-:
ChatGPT
Defining an array size in terms of a symbolic constant instead of a fixed integer quantity
offers several advantages in programming, particularly in C. Here are some key benefits:
1. Improved Readability
• Symbolic Constants: When you define an array size using a symbolic constant (e.g.,
#define SIZE 100), it makes the code more readable and understandable. A
symbolic name like SIZE clearly indicates its purpose compared to a magic number
like 100, which may not convey its meaning.
• Example:
c
Copy code
#define SIZE 100
int arr[SIZE]; // Clearer than int arr[100];
2. Easier Maintenance
• If you need to change the size of the array, you can do so in one place (the definition
of the symbolic constant). This eliminates the risk of missing occurrences of a
hardcoded value throughout your code, which could lead to inconsistencies and
errors.
• Example:
c
Copy code
#define SIZE 100
// Later, if you need to change the size:
#define SIZE 200
• Using symbolic constants helps avoid "magic numbers" in your code. Magic numbers
are hardcoded values that appear without context, making the code harder to
understand. By using constants, you provide context and meaning to the values.
• Example:
c
Copy code
int arr[100]; // Magic number
int arr[SIZE]; // Using a constant for clarity
• When using symbolic constants, the same constant can be used across different
functions or modules. This allows for easier reuse of the array size in multiple
places, enhancing code modularity.
• Example:
c
Copy code
void function() {
int arr[SIZE]; // Consistent use of SIZE in multiple
functions
}
• If the size of the array needs to be modified based on different configurations (e.g.,
user input, project requirements), symbolic constants can be defined based on
preprocessor directives, allowing for flexible and scalable code.
• Example:
c
Copy code
#define SIZE 100
// Change to #define SIZE 200 for a larger array in different
contexts
6. Enhanced Debugging
• Using a symbolic constant for array sizes ensures that the same value is used
consistently throughout the code. This helps avoid bugs that can arise from
mistakenly using different sizes in different places.
Question:
Write a program that calculates & prints the sum of the integers which are divisible by 7
and ranges from 11 to 111.
Ans.:-:
ChatGPT
Here's a C program that calculates and prints the sum of integers that are divisible by 7
within the range of 11 to 111:
int main() {
int sum = 0; // Variable to hold the sum of integers
int start = 11; // Start of the range
int end = 111; // End of the range
return 0;
}
Question:
What is recursion? What are the conditions to be satisfied to write a program recursively?
In which cases you should use recursion?
Ans.:-:
ChatGPT
What is Recursion?
1. Base Case: Every recursive function must have at least one base case to terminate
the recursion. Without it, the function will call itself indefinitely, leading to a
stack overflow error.
2. Progression Towards Base Case: Each recursive call must bring the function
closer to the base case. This usually involves reducing the size of the input or
modifying the input parameters in such a way that it eventually meets the base
case.
3. Well-Defined Problem: The problem should be clearly defined and should be
expressible in terms of smaller instances of the same problem.
Ans.:-:
ChatGPT
return count;
}
int main() {
char str[100];
return 0;
}