C Language Practice Questions & Assignments

This page contains Multiple Choice Questions (MCQs), short answer questions, programming-based assignment questions, and lab practice exercises commonly asked in university examinations, lab exams, and programming interviews. Practice these questions to strengthen your C programming fundamentals and problem-solving skills.

💡 Exam Tip: Try to answer each question yourself before clicking "Show Answer" or "Show Output". This will strengthen your problem-solving skills and exam speed.

Multiple Choice Questions (MCQs)

These MCQs help you revise basic C syntax, operators, data types, arrays, strings, functions, pointers, and fundamental concepts.

1. What is the output of the following code?

printf("%d", sizeof(int));
  • A.1
  • B.2
  • C.4
  • D.Depends on compiler

2. Which operator has the highest precedence?

  • A.+
  • B.*
  • C.()
  • D.=

3. What is the default return type of a function in C?

  • A.void
  • B.int
  • C.char
  • D.float

4. Which of the following is a valid way to declare a pointer?

  • A.int ptr;
  • B.*int ptr;
  • C.int *ptr;
  • D.int ptr*;

5. What is the size of a char array needed to store "Hello"?

  • A.5
  • B.6
  • C.7
  • D.8

6. Which header file is required for printf() and scanf()?

  • A.<string.h>
  • B.<stdlib.h>
  • C.<stdio.h>
  • D.<math.h>

7. What is the value of 5 % 2?

  • A.2
  • B.1
  • C.0
  • D.2.5

8. Which loop executes at least once?

  • A.for
  • B.while
  • C.do-while
  • D.None of the above

9. What does & operator return?

  • A.Value of variable
  • B.Address of variable
  • C.Size of variable
  • D.Type of variable

10. Which function is used to find string length?

  • A.strlen()
  • B.strcpy()
  • C.strcat()
  • D.strcmp()

11. Array index in C starts from:

  • A.0
  • B.1
  • C.-1
  • D.Depends on declaration

12. What is arr[0] if arr is declared as int arr[5]?

  • A.Last element
  • B.First element
  • C.Invalid
  • D.Second element

13. Which function copies one string to another?

  • A.strlen()
  • B.strcpy()
  • C.strcat()
  • D.strcmp()

14. String in C is terminated by:

  • A.' ' (space)
  • B.'\n' (newline)
  • C.'\0' (null)
  • D.'\t' (tab)

15. What is the maximum size of an array in C?

  • A.100
  • B.1000
  • C.Fixed at compile time
  • D.Depends on memory

Short Answer Questions

Answer these questions in 2-3 lines. These questions test your understanding of C programming concepts and terminology.

1. What is C programming language?

C is a general-purpose, procedural programming language developed by Dennis Ritchie. It is used for system programming, application development, and embedded systems.

2. Explain the difference between = and == operators.

= is assignment operator (assigns value to variable), == is equality comparison operator (compares two values and returns true/false).

3. What is a variable? Give examples.

A variable is a named memory location that stores data value. Examples: int age; float salary; char grade;

4. Explain printf() and scanf() functions.

printf() displays formatted output to console. scanf() reads formatted input from user and stores it in variables.

5. What are format specifiers? Give examples.

Format specifiers define data type for I/O operations. Examples: %d (int), %f (float), %c (char), %s (string), %lf (double).

6. Differentiate between if and switch statement.

if checks boolean conditions (true/false). switch checks multiple discrete values and executes matching case. switch is faster for many cases, if is more flexible.

7. Explain break and continue statements.

break exits loop/switch immediately. continue skips remaining statements in current iteration and continues with next iteration of the loop.

8. What is an array? How is it declared?

Array is collection of similar data elements stored in contiguous memory. Declaration: int arr[10]; (array of 10 integers).

9. Explain string in C.

String is array of characters terminated by null character '\0'. Stored as character array. Example: char str[] = "Hello";

10. What is a function? List its advantages.

Function is block of code performing specific task. Advantages: Reusability, modularity, easier debugging, code organization, abstraction.

11. Explain call by value and call by reference.

Call by value passes copy of variable (original unchanged). Call by reference passes address using pointers (original can be modified).

12. What is a pointer? Why is it used?

Pointer stores address of variable. Used for dynamic memory allocation, arrays, functions, efficient parameter passing, and low-level memory access.

13. Explain NULL pointer and void pointer.

NULL pointer points to nothing (address 0). Void pointer can point to any data type but cannot be dereferenced directly without type casting.

14. What is structure? Give example.

Structure groups related data of different types. Example: struct Student { int roll; char name[50]; float marks; };

15. Differentiate between structure and union.

Structure allocates separate memory for each member (size = sum of members). Union shares memory (size = largest member). Only one union member active at a time.

16. Explain dynamic memory allocation.

Allocating memory at runtime using malloc(), calloc(), realloc(). Memory allocated on heap. Must be freed using free() to avoid memory leaks.

17. What is file handling? List file operations.

File handling is reading/writing data to files. Operations: fopen() (open), fclose() (close), fprintf() (write), fscanf() (read), fread()/fwrite() (binary operations).

18. Explain command line arguments.

Arguments passed to main function: int main(int argc, char *argv[]). argc = count of arguments, argv = array of strings (arguments).

19. What are preprocessor directives?

Instructions processed before compilation. Examples: #include (includes header), #define (defines macro), #ifdef/#endif (conditional compilation).

20. Explain scope and lifetime of variables.

Scope: where variable is accessible (local, global, block). Lifetime: how long variable exists in memory (automatic, static, dynamic).

Output-Based Questions

Predict the output of the following C programs before revealing the answer. These questions test your understanding of code execution flow.

1. What is the output of the following program?

#include <stdio.h>

int main() {
    int x = 5;
    int y = ++x + x++;
    printf("%d %d", x, y);
    return 0;
}

2. What is the output of the following program?

#include <stdio.h>

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    printf("%d", arr[5]);
    return 0;
}

3. What is the output of the following program?

#include <stdio.h>

int main() {
    int a = 10;
    int *ptr = &a;
    printf("%d %d", *ptr, a);
    return 0;
}

4. What is the output of the following program?

#include <stdio.h>

int main() {
    int i;
    for (i = 0; i < 5; i++);
    printf("%d", i);
    return 0;
}

5. What is the output of the following program?

#include <stdio.h>

int main() {
    int x = 10, y = 20;
    if (x = 20) {
        printf("True");
    } else {
        printf("False");
    }
    return 0;
}

Programming / Long Answer Questions

Write complete C programs for these questions. Practice writing code for each question and test it thoroughly.

1. Write a program to find factorial of a number using recursion and iteration.

2. Write a program to check if a number is prime or not. Optimize it.

3. Write a program to find GCD and LCM of two numbers.

4. Write a program to print Fibonacci series up to n terms.

5. Write a program to check if a string is palindrome or not.

6. Write a program to sort an array using bubble sort algorithm.

7. Write a program to perform matrix addition and multiplication.

8. Write a program to implement linear search and binary search.

9. Write a program to count vowels, consonants, digits in a string.

10. Write a program to reverse a string without using library function.

11. Write a program to swap two numbers using pointers.

12. Write a program to demonstrate use of structures (Student database).

13. Write a program to dynamically allocate memory for array and perform operations.

14. Write a program to read from file and write to another file.

15. Write a program to count words, lines, characters in a text file.

16. Write a program to implement stack using array.

17. Write a program to find second largest element in array.

18. Write a program to remove duplicates from array.

19. Write a program to merge two sorted arrays.

20. Write a program using command line arguments to perform calculator operations.

Lab Practice Questions

Organized by lab sessions, these questions cover basic programs to advanced concepts progressively.

Lab 1: Basic Programs

  1. Program to display "Hello World"
  2. Program to add two numbers
  3. Program to find area of circle
  4. Program to swap two numbers
  5. Program to calculate simple interest

Lab 2: Control Statements

  1. Program to find largest of three numbers
  2. Program to check even or odd
  3. Program to check leap year
  4. Program to print day name using switch
  5. Program to check alphabet, digit or special character

Lab 3: Loops

  1. Program to print multiplication table
  2. Program to find sum of digits
  3. Program to reverse a number
  4. Program to check Armstrong number
  5. Program to print patterns (star, number patterns)

Lab 4: Arrays

  1. Program to find largest and smallest in array
  2. Program to reverse an array
  3. Program to merge two arrays
  4. Program to find frequency of elements
  5. Program for matrix transpose

Lab 5: Functions

  1. Program with functions for mathematical operations
  2. Program to check prime using function
  3. Program to find factorial using recursion
  4. Program with function overloading concepts
  5. Program using static variables

Lab 6: Pointers and Strings

  1. Program to swap using pointers
  2. Program to access array using pointers
  3. Program for string operations (copy, compare, concatenate)
  4. Program to count words in string
  5. Program to remove spaces from string

Lab 7: Structures

  1. Program for student record management
  2. Program for employee database
  3. Program using nested structures
  4. Program with array of structures
  5. Program using structure pointers

Lab 8: File Handling

  1. Program to create and write to file
  2. Program to read from file
  3. Program to copy file contents
  4. Program to append data to file
  5. Program for student data file operations

Assignment Questions for Students

Weekly assignments organized by topics to help you practice systematically throughout the semester.

Assignment 1: Basic Concepts (Week 1-2)

  1. Write programs demonstrating all operators in C
  2. Create a simple calculator program
  3. Write programs for all control structures
  4. Document all programs with comments

Assignment 2: Arrays and Strings (Week 3-4)

  1. Implement sorting algorithms (bubble, selection, insertion)
  2. Implement searching algorithms (linear, binary)
  3. Create string manipulation programs
  4. Programs on 2D arrays and matrices

Assignment 3: Functions and Pointers (Week 5-6)

  1. Create a library of utility functions
  2. Programs demonstrating pointer arithmetic
  3. Programs on call by value vs call by reference
  4. Recursive problem solving

Assignment 4: Advanced Concepts (Week 7-8)

  1. Student management system using structures
  2. File handling application
  3. Dynamic memory allocation programs
  4. Mini project combining all concepts

📝 Study Tip

Practice these questions regularly. Start with MCQs and short answers, then move to programming questions. Write code for each programming question and test it thoroughly. Understanding concepts is more important than memorizing.