0% found this document useful (0 votes)
4 views13 pages

C Important Questions

Uploaded by

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

C Important Questions

Uploaded by

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

PC IMPORTANT QUESTTIONS

1)What is the difference between low level and high level language.

1)

LOW LEVEL LANGUAGE HIGH LEVEL LANGUAGE


a)Low level language is high memory efficient a)High level language is less memory efficient
b)It cannot be ported b)It can be ported from one loca on to other
c)It require assembler to translate the code c)It require compiler to translate the code
d)It is difficult to debug d)It is easy to debug
e)it is not easily readable. e)It is easily readable
Example:Assembly Language example :C++

2)What is the Difference between Structure and Union.

2)

STRUCTURE UNION
a)To define structure struct keyword is used a)To define union Union Keyword is used.
b)At any me,any member can be accessed by b)At any me only one member can be accessed
the structure. by the union.
c)Member of structure do not share memory and c)Member of union can share memory and there
need separate memory for all its member. is no need to allocate separate memory to all its
members.
d)All member of the structure can be ini alized. d)Only the first member of the union can be
ini alized.
e)struct Structurename{ e)union Unionname{
statement 1; statement 1;
}structvariablename; }unionvariablename;
3)What is flowchart explain all type of symbols used in flowchart.

3) Flowchart is a graphical representation of an algorithm. Programmers often use it as a program-planning tool to solve a
problem. It makes use of symbols which are connected among them to indicate the flow of information and processing.

4)What is Linker ,loader and compiler.

4) a) Compiler:

1)A compiler is a program that translates the source code wri en in C (or any other programming language)
into machine code or an intermediate representa on.

2) It performs lexical analysis, syntax analysis, and code op miza on to generate an executable program or
object code.

3)The compiler checks for syntax errors and type checking during this process and produces object files that
contain the compiled code but are not yet executable.

b)Linker:

1)The linker is a separate tool that takes one or more object files (produced by the compiler) and combines
them to create a single executable program or library.

2) It resolves references to func ons and variables between different object files, ensuring that the code can
be executed as a complete program.

-3)The linker also performs addi onal tasks like crea ng symbol tables, managing memory addresses, and
op mizing the final binary.

c) Loader:

1)The loader is responsible for loading the executable program into memory when it is run by the user. It
prepares the program for execu on.

2)It allocates memory for the program, resolves references to dynamic libraries, and sets up the execu on
environment.

3)The loader loads the program from its stored format (usually an executable file on disk) into memory so
that it can be run by the opera ng system.
5)Explain format specifiers in c language.

5) In C programming, a format specifier is a special sequence used within forma ng func ons, such as `prin `
and `scanf`, to specify how data should be input or output. It tells these func ons how to interpret and display
the data. Format specifiers are preceded by the '%' character and are followed by a le er or le ers that
indicate the data type of the argument. Here are some common format specifiers:

1. `%d`: Used for forma ng and prin ng integers.

2. `%f`: Used for forma ng and prin ng floa ng-point numbers.

3. `%c`: Used for forma ng and prin ng characters.

4. `%s`: Used for forma ng and prin ng strings.

5. `%x`: Used for forma ng and prin ng integers in hexadecimal (base 16) format.

6. `%o`: Used for forma ng and prin ng integers in octal (base 8) format.

7. `%u`: Used for forma ng and prin ng unsigned integers.

6)What is the difference between Entry and Exit control loops.Explain all the there basic loops with syntax.

6)

1. Entry-Controlled Loop:

- An "entry-controlled" loop is another term for a loop that checks the loop condi on before execu ng the
loop body. The most common example of an entry-controlled loop is the `while` loop.

- It evaluates the loop condi on first, and if the condi on is true, it enters the loop body. If the condi on is
false from the beginning, the loop body is never executed.

while (condi on) {

// Loop body

2. Exit-Controlled Loop:

- An "exit-controlled" loop is another way of describing a loop that checks the loop condi on a er execu ng
the loop body. The most common example of an exit-controlled loop is the `do-while` loop.

- In this case, the loop body is executed at least once, and then the condi on is checked to determine
whether to con nue or exit the loop.

do {

// Loop body

} while (condi on);

1. While Loop:

- A `while` loop repeatedly executes a block of code as long as a given condi on is true.

- It checks the condi on before entering the loop, so it's possible that the loop may not execute at all if the
condi on is false from the beginning.

while (condi on) {

// Code to be executed while the condi on is true


}

2. For Loop:

- A `for` loop provides a way to specify ini aliza on, condi on, and itera on expressions in a single line.

- It's typically used when you know in advance how many mes you want the loop to run.

for (ini aliza on; condi on; itera on) {

// Code to be executed as long as the condi on is true

3. Do-While Loop:

- A `do-while` loop is similar to a `while` loop, but it guarantees that the code block is executed at least once
because it checks the condi on a er the code block.

do {

// Code to be executed at least once

} while (condi on);

These control structures allow you to control the flow of your program by itera ng over a block of code
mul ple mes, as long as a certain condi on is met.

7)Explain different types of control structure statements.

7)

1. if Statement:

- The `if` statement is used for condi onal execu on. It allows you to execute a block of code if a specified
condi on is true.

if (condi on) {

// Code to execute when the condi on is true

2. Else Statement:

- The `else` statement is used in combina on with the `if` statement to specify an alterna ve block of code to
execute if the condi on in the `if` statement is false.

if (condi on) {

// Code to execute when the condi on is true

} else {

// Code to execute when the condi on is false

3. Nested if-else:

- You can nest `if` and `else` statements inside one another to create more complex condi onal logic.

if (condi on1) {
// Code to execute when condi on1 is true

} else if (condi on2) {

// Code to execute when condi on2 is true

} else {

// Code to execute when all condi ons are false

4. Switch Statement:

- The `switch` statement allows you to select one of many code blocks to be executed based on the value of
an expression.

- It provides a way to simplify mul ple `if-else` statements when you have a limited number of discrete cases.

switch (expression) {

case value1:

// Code for value1

break;

case value2:

// Code for value2

break;

default:

// Code for when none of the cases match

5. Break Statement:

- The `break` statement is used inside loops and the `switch` statement to exit the loop or switch early.

- It is o en used to terminate the loop when a certain condi on is met or to exit a `switch` statement once a
case is matched.

6. Con nue Statement:

- The `con nue` statement is used within loops to skip the current itera on and move to the next itera on.

- It allows you to avoid execu ng the remaining code in the current itera on when a specific condi on is met.

8)What is call by reference and call by value.

8)Call by Value:

In call by value, when you pass an argument to a func on, a copy of the argument's value is made, and this
copy is used within the func on. Any changes made to the parameter within the func on do not affect the
original variable that was passed as an argument. Here's a step-by-step explana on:

1. The value of the argument is copied to a new memory loca on, which is the func on's parameter.

2. The func on operates on this local copy of the data.


3. Any modifica ons to the parameter inside the func on do not impact the original variable because they are
isolated.

Example:

void modifyValue(int x) {

x = x * 2;

int main() {

int num = 5;

modifyValue(num); // 'num' is passed by value

// 'num' remains 5, as the func on works on a copy of 'num'

In this case, the `num` variable remains 5 because the func on `modifyValue` operates on a copy of `num`, not
the original `num`.

Call by Reference (using Pointers):

In call by reference, you pass a reference to the memory loca on of the argument to the func on. Instead of
copying the value, the func on directly accesses and modifies the original data. Here's how it works:

1. You pass a pointer to the argument, which contains the memory address where the data is stored.

2. The func on uses this pointer to access and modify the data at the original memory loca on.

3. Any changes made within the func on directly affect the original variable.

Example:

void modifyValueByReference(int* x) {

*x = *x * 2; // Dereference the pointer to access and modify the original data

int main() {

int num = 5;

modifyValueByReference(&num); // Pass a pointer to 'num'

// 'num' is modified and becomes 10

In this case, the `num` variable is modified because the func on `modifyValueByReference` works directly on
the original `num` using a pointer.

9)Define the following :


a)Sta c

b)const

c)preprocessor direc ves

9)1. Sta c:

- The `sta c` keyword in C has different meanings depending on its context:

- When used within a func on, a `sta c` variable retains its value between func on calls. It's local to the
func on but persists across mul ple calls.

- When used with a global variable, `sta c` limits the variable's scope to the file where it's defined, making it
accessible only within that file.

- When used with func ons, a `sta c` func on has internal linkage, meaning it's only visible within the same
transla on unit (file).

2. Const:

- The `const` keyword is used to declare constants in C. It indicates that a variable's value cannot be modified
once it is assigned.

- Constants declared with `const` are o en used for readability, op miza on, and to prevent uninten onal
modifica ons of values.

const int MAX_VALUE = 100;

3. Preprocessor Direc ves:

- Preprocessor direc ves are commands that are processed by the C preprocessor before actual compila on.
They start with a `#` symbol.

- Common preprocessor direc ves include `#include`, `#define`, and condi onal direc ves like `#ifdef`,
`#ifndef`, `#else`, and `#endif`.

- Preprocessor direc ves are used for tasks like including header files, defining macros, condi onal
compila on, and more.

#include <stdio.h>

#define PI 3.14159265359.

#ifdef DEBUG

10)Write a program to check whether a year is leap year or not.

10)

#include <stdio.h>

int main() {

int year;

prin ("Enter a year: ");

scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {

prin ("%d is a leap year.\n", year);

} else {

prin ("%d is not a leap year.\n", year);

return 0;

11)Explain arrays,1-D,2-D and mul dimensional array.

11) Arrays are a fundamental data structure in C and many other programming languages. They are used to
store collec ons of data elements of the same data type. In C, there are one-dimensional (1-D) arrays, two-
dimensional (2-D) arrays, and mul dimensional arrays, each with its own characteris cs:

1. One-Dimensional (1-D) Array:

- A 1-D array is a collec on of elements of the same data type arranged in a linear sequence.

- Each element in a 1-D array is iden fied by its posi on or index.

- The index of the first element is typically 0, and it increases sequen ally.

- Example:

int scores[5]; // A 1-D array of integers with 5 elements

2. Two-Dimensional (2-D) Array:

- A 2-D array is an array of arrays, essen ally crea ng a matrix or grid of elements.

- It is organized into rows and columns.

- You access elements in a 2-D array using two indices, one for the row and one for the column.

Example:

int matrix[3][3]; // A 3x3 2-D array of integers

3. Mul dimensional Array:

- A mul dimensional array refers to arrays with more than two dimensions.

- These can be thought of as an extension of 2-D arrays, with mul ple layers.

- In prac ce, 2-D arrays are the most common, but you can have 3-D, 4-D, or higher-dimensional arrays.

- Accessing elements in mul dimensional arrays requires specifying the indices for each dimension.

Example:

int cube[2][3][4]; // A 3-D array with dimensions 2x3x4

12)Expalin the following:

a)Iden fier

b)keywords

3)Tokens
12)

a) Iden fier:

- An iden fier is a name given to a variable, func on, constant, array, or any other user-defined item in a C
program.

- Iden fiers are used to uniquely iden fy these program elements, allowing you to refer to them in your code.

- Rules for valid C iden fiers:

- Must begin with a le er (uppercase or lowercase) or an underscore (_).

- Can be followed by le ers, digits, or underscores.

- Case-sensi ve: "myVariable" and "myvariable" are treated as different iden fiers.

- Should not use C keywords (explained in the next point) as iden fiers.

- Examples of valid iden fiers: `counter`, `_value`, `myVariable1`

b) Keywords:

- Keywords, also known as reserved words, are a set of predefined words in the C programming language.

- These words have specific meanings and are reserved for specific purposes within the language.

- You cannot use keywords as iden fiers for variables, func ons, or other user-defined en es.

- Examples of C keywords include `int`, `for`, `while`, `if`, `else`, `return`, `break`, and many others.

c) Tokens:

- Tokens are the smallest units in a C program, which include iden fiers, keywords, operators, constants, and
symbols.

- Tokens are separated by white spaces, such as spaces and newlines, in the source code.

- C compilers use tokens to analyze and understand the structure of your code during the compila on
process.

- Examples of C tokens:

- Keywords: `if`, `for`, `while`

- Iden fiers: `variableName`, `myFunc on`

13)Write a program to swap two number using call by reference.

13)

#include <stdio.h>

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

int main() {
int num1, num2;

prin ("Enter the first number: ");

scanf("%d", &num1);

prin ("Enter the second number: ");

scanf("%d", &num2);

prin ("Before swapping: num1 = %d, num2 = %d\n", num1, num2);

// Call the swap func on with the addresses of num1 and num2

swap(&num1, &num2);

prin ("A er swapping: num1 = %d, num2 = %d\n", num1, num2);

return 0;

14)Expalin different types of operators in details

14) Operators in C are symbols that perform opera ons on operands, which can be values, variables, or
expressions. They enable you to manipulate data and perform a wide range of tasks. There are various types of
operators in C, and here are the major categories:

1. Arithme c Operators:

- These operators perform mathema cal opera ons on numeric operands.

- Common arithme c operators include `+` (addi on), `-` (subtrac on), `*` (mul plica on), `/` (division), and
`%` (modulo, or remainder).

2. Rela onal Operators:

- Rela onal operators are used to compare two values and determine the rela onship between them. They
return a Boolean result (either true or false).

- Common rela onal operators include `==` (equal to), `!=` (not equal to), `<` (less than), `>` (greater than),
`<=` (less than or equal to), and `>=` (greater than or equal to).

3. Logical Operators:

- Logical operators are used to perform logical opera ons on Boolean values (true or false).

- Common logical operators include `&&` (logical AND), `||` (logical OR), and `!` (logical NOT).

4. Assignment Operators:

- Assignment operators are used to assign values to variables.

- The most basic assignment operator is `=` (assignment), but there are also compound assignment operators
like `+=`, `-=`, `*=`, and `/=`, which perform an opera on and assignment in one step.
5. Increment and Decrement Operators:

- Increment and decrement operators are used to increase or decrease the value of a variable by 1.

- The `++` operator increments, and the `--` operator decrements a variable.

6. Bitwise Operators:

- Bitwise operators perform opera ons at the bit-level. They are o en used in low-level programming.

- Common bitwise operators include `&` (bitwise AND), `|` (bitwise OR), `^` (bitwise XOR), `~` (bitwise NOT),
`<<` (le shi ), and `>>` (right shi ).

7. Condi onal (Ternary) Operator:

- The condi onal operator `? :` is used to return one of two values based on a condi on. It's a shorthand for
an `if-else` statement.

- Example: `condi on ? value_if_true : value_if_false`

15)What is the difference between = and == operator.

15) The `=` operator and the `==` operator serve completely different purposes in C and many programming
languages:

1. `=` Operator (Assignment Operator):

- The `=` operator is the assignment operator.

- It is used to assign a value to a variable.

- When you write `variable = value;`, you are assigning the value on the right side to the variable on the le
side.

- Example: `int x = 5;` assigns the value 5 to the variable `x`.

2. `==` Operator (Equality Operator):

- The `==` operator is the equality operator.

- It is used to compare two values to check if they are equal.

- When you write `expression1 == expression2`, you are tes ng if `expression1` is equal to `expression2`.

- It returns a Boolean value, typically `true` if the expressions are equal and `false` otherwise.

- Example: `if (x == 5)` checks if the value of the variable `x` is equal to 5.

16)Write a program to print factorial of a number.

16) #include <stdio.h>

int main() {

int number;
unsigned long long factorial = 1;

prin ("Enter a non-nega ve integer: ");

scanf("%d", &number);

if (number < 0) {

prin ("Factorial is not defined for nega ve numbers.\n");

else {

for (int i = 1; i <= number; i++) {

factorial *= i;

prin ("Factorial of %d = %llu\n", number, factorial);

return 0;

17)Write a program using array to count the number of even and odd element in the array.

17) #include <stdio.h>

int main() {

int size, evenCount = 0, oddCount = 0;

prin ("Enter the size of the array: ");

scanf("%d", &size);

int numbers[size];

prin ("Enter %d elements:\n", size);

for (int i = 0; i < size; i++) {

scanf("%d", &numbers[i]);

for (int i = 0; i < size; i++) {

if (numbers[i] % 2 == 0) {

evenCount++;

} else {

oddCount++;

}
}

prin ("Even elements: %d\n", evenCount);

prin ("Odd elements: %d\n", oddCount);

return 0;

You might also like