0% found this document useful (0 votes)
20 views4 pages

C 2marks - Unit1 & 2

C

Uploaded by

sshafick748
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)
20 views4 pages

C 2marks - Unit1 & 2

C

Uploaded by

sshafick748
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/ 4

UNIT I: BASICS OF C PROGRAMMING

1. What is a programming paradigm?

o Answer: A programming paradigm is a style or way of programming that provides


the structure and elements of programs. Examples include procedural, object-
oriented, and functional programming paradigms.

2. List two applications of C language.

o Answer:

1. System programming (e.g., operating systems).

2. Embedded systems programming (e.g., microcontroller firmware).

3. What is the structure of a C program?

o Answer: The basic structure of a C program includes:

1. Preprocessor directives.

2. Global declarations (optional).

3. main() function.

4. Function definitions (optional).

4. Define a constant in C.

o Answer: A constant is a fixed value that cannot be changed during the execution of a
program. It can be defined using the const keyword or #define preprocessor
directive.

5. What is an enumeration constant?

o Answer: An enumeration constant is a user-defined data type that assigns names to


a set of integer values, making the code more readable. It is defined using the enum
keyword.

6. What are keywords in C?

o Answer: Keywords are reserved words in C that have predefined meanings and
cannot be used for other purposes like variable names. Examples include int, return,
if.

7. What is operator precedence?

o Answer: Operator precedence defines the order in which different operators in an


expression are evaluated. For example, * has higher precedence than +.

8. What is operator associativity?

o Answer: Operator associativity defines the direction in which operators of the same
precedence are evaluated. For example, + and - are left-associative, so they are
evaluated from left to right.

9. Give an example of an input/output statement in C.


o Answer:

▪ Input statement: scanf("%d", &num);

▪ Output statement: printf("Value is %d", num);

10. What is a switch statement?

o Answer: A switch statement is a multi-way branch that allows a variable to be tested


for equality against a list of values (cases), and specific code is executed depending
on which value matches.

11. Give an example of a preprocessor directive.

o Answer: #include <stdio.h>

#define PI 3.14

12. What is the compilation process in C?

o Answer: The compilation process involves:

1. Preprocessing: Handles directives (e.g., #include).

2. Compilation: Converts code to assembly.

3. Assembly: Converts assembly to machine code.

4. Linking: Combines object files and libraries into an executable.

13. What are decision-making statements in C?

o Answer: Decision-making statements allow the execution of specific code based on


conditions. Examples include if, if-else, and switch.

14. What is a looping statement?

o Answer: Looping statements allow repeated execution of a block of code. Common


looping statements in C are for, while, and do-while.

15. Write syntax for “for loop”

o Answer:

for (initialization; condition; increment/decrement) {

// statements

16. Write syntax for “while loop”

o Answer:

while (condition) {

// Loop body (code to be executed)

}
17. Write syntax for “do-while loop”

o Answer:

do {

// Loop body (code to be executed)

} while (condition);

18. What is the difference between while and do-while loop?

o Answer: A while loop checks the condition before executing the loop body, whereas
a do-while loop executes the body first and checks the condition afterward, ensuring
the loop runs at least once.

UNIT II: ARRAYS AND STRINGS

1. What is an array in C?

o Answer: An array is a collection of elements of the same data type, stored in


contiguous memory locations, and accessed using an index.

2. How is a one-dimensional array declared and initialized?

o Answer:

▪ Declaration: int arr[5];

▪ Initialization: int arr[5] = {1, 2, 3, 4, 5};

3. How do you declare a two-dimensional array?

o Answer: A two-dimensional array is declared as: int arr[3][4];, where 3 is the number
of rows and 4 is the number of columns.

4. What is the syntax to access elements in a two-dimensional array?

o Answer: The syntax to access elements in a 2D array is: arr[i][j], where i is the row
index and j is the column index.

5. What does the strlen() function do?

o Answer: The strlen() function returns the length of a string, excluding the null
terminator (\0).

6. What does the strcmp() function do?

o Answer: The strcmp() function compares two strings lexicographically. It returns 0 if


the strings are equal, a negative value if the first string is less, and a positive value if
the first string is greater.

7. How do you concatenate two strings in C?

o Answer: You can concatenate two strings using the strcat() function. Example:

char str1[20] = "Hello ";


char str2[] = "World!";

strcat(str1, str2);

8. How do you copy one string to another in C?

o Answer: You can copy one string to another using the strcpy() function. Example:

char dest[20];

char src[] = "Copy this";

strcpy(dest, src);

9. What is selection sort?

o Answer: Selection sort is a sorting algorithm that repeatedly selects the smallest (or
largest) element from the unsorted part of the array and swaps it with the first
element of the unsorted part.

10. What is linear search?

o Answer: Linear search is a searching algorithm that sequentially checks each


element of the array until the target element is found or the end of the array is
reached.

11. How do you initialize a character array in C?

o Answer: A character array can be initialized as follows:

char str[] = "Hello";

12. List some of the basic string functions.


- strlen()
- strcmp()
- strcat()
- strcpy()

You might also like