Turning Learners Into Developers
Codekilla
CODEKILLA
back to course
Lesson 39 / 39100%· free preview
Exam Prep2/2

Viva Questions

Master C viva questions with clear answers, practical code, and tips to ace your interviews.

Common Viva Questions and Answers

Here you'll find a curated list of popular C language viva questions, with succinct answers, explanation tables, and short practical code snippets.

1. What is the difference between int main() and void main()?
  • int main(): Standard and recommended; returns a value to the OS.
  • void main(): Not standard; used in older compilers like Turbo C.
  • Always use: int main()
2. What happens if we don’t write return 0; in main()?
  • In modern C, the compiler automatically adds return 0, so the program works as expected.
3. Why is C called a middle-level language?
  • Supports low-level features like memory access via pointers.
  • Supports high-level features like functions and loops.
4. What is the output?
c
#include <stdio.h>
int main() {
    printf("%d", printf("Hello"));
    return 0;
}
Output
text
Hello5
  • printf("Hello") prints 'Hello' and returns 5 (the string's length), so the outer printf prints 5.
5. What is the output?
c
#include <stdio.h>
int main() {
    int a = 5;
    printf("%d %d %d", a, a++, ++a);
    return 0;
}
Output
text
Undefined behavior
  • The variable a is modified more than once between sequence points. Output depends on the compiler and is not predictable.
6. What is a dangling pointer?
  • A pointer pointing to memory that has been freed or deleted.
7. What is the difference between declaration and definition?
DeclarationDefinition
Declares variableAllocates memory
Example: int a;Example: int a=10;
8. What is the output?
c
#include <stdio.h>
int main() {
    int x = 10;
    if (x = 5)
        printf("Yes");
    else
        printf("No");
    return 0;
}
Output
text
Yes
  • x = 5 is assignment, not comparison (==). Assignment returns 5 (true), so 'Yes' is printed.

Common Mistake: Use == for comparison.

Correct Version:

c
#include <stdio.h>
int main() {
    int x = 10;
    if (x == 5)
        printf("Yes");
    else
        printf("No");
    return 0;
}
  • Tip: Always use == in if when comparing values.
9. What is the difference between ++i and i++?
++ii++
Pre-incrementPost-increment
First increase, then printFirst print, then increase
10. What is the size of int?
  • Depends on the system/compiler. Usually 4 bytes.
11. What is the output?
c
#include <stdio.h>
int main() {
    char ch = 'A';
    printf("%d", ch);
    return 0;
}
Output
text
65
  • ASCII value of 'A'.
12. Can we access an array out of bounds?
  • Yes, but it is dangerous: may give garbage values or cause a crash.
13. What is the difference between break and continue?
StatementMeaningExample CodeOutput
breakExit loop completelyif(i == 3) break;1 2
continueSkip current iterationif(i == 3) continue;1 2 4 5
14. What is the output?
c
#include <stdio.h>
int main() {
    int a = 0;
    if (a)
        printf("True");
    else
        printf("False");
    return 0;
}
Output
text
False
  • In C, 0 is equivalent to false.
15. What is NULL?
  • A pointer that points to nothing (address 0).
16. What is the difference between malloc() and free()?
  • malloc(): Allocates memory.
  • free(): Releases allocated memory.
17. What is the output?
c
#include <stdio.h>
int main() {
    printf("%d", sizeof('A'));
    return 0;
}
Output
text
4
  • In C, character literals are treated as int.
18. What is call by value and call by reference?
c
#include <stdio.h>
// Call by Value
void swapValue(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
}
// Call by Reference
void swapRef(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}
int main() {
    int x = 5, y = 10;
    swapValue(x, y);
    printf("After Call by Value: x=%d y=%d\n", x, y); // x=5 y=10
    swapRef(&x, &y);
    printf("After Call by Reference: x=%d y=%d", x, y); // x=10 y=5
    return 0;
}

Memory trick: Value - copy (no change). Reference - address (change happens).

FeatureCall by ValueCall by Reference
What is passedCopy passedAddress passed
Effect on originalNo changeChanges original
Example Codeswap(x, y);swap(&x, &y);
Function Logicvoid swap(int a, int b)void swap(int *a, int *b)
Outputx=5, y=10x=10, y=5
19. What is stack and heap memory?
  • Stack: Automatic memory for local variables.
  • Heap: Dynamic memory allocated using malloc() and free().
20. What is segmentation fault?
  • Accessing invalid memory causes a program crash (segmentation fault).
c
#include <stdio.h>
int main() {
    int *ptr = NULL;
    printf("%d", *ptr); // Invalid memory access
    return 0;
}
21. What is the output?
c
#include <stdio.h>
int main() {
    int a = 5;
    printf("%d", a+++a);
    return 0;
}
Output
text
11
  • Interpreted as: (a++) + a → 5 + 6 = 11
22. Can we compile a program without main()?
  • No; for execution, main() is required.
23. What is volatile keyword?
  • Informs compiler not to optimize variable as its value may change unexpectedly.
24. What is the output?
c
#include <stdio.h>
int main() {
    int a = 3;
    printf("%d", a << 1);
    return 0;
}
Output
text
6
  • Left shift operator multiplies the value by 2.
Key Takeaways
  1. Use int main() for standard C programs.
  2. Be careful with assignments and comparisons inside conditions.
  3. Always release dynamically allocated memory with free().
  4. Misusing array indices or uninitialized pointers may lead to crashes.
  5. Understanding call by value vs. reference is vital for debugging code effects.
Interview Questions

Practice Questions
  1. Write a C function to show the difference between call by value and call by reference.
  2. Predict the output: int x = 0; if (x) printf("One"); else printf("Zero");
  3. Explain what happens when you use break and continue in a loop.
  4. What is the output of printf("%d", sizeof('B'))?
  5. Show a C code example leading to a segmentation fault.
Pro Tips
  1. Always use int main() and include a return statement.
  2. Use == for comparisons and = for assignments.
  3. Avoid modifying variables more than once in a single expression.
  4. Never dereference NULL or uninitialized pointers.
  5. Release all dynamically allocated memory using free().
  6. Understand how pre- and post-increment operators work.
AI-powered recap

Quick recap quiz?

We'll generate 5 MCQs from this lesson and check your understanding instantly. Takes ~30 seconds.

# program

Program

C
#include <stdio.h>
// Call by Value
void swapValue(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
}
// Call by Reference
void swapRef(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}
int main(void) {
    int x = 5, y = 10;
    swapValue(x, y);
    printf("After Call by Value: x=%d y=%d\n", x, y); // x=5 y=10
    swapRef(&x, &y);
    printf("After Call by Reference: x=%d y=%d\n", x, y); // x=10 y=5
    return 0;
}
Ready to move on?
// example library
Want more hands-on snippets in C?
Browse 360 runnable examples · across 36 chapters · short, copy-paste-friendly · grouped by topic
Explore examples
// suggested companion course
Data Structures with C
Comfortable with C? Level up — arrays · linked lists · stacks · queues · trees · graphs · sorting algorithms.
Open course
// did you know?
One surprising fact before your next lesson
Bite-sized programming facts that make your next coffee-break explanation land.
Did You Know
// feedback.matters()
Did this lesson help you?