FY BCA C Exam Paper
FY BCA C Exam Paper
IF Statement
In C programming, the if statement is one of the most fundamental conditional statements.
It allows a program to make decisions and execute a block of code based on whether a
condition evaluates to true or false. The condition is typically a logical expression or
comparison, and if it is true (non-zero), the program executes the associated block of code.
Syntax:
Condition: The condition is any valid expression that results in either a true (non-zero) or
false (zero) value.
Execution: If the condition is true, the code inside the if block is executed. If the condition is
false, the code inside the if block is skipped.
Example:
In this example, the program checks if number is greater than 0. Since number is 5, the
condition is true, and the message "The number is positive." is printed.
Key Points:
• The if statement is useful when you want to execute a block of code only if a
condition holds true.
• A simple if block can be used for error-checking, validation, or specific actions like
verifying if a user’s input is valid.
• If the condition evaluates to false, nothing happens, and the program continues with
the next line of code.
• The if statement is commonly used in loops or more complex decision structures to
guide the flow of control.
Execution Flow:
• If a condition evaluates to true, the program follows the true path and executes the
block inside the if statement.
• If a condition evaluates to false, it skips over the block and continues execution after
the if block.
• if statements can also be combined with other conditionals, such as else, else if, or
even switch.
If-else Statement
The if-else statement is an extension of the if statement that provides an alternative
execution path when the condition evaluates to false. This conditional structure is ideal
when there are two distinct paths your program should follow: one for when the condition
is true and another when the condition is false.
Syntax:
Condition: The condition is evaluated first. If the condition is true, the code inside the if
block is executed.
Else Block: If the condition is false, the code inside the else block is executed.
Example:
In this example, the if checks if number is greater than 0. Since number is -3 (false), the code
inside the else block gets executed, and the message "The number is non-positive." is
printed.
Key Points:
• Use Case: if-else is useful when there are only two possible conditions — one that is
true and another that is false.
• True Path: If the condition is true, the if block executes.
• False Path: If the condition is false, the else block executes.
• Simplicity: The if-else structure simplifies decision-making by eliminating the need
for multiple if statements when only two conditions are involved.
• Error Handling: if-else can be used for error handling, validation, and to create
alternatives when the input or condition fails.
Execution Flow:
• The program checks the condition. If the condition is true, the if block executes.
• If the condition is false, the program skips the if block and executes the else block.
• You can have one and only one path executed in an if-else statement: either the if
block or the else block.
• Multiple Conditions: The if-else if ladder checks each condition in the order they are
written. It starts with the first condition and moves down to the next else if if the
condition is false.
• Else Block: If none of the if or else if conditions are true, the else block is executed.
Example:
In this example, the program checks for various grade ranges. Since score is 85, it matches
the second condition (score >= 80), and the message "Grade: B" is printed.
Key Points:
• Multiple Conditions: The if-else if ladder is ideal when you need to evaluate more
than two conditions.
• Sequence Evaluation: The conditions are checked in sequence. Once a condition
evaluates to true, the corresponding block is executed, and the remaining else if
conditions are skipped.
• Default Case: The else block at the end can act as a default action if none of the if or
else if conditions are true.
Execution Flow:
• The program starts by evaluating condition1. If it's true, the corresponding block
executes and all other conditions are skipped.
• If condition1 is false, it checks condition2, and so on.
• If none of the conditions are true, the program moves to the else block (if provided).
• This structure ensures that only one of the conditions can be true and executed.
File Extension .c, .cpp, .java, .py, .html, etc. .obj, .o, .exe, .a (in some cases).
7.Explain for loop, while loop and Do while loop with example.
1. For Loop:
The for loop is one of the most commonly used loops in C programming. It provides a
concise way of writing the loop structure. It is generally used when the number of iterations
is known in advance or can be determined at the time of writing the code.
Syntax of a For Loop:
Initialization: This part is executed once when the loop starts. It is used to initialize the loop
control variable(s).
• Condition: This is the condition to test before each iteration. If it evaluates to true,
the loop body is executed. Once it evaluates to false, the loop terminates.
• Increment/Decrement: This operation is performed after each iteration of the loop.
It updates the loop control variable(s).
Detailed Explanation:
• Initialization happens only once at the beginning of the loop.
• The condition is checked before every iteration of the loop.
• If the condition is true, the body of the loop is executed.
• After the body is executed, the increment/decrement step happens, and then the
condition is checked again.
• The loop runs repeatedly until the condition becomes false.
Example of a For Loop:
Output:
12345
Explanation of Example:
1. The initialization is i = 1, so the loop control variable i starts at 1.
2. The condition is i <= 5. As long as i is less than or equal to 5, the loop will continue to
execute.
3. The body of the loop prints the value of i followed by a space (printf("%d ", i)).
4. The increment happens after each iteration (i++), which increases the value of i by 1.
5. After printing i, the loop checks if i <= 5 is still true. When i becomes 6, the condition
evaluates to false, and the loop terminates.
Common Usage of For Loop:
• The for loop is often used when the number of iterations is known or can be easily
determined.
• For example, iterating through arrays, repeating a process a fixed number of times,
or when the step size is constant.
2. While Loop:
The while loop is another type of loop that repeatedly executes a block of code as long as a
given condition is true. The primary difference between a for loop and a while loop is that
the initialization, condition checking, and increment/decrement are not bundled together in
one line in a while loop.
Syntax of a While Loop:
Output:
12345
Explanation of Example:
1. Initialization: The loop control variable i is initialized to 1 (int i = 1).
2. Condition: The condition i <= 5 is checked before each iteration. The loop continues
to execute as long as i is less than or equal to 5.
3. Body of the loop: The printf("%d ", i) statement prints the value of i, followed by a
space.
4. Increment: After printing the value of i, i++ increments i by 1.
5. The condition is checked again after each iteration. When i reaches 6, the condition i
<= 5 becomes false, and the loop exits.
Common Usage of While Loop:
• The while loop is typically used when the number of iterations is not known
beforehand and depends on dynamic conditions (e.g., reading input until a certain
condition is met, or processing items in a list until it is empty).
3. Do-While Loop:
The do-while loop is similar to the while loop, but with a key difference: it checks the
condition after the loop's body is executed. This guarantees that the body of the loop will
execute at least once, regardless of whether the condition is true or false.
Syntax of a Do-While Loop:
• Condition: The condition is checked after each iteration. If the condition evaluates to
true, the loop executes again. If it evaluates to false, the loop terminates.
Detailed Explanation:
• The do-while loop guarantees that the body of the loop is executed at least once,
regardless of the condition.
• After the loop body is executed, the condition is checked.
• If the condition is true, the loop executes again. If false, the loop terminates.
Example of a Do-While Loop:
Output:
12345
Explanation of Example:
1. Initialization: The loop control variable i is initialized to 1.
2. Body of the loop: The loop executes the body (printing i and incrementing i) once,
regardless of the condition.
3. Condition check: After executing the body, the condition i <= 5 is checked.
4. Since i starts at 1 and is incremented after each iteration, the condition is true until i
becomes 6. At that point, the condition becomes false, and the loop terminates.
Key Difference from While Loop:
• The do-while loop will always execute at least once, whereas the while loop may
not execute if the condition is false initially.
Common Usage of Do-While Loop:
• Do-while loops are useful when you want to perform an action at least once and
then repeat it based on a condition, such as prompting a user for input until they
enter a valid value.
Summary of Loops in C:
for(int i = 0; i < 5;
Example while(i <= 5) do { ... } while(i <= 5)
i++)
Types of Operators in C:
In C programming, operators are categorized into several types based on the type of
operation they perform. Here is a detailed explanation of each type:
1. Arithmetic Operators:
These operators are used to perform mathematical operations like addition, subtraction,
multiplication, division, and modulus.
Operator Operation Example
+ Addition a+b
- Subtraction a-b
* Multiplication a*b
/ Division a/b
% Modulus (remainder) a % b
Example:
> Greater than Returns true if the left value is greater a>b
< Less than Returns true if the left value is less a<b
>= Greater than or equal to Returns true if the left value is greater or equal a >= b
<= Less than or equal to Returns true if the left value is less or equal a <= b
Example:
3. Logical Operators:
Logical operators are used to perform logical operations. These operators combine multiple
conditions (usually relational expressions) and return a boolean value (true or false).
&& Logical AND Returns true if both conditions are true a > b && b > c
` ` Logical OR
Example:
4. Assignment Operators:
Assignment operators are used to assign values to variables. The basic assignment operator
is =, but there are also compound assignment operators like +=, -=, *=, and others that
combine arithmetic operations with assignment.
Operator Operation Example
= Assign a = 5;
Example:
Example:
6. Bitwise Operators:
Bitwise operators work on bits and perform bit-by-bit operations. These are used to
manipulate individual bits of a number.
` ` Bitwise OR
Example:
9.Explain basic structure of C program with example.
1. Preprocessor Directives:
The preprocessor directives are commands given to the preprocessor before the actual
compilation starts. These directives are mostly used to include header files in the program.
These header files contain definitions of functions and macros that are used in the program.
• Example: #include <stdio.h>
This is the most commonly used preprocessor directive in a C program. It includes the
standard input-output library, which provides functions like printf() and scanf() to perform
input and output operations.
Syntax:
#include <header-file-name>
Example:
#include <stdio.h> // Preprocessor directive to include the standard I/O header file
2. Global Declarations:
Global declarations are used to define variables and functions that can be accessed by any
function in the program, including the main function. However, global variables should be
used cautiously, as they can make the program harder to debug and maintain.
Syntax:
<data-type> <variable-name>;
• Global variables are declared outside any function and are accessible to all functions
in the program.
Example:
int globalVar = 10; // Declaring a global variable
3. Main Function:
The main function is the starting point of execution for any C program. When the program is
executed, the control first enters the main() function. The main() function serves as the
entry point for the program.
Syntax:
int main() {
// Code block
return 0;
}
• The int before main means that the function will return an integer to the operating
system, indicating how the program executed. Typically, returning 0 indicates
successful execution.
4. Variable Declarations:
Variables in C are used to store data, and each variable must have a declared type, such as
int, float, or char. The declaration of a variable tells the compiler the type of data the
variable will store.
Syntax:
<data-type> <variable-name>;
• Variables must be declared before they are used in the program.
Example:
int num = 5; // Declaring an integer variable
float pi = 3.14; // Declaring a float variable
char grade = 'A';// Declaring a character variable
6. Return Statement:
The return statement is used to exit the main() function. It indicates the end of the program
and optionally returns a value to the operating system. A return value of 0 typically indicates
successful execution.
Syntax:
return 0;
Complete Example of a C Program:
Components of a C Program:
1. Comments:
o C programs can include comments to make the code more understandable.
o Single-line comments: // This is a comment
o Multi-line comments: /* This is a multi-line comment */
2. Functions:
o C programs can have multiple functions. The main() function is the starting
point, but you can define additional functions to handle specific tasks.
4. Unions:
o A union is a special data type that allows storing different data types in the
same memory location.
o Example:
2. Constants in C Programming
A constant in C is a value that does not change during the execution of the program.
Constants are used to represent fixed values that are often used repeatedly in the program,
such as mathematical constants (PI), configuration values, or specific limits.
Constants can be categorized into different types based on their nature:
3. Variables in C Programming
A variable in C is a named location in memory that stores data, and its value can be changed
during program execution. The type of data a variable can store depends on its declared
data type.
1. break Statement
The break statement is used to terminate the loop or switch statement early. When the
break statement is executed inside a loop or a switch, it immediately exits the current loop
or switch, and the program continues execution with the next statement after the loop or
switch block.
Key Characteristics of break:
• Exits the loop: The break statement causes the loop to terminate prematurely.
• Exits the switch: In a switch-case structure, break is used to exit the current case and
prevent fall-through to the next case.
• Immediate exit: Once the break statement is encountered, the loop or switch block
is exited, and the program continues from the first statement after the loop or
switch.
Usage of break in Loops:
• When the condition to terminate the loop is met before the loop naturally ends, the
break statement can be used to exit early.
Example 1: Break in a for loop
Explanation:
• The loop will print numbers from 1 to 5, and when i == 6, the break statement is
executed, causing the loop to exit immediately.
• The program then prints "Loop exited." after the loop ends.
Output:
12345
Loop exited.
2. continue Statement
The continue statement is used to skip the current iteration of a loop and move to the next
iteration. It does not terminate the loop, but rather jumps to the next loop cycle. This means
that when continue is encountered, the program immediately jumps to the condition check
of the loop to decide whether to execute the next iteration or terminate the loop.
Key Characteristics of continue:
• Skips the current iteration: The continue statement skips the rest of the loop body
for the current iteration and continues with the next iteration of the loop.
• Does not exit the loop: Unlike break, continue does not exit the loop. The loop
continues as long as its condition remains true.
• Can be used in any loop: It can be used in for, while, and do-while loops to skip a
particular iteration based on some condition.
Usage of continue in Loops:
• The continue statement is often used when you want to skip certain values but still
continue iterating over the rest of the loop. For example, you might want to skip
even numbers in a loop that prints odd numbers.
Example 1: Continue in a for loop
Explanation:
• The loop prints only the odd numbers between 1 and 10. When i is even, the
continue statement skips the printf function and moves to the next iteration of the
loop.
Output:
13579
Key Differences Between break and continue
The break statement terminates the The continue statement skips the
current loop or switch block entirely. It current iteration of the loop and
immediately stops the execution of the moves to the next iteration. It does
Purpose
loop or switch case, allowing the not exit the loop but allows you to
program to resume from the first bypass the rest of the code in the
statement after the loop or switch. loop for the current iteration.
In nested loops, break exits only the continue also works in nested loops,
Usage in innermost loop. To break out of outer skipping only the current iteration of
Nested loops, you would typically need the innermost loop in which it is
Loops multiple break statements or use a flag called. The outer loops continue
variable. unaffected.
1. Integer Types
Integer types are used to store whole numbers, both positive and negative, without any
decimal places.
• int: The most commonly used integer data type. It can hold both positive and
negative numbers.
o Size: Typically 4 bytes (32 bits) on most systems.
o Range: It can range from -32,768 to 32,767 (for 16-bit systems) or -
2,147,483,648 to 2,147,483,647 (for 32-bit systems).
• short int or short: A shorter integer type, used when you need to save memory
space, especially for smaller ranges of integers.
o Size: 2 bytes (16 bits).
o Range: Typically from -32,768 to 32,767.
• long int or long: Used when you need a larger range of integer values than a regular
int.
o Size: Typically 4 bytes (32 bits) or 8 bytes (64 bits) depending on the system
architecture.
o Range: A typical range for a 32-bit system is -2,147,483,648 to 2,147,483,647,
and for a 64-bit system, it can be -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807.
• long long int or long long: An extended version of long for storing even larger
integers.
o Size: Typically 8 bytes (64 bits).
o Range: From -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
• unsigned int: An integer type that can only store positive numbers or zero,
effectively doubling the range of positive integers it can store compared to a regular
signed int.
o Size: Same as int (typically 4 bytes).
o Range: 0 to 4,294,967,295 (for a 32-bit system).
• unsigned short int: An unsigned version of the short int type.
o Size: 2 bytes (16 bits).
o Range: 0 to 65,535.
• unsigned long int: An unsigned version of the long int type.
o Size: Typically 4 bytes (32 bits) or 8 bytes (64 bits).
o Range: 0 to 4,294,967,295 (for a 32-bit system) or larger for a 64-bit system.
• unsigned long long int: An unsigned version of the long long int type.
o Size: Typically 8 bytes (64 bits).
o Range: 0 to 18,446,744,073,709,551,615.
2. Floating-Point Types
Floating-point types are used to store real numbers (i.e., numbers with decimal points).
These types are capable of representing both very small and very large numbers.
• float: A single-precision floating-point type used to store numbers with decimal
points.
o Size: 4 bytes (32 bits).
o Precision: It can hold up to 6-7 decimal digits of precision.
o Range: Approximately 1.5 × 10^-45 to 3.4 × 10^38.
• double: A double-precision floating-point type used for more precise real numbers.
o Size: 8 bytes (64 bits).
o Precision: It can hold up to 15-16 decimal digits of precision.
o Range: Approximately 5.0 × 10^-324 to 1.7 × 10^308.
• long double: An extended-precision floating-point type, offering even more precision
than double.
o Size: 8 bytes or 12 bytes (depends on the system/compiler).
o Precision: It can hold up to 18-19 decimal digits of precision.
o Range: Varies based on the system but typically has a larger range than
double.
3. Character Type
The char data type is used to store single characters or small integers.
• char: Used to store a single character (e.g., 'a', 'B', '1', etc.) or a small integer.
o Size: 1 byte (8 bits).
o Range: For signed char, it ranges from -128 to 127, and for unsigned char, it
ranges from 0 to 255.
4. Void Type
The void type is a special type in C that indicates the absence of any data type. It is
commonly used in the following contexts:
• void as the return type of functions: Functions that do not return a value use void as
their return type.
o Example: void function_name() signifies that the function doesn’t return any
value.
• void pointers: A pointer that can point to any data type. It allows for greater
flexibility, particularly when you don't know the data type in advance.
o Example: void *ptr; can point to an integer, float, or any other data type.
Single-precision floating-
float 4 bytes 1.5 × 10^-45 to 3.4 × 10^38
point type
Double-precision floating-
double 8 bytes 5.0 × 10^-324 to 1.7 × 10^308
point type
Data Type Description Size Range/Precision