[go: up one dir, main page]

0% found this document useful (0 votes)
24 views9 pages

Cse Question Ans

Uploaded by

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

Cse Question Ans

Uploaded by

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

Computer Science and Engineering

Part A
1. a) Is it possible to convert all if else code into switch code? Give an example?
Ans :No, it is not possible to convert all if-else code into switch code in C
programming .Here are some reasons why not all if-else code can be converted into switch
code:
 Complex conditions: if-else statements can handle complex conditions involving
logical operators (&&, ||), comparisons, and expressions. On the other hand, switch
statements only support simple equality checks on a single integral type. If your
conditions involve complex logic or non-constant expressions, it cannot be directly
converted into a switch statement.

 Range-based conditions: if-else statements can handle conditions based on ranges of


values. For example, if (x > 0 && x < 10). switch statements, however, only support
discrete values. They cannot directly express conditions based on ranges.

 Non-integral types: switch statements in C are designed to work with integral types
(such as int or char). They cannot be used with other types, such as floating-point
numbers or strings. if-else statements provide more flexibility in handling conditions
with different data types.
While switch statements can offer a more concise and readable approach in certain cases,
they have limitations compared to the flexibility of if-else statements.

b) Write an algorithm to count the digits of a number .Draw the flowchart?


Ans: The algorithm to count the number of digits of a given number is below :
1. Accept an input number.
2. Initialize a variable count to 0.
3. Take the absolute value of the input number.
4. While the input number is greater than zero:
 Divide the input number by 10.
 Increment count by 1.
5. Display the value of count as the number of digits in the original input number.
This algorithm iteratively divides the number by 10 and increments the count until the
number becomes zero, effectively counting the number of digits in the original input number.
For example,

Abeer Hasan Shuvo


220203017
2. a) Explain the function of prefix and postfix increment expression ?
Ans : Prefix Increment (++x):
 The prefix increment expression increases the value of a variable (x) by 1 before
using it in the surrounding code.
 It updates the variable immediately and returns the updated value.
 For example, if x has the value 5, ++x will make it 6 and return 6.
 Postfix Increment (x++):
Postfix increment (x++)
 The postfix increment expression increases the value of a variable (x) by 1 after
using it in the surrounding code.
 It uses the current value of the variable in the surrounding code, and then increments
the variable's value.
 It returns the original value of the variable before the increment.
 For example, if x has the value 5, x++ will use 5 in the surrounding code, then make
x 6, and return 5.
In simpler terms, prefix increment increases the value of a variable before using it, while
postfix increment uses the current value and then increases it. Both expressions ultimately
increase the variable's value by 1.

Abeer Hasan Shuvo


220203017
b) write a c program to converts a binary number to decimal number?
Ans:
#include <stdio.h>
int main() {
long long binaryNum;
int decimalNum = 0, base = 1, remainder;
printf("Enter a binary num: ");
scanf("%lld", &binaryNum);

while (binaryNum != 0) {
remainder = binaryNum % 10;
decimalNum += remainder * base;
base *= 2;
binaryNum /= 10;
}
printf("Decimal equivalent: %d\n", decimalNum);
return 0;
}

3. a) define the limitation of modulus operation.


Ans : The limitations of the modulus operation (%) in C are as follows:
1. Limited to integral types, cannot be directly applied to floating-point numbers.
2. Division by zero is undefined and should be avoided.
3. The sign of the remainder matches the sign of the dividend.
4. The operation is subject to range limitations of the data types involved, potentially
leading to overflow or truncation issues.
5. Floating-point modulus operations may produce approximate results due to limited
precision.
b) Describe the basic parts of a c programming with example?
Ans: A basic C program consists of:
 Preprocessor command: Preprocessor commands in C are directives that provide
instructions to the preprocessor, such as including header files and defining constants,
enabling conditional compilation, and modifying the source code before actual
compilation.

 Main function : The main function in a C program serves as the entry point for
program execution and is required in every C program. It contains the program's
executable statements and can optionally return a value to indicate the program's
status upon completion.
 Comment : Comments in a C program are used to provide explanations or notes
within the code that are ignored by the compiler during the compilation process.

Abeer Hasan Shuvo


220203017
 Library function: Library functions in a C program are pre-written functions provided
by C libraries that can be used to perform specific tasks without having to write the
code from scratch
 End of function : The end of a function in a C program is reached when the closing
brace '}' of the function block is encountered, indicating the completion of the
function's execution.

c) Describe the difference between compiler and interpreter.


Ans: The difference between compiler and interpreter are given below:
 A complier converts the high level instruction into machine language while an
interpreter converts the high level instruction into an intermediate form.
 The compiler executes the entire program at a time, but the interpreter executes each
and every line individually.
 List of errors is created by the compiler after the compilation process while an
interpreter stops translating after the first error.
 Autonomous executable file is generated by the compiler while interpreter is
compulsory for an interpreter program.
 Interpreter is smaller and simpler than compiler
 Interpreter is slower than compiler.

Part B
5. a) Explain the function general form?
Ans : The general form of a function in C programming includes the function's return type,
name, optional parameters enclosed in parentheses, and the function body enclosed in curly
braces.
 return_type: Specifies the data type of the value that the function returns to the caller.
Use void if the function does not return a value.
 function_name: Name of the function, which is used to call the function from other
parts of the program.
 parameters: Optional input parameters enclosed in parentheses, which are variables or
values passed to the function for processing.
 function_body: The block of code enclosed in curly braces, containing the statements
and operations to be executed when the function is called.
b) Find the output of following c programming.
For(i=0;i<10;i++)
{
For(j=0;j<=I;j++)//nested
Printf(“*”);
Printf(“\n”);
}

Abeer Hasan Shuvo


220203017
Ans: The out put of the code is below:
*
**
***
****
*****
c) Differentiate between arrey and structure?
Ans : difference between arrey and stracture are:
Parameter Structure in C Array in C
Definition It is a type of data structure It is a type of data structure
in the form of a container that works as a container to
that holds variables of hold variables of the very
different types. same type. Array does not
support variables of multiple
data types.
Types of Data Type A structure includes A user cannot have multiple
Variable multiple forms of data-type forms of data-type variables
variables in the form of in an array because it
input. supports only the same form
of data-type variables.
Size The various elements in a The array contains various
structure are of different elements of the same size.
sizes each.
Keyword We use the keyword “struct” No keyword is present to
to define a structure. declare an array.
User-defined The structure is a user- An array isn’t user-defined.
defined form of data type. It is declared directly.

6. a) Distinguish between call by value and call by reference with an example.


Ans : distinction between call by value and call by reference:
Call by value:
1. While calling a function, when you pass values by copying variables, it is known as
“Call By Values.”
2. In call by value, a copy of the value of the actual argument is passed to the function.
3. The function works with the copied value, and any changes made to the parameter
within the function do not affect the original argument outside the function.
4. It is the default method of parameter passing in most programming languages.
5. Call by value is generally used for passing basic data types like integers, floats,
characters, etc.

Abeer Hasan Shuvo


220203017
For example:

Output:
Inside the function: 6
Outside the function: 5
Call by reference :
1. While calling a function, in programming language instead of copying the values of
variables, the address of the variables is used it is known as “Call By References.
2. In call by reference, the memory address (reference) of the actual argument is passed
to the function.
3. The function can directly access and modify the value at that memory address, which
affects the original argument outside the function.
4. Call by reference is typically used when we want to modify the original value of the
argument or pass large data structures efficiently.
5. It requires explicit use of pointers or references in the function parameters.

For example:

Output:
Inside the function: 6
Outside the function: 6

Abeer Hasan Shuvo


220203017
In summary, call by value passes a copy of the value to the function, while call by reference
passes the memory address of the argument. Call by value does not modify the original
argument, while call by reference can modify the original argument.
b) Write down the output of the following code segment .
int i=0,n=3;
while (i!=n)
if (i=3){

continue ; }

Printf(“%d”,i);
i++ ; }

output :
0
1
2
7. a) Explain the operation of *ptr++ and ++*ptr with example ?
Ans :
*ptr++:
 In this expression, ptr is a pointer variable.
 The * operator dereferences the pointer, giving access to the value it points to.
 The ++ operator increments the pointer to point to the next memory location.
 The order of operations is postfix increment (++) first, followed by dereference (*).
 After the increment, ptr points to the next memory location, and the value at the original
memory location is accessed.

Output:

Abeer Hasan Shuvo


220203017
Value: 1
Value: 2
Value: 3
++*ptr:
 In this expression, ptr is a pointer variable.
 The * operator dereferences the pointer, giving access to the value it points to.
 The ++ operator increments the value at the memory location.
 The order of operations is prefix increment (++) first, followed by dereference (*).
 After the increment, the value at the original memory location is updated.
Example:

Output:
Value: 2
Value: 3
Value: 4
8. a) Give an example of a function with no arguments and return values.
Ans :
#include <stdio.h>

void greet() {
printf ("Hello! Welcome to the program.\n");
}

int main() {
greet(); // Calling the function
return 0;
}
In this example, we have a function named greet() that doesn't have any parameters and has a
return type of void, indicating that it doesn't return any value. Inside the function, it prints the
greeting message "Hello! Welcome to the program." to the console.

Abeer Hasan Shuvo


220203017
In the main() function, we call the greet() function, which executes the code inside the
function. When the program is run, the output will be:
“Hello! Welcome to the program.”
The greet() function serves as a way to encapsulate and execute a specific task or block of
code, even though it doesn't require any inputs or produce a return value.
b) Find the output
#include<stdio.h>
Int main(){
Char s1[10]=”aaa”,s2[10]=”bbb”;
Int result ;
Result=strcat(s1,s2);
Printf(result);
}

Ans : output will be:


“Aaabbb”
c) How do you access the values within an arrey?
Ans: To access the values within an array in C, you can use the array index notation. Each
element in the array has a unique index, starting from 0 for the first element and incrementing
by 1 for each subsequent element.
#include <stdio.h>

int main() {
int numbers[] = {10, 20, 30, 40, 50};

// Accessing individual elements


printf("Element at index 0: %d\n", numbers[0]); // Output: 10
printf("Element at index 2: %d\n", numbers[2]); // Output: 30
printf("Element at index 4: %d\n", numbers[4]); // Output: 50

// Modifying element at a specific index


numbers[1] = 25;
printf("Modified element at index 1: %d\n", numbers[1]); // Output: 25

return 0;
}

In this example, we have an integer array numbers initialized with values. To access a
specific element, we use the array name followed by the index enclosed within square
brackets, such as numbers[0], numbers[2], and numbers[4].You can also modify the value at
a specific index by assigning a new value to it, as shown with numbers[1] = 25;. This changes
the value at index 1 from 20 to 25.

Abeer Hasan Shuvo


220203017

You might also like