DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
18CS101 – Fundamentals of Computer Science- Question Bank
PART A
Identify the six problem-solving steps in developing the best solution
for a problem.
For developing the best solution the six steps are very important. The
six steps in problem solving include the following:
1. Identify the problem.
2. Understand the problem.
1.
3. Identify alternative ways to solve the problem.
4. Select the best way to solve the problem from the list of alternative
solutions.
5. List instructions that enable you to solve the problem using the selected
solution.
6. Evaluate the solution.
Discuss the characteristics of a good algorithm.
The characteristics of a good algorithm are:
Definiteness /Precision – the steps are precisely stated(defined).
Uniqueness – results of each step are uniquely defined and only depend
on the input and the result of the preceding steps.
Finiteness – the algorithm stops after a finite number of instructions are
2. executed.
Input – the algorithm receives input.
Output – the algorithm produces output.
Generality – the algorithm applies to a set of inputs.
Language independent- the algorithm applies to any programming
language
Ramesh’s basic salary is input through the keyboard. His dearness allowance
is 40% of basic salary, and house rent allowance is 20% of basic salary. Draw
a flowchart to calculate his gross salary using the formula gross salary=basic
salary+ dearness allowance + house rent allowance.
3.
Consider part of the code:
i=1
while(i<5)
4. display “welcome”
end while
How many times welcome will be displayed.
Answer: Infinite times
Interpret the statement #include <stdio.h> in a C program.
5. It instructs the compiler to include the header file stdio.h from the system
library to the C program. It contains the Standard Input and output functions.
Which of the following is FALSE in C
(1) Keywords can be used as variable names
(2) Variable names can contain a digit
(3) Variable names do not contain a blank space
6.
(4) Capital letters can be used in variable names
Answer:
(1) Keywords can be used as variable names
Evaluate the following expression
x*(-2*(y+z)/3)
7. Assume x=2 y=3 z=4.
Answer:
-8
If a = 10, b = 12, c = 0, find the values of the expressions in
the following table:
Expression Value(T/F)
8. a != 6 && b > 5 T
a == 9 || b < 3 F
! ( a < 10 ) T
! ( a > 5 && c ) T
Distinguish between compiler and interpreter.
Compiler Interpreter
Compiler Takes Entire program as Interpreter Takes Single instruction as
input input .
Intermediate Object Code is No Intermediate Object Code is
Generated Generated
Memory Memory Requirement is Less
9. Requirement : More(Since Object
Code is Generated)
Program need not Every time higher level program is
be compiled every time converted into lower level program
Errors are displayed after entire Errors are displayed for every
program is checked instruction interpreted
Example : C Compiler Example : BASIC
Demonstrate the purpose of on-page-connector in flowchart
Ans:
this symbol is typically small and is used as a Connector to show a jump from one point in the
10. process flow to another. Connectors are usually labeled with capital letters (A, B, AA) to show
matching jump points. They are handy for avoiding flow lines that cross other shapes and flow
lines.
Prepare an algorithm to calculate factorial of a given number.
Ans:
11.
step 1. Start
Demonstrate the rules for naming variable in C.
Ans:
12.
1. Characters Allowed :
o Underscore(_)
o Capital Letters ( A – Z )
Differentiate between = and ==
Ans:
13. = is a assignment operator and == is a comparison operator.
= operator is used to assign value to a variable and == operator is used to compare two variable or
constants.
List some of the keywords used in C.
Ans:
break else long switch
case enum register typedef
char extern return union
14.
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Predict the output for the following program
int main()
{
int c = 2 ^ 3;
printf("%d\n", c);
15.
return 0;
}
Ans:
1
Summarize Comments in C language
Ans:
There are 2 types of comments in the C language.
Single Line Comments
//code to be commented
16.
Multi-Line Comments :
/*
code
to be commented
*/
Interpret the structure of precondition and post condition loop.
Ans:
17.
Give guidelines for drawing a flowchart
• The flowchart should be clear, neat and easy to follow.
• The flowchart should have a logical start and a stop.
18. • The direction of the flow of a procedure or system should always start from left to right or
from top to bottom.
• Only one flow line should come out from a process symbol.
• Only one flow line should enter a decision symbol. However two or three flow lines, one
for each possible solution, may leave the decision symbol.
What will be the final value of x in the following C code?
#include <stdio.h>
void main()
19. {
int x = 5 * 9 / 3 + 9;
}
OUTPUT : 24
What is the difference between declaration and definition of a variable?
20. A function declaration tells the compiler about a function's name, return type, and parameters.
A function definition provides the actual body of the function.
List the characteristics of C language.
It is a robust language with rich set of built-in functions and operator.
21. The C compiler combines the capabilities of an assembly language with features of a high-
level language.
Programs Written in C are efficient and fast
Difference between i++ and ++i.
Both i++ and ++i basically serve the same function: they increase the value of i by 1.
22. When we assign i++ to any variable, it takes the current value of i, and then increments i.
When we assign ++i to any variable, it first increments the value of i, and then assigns it to
the variable
Define modulus operator.
The modulus operator returns the remainder of a division of one number by another.
23.
% is modulo operator
Example : 25%5 = 0
List out the qualities of good algorithm.
Time
Memory
24.
Accuracy
Sequence
Generability
What are the basic data types associated with C?
Primitive – char, int, float/double, void
25.
Derived – signed/unsigned of primitive types
User derived – array, enum, struct, union
What will be the output of following program?
#include <stdio.h>
int main()
{
int x = 1;
26. if (x = 0)
printf("hello");
else
printf("hello world");
}
OUTPUT : hello world
PART B
1 A person from USA wants to know his Body Mass index
(BMI).He knows his weight in pounds and height in inches.
The evaluator knows the formula for calculating BMI
BMI=(weight in kilograms)/(height in cm*height in cm) ,
1 pound =0.45 kilogram and 1 inch =2.5 cm.
Write an algorithm to help the person in finding his BMI.
Algorithm:
Start
Declare weight,height,BMI
Read weight,height
Calculate BMI=(weight*0.45)/((height *2.5)*(height *2.5))
Print BMI
Stop
2 Any character is entered through the keyboard; Draw a flowchart
to a make decision whether the character entered is a capital
letter, a small case letter, a digit or a special symbol.
The following table shows the range of ASCII values for various characters.
Characters ASCII Values
A–Z 65 – 90
a–z 97 – 122
0–9 48 – 57
special 0 - 47, 58 - 64, 91 -
symbols 96,
123 - 127
3 Draw the structure of a C program and demonstrate the purpose
of each section with examples.
Documentation Section- consist of set of comment lines (Eg: name of
the program, author and other details)
/*
This is a comment
Block
*/
// Single line comment
Link section- provides instruction to the compiler to link functions
from system library.
# - preprocessor directive
.h-header files in library
#include <stdio.h> //Standard Input/Output library –
printf(),scanf()etc
#include <math.h> //if math library is used
sqrt( ),sin( )etc
Definition Section- defines all symbolic constant.
#define PI 3.14159
#define MY_MESSAGE “Good Morning!”
#define MAX 100
Global declaration section- global variables and user defined
functions are declared.
// declare global variables
int a;
char b=‘a’;
//declare user defined functions
void add(int,int);
main() Function Section- c program starts with main() function.
It includes declaration and executable parts.
// define main function
void main ()
{
int a,b; // declare local variables
a = 2;b=3;
printf(“Value of a=%d b=%d”,a,b);
add(a,b); // call function
}
Subprogram section Section- main() function calls the subprogram
section to execute some actions .
// define add function
void add (int a, int b)
{
int c; // declare local variables
c=a+b;
printf(“Sum of Values a,b is %d ,c);
}
4 Write a C program to calculate the simple interest using the
formula
SI=PNR/100.
#include <stdio.h>
void main( )
{
int p, n; float r, si ;
p=1000;
n=2;
r=6.5;
si=(p*n * r )/ 100 ;
printf ( "Simple interest = Rs. %f", si ) ;
}
5 Prepare flowchart for sum of digits in a given number.
6 Prepare algorithm to find area of a circle:
step 1. Start
step 2. Declare Area, radius.
step 3. Read the value of radius
step 4. Area = 3.14 * radius * radius.
step 5. Print “Area of Circle = ”
step 6. Print Area.
Step 7: Stop
7 Explain logical operators with example
An expression containing logical operator returns either 0 or 1 depending upon whether
expression results true or false. Logical operators are commonly used in decision making in
C programming.
Operator Meaning Example
Logical AND. True only if all operands If c = 5 and d = 2 then, expression ((c==5) &&
&& are true (d>5)) equals to 0.
Logical OR. True only if either one If c = 5 and d = 2 then, expression ((c==5) ||
|| operand is true (d>5)) equals to 1.
Logical NOT. True only if the operand
! is 0 If c = 5 then, expression !(c==5) equals to 0.
Example:
int main()
{
int a=10, b=4, c = 10, d = 20;
// logical operators
// logical AND example
if (a>b && c==d)
printf("a is greater than b AND c is equal to d\n");
else
printf("AND condition not satisfied\n");
// logical OR example
if (a>b || c==d)
printf("a is greater than b OR c is equal to d\n");
else
printf("Neither a is greater than b nor c is equal to d\n");
// logical NOT example
if (!a)
printf("a is zero\n");
else
printf("a is not zero");
return 0;
}
8 Write a C program to convert Celsius to Fahrenheit.
Ans:
1. #include <stdio.h>
2. int main()
3. {
4. float celsius, fahrenheit;
5. printf("\nEnter temperature in celsius: ");
6. scanf("%f", &celsius);
7. fahrenheit = (1.8) * celsius + 32;
8. printf("\n%f deg celsius is %f fahrenheit\n", celsius, fahrenheit);
9. return 0;
}
9 Draw a flowchart to find largest among three numbers
Write a C program to perform addition and subtraction of given numbers
10
#include <stdio.h>
int main()
{
int num1, num2, add, subtract;
printf("Enter the num1=\n");
scanf("%d", &num1);
printf("Enter the num2=\n");
scanf("%d", &num2);
add = num1 + num2;
subtract = num1 – num2;
printf("Sum = %d\n", add);
printf("Difference = %d\n", subtract);
return 0;
}
OUTPUT:
Enter the num1 = 20
Enter the num2 = 10
Sum = 30
Difference = 10
11
Explain the types of operators with examples
12 Write an algorithm and draw a flowchart to find whether given number is prime or not.