0% found this document useful (0 votes)
94 views11 pages

Document 1

The document contains a comprehensive set of programming questions and answers related to C programming, covering topics such as program structure, input/output, operators, conditional statements, loops, arrays, strings, functions, recursion, and memory management. Each section provides clear definitions, examples, and explanations to help understand fundamental concepts in C. It serves as a valuable resource for beginners and those looking to reinforce their knowledge of C programming.

Uploaded by

Yash Tiwari
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)
94 views11 pages

Document 1

The document contains a comprehensive set of programming questions and answers related to C programming, covering topics such as program structure, input/output, operators, conditional statements, loops, arrays, strings, functions, recursion, and memory management. Each section provides clear definitions, examples, and explanations to help understand fundamental concepts in C. It serves as a valuable resource for beginners and those looking to reinforce their knowledge of C programming.

Uploaded by

Yash Tiwari
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

General Programming Questions

1. What is the structure of a C program?

- It consists of preprocessor directives, the main function, variable declarations, statements, and
a return statement.

2. What is `#include <stdio.h>` and why is it used?

- It includes the standard input-output library for functions like `printf()` and `scanf()`.

3. What is the role of `main()` function in C?

- It serves as the entry point of a C program where execution begins.

4. What is `printf()` and how is it used?

- It is used to print output on the console.

5. What does `return 0;` signify in a C program?

- It indicates successful execution of the program.

Basic Input/Output and Operators

6. How do you take user input in C?

- By using `scanf()`, which reads input from the user.

7. What is `scanf()` and how does it work?

- It takes formatted input and stores it in a variable.

8. What are different types of operators in C?

- Arithmetic, relational, logical, bitwise, assignment, and special operators.

9. What is the difference between `=` and `==` in C?

- `=` is an assignment operator, while `==` is a comparison operator.


10. How do you find the cube of a number in C?

- By multiplying the number three times.

Conditional Statements

11. What are conditional statements in C?

- Statements that control program flow based on conditions, like `if`, `if-else`, and `switch`.

12. What is the syntax of an `if` statement?

- It checks a condition and executes a block if the condition is true.

13. How does an `if-else` statement work?

- It executes one block if the condition is true and another if it is false.

14. What is the difference between `if-else` and `else-if` ladder?

- `if-else` has one alternative, while `else-if` allows multiple conditions.

15. Write a program to check if a number is even or odd.

- Use the modulus operator to check divisibility by 2.

Loops and Iterative Statements

16. What are the different types of loops in C?

- `for`, `while`, and `do-while` loops.

17. What is the syntax of a `for` loop?

- It consists of initialization, condition, and increment/decrement.

18. What is the syntax of a `while` loop?

- It executes a block while a condition remains true.


19. What is a `do-while` loop and when is it used?

- It executes at least once before checking the condition.

20. How does a `while` loop differ from a `do-while` loop?

- `while` checks the condition first, while `do-while` runs once before checking.

21. Write a program to print the multiplication table of a number.

- Use a loop to multiply and print the values.

Switch Case Statements

22. What is a `switch` statement in C?

- A multi-way branching statement based on a variable's value.

23. How does a `switch` statement differ from `if-else`?

- `switch` is used for multiple fixed conditions, while `if-else` is for dynamic conditions.

24. What happens if we don't use a `break` statement in a `switch` case?

- Execution continues to the next case.

25. Write a program to display messages based on different input values using `switch`.

- Use `switch` with `case` labels and `break` statements.

Arrays

26. What is an array in C?

- A collection of elements of the same data type stored in contiguous memory.

27. How do you declare and initialize a 1D array?

- By specifying the data type, array name, and values.


28. How do you declare and initialize a 2D array?

- By defining rows and columns in a matrix format.

29. Write a program to take input from the user and store it in an array.

- Use a loop to take input for each index.

30. How do you access elements of an array using loops?

- Iterate over the array using a loop.

String Handling

31. What is a string in C?

- A sequence of characters stored as an array.

32. How is a string stored in memory?

- As a character array ending with `\0`.

33. What are some common string functions in C?

- `strcpy()`, `strcat()`, `strlen()`, `strcmp()`, `strchr()`, `strstr()`.

34. What is the difference between `strcpy()` and `strcat()`?

- `strcpy()` copies a string, while `strcat()` appends one string to another.

35. What does `strlen()` function do?

- It returns the length of a string.

36. Write a program to demonstrate string concatenation.

- Use `strcat()` to join two strings.

Functions and Recursion


37. What is a function in C?

- A block of reusable code that performs a specific task.

38. What are the different types of functions?

- Built-in functions and user-defined functions.

39. What is the difference between function declaration and function definition?

- Declaration tells the compiler about a function, definition provides its implementation.

40. What is recursion? Give an example.

- A function that calls itself to solve a problem.

41. Write a recursive function to find the factorial of a number.

- Multiply the number by the factorial of `n-1` until `n=1`.

42. How does a recursive function work internally?

- It pushes function calls onto the stack until a base condition is met.

43. Write a recursive function to find the nth Fibonacci number.

- Return `n` for `n=0` or `n=1`, otherwise sum the previous two Fibonacci numbers.

Call by Value & Call by Reference

44. What is the difference between call by value and call by reference?

- Call by value passes a copy, call by reference passes the actual address.

45. Explain call by value with an example.

- The function gets a copy of the variable, changes don’t affect the original.

46. Explain call by reference with an example.


- The function gets a reference to the variable, changes reflect in the original variable.

47. What happens when a function is called using call by reference?

- The original variable is modified inside the function.

48. When should we use call by reference instead of call by value?

- When modifying the original value or handling large data structures.

Miscellaneous

49. What is the difference between `while` and `for` loops?

- `for` is used when iterations are known, `while` is used when conditions are evaluated
dynamically.

50. What is the use of the `break` statement in loops?

- It terminates the loop immediately.

51. What is the use of the `continue` statement in loops?

- It skips the current iteration and continues with the next.

52. What is an infinite loop? Give an example.

- A loop that runs indefinitely due to an always-true condition.

53. How do you print a 2D array in matrix form?

- Use nested loops to access rows and columns.

54. Write a program to check if a person is eligible to vote.

- Check if the age is 18 or above.

55. Write a program to find the largest of three numbers using `if-else`.

- Compare numbers using `if-else`.


56. Explain the difference between a pointer and an array.

- An array holds values, while a pointer stores the address of a value.

57. How does memory allocation work in an array?

- Memory is allocated in a contiguous block.

58. How can we store multiple characters in a string without using an array?

- Using pointers.

59. What are the differences between an array and a structure in C?

- An array holds elements of the same type, a structure can hold multiple data types.

60. What is the difference between `printf` and `puts` for printing strings?

- `printf` allows formatting, `puts` prints with a newline.

That's a lot of questions, but I'll provide detailed answers for each one in an easy-to-copy format.

ARRAY THEORY QUESTIONS

Basic Array Questions

1. What is an array? Why is it used?

• An array is a collection of elements of the same data type stored in contiguous memory
locations.

• It is used to store multiple values in a single variable, making it easier to manage large
amounts of data.

2. How is an array declared in C?

• An array is declared by specifying the data type, array name, and size.

• Example: int arr[5];

3. How do you initialize an array in C?

• Arrays can be initialized in multiple ways:

o At declaration: int arr[3] = {1, 2, 3};

o Without specifying size: int arr[] = {1, 2, 3};

4. How does an array differ from a normal variable?


• A normal variable stores only one value at a time.

• An array can store multiple values under a single name.

5. How are arrays stored in memory?

• Arrays are stored sequentially in memory, meaning the elements are placed in adjacent
memory locations.

6. What is the size of an array? How is it determined?

• The size of an array is determined by:


Size = Number of elements × Size of each element

• Example: int arr[5]; takes 5 × 4 = 20 bytes (if an int is 4 bytes).

7. Why does an array index always start from zero?

• The index represents an offset from the base memory address.

• The first element is at offset 0, so indexing starts from 0.

8. What is the difference between static and dynamic arrays?

• Static arrays: Size is fixed at compile time.

• Dynamic arrays: Size can be changed at runtime using malloc() or calloc().

STRING THEORY QUESTIONS

31. What is a string in C? How is it different from a character array?

• A string is a sequence of characters ending with a null character (\0).

• A character array is simply an array of characters, but it may or may not be null-
terminated.

32. How do you declare a string in C?

• char str[10]; (declares a string of length 10).

33. How is a string stored in memory?

• Strings are stored as character arrays with an extra \0 at the end.

• Example: "HELLO" is stored as { 'H', 'E', 'L', 'L', 'O', '\0' }.

34. What is the role of \0 in strings?

• It marks the end of the string, preventing unwanted characters from being read.

35. How do you initialize a string?

• char str[] = "Hello"; (Automatically adds \0).

36. What are the different ways to take input for a string?

• Using scanf("%s", str); (reads until a space).


• Using gets(str); (reads a full line).

37. How does gets() differ from scanf()?

• scanf() stops at whitespace, while gets() reads until a newline.

38. What happens if a string is not null-terminated?

• The program keeps reading until it finds a \0, causing unexpected output.

39. How do you print a string using printf() and puts()?

• printf("%s", str);

• puts(str); (adds a newline automatically).

40. What is the difference between puts() and printf()?

• puts() automatically adds a newline (\n).

• printf() does not unless explicitly specified (\n).

LOOP THEORY QUESTIONS

61. What is a loop in C?

• A loop is a control structure that repeats a block of code until a condition is met.

62. What are the different types of loops in C?

1. For loop – Used when the number of iterations is known.

2. While loop – Used when the number of iterations is unknown.

3. Do-while loop – Executes at least once, then checks the condition.

63. What is the difference between entry-controlled and exit-controlled loops?

• Entry-controlled loops (for, while) check the condition before executing the loop body.

• Exit-controlled loops (do-while) execute the body at least once, then check the condition.

64. What is an infinite loop? Give an example.

• A loop that never ends due to a condition that always remains true.

• Example:

• while(1) {

• printf("This will never end.");

• }

65. How do you avoid an infinite loop?

• Ensure that loop conditions are properly defined and updated inside the loop.

66. What is a for loop?


• A loop that executes a fixed number of times.

• Syntax: for(initialization; condition; increment/decrement) { // Code }

67. How does a for loop work?

• It has three parts:

o Initialization (int i = 0;)

o Condition (i < 5;)

o Increment (i++)

68. How is a for loop different from a while loop?

• for is used when the number of iterations is known.

• while is used when the number of iterations is unknown.

69. Can a for loop run infinitely? How?

• Yes, if the condition is always true.

• for(;;) {

• printf("Infinite loop");

• }

70. What happens if we omit the condition in a for loop?

• The loop becomes infinite unless a break statement is used.

71. What is a nested for loop?

• A loop inside another loop.

• Example:

• for(int i=0; i<3; i++) {

• for(int j=0; j<3; j++) {

• printf("%d%d ", i, j);

• }

• }

72. What is a while loop?

• A loop that runs while a condition is true.

• Syntax:

• while(condition) {

• // Code

• }
73. How does a while loop work?

• It checks the condition first and executes only if the condition is true.

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

• while loop checks the condition first.

• do-while executes at least once before checking the condition.

75. Can a while loop run infinitely? How?

• Yes, if the condition is always true (while(1);).

76. What is the purpose of the break statement in loops?

• It terminates the loop immediately.

77. What is the purpose of the continue statement?

• It skips the current iteration and moves to the next.

78. How do loops help in working with arrays?

• Loops allow iterating through array elements efficiently.

This is a complete detailed answer set for all possible theory questions on Arrays, Strings, and
Loops in C.

Let me know if you need more examples or explanations!

You might also like