XII CS Chapter 1 TO 4 - NOTES
XII CS Chapter 1 TO 4 - NOTES
HIGHE
IGHER SECONDARY SCHOOL
PUDUCHERRY
1 • FUNCTION
2 • DATA ABSTRACTIONS
3 • SCOPING
4 • ALGORITHMIC STRATEGIES
CHAPTER – 1 FUNCTION
Part - I
Choose the best answer (1 Mark)
1. The small sections of code that are used to perform a particular task is called
called
(a) Subroutines (b) Files (c) Pseudo code (d) Modules
2. Which of the following is a unit of code that is often defined within a greater code structure?
(a) Subroutines (b) Function (c) Files (d) Modules
3. Which of the following is a distinct
nct syntactic block?
(a) Subroutines (b) Function (c) Definition (d) Modules
4. The variables in a function definition are called as
(a) Subroutines (b) Function (c) Definition (d) Parameters
5. The values which are passed to a function definition are called
(a) Arguments (b) Subroutines (c) Function (d) Definition
6. Which of the following are mandatory to write the type annotations in the function definition?
(a) Curly braces (b) Parentheses (c) Square brackets (d) indentations
7. Which of thee following defines what an object can do?
(a) Operating System (b) Compiler (c) Interface (d) Interpreter
8. Which of the following carries out the instructions defined in the interface?
(a) Operating System (b) Compiler (c) Implementation (d) Interpreter
In
9. The functions which will give exact result when same arguments are passed are called
(a) Impure functions (b) Partial Functions (c) Dynamic Functions (d)Pure functions
10. The functions which cause side effects to the arguments passed are called
(a) impure function (b) Partial Functions (c) Dynamic Functions (d) Pure functions
Part - II
Answer the following questions (2 Marks)
1. What is a subroutine?
Subroutines are small sections of code that are used to perform a particular task that can be used
repeatedly.
2. Define Function with respect to Programming language.
A function is a unit of code that is often defined within a greater code structure.
Specifically, a function contains a set of code that works on many kinds of inputs, like variants,
variant
expressions and produces a concrete output.
3. Write the inference you get from X:=(78).
X:= (78) has an expression in it but (78) is not itself an expression. Rather, it is a function definition.
Definitions bind values to names, in this case the value 7878 being bound to the name ‘X’.
4. Differentiate interface and implementation.
Interface Implementation
Interface just defines what an object can do, but Implementation carries out the instructions
won’t actually do it defined in the interface.
5. Which of the
he following is a normal function definition and which is recursive function
definition?
i) let rec sum x y:
return x + y
ii) let disp :
print ‘welcome’
iii) let rec sum num:
if (num!=0) then
return num + sum(num-1)
sum(num
else
return num
Ans: i) Recursive function ii) Normal function iii)Recursive function
Part - III
Answer the following questions (3 Marks)
1. Mention the characteristics of Interface.
The class template specifies the interfaces to enable an object to be created and operated properly.
An object's attributes and behaviour is controlled by sending functions to the object.
In the above example the value of y get changed inside the function definition due to which the
result will change each time.
The side effect of the inc () function is it is changing the data of the external visible variable
‘y’.
4. Differentiate pure and impure function.
The return value of the pure functions solely The return value of the impure functions does
depends on its arguments passed. not solely depend on its arguments passed.
Hence, if you call the pure functions with the Hence, if you call the impure functions with
same set of arguments, you will always get the the same set of arguments, you might get the
same return values. different return values For example, random(),
Date().
They do not have any side effects. They have side effects.
They do not modify the arguments which are They may modify the arguments which are
passed to them passed to them
5. What happens if you modify a variable outside the function? Give an example.
One of the most popular groups of side effects is modifying the variable outside of
function.
For example
let y: = 0
(int) inc (int) x
y: = y + x;
return (y)
In the above example the value of y get changed inside the function definition due to which the
result will change each time.
The side effect of the inc () function is it is changing the data of the external visible variable ‘y’.
Part - IV
Answer the following questions (5Marks)
1. What are called Parameters and write a note on
(i) Parameter without Type (ii) Parameter with Type
Parameters are the variables in a function definition and arguments are the values which are passed to
a function definition.
1. Parameter without Type:
Let us see an example of a function definition:
(requires: b>=0 )
(returns: a to the power of b)
let rec pow a b:=
if b=0 then 1
else a * pow a (b-1)
In the above function definition variable ‘b’ is the parameter and the value which is passed to the
variable ‘b’ is the argument.
The precondition (requires) and postcondition (returns) of the function is given. Note we have
not mentioned any types: (data types).
Some language compiler solves this type (data type) inference problem algorithmically.
In the above function definition if expression can return 1 in the then branch, by the typing rule
the entire if expression has type int.
Since the if expression has type ‘int’, the function's return type also be ‘int’. ‘b’ is compared to 0
with the equality operator, so ‘b’ is also a type of ‘int’.
Since ‘a’ is multiplied with another expression using the * operator, ‘a’ must be an int.
2. Parameter with Type:
Now let us write the same function definition with types for some reason:
(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 a (b-1)
When we write the type annotations for ‘a’ and ‘b’ the parentheses are mandatory.
There are times we may want to explicitly write down types.
This is useful on times when you get a type error from the compiler that doesn't make sense.
Explicitly annotating the types can help with debugging such an error message.
2. Identify in the following program :
let rec gcd a b :=
if b <> 0 then gcd b (a mod b) else return a
i) Name of the function - gcd
ii) Identify the statement which tells it is a recursive function - rec
iii) Name of the argument variable - a,b
iv) Statement which invoke the function recursively – gcd b (a mod b)
v) Statement which terminates the recursion – 0
return a
3. Explain with example Pure and impure functions.
Pure functions
Pure functions are functions which will give exact result when the same arguments are
passed.
For example the mathematical function sin(0) always results 0.
This means that every time you call the function with the same arguments, you will always get
the same result.
Let us see an example
let square x
return: x * x
The above function square is a pure function because it will not give different results for same
input.
One advantage of pure function is that if a function is pure, then if it is called several times with
the same arguments, the compiler only needs to actually call the function once.
Impure functions :
The variables used inside the function may cause side effects though the functions which are
not passed with any arguments.
In such cases the function is called impure function.
When a function depends on variables or functions outside of its definition block, you can
never be sure that the function will behave the same every time it’s called.
For example the mathematical function random() will give different outputs for the same
function call.
let Randomnumber
let a := random()
if a > 10 then
return: a
else
return: 10
Here the function Random is impure as it is not sure what will be the result when we call the
function.
4. Explain with an example interface and implementation.
Interface:
An interface is a set of action that an object can do.
For example when you press a light switch, the light goes on, you may not have cared how it
splashed the light.
In Object Oriented Programming language, an Interface is a description of all functions that a
class must have.
In our example, anything that "ACTS LIKE" a light, should have function definitions like turn_on
() and a turn_off ().
The purpose of interfaces is to allow the computer to enforce the properties of the class of TYPE
T (whatever the interface is) must have functions called X, Y, Z, etc.
The interface defines an object’s visibility to the outside world.
Interface just defines what an object can do, but won’t actually do it.
In object oriented programs classes are the interface.
Implementation:
Implementation carries out the instructions defined in the interface.
How the object is processed and executed is the implementation.
CHAPTER – 2 DATA ABSTRACTION
Part - I
Choose the best answer (1 Mark)
1. Which of the following functions that build the abstract data type ?
(a) Constructors (b) Destructors (c) recursive (d)Nested
2. Which of the following functions that retrieve information from the
the data type?
(a)Constructors (b) Selectors (c) recursive (d)Nested
3. The data structure which is a mutable ordered sequence of elements is called
(a)Built in (b) List (c) Tuple (d) Derived data
4. A sequence of immutable objects is called
(a)Built in (b) List (c) Tuple (d) Derived data
5. The data type whose representation is known are called
(a)Built in datatype (b) Derived datatype (c)Concrete datatype (d) Abstract datatype
6. The data type whose representation is unknown are called
(a)Built in datatype (b) Derived datatype (c)Concrete datatype (d) Abstract datatype
7. Which of the following is a compound structure?
(a)Pair (b) Triplet (c) single (d) quadrat
8. Bundling two values together into one can be considered as
(a)Pair (b) Triplet
riplet (c) single (d) quadrat
9. Which of the following allow to name the various parts of a multi-item
multi item object?
(a)Tuples (b) Lists (c) Classes (d) quadrats
10. Which of the following is constructed by placing expressions within square brackets?
(a)Tuples (b) Lists (c) Classes (d) quadrats
Part - II
Answer the following questions (2 Marks)
1. What is abstract data type?
Abstract Data type (ADT) is a type (orclass) for objects whose behavior is definedby a set of value
and a set of operations.
Abstraction provides modularity(modularity means splitting a program into many modules).
2. Differentiate constructors and selectors.
Constructors Selectors
Constructors are functions that build the Selectors are functions that retrieve
retriev information
abstract data type. from the data type.
city = makecity (name, lat, lon) getname(city)
getlat(city)
Here, makecity (name, lat, lon) is the
getlon(city)are the selectors
constructor which creates the object city.
Selectors are nothing but thefunctions that retrieve information from the data type.
• getname(city)
• getlat(city)
• getlon(city)
are the selectors because these functions extract the information of the city object
2. What is a List? Why List can be called as Pairs. Explain with suitable example.
List is constructed by placing expressions within square brackets separated by commas.
The elements of a list can be accessed in two ways.
The first way is via our familiar method of multiple assignment, which unpacks a list into its
elements and binds each element to a different name.
lst := [10, 20]
x, y := lst
In the above example x will become10 and y will become 20.
A second method for accessing the elements in a list is by the element selection operator, also
expressed using square brackets.
Unlike a list literal, a square brackets expression directly following another expression does not
evaluate to a list value, but instead selects an element from the value of the preceding
expression.
lst[0]
10
lst[1]
20
In both the example mentioned above mathematically we can represent list similar to a set.
lst[(0, 10), (1, 20)] – where
Any way of bundling two values together into one can be considered as apair.
Lists are a common method to do so.
Therefore List can be called as Pairs.
3. How will you access the multi-item.
multi Explain with example.
When we have a multi item object where each 'item' is a named thing: the firstName, the lastName,
the id, and the email,One
One could use a list to represent a person:
person=['Padmashri', 'Baskar', '994-222-1234',
'994 1234', 'compsci@gmail.com']
Butut such a representation doesn't
doesn' explicitly specify what each part represents.
instead of using a list, you can use the structure construct (In OOP languages it's called class
construct) to represent multi--part
part objects where each part is named (given a name).
Consider the following pseudoudo code:
class Person:
creation( )
firstName := " "
lastName := " "
id := " "
email := " "
The new data type Person is pictorially represented as
On execution of the above code the variable a displays the value 7, because it is defined
and available in the local scope.
2. Definee Global scope with an example.
A variable which is declared outside of all the functions in a program is known as global
variable.
This means, global variable can be accessed inside or outside of all the functions in a program.
Consider the following example
ex
2. Disp(): a:=10 7
3. a:=7 Disp() 10
4. print a a:=7
5. Disp() print a
6. print a Disp()
print a
Normally only Functions or modules come along with the software, as packages.
Thereforefore they will come under Built-in
Built scope.
B)Explain LEGB rule with example.
examp
Scope also defines the order in which variables have to be mapped to the object in order to obtain
the value.
Let us take as imple example as shown below:
1. x:= 'outer x variable'
2. display():
3. x:= 'inner x variable'
4. print x
5. display()
When the above statements are executed the statement (4) and (5) display the result as
Output:
outer x variable
inner x variable
Above statements give different outputs because the same variable name x resides in different
scopes, one inside the function display()
display() and the other in the upper level.
The value ‘outer x variable’ is printed when x is referenced outside the function definition.
Whereas when display() gets executed, ‘inner x variable’ is printed which is the x value inside
the function definition.
From the above example, we can guess that there is a rule followed, in order to decide from
which scope a variable has to be picked.
The LEGB rule is used to decide the order in which the scopes are to be searched for scope
resolution.
The scopes are listed
ed below in terms of hierarchy (highest to lowest).
First, we find index of middle element of the array by using this formula :
mid = low + (high - low) / 2
Here it is, 0 + (9 - 0 ) / 2 = 4 (fractional part ignored). So, 4 is the mid value of the array.
Now compare the search element with the value stored at mid value location 4.
The value stored at location or index 4 is 50, which is not match with search element.
As the search value 60 is greater than 50.
Now we change our low to mid + 1 and find the new mid value again using the formula.
low to mid + 1
mid = low + (high - low) / 2
Our new mid is 7 now.
We compare the value stored at location 7 with our target value 31.
The value stored at location or index 7 is not a match with search element, rather it is more
than what we are looking for.
So, the search element must be in the lower part from the current mid value location.
location
Now we compare the value stored at location 5 with our search element.
We found that it is a match.
We can conclude that the search element 60 is found at location
l ation or index 5.
For example if we take the search element as 95, For this value this binary
bin search algorithm
return unsuccessful result.