0% found this document useful (0 votes)
9 views

Question Bank Theory Answers C Programming

Uploaded by

khushibishtania
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Question Bank Theory Answers C Programming

Uploaded by

khushibishtania
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1

AEC (Anjali Engineering Classes)


Most Important Questions &
PYQs – C Programming

Q1) What is the use of #define preprocessor?


Ans. The #define preprocessor directive in C is used for defining macros, which are
symbolic constants or functions that are substituted directly into the code during the
preprocessing phase (before compilation). It is a powerful feature of the
preprocessor that helps improve code readability, maintainability, and efficiency.
Uses of #define
1. Defining Constants
o You can use #define to create symbolic names for constants instead of
using magic numbers, making the code easier to read and update.
#define PI 3.14159
#define MAX_LENGTH 100
double area = PI * radius * radius;
2. Defining Macros (Inline Functions)
• #define can be used to define small, inline functions to save overhead
from function calls.
#define SQUARE(x) ((x) * (x))

int result = SQUARE(5); // Expands to (5 * 5)

Q2) Explain the usage of break, labelled break, continue and labelled continue
statements?
Ans. In C programming, break and continue are control flow statements used to
alter the normal execution of loops or switch statements. While labelled break and
labelled continue are not directly supported in C, similar behavior can be achieved
using the goto statement. Below are detailed explanations of each
1. break Statement
The break statement is used to:
• Exit a for, while, or do-while loop prematurely.

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)
2

• Exit a switch statement after a case is executed.


Example:
for (int i = 0; i < 5; i++) {
if (i == 3) {
break;
}
printf("%d ", i);
}
Output: 0 1 2
In a switch statement:
int num = 2;
switch (num) {
case 1: printf("One"); break;
case 2: printf("Two"); break;
default: printf("Default");
}
Output: Two
2. Labelled break Statement
C does not directly support labelled break. However, similar functionality can be
achieved using the goto statement to exit nested loops.
Example:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == 1 && j == 1) {
goto end; // Simulates labelled break
}
printf("i=%d, j=%d\n", i, j);
}
}
end:
printf("Exited nested loops.\n");

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)
3

Output:
i=0, j=0
i=0, j=1
i=0, j=2
i=1, j=0
Exited nested loops.
3. continue Statement
The continue statement is used to:
• Skip the rest of the code in the current iteration of a loop.
• Immediately proceed to the next iteration of the loop.
Example:
for (int i = 0; i < 5; i++) {
if (i == 3) {
continue; // Skip printing when i equals 3
}
printf("%d ", i);
}
// Output: 0 1 2 4

4. Labelled continue Statement


C does not directly support labelled continue. However, you can use the goto
statement to simulate skipping the remainder of nested loops.
Example:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == 1 && j == 1) {
goto next_iteration; // Simulates labelled continue
}
printf("i=%d, j=%d\n", i, j);
}
continue;

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)
4

next_iteration:;
}
Output:
i=0, j=0
i=0, j=1
i=0, j=2
i=1, j=0
i=1, j=2
i=2, j=0
i=2, j=1
i=2, j=2

Comparison of Statements

Statement Purpose

break Exits the nearest enclosing loop or switch statement immediately.

Exits nested loops or blocks by using goto (since C lacks direct


Labelled break
support for labelled break).

Skips the remaining part of the loop body for the current iteration
continue
and moves to the next iteration.

Labelled Skips nested iterations using goto (since C lacks direct support for
continue labelled continue).

Q3) Write example code to declare two dimensional array.


Ans: A two-dimensional array in C is essentially an array of arrays. Here's an
example demonstrating how to declare, initialize, and access a 2D array:
1. Declaring a Two-Dimensional Array
You can declare a 2D array using the following syntax:
data_type array_name[rows][columns];
2.Example Code
#include <stdio.h>

int main() {

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)
5

// Declare and initialize a 2D array with 3 rows and 4 columns


int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};

// Print the elements of the 2D array


printf("The 2D array elements are:\n");
for (int i = 0; i < 3; i++) { // Loop through rows
for (int j = 0; j < 4; j++) { // Loop through columns
printf("%d ", matrix[i][j]); // Access elements using [row][column]
}
printf("\n"); // New line after each row
}

return 0;
}

Q4) Differentiate between Local & Global Variable in C.


Ans: Local and global variables are distinguished by their scope (the part of the program
where they can be accessed) and lifetime (how long they exist during program execution).
Below is a detailed comparison:

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)
6

Q5) What is the difference between ++a and a++?


Ans : In C programming, ++a (pre-increment) and a++ (post-increment) are increment
operators that increase the value of the variable a by 1. The difference lies in when the
increment happens relative to the value being used in an expression.

1. Standalone Usage
In standalone usage (not part of another expression), ++a and a++ behave
identically.
int a = 5;
++a; // Pre-increment
printf("%d\n", a); // Output: 6
a++; // Post-increment
printf("%d\n", a); // Output: 7
Here there is no difference in ++a or a++.
2. Usage in Expressions
Case 1: Pre-increment (++a)
• The variable a is incremented first, and the new value is used in the
expression.
int a = 5;
int b = ++a; // Increment first, then assign
printf("a = %d, b = %d\n", a, b); // Output: a = 6, b = 6
Case 2: Post-increment (a++)
• The current value of a is used in the expression first, and then a is
incremented.
int a = 5;
int b = a++; // Assign first, then increment
printf("a = %d, b = %d\n", a, b); // Output: a = 6, b = 5
Another example :
int a = 5, result;

// Using Pre-increment
result = ++a + 10; // Increment a to 6, then add 10
printf("Result = %d, a = %d\n", result, a); // Output: Result = 16, a = 6

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)
7

// Using Post-increment
a = 5; // Reset a
result = a++ + 10; // Use a (5) first, then increment to 6
printf("Result = %d, a = %d\n", result, a); // Output: Result = 15, a = 6

Q6) Explain various types of operators available in C.


Ans. In C programming, operators are symbols or keywords used to perform
operations on operands (variables, constants, or data). C provides a rich set of
operators categorized based on their functionality. Below is an explanation of the
various types of operators available in C:

Q7) What is Recursion? Explain with the help of a program in C.


Q8) Explain various looping statements in C with the help of suitable example.

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)
8

Q9) Differentiate between gets() and scanf() function with help of an example.
Q10) Explain the storage classes in C with the help of example program.
Q11) What are the various dynamic memory allocation functions available in C?
Q12) What is a Pointer? Explain array of pointers to function with help of an
example.
Q13) Differentiate between formatted and unformatted input and output functions.
Q14) Write short note on Structure and Unions.
Q15) Write short note on File Access Modes
Q16) Write short note on Passing Structures to Functions
Q17) What is the use of atoi() function? Explain with an example.
Q18) Compare Array & Strutures. Justify your comparison with help of an example.
Q19) Explain the syntax and usage of inbuilt string functions with suitable example.
Q20) Write a program to find largest and smallest word in a string.
Q21) Explain the following header file math.h, stdlib.h and time.h
Q22) Write a program to check if input word is a palindrome or not.
Q23) Explain switch case statement with help of an example.
Q24) What is Recursive Function? Write a program to find factorial of a number
using Recursion.
Q25) What is Structure? Explain with programming example.
Q26) Write a program to multiply 3 X 3 matrix.
Q27) Explain pointer to pointer and void pointer.
Q28) Explain about conditional compilation directive with its types and suitable
example.
Q29) Explain about fopen(), feof(), fflush() and fclose(0 function.
Q30) What are the data types in C language? Explain with example.
Q31) Differentiate between while and do while statement with example.
Q32) Write any C program using Logical And and Logical Or Operator.
Q33) What do you understand by an array? Explain single dimensional array using
an example.
Q34) Write a Program to input 5 strings and display the same.
Q35) Explain Function Declaration, Function Calling and Function Definition with
example.

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)
9

Q36) Explain array within structure using example.


Q37) Explain Array of structures using an example.
Q38) Explain Nested Structure.
Q39) What is enumerated data type?
Q40) Explain pointer to function with suitable example.
Q41) Write a program to show read and write operation on a file in C.
Q42) Explain fseek and ftell .
Q43) What are preprocessor directives?
Q44) Write short note on getch(), getche(), putchar, getchar().
Q45) What do you understand by multidimensional array? Write a program to search
an element in an array.
Q46) What is call by Value and Call by reference in C Programming ?
Q47) Write short note on typedef statement.
Q48) What is Pointer Arithmetic?
Q49) Write a Program in C to print Fibonacci Series.
Q50) Write a program in C to print all prime numbers from 1 to 100.
Q51) Write a C Program to sort an array elements.
Q52) Write a program in c to demonstrate Command line arguments.
Q53) Write a program in C to find sum of numbers from 1 to n
Q54) What is prototyping? Explain with example.
Q55) What is the difference between scanf and getchar statement
Q56) Write a program in C to add two n X m matrices.
Q57) What is sizeof operator?
Q58) What is a pointer? Describe the application of pointers in a C program
Q59) What is a dynamic array? Show with an example how to create a dynamic
array in C.
Q60) Write a program in C language to copy content of one file to another file.
Q61) What is a file? How does append mode differ from write mode?
Q62) Describe the following functions: fseek(), ftell() and rewind()
Q63) Explain different types of keywords and identifiers in C.
Q64) Write a C program to check if a number is palindrome or not.

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)
10

Q65) Write a C program to find greatest of three numbers.


Q66) Write a program to print following :
*******
*****
***
*
Q67) Write a program to read 4 digit number and find average of those digits.
Q68) What is the use of goto statement? Explain with program.
Q69) What is a Macro? Explain parameterized and non parameterized macros.
Q70) Explain about fprintf and fscanf function.
Q71) What is type casting? Explain various types of type casting.
Q72) Explain basic structure of C Program.
Q73) Write a peogram in C to concatenate two strings.
Q74) What is an expression? Explain the precedence of operators and associativity.
Q75) Explain formatted console i/o function. Also write various format specifiers used
with these function.

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)

You might also like