0% found this document useful (0 votes)
7 views12 pages

C Viva Questions

The document provides a comprehensive set of viva questions and answers related to the C programming language, covering fundamental concepts such as data types, control structures, functions, and operators. It includes definitions, syntax examples, and explanations of key programming principles, making it a useful resource for first-year engineering students. The content is structured to facilitate understanding of C programming essentials for academic assessments.

Uploaded by

Harshal Nagpure
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)
7 views12 pages

C Viva Questions

The document provides a comprehensive set of viva questions and answers related to the C programming language, covering fundamental concepts such as data types, control structures, functions, and operators. It includes definitions, syntax examples, and explanations of key programming principles, making it a useful resource for first-year engineering students. The content is structured to facilitate understanding of C programming essentials for academic assessments.

Uploaded by

Harshal Nagpure
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

Department of First Year Engineering

Session 2019-20
Computer Programming in C
Viva-Questions
1) What is C language?
Ans: C is a programming language developed at AT&T’s Bell Laboratories of USA in 1972 by Dennis
Ritchie. It has since spread to many other operating systems, and is one of the most widely used
programming languages.
2) What are the key features in the C programming language?
Ans:
 Portability – Platform independent language.
 Modularity – Possibility to break down large programs into small modules.
 Flexibility – The possibility to a programmer to control the language.
 Speed – C comes with support for system programming and hence it is compiling and executes
with high speed when compared with other high-level languages.
 Extensibility – Possibility to add new features by the programmer.

3) What is the use of header files as used in C programming?


Ans: Header files are used to have declarations. It is simple to include a single header file than
writing all the needed functions prototypes. Example <stdio.h> which includes printf() and scanf
function to read and print.

4) What are printf() and scanf() Functions :


Ans: printf() and scanf() functions are inbuilt library functions in C which are available in which are
available in <stdio.h> header file.

printf() function is used to print the “character, string, float, integer, octal and hexadecimal values”
onto the output screen.

scanf() function is used to read character, string, numeric data from keyboard

5) What is the use of main() function?


Ans: main() is the starting point of program execution.

6) What is a Compiler?
Ans: A compiler is a computer program (or set of programs) that transforms source code written in a
programming language (the source language) into another computer language (the target language,
often having a binary form known as object code).

7) What is a Data Type and List the different Data types?


Ans: C data types are defined as the data storage format that a variable can store a data to perform a
specific operation.
List of Data Types:
1) Basic Data Types: int, float, char, double
2) Derived Data Type: Pointer, array, structure, union
3) Enumeration Data Type: enum
4) Void Data Type: void
8) In C language, the variables NAME, name, and Name are all the same. TRUE or FALSE?
Ans: FALSE. C language is a case sensitive language. Therefore, NAME, name and Name are three
uniquely different variables.

9) What is wrong with this program statement? int void = 10;


Ans: The word void is a reserved word in C language. You cannot use reserved words/keywords as a
user-defined variable.

10) What are control structures?


Ans: Control structures take charge at which instructions are to be performed in a program. This means
that program flow may not necessarily move from one statement to the next one, but rather some
alternative portions may need to be pass into or bypassed from, depending on the outcome of the
conditional statements.
11) What is a void Data Type?
Ans: void is an empty data type that has no value.

12) What are the different types of control structures in programming?


There are 3 main control structures in programming: Sequence, Selection and Repetition.
 Sequential control follows a top to bottom flow in executing a program, such that step 1 is first
perform, followed by step 2, all the way until the last step is performed.
 Selection deals with conditional statements, which mean codes are executed depending on the
evaluation of conditions as being TRUE or FALSE. This also means that not all codes may be
executed, and there are alternative flows within.
 Repetitions are also known as loop structures, and will repeat one or two program statements
set by a counter.

13) What is an Identifier in C?


Ans: A C identifier is a name used to identify a variable, function, or any other user- defined item.
An identifier starts with a letter A to Z or a to z or an underscore _ followed by zero or more letters,
underscores, and digits (0 to 9). C does not allow punctuation characters such as @, $, and % within
identifiers. C is a case sensitive programming language.

14) What is a Keyword in C?


Ans: Keywords are reserved words in C and Keywords are may not be used as constant or variable or
any other identifier names.

15) How many Keywords are there in C and List out the Keywords?
Ans: There are 32 reserved keywords are there in C. Keywords in C Programming are
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while

.
16) Define a Variable?
Ans: A variable is a name given to a storage area that our programs can manipulate. Each variable in
C has a specific datatype, which determines the size and layout of the variable’s memory.
Datatype variable_name1, variable_name2....;
e.g.
int i, j, k;
char c, ch;
float x,y;

17) What is an Operator and list different types of operators in C?


Ans: The symbols used to perform arithmetic and logical in a C program are called C operators. The
different operators in C are:
a. Arithmetic operators: These are used to perform mathematical calculations like addition,
subtraction, multiplication, division and modulus (+, -, *, /, %)
b. Assignment operators: These are used to assign the values for the variables in C programs
(= += -= *= /= %=).
c. Relational operators: These operators are used to compare the value of two variables
(<, <=, >, >=, ==, !=).
d. Logical operators: These operators are used to perform logical operations on the given two
variables (&&, ||, !).
e. Bit wise operators: These operators are used to perform bit operations on given two variables.
( &, ^, | )
f. Conditional (ternary) operators: Conditional operators return one value if condition is true
and returns another value is condition is false. .( ? : )
g. Increment/decrement operators: These operators are used to either increase or decrease the
value of the variable by one (++,--).
h. Special operators: &, *, sizeof( ) and ternary operators.
i.
18) What is sizeof() operator?
Ans: It returns the number of bytes the operand occupies. e.g int a; then sizeof(a) is 2 as sizeof integer
is 2 Bytes.

19) What are the Format Specifiers or Type Specifiers or Conversion Specifiers?
The format specifiers are used in C for input and output purposes. Using this concept the compiler can
understand that what type of data is in a variable during taking input using the scanf() function and
printing using printf() function. Here is a list of format specifiers.

Format Specifier Type


%c Character
%d Signed integer
%ld Long integer
%f Float
%lf Double
%Lf Long double
These are the basic format specifiers. We can add some other parts with the format specifiers. These
are like below −
 A minus symbol (-) sign tells left alignment
 A number after % specifies the minimum field width. If string is less than the width, it will be
filled with spaces
 A period (.) is used to separate field width and precision

20) What is a Statement in C?


Ans: A statement is a block of code that does something.

21) Different Types of Statements?


Ans: Expression Statement, Compound Statement, Return Statement, Conditional Statements,
Iterative or Looping Statements, Unconditional Statements.
22) Define Compound Statement?
Ans: A compound statement (also called a “block”) typically appears as the body of another
statement which is in between {and}
23) Define Conditional Statements and give the list of them?
Ans: Conditional Statements which allows to perform actions depending upon some conditions
provided by the programmer. The Different types of conditional statements are:
1) If Statement 3) Nested- if else statement
2) If else statement 4) Switch Statement
24) Syntax of if statement:
The statements inside the body of “if” only execute if the given condition returns true. If the
condition returns false then the statements inside “if” are skipped.
if (condition)
{
//Block of C statements here
//These statements will only execute if the condition is true
}
25) Syntax of if-else statement:
If condition returns true then the statements inside the body of “if” are executed and the statements
inside body of “else” are skipped. If condition returns false then the statements inside the body of “if”
are skipped and the statements in “else” are executed.
if(condition) {
// Statements inside body of if
}
else {
//Statements inside body of else
}
26) Syntax of nested if-else statement:
if(condition) {
//Nested if else inside the body of "if"
if(condition2) {
//Statements inside the body of nested "if"
}
else {
//Statements inside the body of nested "else"
}
}
else {
//Statements inside the body of "else"
}
27) Syntax of else..if statement:
if (condition1)
{
//These statements would execute if the condition1 is true
}
else if(condition2)
{
//These statements would execute if the condition2 is true
}
else if (condition3)
{
//These statements would execute if the condition3 is true
}
.
.
else
{
//These statements would execute if all the conditions return false.
}
28) What are Iterative or Looping Statements?
Ans: Iterative or Looping statement which executes the statements within the compound statement by
checking the condition, If the condition is found true, then statements written in the body of the loop
i.e., inside the braces { } are executed. Then, again the condition is checked, and if found true then
statements in the body of the while loop are executed again. This process continues until the condition
becomes false.
The Iterative or Looping Statements are: While, do-while, for

29) Syntax for while statement


while (test condition)
{
//Statements to be executed repeatedly
// Increment (++) or Decrement (--) Operation
}

30) Syntax for do-while loop


do
{
//Statements

}while(test_condition);

31) Syntax for ‘for’ loop


for (initialization; condition test; increment or decrement or terminating statement)
{
//Statements to be executed repeatedly
}

32) Example of Multiple initialization inside for Loop in C


for (i=1,j=1;i<10 && j<10; i++, j++)
33) What is the difference between for loop and while loop?
Ans: For Loop is used to execute a set of statements in fixed number of times. While loop is used
when the number of iterations to be performed is not known in advance we use while loop.

34) Define Break Statement, Write syntax for Break statement?


Ans: The break statement terminates the execution of the nearest enclosing do, for, switch, or while
statement in which it appears. Control passes to the statement that follows the terminated statement.
Syntax: break;

35) Define Continue Statement, Write syntax for Continue statement?


Ans: The continue statement passes control to the next iteration of the nearest enclosing do, for,
or while statement in which it appears. It doesn’t ork with switch statement.
Syntax: continue;

36) Define Array


Ans: An array is defined as the collection of similar type of data items stored at contiguous memory
locations. Arrays are the derived data type in C programming language which can store the primitive
type of data such as int, char, double, float, etc. It also has the capability to store the collection of
derived data types, such as pointers, structure, etc. The array is the simplest data structure where each
data element can be randomly accessed by using its index number. There are 2 types of C arrays. They
are,
 One Dimensional array
 Multi-Dimensional array
o Two dimensional array
o Three dimensional array
o Four dimensional array etc…

37) How array is declared


Ans: We can declare an array in the c language in the following way.
data_type array_name[array_size];
Now, let us see the example to declare the array.
int marks[5];
Here, int is the data_type, marks are the array_name, and 5 is the array_size.
38) Define Pointer with Syntax and example?
Ans: C Pointer is a variable that stores/points the address of another variable. C Pointer is used to
allocate memory dynamically i.e. at run time.
Syntax: data_type *var_name;
Example: int *p; char *p;
39) What are the uses of Pointers?
Ans: Pointer is used in the following cases
 It is used to access array elements.
 It is used for dynamic memory allocation.
 It is used in Call by reference.
 It is used in data structures like trees, graph, linked list etc.

40) What is Recursion?


Ans: A recursion function is one which calls itself either directly or indirectly it must halt at a definite
point to avoid infinite recursion.
Advantages of using recursion:-
 Avoid unnecessary calling of functions
 Substitute for iteration
 Useful when applying the same solution
41) What is an Argument?
Ans: An argument is an entity used to pass data from the calling to a called function.

42) What are Built-in-Functions/ Pre Defined Functions / Library Functions?


Ans: The functions that are predefined and supplied along with the compiler are known as built in
functions. They are also known as library functions.

43) What are the uses of Functions?


Ans: C functions are used to avoid rewriting same logic/code again and again in a program. There is
no limit in calling C functions to make use of same functionality wherever required.
We can call functions any number of times in a program and from any place in a program.
A large C program can easily be tracked when it is divided into functions.
The core concept of C functions are, re-usability, dividing a big task into small pieces
to achieve the functionality and to improve understand-ability of very large C programs.

44) Define Function Declaration, Function Call and Function Definition with Syntaxes:
Ans:
 Function declaration or prototype – This informs compiler about the function name, function
parameters and return value’s data type.
 Function call – This calls the actual function
 Function definition – This contains all the statements to be executed.

45) Define Call by Value method of function?


Ans: In call by value method, the value of the variable is passed to the function as parameter. The
value of the actual parameter cannot be modified by formal parameter. Different Memory is allocated
for both actual and formal parameters.
 Actual parameter – This is the argument which is used in function call.
 Formal parameter – This is the argument which is used in function definition
46) Define Call by Reference?
Ans: In call by reference method, the address of the variable is passed to the function as parameter.
The value of the actual parameter can be modified by formal parameter. Same memory is used for both
actual and formal parameters since only address is used by both parameters.
47) Differentiate call by value and call by reference?
Ans: Call by value: A process in which the values of the actual parameters sent by the calling function
are copied to the formal parameters of the called function.
Call by reference: A process in which the parameters of a calling function are passed to the parameters
of the called function using an address.
48) Explain types of function
Ans:
[Link] C function syntax
int function ( int ); // function declaration
with arguments and with function ( a ); // function call
return values int function( int a ) // function definition
{
1 statements;
return a;
}
void function (int); // function prototype
with arguments and function( a ); // function call
without void function( int a ) // function definition
return values {
2
statements;
}
void function(); // function prototype
without arguments and function(); // function call void
without return values function() // function definition
{
3
statements;
}
int function ( ); // function prototype
without arguments function ( ); // function call
and with return values int function( ) // function definition
{
4 statements;
return a;
}

49) What is a C Preprocessor?


Ans: C Preprocessor is a program that processes our source program before it is passed to the compiler.
The preprocessor provides the ability for the inclusion of header files, macro expansions, conditional
compilation, and line control.

50) What is meant by <stdio.h>


Ans: This is standard input/output header file in which Input/Output functions are declared.

51) What do you mean by #include<stdio.h>?


Ans: In C, the hash function # tells the compiler that a statement should be sent to the C preprocessor.
The include looks after the new files and replace the contents of those files. and stdio.h will be used
printf, scanf functions.

52) What is a structure in C Language? How to initialise a structure in C?


Ans: A structure is a composite data type declaration that defines a physically grouped list of variables
to be placed under one name in a block of memory, allowing the different variables to be accessed via
a single pointer.
Defining a structure in C: In C language a structure is defined using struct keyword followed by
variable or pointer name below is the basic syntax for declaring a structure in C language.
struct structure_name {
type member1;
type member2;
};

53) Compare array data type to pointer data type


Ans: Array is a collection of variables of same type that are referred through a common name and a
pointer is a variable that holds a memory address. pointers can point to array and array to pointers

54) If the size of int data type is two bytes, what is the range of signed int data type?
Ans: The range of signed int data type if from -32768 to 32767

55) Explain bit masking in C?


Ans: Bit masking refers to selecting a particular set of bits from the byte(s) having many values of
bits. Bit masking is used to examine the bit values and can be done by 'AND' operation of byte, bitwise.

56) What are tokens in C


Ans: The Token is an identifier. It can be constant, keyword, string literal, etc. A token is the smallest
individual unit in a program.
C has the following tokens:
Identifiers: Identifiers refer to the name of the variables.
Keywords: Keywords are the predefined words that are explained by the compiler.
Constants: Constants are the fixed values that cannot be changed during the execution of a program.
Operators: An operator is a symbol that performs the particular operation.
Special characters: All the characters except alphabets and digits are treated as special characters.

57) What are escape sequences in C


Ans: An escape sequence is a sequence of characters used in formatting the output and are not
displayed while printing text on to the screen, each having its own specific function.
Escape Sequence Meaning
\a Alarm or Beep
\b Backspace
\f Form Feed
\n New Line
\r Carriage Return
\t Tab (Horizontal)
\v Vertical Tab
\\ Backslash
\' Single Quote
\" Double Quote
\? Question Mark

58) What is typecasting?


The typecasting is a process of converting one data type into another is known as typecasting. If we
want to store the floating type value to an int type, then we will convert the data type into another data
type explicitly.

Syntax: (type_name) expression;

59) What is Algorithms?


Ans: An algorithms refer to the step by step instructions written to solve any problem.

60) What is Flowchart?


Ans: A flowchart is a diagrammatic or symbolic representation of an algorithms. It uses various
symbols to represent the operations to be performed

61) Define and explain scanf () function?


The scanf () function is used to get input into a program and it requires two arguments. First a format
specifier defines the type of data to be entered, then the name of the variable in which the input will
be stored. This scanf () function is responsible for giving input into the program.

62) Define and explain printf () function?


Ans: The printf() function is used to display/output values of variable in the monitor. The printf
function has general form: printf (“format specifiers and message”, variables)

63) What exactly is a ‘variable scope’, ‘local variables’ and ‘global variables’?
Ans: The extent to which a variable is accessible in a program is called the ‘variable scope’. Variables
declared internally inside a function are known as ‘local’ variables.
Variables declared externally outside a function are known as ‘global’ variables.

64) Define string ?


Ans: An array of characters is known as a string. for example
char st[80]; this statement declares a string array with 80 characters .
65) Mention four important string handling functions in C languages.
Ans: There are four important string handling functions in C languages.
strlen();
strcpy();
strcat();
strcmp();
The header file #include<string.h> is used when these functions are called in a C program.

66) What is operator precedence?


Ans: An Operator precedence defines the order in which C evaluates expressions e.g. in the expression
a=6+b*3, the order of precedence determines whether the addition or the multiplication is completed
first. Operators on the same row have equal precedence.

67) What is the use of a semicolon (;) at the end of every program statement?
Ans: A semicolon acts as a delimiter, so that the compiler knows where each statement ends, and can
proceed to divide the statement into smaller elements for syntax checking.

68) Syntax of switch statement


Ans: switch (expression)
{
case constant1:
// statements
break;

case constant2:
// statements
break;
.
.
.
default:
// default statements
}

69) In a switch statement, what will happen if a break statement is omitted?


Ans: If a break statement was not placed at the end of a particular case portion? It will move on to the
next case portion, possibly causing incorrect output.

70) When is a “switch” statement preferable over an “if” statement?


Ans: The switch statement is best used when dealing with selections based on a single variable or
expression. However, switch statements can only evaluate integer and character data types.
71) What is the difference between the expression “++a” and “a++”?
Ans: In the first expression, the increment would happen first on variable a, and the resulting value
will be the one to be used. This is also known as a prefix increment. In the second expression, the
current value of variable a would the one to be used in an operation, before the value of a itself is
incremented. This is also known as postfix increment.

72) What are logical errors and how does it differ from syntax errors?
Ans: Program that contains logical errors tend to pass the compilation process, but the resulting output
may not be the expected one. This happens when a wrong formula was inserted into the code, or a
wrong sequence of commands was performed. Syntax errors, on the other hand, deal with incorrect
commands that are misspelled or not recognized by the compiler.
73) What does the format %10.2 mean when included in a printf statement?
Ans: This format is used for two things: to set the number of spaces allotted for the output number and
to set the number of decimal places. The number before the decimal point is for the allotted space, in
this case it would allot 10 spaces for the output number. If the number of space occupied by the output
number is less than 10, addition space characters will be inserted before the actual output number. The
number after the decimal point sets the number of decimal places, in this case, it’s 2 decimal spaces.

74) What are comments and how do you insert it in a C program?


Comments are a great way to put some remarks or description in a program. It can serves as a reminder
on what the program is all about, or a description on why a certain code or function was placed there
in the first place. Comments begin with /* and ended by */ characters. Comments can be a single line
that begin with //, or can even span several lines. It can be placed anywhere in the program.

75) What is wrong in this statement? scanf(“%d”,number);


Ans: An ampersand & symbol must be placed before the variable name whatnumber. Placing & means
whatever integer value is entered by the user is stored at the “address” of the variable name. This is a
common mistake for programmers, often leading to logical errors.

76) When is the “void” keyword used in a function?


Ans: When declaring functions, you will decide whether that function would be returning a value or
not. If that function will not return a value, such as when the purpose of a function is to display some
outputs on the screen, then “void” is to be placed at the leftmost part of the function header. When a
return value is expected after the function execution, the data type of the return value is placed instead
of “void”.
77) Can I use “int” data type to store the value 32768? Why?
Ans: No. “int” data type is capable of storing values from -32768 to 32767. To store 32768, you can
use “long int” instead. You can also use “unsigned int”, assuming you don’t intend to store negative
values.
78) What is syntax error?
Ans: Syntax errors are associated with mistakes in the use of a programming language. It maybe a
command that was misspelled or a command that must was entered in lowercase mode but was instead
entered with an upper case character. A misplaced symbol, or lack of symbol, somewhere within a line
of code can also lead to syntax error.
79) What is variable initialization and why is it important?
This refers to the process wherein a variable is assigned an initial value before it is used in the program.
Without initialization, a variable would have an unknown value, which can lead to unpredictable
outputs when used in computations or other operations
80) What is the use of a ‘\0’ character?
Ans: It is referred to as a terminating null character, and is used primarily to show the end of a string
value.
81) What is the difference between the = symbol and == symbol?
Ans : The = symbol is often used in mathematical operations. It is used to assign a value to a given
variable. On the other hand, the == symbol, also known as “equal to” or “equivalent to”, is a relational
operator that is used to compare two values
82) What is a nested loop?
A nested loop is a loop that runs within another loop. Put it in another sense, you have an inner loop
that is inside an outer loop. In this scenario, the inner loop is performed a number of times as specified
by the outer loop. For each turn on the outer loop, the inner loop is first performed.

83) What are variables and it what way is it different from constants?
Ans: Values held by a variable can be altered throughout the program, and can be used in most
operations and computations. Constants are given values at one time only, placed at the beginning of
a program. This value is not altered in the program. For example, you can assigned a constant named
PI and give it a value 3.1415. You can then use it as PI in the program, instead of having to write
3.1415 each time you need it.

84) When is the “void” keyword used in a function?


Ans: When declaring functions, you will decide whether that function would be returning a value or
not. If that function will not return a value, such as when the purpose of a function is to display some
outputs on the screen, then “void” is to be placed at the leftmost part of the function header. When a
return value is expected after the function execution, the data type of the return value is placed instead
of “void”

85) What is the advantage of an array over individual variables?


Ans: When storing multiple related data, it is a good idea to use arrays. This is because arrays are
named using only 1 word followed by an element number. For example: to store the 10 test results of
1 student, one can use 10 different variable names (grade1, grade2, grade3… grade10). With arrays,
only 1 name is used, the rest are accessible through the index name (grade[0], grade[1], grade[2]…
grade[9]).

-Prof. Sandesh D. Jain

You might also like