You are on page 1of 14

C viva questions

1. Who developed C language?


C language was developed by Dennis Ritchie in 1970 at Bell Laboratories.
2. Which type of language is C?
C is a high – level language and general purpose structured programming language.
3. What is a compiler?
Compile is a software program that transfer program developed in high level language into executable
object code
4. What is IDE?
The process of editing, compiling, running and debugging is managed by a single integrated application
known as Integrated Development Environment (IDE)
5. What is a program?
A computer program is a collection of the instructions necessary to solve a specific problem.
6. What is an algorithm?
The approach or method that is used to solve the problem is known as algorithm.
7. What is structure of C program?
A C program contains Documentation section, Link section, Definition section, Global declaration
section, Main function and other user defined functions
8. What is a C token and types of C tokens?
The smallest individual units are known as C tokens. C has six types of tokens Keywords, Constants,
Identifiers, Strings, Operators and Special symbols.
9. What is a Keyword?
Keywords are building blocks for program statements and have fixed meanings and these meanings
cannot be changed.
10. How many Keywords (reserve words) are in C?
There are 32 Keywords in C language.
11. What is an Identifier?
Identifiers are user-defined names given to variables, functions and arrays.
12. What is a Constant and types of constants in C?
Constants are fixed values that do not change during the program execution. Types of constants are
Numeric Constants (Integer and Real) and Character Constants (Single Character, String Constants).
13. What are the Back Slash character constants or Escape sequence characters available in C?
Back Slash character constant are \t, \n, \0
14. What is a variable?
Variables are user-defined names given to memory locations and are used to store values. A variable may
have different values at different times during program execution
15. What are the Data Types present in C?
Primary or Fundamental Data types (int, float, char), Derived Data types(arrays, pointers) and User-
Defined data types(structures, unions, enum)
16. How to declare a variable?
The syntax for declaring variable is
data type variable_name-1, variable_name-2,....variable_name-n;
17. What is meant by initialization and how we initialize a variable?
While declaring a variable assigning value is known as initialization. Variable can be initialized by using
assignment operator (=).
18. What are integer variable, floating-point variable and character variable?
A variable which stores integer constants are called integer variable. A variable which stores real values
are called floating-point variable. A variable which stores character constants are called character
variables.
19. How many types of operator or there in C?
C consist Arithmetic Operators (+, -, *, /,%), Relational Operators (<, <=, >, >=, !=), Logical Operators
(&&, ||, !), Assignment Operators (=, +=, -=, *=, /=), Increment and Decrement Operators (++, --),
Conditional Operator(?:), Bitwise Operators(<<, >>, ~, &, |, ^) and Special Operators (. , ->, &, *, sizeof)
20. What is a Unary operator and what are the unary operators present in C?
An operator which takes only one operand is called unary operator. C unary operators are
Unary plus (+), Unary minus (-), Increment and Decrement operators (++,--), Address of operator (&),
Value at operator (*), sizeof operator, ones complement operator (~).
21. What is a ternary operator and which is called ternary operator is C?
An operator which takes three operands is called ternary operator. Conditional operator (? :) is knows as
ternary operator in C.
22. What is the use of modulus (%) operator?
The modulus operator produces the remainder of an integer division. It cannot be used on floating point
data.
23. What is the use of printf and scanf functions in C?
Values of variables and results of expressions can be displayed on the screen using printf functions.
Values to variables can be accepted through the keyboard using scanf function.
24. What are the format codes used in printf and scanf functions in C?
%c (for reading or printing a single character), %d (for reading or printing signed integer), %u (for
reading or printing unsigned integer), %ld (for reading or printing long signed integer), %lu (for reading
or printing long unsigned integer), %f (for reading or printing floating point value), %lf (for reading or
printing double floating point value), %Lf (for reading or printing long double value, %s (for reading or
printing string value)
25. What are the decision making statements available in C?
IF statement, Switch statement and conditional statement
26. What is the use of IF statement and how it works?
The IF statement is used to control the flow of execution of statements. It first evaluates the given
expression and then, depending on whether the value of the expression is true or false, it transfers the
control to a particular statement.
27. Forms of IF statements?
Simple IF statement, IF-ELSE statement, NESTED IF-ELSE statement and ELSE IF ladder
28. How switch statement works?
The switch statement tests the value of a given variable against a list of case values and when a match is
found, block of statement associated with that case is executed and if no match is found then the block of
statements associated with the optional default is executed. If default case not present, control goes to the
statement after the switch.
29. What is the difference between IF and SWITCH statement?
IF works on integers, floats and characters whereas SWITCH works on only integers and characters.
Switch statement cannot perform inequality comparisons whereas IF can perform both equality and
inequality comparisons.
30. How conditional operator (? :) works?
The conditional expression is evaluated first. If the expression is true then expression after the question
mark is executed otherwise expression after the colon is executed
31. What is GOTO statement?
GOTO is an unconditional branching statement which transfer control to the specified label
32. What is a LOOP?
Loop is a sequence of statements that are executed repeatedly
33. What are the types of LOOP control statement?
Entry-Controlled Loop statement and Exit-Controlled loop statement
34. What are the LOOP control statements present in C?
WHILE, DO-WHILE, FOR
35. What are the sections present in FOR loop?
Initialization section, Conditional section, increment or decrement section
36. How a FOR loop works?
In FOR loop first initialization section is executed, then given condition is checked. If condition becomes
true then body of the loop is executed after that increment or decrement section is executed
37. What is the use of break statement?
Break statement is used to exit from a loop

1. What is the difference between reading strings using scanf and gets?
Scanf cannot read strings with multiple words whereas gets can read strings with multiple words
2. What are the String-Handling functions available in C?
gets, puts, strcat, strcmp, strcpy and strlen.
3. What are the types of functions?
C functions are divided into two categories user-defined function and built-in functions
4. What is a function?
Function is a self contained block of statement which is used to perform certain task
5. Which are called built-in functions?
Printf, scanf, clrscr, gotoxy, string handling functions and file handling functions
6. What is function prototype declaration?
A function declaration is also known as function prototype declaration which contains function
return type, function name, parameter list and terminating semicolon

Viva Questions for “Programming in C”


General algorithms and flow chart.

Q1: What is a flowchart?


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

Q2: What is an Algorithm?


A2: An algorithm is an effective method for solving a problem expressed as a finite sequence of steps.
Algorithms are used for calculation, data processing, and many other fields. or It is a step by step
instructions written or procedure or process for solving a problem.

Q3: What is a Flowchart? What are different symbols in flowcharts?

A3: Flowchart is diagram representation of algorithms used to show flow of data in program using various
symbols.

Q4: What is a connector


symbol?
A4: Connector Symbol: This symbol is used to connect the various portion of a flow chart. This is
normally used when the flow chart is split between two pages.

Character, operators symbols of C Language


Q1: What are the three primary constants and variable types n C?
A1: integer, float , character.

Q2: What is the maximum length of


variable? A2: 31 characters.

Q3: Can a C keyword used as a variable name?


A3: No.

A4: What does an expression contains?


A4: An expression may contains any sequence of constants variable and operators.

Q5:How is an expression evaluated?


A5: An expression is evaluated based on the hierarchy and operator precedence.

Q6: What does the modulus performs?


A6: Modulus operator performs division and gives the remainder.

Q7: Name different logical operators.


A7: Logical OR, logical AND , Logical NOT.

Q8: What is syntax of conditional operator?


A8: expression1? Expression 2: expression 3.

Q9: What is a type conversion?


A10: The process of converting one predefined type into another is called type conversion.

Q10: What is a variable?


A10: Variable represents a named memory location whose values can be manipulated during program
execution
Q11: How a variable is declared?
A11:data_ type varianble_name;

Q13: What is a constant?


A13: A constant is a data item whose data value can never change during the program run.

Q14: List some valid logical expression.


A14: x>y, (y=z)>=(x/z), x||y&&z.

Q15: What is a keyword?


A15: Keyword is a word or a reserved word having fixed or pre-defined meaning and purpose.

Q6: What are Literals?


A6: Literals are data items that never change their values during a program execution. It is also called as
constants.

Q7: What are two categories of C constants?


A7: (a) Primary constants (b) Secondary constant

Q8: What is the range of integer constant?


A8: The range of integer costant depends upon the compiler. The allowable range for integer constants is
-32768 to 32767 for 16-bit compiler.

Q9: What is the range of integer constant for compilers like Turbo
C? A9: Range: -32768 to 32767.

Q10: What is the maximum length of character constants?


A10: The maximum range of character constant can be 1 character.

Input/Output statements.
Q1: What is the output of printf("%d") ?
A1: When we write printf("%d", n); this means compiler will print the value of n. But here, there is
nothing after %d so compiler will show in output window garbage value.

Q2: What is the difference between printf() and sprintf() ?


A2 : sprintf() writes data to the character array whereas printf(...) writes data to the standard output
device.

Q3: What three primary constants and variable


type in c?
A3: integer float and character.
Q4: What is a variable?
A4: A variable is a named storage location.

Q5: What is the maximum length of a ariable name?


A5: A variable name can be maximum 31 characters.

Q6: How input and output in c can be achieved?


A6: Using scanf() and printf().

Q7: What is the function of prefix version of increment and decrement?


A7: The prefix first changes (increments or decrements) its operand’s value and then uses it i.e. , it
follows change-then- use rule.

Q8: How operators with equal precedence are evaluated?


A8: Using associativity.

Program based on Arithmetic expression

Q1: What type of expression is arithmetic expression?


A1: Arithmetic expression can either be integer expression or real expressions or mixed mode
expressions.

Q2: Name the four basic data types in “C” language?


A2: The four basic data types in “c” language are:
(i) Char
(ii) Int
(iii) Float
(iv) Double

Q3: Describe at least five different format specifiers?


A3: %d: -An integer whole number
%f: -a floating point number
%c: -a single character
% p: -a machine memory address
%ld – long int
%s- string
%lf- double

Q4: Define and explain scanf () function?


A4: The Scanf () function can be 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. The scanf () function is responsible for giving input into the program.

Q5: What are the maximum and minimum possible ranges of values for long and short type?
A5: If the int variable is created by default as a “long‟ type it will have a possible range of values from a
maximum of +214748347 and a minimum of -2147483648. For “short‟ type the maximum and
minimum values +327676 and minimum -32768.
Type Storage size Value range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to
2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295

Q6: What exactly is a “variable scope‟, “local variables” and “global variables”?
A6: 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.

Q7: What are signed values?


A7: When an int variable is declared it can by default contain either positive of negative integer values.
These are known as “signed” values.

Q8: Explain about register variables relative to compiler?


A8: A register variable declaration informs the compiler that the specified variable will be heavily used
by the program. The intention is for the compiler to place register variables in the machine register of
the computer to speed access times. Local internal variables can only be declared as register variables.

Questions on Library functions.

Q1: Define and explain about “!” Operator?


A1: The logical operator! NOT is a unary operator that is used before a single operand. It returns the
inverse value of the given operand so if the variable “c” had a value of true then! C would return
value of false. The not operator is very much useful in C programs because it can change the value of
variables with successful iterations. This ensures that on each pass the value is changed.

Q2: What is operator precedence?


A2: 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.

Questions based on goto statement:

Q1: What is a goto statement ?


A1: The goto statement is a jump statement which jumps from one point to another point within a
function.

Q2: What is the problem with goto statement?


A2: The big problem with goto statement is that when we do use them we can never sure how we got to
a certain point in our code.

Q3:Why the use of goto keyword must be avoided?


A3: Because it usually violets the flow of execution.

Q5: Define structural language and procedural language?


A5: Structural language is one which can have a specific structure. C is a structured language before
structural language procedural language was used.

Q6:What is Preprocessor?
A6:preprocessor is a program which is invoked before the compilation process. it access input data and
process output data which has been used as input data to the other program like compiler.

Q7: What is modular programming?


A7: A large program can be divided into small Modules or into small programs or functions To solve a
particular problem all the Modules are combined together. This is called Modular Programming.

Q8: What is the code for clrscr() function?


A8:clrscr() is a function which creates a screenful of black dots which seems us to be clearing the screen
or making a new screen.

Q9:What is the purpose of main( ) function?


A9: main() is the user defined function . the program execution is always started from main. it is not
possible to write the program without main().

Q10: What is the function of break statement when used within switch?
A10: The break statement when used in aswitch takes the control outside the switch .

Questions based on `if' and `Nested if ‟.


Q1: What is the purpose of if statement?
A1: The keyword if tells the compiler that whatever is present in the if is a decision control instruction.

Q2:How does if statement works?


A2: If the condition within the parenthesis is true then the statement is executed otherwise not.

Q3: How do we express a condition itself in C?


A3: We express a condition using C‟s relational operators.
Q4: What is equality operator?
A4:== is used to compare the equality of two statements.

Q5: What is the syntax of if statement?


A5: if(expression)
Statement;

Q6: What are the different ways of decision making in a program?


A6: There are the ways:- if- else , conditional operators, switch statement.

Q7: What is the default scope of if statement?


A7: Next statement after the if block.

Q8: What are && and || ?


A8: These two are binary operators.

Q9: What is important in if else block?


A9: An if block need not be always associated with an else block. However an else block must be
associated with an if block.

Q10: Name a unary operator.


A10: ! (not equal to) is a unary operator.

Questions based on “switch case” statement.

Q1: What is a switch?


A1: The control statement that allows us to make a decision from the number of choices is called a switch
.
Q2: What is case keyword?
A2: The case keyword is followed by an integer or a character constant.

Q3: What happens when we run a program containing a switch?


A3: First, the integer expression following the keyword switch is evaluated.

Q4: Is switch a replacement of if? Yes and No.


A4: Yes, because it offers a better way of writing program as compared to if, and no, because in certain
cases we are left with no choices.

Q9:Where is switch statement is useful?


A9: Switch statement is useful while writing menu driven programs..

Title of the Practical: . At least one program based on each


(a) For statement, (b) While statement ,(c) Do-while statement, (d) Break statement
Q1. What is the difference between declaring a variable and defining a variable?
A1: Declaration of a variable in C hints the compiler about the type and size of the variable in
compile time. Similarly, declaration of a function hints about type and size of function
parameters. No space is reserved in memory for
any variable in case of declaration.

Q2. What is a static variable?


A2: A static variable is a special variable that is stored in the data segment unlike the default
automatic variable that is stored in stack. A static variable can be initialized by using keyword
static before variable name.
Example: static int a = 5;
Q3. What is a register variable?
A3: Register variables are stored in the CPU registers. Its default value is a garbage value.
Scope of a register variable is local to the block in which it is defined. Lifetime is till control
remains within the block in which the register variable is defined.

Q4. Where is an auto variable stored?


A4: Main memory and CPU registers are the two memory locations where auto variables are
stored. Auto variables are defined under automatic storage class. They are stored in main memory.
Memory is allocated to an
automatic variable when the block which contains it is called and it is de-allocated at the
completion of its block execution.
Auto variables: Storage : main memory. Default value: garbage value. Scope : local to the block in
which the variable is defined. Lifetime : till the control remains within the block in which the variable
is defined.

Q5. What is scope & storage allocation of extern and global variables?
A5: Extern variables: belong to the External storage class and are stored in the main memory.
extern is used when we have to refer a function or variable that is implemented in other file in the
same project. The scope of the
extern variables is Global. Global variables: are variables which are declared above the main( )
function. These variables are accessible throughout the program. They can be accessed by all the
functions in the program. Their default value is zero.

Q6. What is scope & storage allocation of static and local variables?
A6. Static variables: Memory is allocated at the beginning of the program execution and it is
reallocated only after the program terminates. The scope of the static variables is local to the
block in which the variables are defined.
Local variables: are variables which are declared within any function or a block. They can be
accessed only by function or block in which they are declared. Their default value is a garbage
value.

Q7. What are storage memory, default value, scope and life of Automatic and Register storage class?
A7. 1. Automatic storage class: Storage : main memory. Default value : garbage value. Scope : local
to the block in which the variable is defined. Lifetime : till control remains within the block.
2. Register storage class: Storage : CPU registers. Default value : garbage value.Scope : local to the
block in which the variable is defined. Lifetime : till control remains within the block.

Q8. What are storage memory, default value, scope and life of Static and External storage class?
A8. Static storage class:Storage : main memory.Default value : zeroScope : local to the block in
which the variable is defined.Lifetime : till the value of the variable persists between different function
calls.
2. External storage class:Storage : main memoryDefault value : zeroScope : globalLifetime : as long
as the program execution doesn't come to an end.

Q9. What is the difference between 'break' and 'continue' statements?


A9. 1. break is a keyword used to terminate the loop or exit from the block. The control jumps to next
statement after the loop or block. And continue is a keyword used for skipping the current iteration and
go to next iteration of the loop

10. What is the difference between 'for' and 'while' loops?


A10. for loop: When it is desired to do initialization, condition check and increment/decrement in a
single statement of an iterative loop, it is recommended to use 'for' loop.
Syntax: for(initialization; condition; increment/decrement)
while loop: When it is not necessary to do initialization, condition check and increment/decrement
in a single statement of an iterative loop, while loop could be used. In while loop statement, only
condition statement is present.

Q1:What is the purpose of main() function?


A1: In C, program execution starts from the main() function. Every C program must contain a
main() function. The main function may contain any number of statements. These statements are
executed sequentially in the order which they are written. The main function can in-turn call
other functions. When main calls a function, it passes the
execution control to that function. The function returns control to main when a return statement is
executed or when end of function is reached.

Q2. Explain command line arguments of main function?


A2: In C, we can supply arguments to 'main' function. The arguments that we pass to main ( ) at
command prompt are called command line arguments. These arguments are supplied at the time
of invoking the program.
The main ( ) function can take arguments as: main(int argc, char *argv[]) { }
The first argument argc is known as 'argument counter'. It represents the number of arguments
in the command line. The second argument argv is known as 'argument vector'. It is an array
of char type pointers that points to the command line arguments. Size of this array will be
equal to the value of argc.

Q3. What are header files? Are functions declared or defined in header files ?
A3: Functions and macros are declared in header files. Header files would be included in source files by
the compiler
at the time of compilation. Header files are included in source code using #include directive A header
file may contain declarations of sub-routines, functions, macros and also variables which we may want
to use in our program. Header files help in reduction of repetitive code

4. What are the differences between getchar() and scanf() functions for reading strings?
A4: scanf();-Entering of each character should be followed by return key 2. Continuous stream of
characters cannot be directly supplied using scanf function.
getchar():- 1. Need not type return key. 2. Continuous stream of characters can be directly supplied using
getchar function

Q5: What is the purpose gets()?


A5: gets() receives a string from a keyboard.

6. The four basic data types in “c” language are as follows


char – a character
int – an integer, in the range -32,767 to 32,767
long int – a larger integer (up to +-2,147,483,647)
float – a floating-point number
double – a floating-point number, with more precision and perhaps greater range than float

7. Define and explain printf () function?


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

8. What is preprocessor?
The preprocessor is a program which is executed before the compilation of a source program written by
the user. The preprocessor directives begines with hash (#) followed by the command. e.g #include – it
is a directive to include file.

9. What is the purpose of type declaration in C?


All variables used in a C program are declared using the appropriate data types to enable the compiler to
allocate the required number by bytes in RAM to store values of these variables in memory

10. What is identifier ?


An identifier is a name used to identify a variable, function, symbolic constsnt and so on.

11) Mention the different types of operators used in C ?


1. Arithmetic operator
2. Relational operators
3. Logical Operators
4. Increment and decrements operators
5. Assignment operators
6. Conditional operator
7. Bitwise operators
8. Special Operators

12. What are the Loop control statements?


Loop control structures are used to execute and repeat a block of statements depending on the value of a
condition. There are 3 types of loop control statements in C
1. for loop
2. while loop
3. do – while loop

13) Explain while loop .


A while loop has one control expression. It executes it as long as that expression is true. The general
syntax of a while loop is
while( expression or condition )
{
statements;
}

we use a while loop when a statement or group of statements which may have to be executed a number
of times to complete their task. The controlling expression represents the condition

14) Explain for loop.


A for loop is used to execute and repeat a block of statements depending on a condition. The syntax of a
for loop is
for(initialization ;condition; increment/decrement)
{
statements;
}

15. List a few unconditional control statements in C.


1. break statement
2. continue statement
3. goto statement
4. exit() function

16. Define function


A function is a module or block of program code which deals with a particular task. Each function has a
name or identifier by which is used to refer to it in a program. A function can accept a number of
parameters or values which pass information from outside, and consists of a number of statements and
declarations, enclosed by curly braces { }, which make up the doing part of the object

17. Differentiate built-in functions and user – defined functions.


Built – in functions are used to perform standard operations such as finding the square root of a number,
absolute value and so on. These are available along with the C compiler and are included in a program
using the header files math.h, s tring.h and so on.
User defined functions are written by the user or programmer to compute a value or perform a task. It
contains a statement block which is executed during the runtime whenever it is called by the main
program.

18. Explain the concept and use of type void.


A function which does not return a value directly to the calling program is referred as a void function.
The void functions are commonly used to perform a task and they can return many values through
global variable declaration.

19. What are Library functions?


Library functions are built in programs available along with the compiler which perform some standard
mathematical operations.

20. How does the type float differ from double in C language ?
Float data type refers real number in single precision and has 6 decimal digits. It takes 4 bytes in
memory to refer values ranging from 3.4e-38 to 3.4e+38
double data type also refers to real number but in double precision and has 12 decimal digits. It takes 8
bytes of memory to refer values ranging from 1.7e-308 to 1.7e+308

21 What is an operator and operand?


An operator performs an operation like addition, subtraction and so on and produce a value. Variables
and constants upon which operations are performed are called operands.

22. What are control statements?


All the statements written in a program are executed from top to bottom one by one. Control statements
are used to execute / transfer the control from one part of the program to another depending on a
conditions.

23. Explain increment and decrements operators.


++ increment operator which add one to the value,
example : i++ (which adds one to i and results is scored back to)
— decrement operator which subtracts one from the value
example — i ( which subtracts one from i)

You might also like