0% found this document useful (0 votes)
35 views

Viva Question

Uploaded by

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

Viva Question

Uploaded by

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

Viva Question.

Note: Read both the Assignment for Viva.


1. Why is the main() function necessary in every C program?
The main() function is the entry point of a C program. It is where the program
starts executing. Every C program must have a main() function to begin execution.

2. What is the purpose of #include <stdio.h>?


#include <stdio.h> is a preprocessor directive that includes the standard
input/output library in the program. This allows you to use functions like printf()
and scanf() for output and input operations.

3. Write a small C program to print "Hello, World!" and explain its


structure.
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Explanation:
#include <stdio.h>: Includes standard input/output library.
int main(): Defines the main function which returns an integer.
printf("Hello, World!\n");: Prints the string to the screen.
return 0; Indicates the program ended successfully.
Compilation Process
4. Explain the steps of the compilation process. The compilation process in
C involves the following steps:
Preprocessing: Includes header files and performs macro substitution.
Compilation: Translates the preprocessed code into assembly language.
Assembly: Converts the assembly code into machine code (object code).
Linking: Combines object files and libraries to create an executable.

5. What are keywords? Give two examples.


Keywords are reserved words in C with predefined meanings. They cannot be used
as identifiers.
Examples: int, if.

6. What are identifiers? What rules should you follow when naming them?
Identifiers are names used to represent variables, functions, arrays, etc. Rules for
naming:
Start with a letter or underscore.
Contain only letters, digits, or underscores.
Cannot be a keyword.

7. What is a constant in C? How is it different from a variable?


A constant is a fixed value that cannot be changed during program execution. A
variable, on the other hand, can hold different values during program execution.

8. How do you declare and initialize a variable in C?


Example:
int age = 25; // Declare and initialize a variable
9. What are the basic data types in C?
The basic data types in C are:
int: Integer type.
float: Floating-point type.
char: Character type.
double: Double-precision floating-point type.

10.What are format specifiers?


Format specifiers are used in functions like printf() and scanf() to specify the type
of data being printed or inputted. Example: %d for integers, %f for floating-point
numbers.

11.What is an operator in C? Explain its types with example.


An operator is a symbol used to perform operations on variables or values. Types
include:
Arithmetic Operators: +, -, *, /, %.
Relational Operators: ==, !=, <, >.
Logical Operators: &&, ||, !.

12.What is the difference between = and ==?


= is an assignment operator, used to assign values to variables.
== is a relational operator, used to compare two values.

13.Name any three arithmetic operators in C.


+, -, *.
14.What is a relational operator? Give an example.
Relational operators are used to compare two values.
Example: == (equal to).

15.What does the logical AND (&&) operator do?


The logical AND operator returns true if both conditions are true.
Example: (x > 0 && y < 10).

16.Write an example of using the ternary operator.


int max = (a > b) ? a : b;

17.What is the purpose of the modulus (%) operator?


The modulus operator returns the remainder of a division.
Example: 5 % 2 returns 1.

18.What is the purpose of printf() and scanf()?


printf() is used for output, and scanf() is used for input in C.

19.What are different data input and output functions?


Output: printf(), puts(), putchar().
Input: scanf(), gets(), getchar().
20.What is an if statement used for?
An if statement is used to execute a block of code if a given condition is true.

21.How does an if-else statement work?


The if-else statement executes one block of code if the condition is true and
another block if the condition is false.

22.What is an else-if ladder?


An else-if ladder is used when you have multiple conditions to check. It allows you
to execute different blocks of code based on several conditions.
23.What is the difference between a while loop and a do-while loop?
In a while loop, the condition is checked before executing the loop body. In a do-
while loop, the loop body is executed at least once, and then the condition is
checked.

24.What is a loop in C programming?


A loop is used to repeat a block of code multiple times based on a condition.

25.Write the syntax of a while loop.


while (condition) {
// code to execute
}

26.Write the syntax of a for loop.


for (initialization; condition; increment/decrement) {
// code to execute
}
27.What is a switch statement, and why do we use it?
A switch statement is used to execute one out of multiple possible blocks of code
based on the value of an expression.

28.What is the difference between break and continue?


break exits a loop or switch statement; continue skips the current iteration of a loop
and proceeds to the next one.

29.What is a goto statement?


goto is used to jump to a specific label in the program. It is generally discouraged
as it can lead to confusing and hard-to-maintain code.

30.What is a function in C?
A function is a block of code that performs a specific task. It can take input
(parameters) and return a result (return value).

31.Write the syntax of a function in C.


return_type function_name(parameters) {
// function body
}

32.What is the difference between function declaration and function


definition?
Function declaration declares the function's signature without the body.
Function definition provides the actual implementation of the function.
33.What does the return statement do in a function?
The return statement exits the function and optionally returns a value to the caller.

34.What is a function prototype/declaration? Why is it used?


A function prototype is a declaration of a function that specifies the function's
name, return type, and parameters. It is used to inform the compiler about the
function's existence before its definition.

35.What is the difference between pass-by-value and pass-by-reference?


In pass-by-reference, the address of the variable is passed to the function, allowing
the function to modify the original variable. In pass-by-value, only a copy is
passed, and changes do not affect the original.

36.What is recursion? Give an example of where it can be used.


Recursion is a function calling itself. It is used in problems like calculating
factorials or tree traversal.
Example:
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}

37.What are storage classes in C?


Storage classes define the scope, lifetime, and visibility of variables in C.
38.Explain the four types of storage classes in C.
auto: Local variables with automatic storage duration (default for local variables).
register: Local variables stored in CPU registers for faster access.
static: Retains the value of a variable between function calls.
extern: Used to declare a variable defined in another file or function.

39. Types of function, and parameters?


Built-in Functions:
These are functions that are already defined in C libraries and are ready to be
used without any further definition.
Example: printf(), scanf(), strlen(), sqrt().

User-defined Functions:
These are functions defined by the programmer to perform specific tasks in
the program.
A user-defined function can either return a value or not and can accept
parameters or not.

Parameter:
1. Actual Parameters:
These are the values or variables passed to the function when it is called.
2. Formal Parameters:
These are the variables declared in the function definition that receive the
values passed from the actual parameters.

40.What is an array in C? Write syntax.


An array is a collection of elements of the same type, stored in contiguous memory
locations.
Syntax:
data_type array_name[size];
41.How do you declare a one-dimensional array?
int arr[5];

42.How is a two-dimensional array different from a one-dimensional


array?
A one-dimensional array stores a list of elements, while a two-dimensional array
stores a matrix (table of rows and columns).

43.What is the index of the first element in an array?


The index of the first element in an array is 0.

44.What is a string in C?
A string in C is an array of characters terminated by a null character ('\0').

45.Write an example of declaring and initializing a string.


char str[] = "Hello";
What is the null character (\0) in strings?
The null character \0 marks the end of a string in C.

46.What is a structure in C?
A structure is a user-defined data type that can store multiple variables of different
data types.

47.Write the syntax for defining a structure in C.


struct StructureName {
data_type member1;
data_type member2;
};
48. What is a pointer in C and how is it different from a regular variable?
A pointer in C is a variable that stores the memory address of another
variable. The key difference is that a regular variable stores data, while a
pointer stores the address of the data in memory.

49. How do you initialize a pointer in C?


You initialize a pointer by assigning it the address of a variable using the
address-of operator (&).
Ex.
int num = 10;
int *ptr = &num;

50.What is pointer declaration, definition, and function calling?

Pointer declaration specifies the type of variable the pointer will point to. It
tells the compiler what type of data the pointer will hold the address of.

Pointer definition means assigning a value (the address of a variable) to the


pointer. It is also known as pointer initialization.

Pointer calling refers to using the pointer to access or manipulate the value
stored at the memory address it points to. This is typically done using the
dereference operator (*).

You might also like