CH 1 4
CH 1 4
COMPUTER SCIENCE
CHAPTER
NO CONTENT
1 FUNCTIONS
2 DATA ABSTRACTION
3 SCOPING
4 ALGORITHMIC STRATEGIES
1. FUNCTION
2 Mark
1. What is a subroutine?
Subroutines are the basic building blocks of computer programs. Subroutines
are small sections of code that are used to perform a particular task that can be used
repeatedly.
5. Which of the following is a normal function definition and which is recursive function
definition?
i) let sum x y:
return x+y
Ans: Normal Function
ii) let disp:
print ‘welcome’
Ans: Normal Function
ii) let rec sum num:
if(num!=0) then
return num + sum (num-1)
else
return num
Ans: Recursive function
2
3 Mark
3
4. Differentiate pure and impure function
Example:
4
5 Mark
(requires : b>=0)
(returns : a to the power of b)
let rec pow a b:=
if b=0 then 1
else a * pow b(a-1)
(requires : b>=0)
(returns : a to the power of b)
let rec pow (a : int) (b : int) : int:=
if b=0 then 1
else a*pow b(a-1)
The types of argument and return type as „int‟. The type annotations for
„a‟ and „b‟ the parentheses are mandatory. This is the way passing parameter
with type which helps the compiler to easily infer them.
5
2. Identify in the following program
6
Example: Example:
let square x let a:= random( )
return: x * x let random( ) :=
if a>10 then
return: a
else
return: 10
4. Explain with an example interface and implementation.
Interface
An interface is a set of action that an object can do.
Interface just defines what an object can do, but won‟t actually do it.
7
when you press a light switch, the light goes on, you may not
have cared how is splashed the light .
A light should have function definitions like turn_on( ) and turn_off( ).
Implementation
Implementation carries out the instructions defined in the interface.
A class declaration combines the external interface with an
implementation of the interface.
An object is an instance created from the class
Example
The person who drives the car doesn’t care about the internal working.
2. DATA ABSTRACTION
2 Mark
1. What is abstract data type?
3 Mark
10
lst[1]
20
10
4. Identify which of the following are List, Tuple and Class?
a arr[1,2,3,4] a List
b arr(1,2,34) b Tuple
c student[rno, name, mark] c Class
d day = („sun‟, „mon‟ ,„tue‟, „wed‟) d Tuple
e x = [2, 5, 6.5,[5,6],8.2] e List
f employee[eno, ename, esal, eaddress] f Class
5 Mark
1. How will you facilitate data abstraction. Explain it with suitable example.
Data abstraction is supported by defining an abstract data type,
which is a collection of constructors and selectors.
Constructors
Constructors are functions that build the abstract data type.
Constructors create an object, bundling together different pieces of
information.
Example
city = makecity(name, lat, lon)
Here makecity(name, lat, lon) is the constructor which creates the object
city.
Selectors
Selectors are functions that retrieve information from the data type.
Selectors extract individual pieces of information from the object.
Example
getname(city)
getlat(city)
getlon(city)
10
lst[1]
20
Pair
Any way of bundling two values together into one can be considered as a
pair, Lists are a common method to do so. Therefore List can be called
as Pairs. Example
3. SCOPING
2 Marks
1. What is a scope?
Scope refers to the visibility of variables, parameters and functions in one
part of a program to another part of the same rogram.
2. Why scope should be used for variable. State the reason.
Scope should be used for variable because every part of the program can
access the variable.
3. What is Mapping?
a := 5
14
3 Marks
A function will first look up for a variable name in its local scope.
Output
Example
a := 10
Disp( ):
a := 7
print a
Disp( )
print a
Output
7
10
15
3. Define Enclosed scope with an example.
a:=10
Disp1( ):
print a
Disp1( )
print a
Disp( )
Output
10
10
16
5. Identify the scope of the variables in the following pseudo code
and write its Output
color:= ‘Red’
mycolor( ):
b:= ‘Blue’
myfavcolor( ):
g:= ‘Green’
print color, b,
g myfavcolor( )
print color, b
mycolor(
) print
color
Ans:
Output
Red Blue Green
Red Blue
Red
Scope of the variables
Variables Scope
color:= ‘Red’ Global Scope
b:= ‘Blue’ Enclosed Scope
g:= ‘Green’ Local Scope
5 Marks
1. Explain the types of scopes for variable or LEGB rule with example.
LEGB rule is used to decide the order in which the scopes are to be
searched for scope resolution.
17
Local Scope
Enclosed Scope
Global Scope
Built-in Scope
Local Scope
A function will first look up for a variable name in its local scope.
Only if it does not find it there, the outer scopes are Checked. Example
Disp( ):
a := 7
print a
Disp( )
Output
Global Scope 7
18
Enclosed Scope
Output
10
10
19
Built-in-Scope
The built-in scope has all the names that are pre-loaded into the
program scope when we start the compiler or interpreter.
Library files
associated
Built in/ module scope
with the
Disp( ):
Disp1( ): software.
print a
Disp1( )
print a
Disp( )
20
4. ALGORITHMIC STRATEGIES
2 Marks
1. What is an Algorithm?
An algorithm is a finite set of instructions to accomplish a particular task.
It is a step-by-step procedure for solving a given problem.
Input Simplicity
Output Unambiguous
Finiteness Feasibility
Definiteness Portable
Effectiveness Independent
Correctness
21
2. Discuss about Algorithmic complexity and its types.
Algorithmic complexity is concerned about running time and storage space
required by the algorithm. f(n) – n as the size of input data.
Time Complexity
The Time complexity of an algorithm is given by the number of steps
taken by the algorithm to complete the process.
Space Complexity
Space complexity of an algorithm is the amount of memory required to
run to its completion.
3. What are the factors that influence time and space complexity.
Time Factor is measured by counting the number of key operations like
comparisons in the sorting algorithm.
Space Factor is measured by the maximum memory space required by
the algorithm.
Algorithmic complexity is concerned about running time and storage space
required by the algorithm. f(n) – n as the size of input data.
5 Marks
1. Explain the characteristics of an algorithm.
Index 0 1 2 3 4
values 10 12 20 25 30
Example 1
Input:
values[ ] = {10, 12, 20, 25, 30}
target = 25
Output: 3
Example 2
Input:
values[ ] = {10, 12, 20, 25, 30}
target = 50
Output:
-1
24
3. What is Binary Search? Discuss with example.
Binary Search also called half-interval search algorithm.
It finds the position of a search element with a sorted array.
It can be done as divide and conquer search algorithm .
Pseudo code
1. Start with the middle element:
middle value = number of elements in array/2.
2. If not, then compare the middle element with the search value,
3. If the search element is greater than the number in the middle index, then
select the elements to the right side of the middle index, and go to step 1.
4. If the search element is less than the number in the middle index, then select
the elements to the left side of the middle index, and start with step1.
5. when a match is found, display success message with the index of the
element matched.
6. If no match is found for all comparisons, then display unsuccessful message.
Example
List of elements in an array must be sorted first for Binary Search.
The search element is 60.
60>50
10 20 30 40 50 60 70 80 90 99
0 1 2 3 4 5 6 7 8 9
25
60<80
10 20 30 40 50 60 70 80 90 99
0 1 2 3 4 5 6 7 8 9
10 20 30 40 50 60 70 80 90 99
0 1 2 3 4 5 6 7 8 9
10 20 30 40 50 60 70 80 90 99
0 1 2 3 4 5 6 7 8 9
It compares each pair of adjacent elements and swaps them If they are in
26
Pseudo code
1. Start with the first element index = 0, compare the current element with the
next element of the array.
2. If the current element is greater than the next element of the array, swap
them.
3. If the current element is less than the next or right side of the element, move
to the next element. Go to step 1 and repeat until end of the index in reached.
Example
An array with values {15, 11, 16, 12, 14, 13}
15 > 11
15 11 16 12 14 13
So interchange
15 < 16
15 11 16 12 14 13
No swapping
16 > 12
11 15 16 12 14 13
So interchange
16 > 14
So interchange 11 15 12 16 14 13
16 > 13
11 15 12 14 16 13
So interchange
11 15 12 14 13 16
At the end of all the iterations we will get the sorted values in an array
11 12 13 14 15 16
27
5. Explain the concept of Dynamic programming with suitable Example.
Dynamic programming is an algorithmic design method is used when the
solution to a problem can be viewed as the result of a sequence of decisions.
Dynamic programming approach is similar to divide and conquer. The
problem can be divided into smaller sub-problems.
Results can be re-used to complete the process.
Dynamic programming approaches are used to find the solution in optimized
way.
Step 5: Go to step 2 and repeat until the specified number of terms generated.
28